mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
SSR: cleaner java replacement results on empty substitution
This commit is contained in:
+10
-24
@@ -1,37 +1,35 @@
|
||||
// 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.psi.PsiElement;
|
||||
|
||||
public final class ParameterInfo {
|
||||
private String name;
|
||||
private int startIndex;
|
||||
private final String name;
|
||||
private final int startIndex;
|
||||
private final boolean replacementVariable;
|
||||
private boolean argumentContext;
|
||||
private boolean methodParameterContext;
|
||||
private boolean statementContext;
|
||||
private boolean variableInitializerContext;
|
||||
private int afterDelimiterPos;
|
||||
private boolean hasCommaBefore;
|
||||
private int beforeDelimiterPos;
|
||||
private boolean hasCommaAfter;
|
||||
private boolean replacementVariable;
|
||||
private PsiElement myElement;
|
||||
|
||||
public ParameterInfo(String name, int startIndex, boolean replacementVariable) {
|
||||
this.name = name;
|
||||
this.startIndex = startIndex;
|
||||
this.replacementVariable = replacementVariable;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getStartIndex() {
|
||||
return startIndex;
|
||||
}
|
||||
|
||||
public void setStartIndex(int startIndex) {
|
||||
this.startIndex = startIndex;
|
||||
}
|
||||
|
||||
public boolean isArgumentContext() {
|
||||
return argumentContext;
|
||||
}
|
||||
@@ -56,14 +54,6 @@ public final class ParameterInfo {
|
||||
this.statementContext = statementContext;
|
||||
}
|
||||
|
||||
public boolean isVariableInitializerContext() {
|
||||
return variableInitializerContext;
|
||||
}
|
||||
|
||||
public void setVariableInitializerContext(boolean variableInitializerContext) {
|
||||
this.variableInitializerContext = variableInitializerContext;
|
||||
}
|
||||
|
||||
public int getAfterDelimiterPos() {
|
||||
return afterDelimiterPos;
|
||||
}
|
||||
@@ -100,10 +90,6 @@ public final class ParameterInfo {
|
||||
return replacementVariable;
|
||||
}
|
||||
|
||||
public void setReplacementVariable(boolean replacementVariable) {
|
||||
this.replacementVariable = replacementVariable;
|
||||
}
|
||||
|
||||
public PsiElement getElement() {
|
||||
return myElement;
|
||||
}
|
||||
|
||||
+19
-22
@@ -1,4 +1,4 @@
|
||||
// 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.
|
||||
// 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.codeInsight.template.Template;
|
||||
@@ -31,46 +31,42 @@ import java.util.Map;
|
||||
public final class ReplacementBuilder {
|
||||
private final String replacement;
|
||||
private final List<ParameterInfo> parameterizations = new SmartList<>();
|
||||
private final Map<String, ScriptSupport> replacementVarsMap;
|
||||
private final Map<String, ScriptSupport> replacementVarsMap = new HashMap<>();
|
||||
private final ReplaceOptions options;
|
||||
private final Project myProject;
|
||||
|
||||
ReplacementBuilder(final Project project,final ReplaceOptions options) {
|
||||
ReplacementBuilder(Project project, ReplaceOptions options) {
|
||||
myProject = project;
|
||||
replacementVarsMap = new HashMap<>();
|
||||
this.options = options;
|
||||
String _replacement = options.getReplacement();
|
||||
FileType fileType = options.getMatchOptions().getFileType();
|
||||
|
||||
final Template template = TemplateManager.getInstance(project).createTemplate("","",_replacement);
|
||||
|
||||
final int segmentsCount = template.getSegmentsCount();
|
||||
final Template template = TemplateManager.getInstance(project).createTemplate("", "", options.getReplacement());
|
||||
replacement = template.getTemplateText();
|
||||
|
||||
for(int i=0;i<segmentsCount;++i) {
|
||||
int prevOffset = 0;
|
||||
for (int i = 0; i < template.getSegmentsCount(); i++) {
|
||||
final int offset = template.getSegmentOffset(i);
|
||||
final String name = template.getSegmentName(i);
|
||||
|
||||
final ParameterInfo info = new ParameterInfo();
|
||||
info.setStartIndex(offset);
|
||||
info.setName(name);
|
||||
info.setReplacementVariable(options.getVariableDefinition(name) != null);
|
||||
final ParameterInfo info = new ParameterInfo(name, offset, options.getVariableDefinition(name) != null);
|
||||
|
||||
// find delimiter
|
||||
int pos;
|
||||
for(pos = offset-1; pos >=0 && pos < replacement.length() && Character.isWhitespace(replacement.charAt(pos));) {
|
||||
--pos;
|
||||
int pos = offset - 1;
|
||||
while (pos >= prevOffset && pos < replacement.length() && StringUtil.isWhiteSpace(replacement.charAt(pos))) {
|
||||
pos--;
|
||||
}
|
||||
|
||||
if (pos >= 0) {
|
||||
if (replacement.charAt(pos) == ',') {
|
||||
info.setHasCommaBefore(true);
|
||||
}
|
||||
while (pos > prevOffset && StringUtil.isWhiteSpace(replacement.charAt(pos - 1))) {
|
||||
pos--;
|
||||
}
|
||||
info.setBeforeDelimiterPos(pos);
|
||||
}
|
||||
|
||||
for(pos = offset; pos < replacement.length() && Character.isWhitespace(replacement.charAt(pos));) {
|
||||
++pos;
|
||||
pos = offset;
|
||||
while (pos < replacement.length() && StringUtil.isWhiteSpace(replacement.charAt(pos))) {
|
||||
pos++;
|
||||
}
|
||||
|
||||
if (pos < replacement.length()) {
|
||||
@@ -85,15 +81,16 @@ public final class ReplacementBuilder {
|
||||
}
|
||||
}
|
||||
info.setAfterDelimiterPos(pos);
|
||||
|
||||
prevOffset = offset;
|
||||
parameterizations.add(info);
|
||||
}
|
||||
|
||||
FileType fileType = options.getMatchOptions().getFileType();
|
||||
final StructuralSearchProfile profile = StructuralSearchUtil.getProfileByFileType(fileType);
|
||||
if (profile != null) {
|
||||
try {
|
||||
final PsiElement[] elements = MatcherImplUtil.createTreeFromText(
|
||||
_replacement,
|
||||
options.getReplacement(),
|
||||
PatternTreeContext.Block,
|
||||
fileType,
|
||||
options.getMatchOptions().getDialect(),
|
||||
|
||||
+4
-4
@@ -1109,7 +1109,7 @@ public class StructuralReplaceTest extends StructuralReplaceTestCase {
|
||||
|
||||
String expectedResult = "class A {\n" +
|
||||
" public int a = 1;\n" +
|
||||
" public int b ;\n" +
|
||||
" public int b;\n" +
|
||||
" private int c = 2;\n" +
|
||||
"}";
|
||||
|
||||
@@ -2046,7 +2046,7 @@ public class StructuralReplaceTest extends StructuralReplaceTestCase {
|
||||
assertEquals("class A { int i = 1 + 3;}", replacer.testReplace(in1, what2, by5, options, false));
|
||||
|
||||
final String by6 = "1 + $a$ + 3";
|
||||
assertEquals("class A { int i = 1 + 3;}", replacer.testReplace(in1, what2, by6, options, false));
|
||||
assertEquals("class A { int i = 1 + 3;}", replacer.testReplace(in1, what2, by6, options, false));
|
||||
|
||||
final String in2 = "class A {" +
|
||||
" boolean b = true && true;" +
|
||||
@@ -2069,7 +2069,7 @@ public class StructuralReplaceTest extends StructuralReplaceTestCase {
|
||||
|
||||
final String what = "assert '_a > '_b : '_c?;";
|
||||
final String by = "assert $b$ < $a$ : $c$;";
|
||||
assertEquals("class A { void m(int i) { assert i < 10 ; }}", replacer.testReplace(in, what, by, options, false));
|
||||
assertEquals("class A { void m(int i) { assert i < 10; }}", replacer.testReplace(in, what, by, options, false));
|
||||
}
|
||||
|
||||
public void testReplaceMultipleVariablesInOneDeclaration() {
|
||||
@@ -2085,7 +2085,7 @@ public class StructuralReplaceTest extends StructuralReplaceTestCase {
|
||||
|
||||
final String what2 = "int '_a, '_b, '_c = '_d?;";
|
||||
final String by2 = "float $a$, $b$, $c$ = $d$;";
|
||||
assertEquals("class A { private float i, j, k ; void m() { float i, j, k ; }}", replacer.testReplace(in, what2, by2, options));
|
||||
assertEquals("class A { private float i, j, k; void m() { float i, j, k; }}", replacer.testReplace(in, what2, by2, options));
|
||||
}
|
||||
|
||||
public void testReplaceWithScriptedVariable() {
|
||||
|
||||
Reference in New Issue
Block a user