diff --git a/platform/structuralsearch/source/com/intellij/structuralsearch/plugin/replace/impl/ParameterInfo.java b/platform/structuralsearch/source/com/intellij/structuralsearch/plugin/replace/impl/ParameterInfo.java index 3e232731bd88..f6fbead8cceb 100644 --- a/platform/structuralsearch/source/com/intellij/structuralsearch/plugin/replace/impl/ParameterInfo.java +++ b/platform/structuralsearch/source/com/intellij/structuralsearch/plugin/replace/impl/ParameterInfo.java @@ -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; } diff --git a/platform/structuralsearch/source/com/intellij/structuralsearch/plugin/replace/impl/ReplacementBuilder.java b/platform/structuralsearch/source/com/intellij/structuralsearch/plugin/replace/impl/ReplacementBuilder.java index 04304f45a72c..4708ee31b176 100644 --- a/platform/structuralsearch/source/com/intellij/structuralsearch/plugin/replace/impl/ReplacementBuilder.java +++ b/platform/structuralsearch/source/com/intellij/structuralsearch/plugin/replace/impl/ReplacementBuilder.java @@ -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 parameterizations = new SmartList<>(); - private final Map replacementVarsMap; + private final Map 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=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(), diff --git a/platform/structuralsearch/testSource/com/intellij/structuralsearch/StructuralReplaceTest.java b/platform/structuralsearch/testSource/com/intellij/structuralsearch/StructuralReplaceTest.java index 1402b8e09cbc..a8a175e10cae 100644 --- a/platform/structuralsearch/testSource/com/intellij/structuralsearch/StructuralReplaceTest.java +++ b/platform/structuralsearch/testSource/com/intellij/structuralsearch/StructuralReplaceTest.java @@ -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() {