SSR: get rid of replacement context class

This commit is contained in:
Bas Leijdekkers
2018-08-23 15:48:18 +02:00
parent 28020806c5
commit 7bce46ecef
8 changed files with 61 additions and 87 deletions

View File

@@ -2,9 +2,9 @@
package com.intellij.structuralsearch;
import com.intellij.codeInsight.AnnotationUtil;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
import com.intellij.psi.javadoc.PsiDocComment;
import com.intellij.psi.util.PsiTreeUtil;
@@ -14,7 +14,6 @@ import com.intellij.structuralsearch.impl.matcher.MatcherImplUtil;
import com.intellij.structuralsearch.impl.matcher.PatternTreeContext;
import com.intellij.structuralsearch.plugin.replace.ReplaceOptions;
import com.intellij.structuralsearch.plugin.replace.ReplacementInfo;
import com.intellij.structuralsearch.plugin.replace.impl.ReplacementContext;
import com.intellij.structuralsearch.plugin.replace.impl.Replacer;
import com.intellij.structuralsearch.plugin.replace.impl.ReplacerUtil;
import com.intellij.util.IncorrectOperationException;
@@ -22,6 +21,7 @@ import com.intellij.util.ObjectUtils;
import com.intellij.util.SmartList;
import com.siyeh.ig.psiutils.ImportUtils;
import com.siyeh.ig.psiutils.PsiElementOrderComparator;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.HashMap;
@@ -33,16 +33,19 @@ import java.util.Map;
* @author Eugene.Kudelevsky
*/
public class JavaReplaceHandler extends StructuralReplaceHandler {
private final ReplacementContext myContext;
private final PsiElement[] patternElements;
@NotNull private final Project myProject;
@NotNull private final ReplaceOptions myReplaceOptions;
public JavaReplaceHandler(ReplacementContext context) {
this.myContext = context;
public JavaReplaceHandler(@NotNull Project project, @NotNull ReplaceOptions replaceOptions) {
myProject = project;
myReplaceOptions = replaceOptions;
final MatchOptions matchOptions = replaceOptions.getMatchOptions();
patternElements = MatcherImplUtil.createTreeFromText(
myContext.getOptions().getMatchOptions().getSearchPattern(),
matchOptions.getSearchPattern(),
PatternTreeContext.Block,
myContext.getOptions().getMatchOptions().getFileType(),
myContext.getProject()
matchOptions.getFileType(),
project
);
}
@@ -52,9 +55,7 @@ public class JavaReplaceHandler extends StructuralReplaceHandler {
el = el.getParent();
}
if (el instanceof PsiReferenceExpression &&
el.getParent() instanceof PsiMethodCallExpression
) {
if (el instanceof PsiReferenceExpression && el.getParent() instanceof PsiMethodCallExpression) {
// method
el = el.getParent();
}
@@ -347,10 +348,13 @@ public class JavaReplaceHandler extends StructuralReplaceHandler {
replacementToMake = "@" + replacementToMake;
}
final PsiElement[] replacements = ReplacerUtil
.createTreeForReplacement(replacementToMake, elementToReplace instanceof PsiMember && !isSymbolReplacement(elementToReplace) ?
PatternTreeContext.Class :
PatternTreeContext.Block, myContext);
final PsiElement[] replacements = MatcherImplUtil.createTreeFromText(
replacementToMake,
elementToReplace instanceof PsiMember && !isSymbolReplacement(elementToReplace) ?
PatternTreeContext.Class :
PatternTreeContext.Block,
myReplaceOptions.getMatchOptions().getFileType(),
myProject);
if (elementToReplace instanceof PsiAnnotation && replacements.length == 1) {
final PsiElement replacement = replacements[0];
@@ -394,8 +398,8 @@ public class JavaReplaceHandler extends StructuralReplaceHandler {
if (unmatchedElements != null) {
final PsiElement firstElement = unmatchedElements.get(0);
if (firstElement instanceof PsiResourceList) addElementAfterAnchor(tryStatement, firstElement, tryStatement.getFirstChild());
outer: for (int i = 0, max = unmatchedElements.size(); i < max; i++) {
final PsiElement element = unmatchedElements.get(i);
outer:
for (final PsiElement element : unmatchedElements) {
if (element instanceof PsiCatchSection) {
final PsiCatchSection[] catches = tryStatement.getCatchSections();
final PsiCatchSection catchSection = (PsiCatchSection)element;
@@ -570,10 +574,10 @@ public class JavaReplaceHandler extends StructuralReplaceHandler {
if (!affectedElement.isValid()) {
return;
}
if (options.isToUseStaticImport()) {
if (myReplaceOptions.isToUseStaticImport()) {
shortenWithStaticImports(affectedElement, 0, affectedElement.getTextLength());
}
if (options.isToShortenFQN()) {
if (myReplaceOptions.isToShortenFQN()) {
final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(affectedElement.getProject());
codeStyleManager.shortenClassReferences(affectedElement, 0, affectedElement.getTextLength());
}

View File

@@ -32,7 +32,6 @@ import com.intellij.structuralsearch.plugin.replace.ReplaceOptions;
import com.intellij.structuralsearch.plugin.replace.ReplacementInfo;
import com.intellij.structuralsearch.plugin.replace.impl.ParameterInfo;
import com.intellij.structuralsearch.plugin.replace.impl.ReplacementBuilder;
import com.intellij.structuralsearch.plugin.replace.impl.ReplacementContext;
import com.intellij.structuralsearch.plugin.replace.impl.Replacer;
import com.intellij.structuralsearch.plugin.ui.Configuration;
import com.intellij.structuralsearch.plugin.ui.UIUtil;
@@ -253,8 +252,8 @@ public class JavaStructuralSearchProfile extends StructuralSearchProfile {
}
@Override
public StructuralReplaceHandler getReplaceHandler(@NotNull ReplacementContext context) {
return new JavaReplaceHandler(context);
public StructuralReplaceHandler getReplaceHandler(@NotNull Project project, @NotNull ReplaceOptions replaceOptions) {
return new JavaReplaceHandler(project, replaceOptions);
}
@NotNull

View File

@@ -24,7 +24,6 @@ import com.intellij.structuralsearch.plugin.replace.ReplaceOptions;
import com.intellij.structuralsearch.plugin.replace.ReplacementInfo;
import com.intellij.structuralsearch.plugin.replace.impl.ParameterInfo;
import com.intellij.structuralsearch.plugin.replace.impl.ReplacementBuilder;
import com.intellij.structuralsearch.plugin.replace.impl.ReplacementContext;
import com.intellij.structuralsearch.plugin.replace.impl.Replacer;
import com.intellij.structuralsearch.plugin.ui.Configuration;
import com.intellij.structuralsearch.plugin.ui.UIUtil;
@@ -160,7 +159,7 @@ public abstract class StructuralSearchProfile {
}
@Nullable
public StructuralReplaceHandler getReplaceHandler(@NotNull ReplacementContext context) {
public StructuralReplaceHandler getReplaceHandler(@NotNull Project project, @NotNull ReplaceOptions replaceOptions) {
return null;
}

View File

@@ -28,7 +28,6 @@ import com.intellij.structuralsearch.impl.matcher.handlers.*;
import com.intellij.structuralsearch.impl.matcher.iterators.SsrFilteringNodeIterator;
import com.intellij.structuralsearch.impl.matcher.strategies.MatchingStrategy;
import com.intellij.structuralsearch.plugin.replace.ReplaceOptions;
import com.intellij.structuralsearch.plugin.replace.impl.ReplacementContext;
import com.intellij.util.ArrayUtil;
import com.intellij.util.LocalTimeCounter;
import org.jetbrains.annotations.NotNull;
@@ -205,8 +204,8 @@ public abstract class StructuralSearchProfileBase extends StructuralSearchProfil
public void checkReplacementPattern(Project project, ReplaceOptions options) {}
@Override
public StructuralReplaceHandler getReplaceHandler(@NotNull ReplacementContext context) {
return new DocumentBasedReplaceHandler(context.getProject());
public StructuralReplaceHandler getReplaceHandler(@NotNull Project project, @NotNull ReplaceOptions replaceOptionss) {
return new DocumentBasedReplaceHandler(project);
}
@NotNull

View File

@@ -18,7 +18,6 @@ import com.intellij.structuralsearch.impl.matcher.compiler.GlobalCompilingVisito
import com.intellij.structuralsearch.impl.matcher.compiler.XmlCompilingVisitor;
import com.intellij.structuralsearch.plugin.replace.ReplaceOptions;
import com.intellij.structuralsearch.plugin.replace.ReplacementInfo;
import com.intellij.structuralsearch.plugin.replace.impl.ReplacementContext;
import com.intellij.structuralsearch.plugin.replace.impl.Replacer;
import com.intellij.structuralsearch.plugin.replace.impl.ReplacerUtil;
import com.intellij.structuralsearch.plugin.ui.Configuration;
@@ -152,15 +151,18 @@ public class XmlStructuralSearchProfile extends StructuralSearchProfile {
}
@Override
public StructuralReplaceHandler getReplaceHandler(@NotNull ReplacementContext context) {
return new XmlReplaceHandler(context);
public StructuralReplaceHandler getReplaceHandler(@NotNull Project project, @NotNull ReplaceOptions replaceOptions) {
return new XmlReplaceHandler(project, replaceOptions);
}
private static class XmlReplaceHandler extends StructuralReplaceHandler {
private final ReplacementContext myContext;
XmlReplaceHandler(ReplacementContext context) {
myContext = context;
@NotNull private final Project myProject;
@NotNull private final ReplaceOptions myReplaceOptions;
XmlReplaceHandler(@NotNull Project project, @NotNull ReplaceOptions replaceOptions) {
myProject = project;
myReplaceOptions = replaceOptions;
}
@Override
@@ -172,12 +174,15 @@ public class XmlStructuralSearchProfile extends StructuralSearchProfile {
final boolean listContext = elementParent instanceof XmlTag;
if (listContext) {
doReplaceInContext(info, elementToReplace, replacementToMake, elementParent, myContext);
doReplaceInContext(info, elementToReplace, replacementToMake, elementParent);
}
else {
final PsiElement[] statements = ReplacerUtil.createTreeForReplacement(replacementToMake, PatternTreeContext.Block, myContext);
if (statements.length > 0) {
PsiElement replacement = ReplacerUtil.copySpacesAndCommentsBefore(elementToReplace, statements, replacementToMake, elementParent);
final PsiElement[] replacements = MatcherImplUtil.createTreeFromText(replacementToMake,
PatternTreeContext.Block,
myReplaceOptions.getMatchOptions().getFileType(),
myProject);
if (replacements.length > 0) {
PsiElement replacement = ReplacerUtil.copySpacesAndCommentsBefore(elementToReplace, replacements, replacementToMake, elementParent);
// preserve comments
Replacer.handleComments(elementToReplace, replacement, info);
@@ -189,23 +194,22 @@ public class XmlStructuralSearchProfile extends StructuralSearchProfile {
}
}
private static void doReplaceInContext(ReplacementInfo info,
PsiElement elementToReplace,
String replacementToMake,
PsiElement elementParent,
ReplacementContext context) {
final PsiElement[] statements = ReplacerUtil.createTreeForReplacement(replacementToMake, PatternTreeContext.Block, context);
private void doReplaceInContext(ReplacementInfo info, PsiElement elementToReplace, String replacementToMake, PsiElement elementParent) {
final PsiElement[] replacements = MatcherImplUtil.createTreeFromText(replacementToMake,
PatternTreeContext.Block,
myReplaceOptions.getMatchOptions().getFileType(),
myProject);
if (statements.length > 1) {
elementParent.addRangeBefore(statements[0], statements[statements.length - 1], elementToReplace);
if (replacements.length > 1) {
elementParent.addRangeBefore(replacements[0], replacements[replacements.length - 1], elementToReplace);
}
else if (statements.length == 1) {
Replacer.handleComments(elementToReplace, statements[0], info);
else if (replacements.length == 1) {
Replacer.handleComments(elementToReplace, replacements[0], info);
try {
elementParent.addBefore(statements[0], elementToReplace);
elementParent.addBefore(replacements[0], elementToReplace);
}
catch (IncorrectOperationException e) {
elementToReplace.replace(statements[0]);
elementToReplace.replace(replacements[0]);
}
}

View File

@@ -1,24 +0,0 @@
// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.structuralsearch.plugin.replace.impl;
import com.intellij.openapi.project.Project;
import com.intellij.structuralsearch.plugin.replace.ReplaceOptions;
public class ReplacementContext {
private final ReplaceOptions options;
private final Project project;
ReplacementContext(ReplaceOptions _options, Project _project) {
options = _options;
project = _project;
}
public ReplaceOptions getOptions() {
return options;
}
public Project getProject() {
return project;
}
}

View File

@@ -44,7 +44,7 @@ public class Replacer {
this.options = options;
final StructuralSearchProfile profile = StructuralSearchUtil.getProfileByFileType(options.getMatchOptions().getFileType());
assert profile != null;
replaceHandler = profile.getReplaceHandler(new ReplacementContext(options, project));
replaceHandler = profile.getReplaceHandler(project, options);
assert replaceHandler != null;
replacementBuilder = new ReplacementBuilder(this.project, this.options);
}
@@ -66,7 +66,7 @@ public class Replacer {
}
public static String testReplace(String in, String what, String by, ReplaceOptions options, Project project, boolean sourceIsFile) {
FileType type = options.getMatchOptions().getFileType();
final FileType type = options.getMatchOptions().getFileType();
return testReplace(in, what, by, options, project, sourceIsFile, false, type, null);
}
@@ -105,7 +105,7 @@ public class Replacer {
lastElement = parent.getLastChild();
}
CollectingMatchResultSink sink = new CollectingMatchResultSink();
final CollectingMatchResultSink sink = new CollectingMatchResultSink();
matcher.testFindMatches(sink, matchOptions);
final List<ReplacementInfo> resultPtrList = new SmartList<>();

View File

@@ -1,13 +1,11 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.structuralsearch.plugin.replace.impl;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.psi.PsiComment;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiWhiteSpace;
import com.intellij.structuralsearch.StructuralSearchProfile;
import com.intellij.structuralsearch.impl.matcher.MatcherImplUtil;
import com.intellij.structuralsearch.impl.matcher.PatternTreeContext;
/**
* @author Eugene.Kudelevsky
@@ -16,15 +14,10 @@ public class ReplacerUtil {
private ReplacerUtil() {
}
public static PsiElement[] createTreeForReplacement(String replacement, PatternTreeContext treeContext, ReplacementContext context) {
FileType fileType = context.getOptions().getMatchOptions().getFileType();
return MatcherImplUtil.createTreeFromText(replacement, treeContext, fileType, context.getProject());
}
public static PsiElement copySpacesAndCommentsBefore(PsiElement elementToReplace,
PsiElement[] patternElements,
String replacementToMake,
PsiElement elementParent) {
PsiElement[] patternElements,
String replacementToMake,
PsiElement elementParent) {
int i = 0;
while (true) { // if it goes out of bounds then deep error happens
if (!(patternElements[i] instanceof PsiComment || patternElements[i] instanceof PsiWhiteSpace)) {