From 2b0b340c46a22d230fd4378be4d7ba5d4c05f01d Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Sat, 24 Feb 2018 17:24:15 +0100 Subject: [PATCH] SSR: remove "context match" difference between test & actual search Fixes some failing cases of target replacement (IDEA-146380) --- ...igrateAssertToMatcherAssertInspection.java | 4 +- .../GuavaFluentIterableConversionRule.java | 22 +----- .../guava/GuavaOptionalConversionRule.java | 24 ++---- .../rules/guava/GuavaPredicatesUtil.java | 18 +---- .../inspections/GuavaInspectionTest.java | 18 +---- .../structuralsearch/MatchOptions.java | 8 -- .../structuralsearch/MatchResult.java | 2 + .../impl/matcher/GlobalMatchingVisitor.java | 4 +- .../impl/matcher/MatchResultImpl.java | 14 ++++ .../replace/impl/ReplacementInfoImpl.java | 2 +- .../plugin/replace/impl/Replacer.java | 6 +- .../StructuralReplaceTest.java | 75 ++++++++++++++----- .../XmlStructuralReplaceTest.java | 12 +-- 13 files changed, 98 insertions(+), 111 deletions(-) diff --git a/java/typeMigration/src/com/intellij/refactoring/typeMigration/inspections/MigrateAssertToMatcherAssertInspection.java b/java/typeMigration/src/com/intellij/refactoring/typeMigration/inspections/MigrateAssertToMatcherAssertInspection.java index 03cc2b60b11e..06a346495b3a 100644 --- a/java/typeMigration/src/com/intellij/refactoring/typeMigration/inspections/MigrateAssertToMatcherAssertInspection.java +++ b/java/typeMigration/src/com/intellij/refactoring/typeMigration/inspections/MigrateAssertToMatcherAssertInspection.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.refactoring.typeMigration.inspections; import com.intellij.codeInsight.intention.impl.AddOnDemandStaticImportAction; @@ -169,7 +169,7 @@ public class MigrateAssertToMatcherAssertInspection extends AbstractBaseJavaLoca return null; } final boolean hasMessage = hasMessage(method); - final String searchTemplate = "'Assert*." + method.getName() + "(" + (hasMessage ? "$msg$, " : "") + templatePair.getFirst() + ")"; + final String searchTemplate = "'_Assert?." + method.getName() + "(" + (hasMessage ? "$msg$, " : "") + templatePair.getFirst() + ")"; final PsiClass containingClass = method.getContainingClass(); LOG.assertTrue(containingClass != null); final String qualifier = containingClass.getQualifiedName(); diff --git a/java/typeMigration/src/com/intellij/refactoring/typeMigration/rules/guava/GuavaFluentIterableConversionRule.java b/java/typeMigration/src/com/intellij/refactoring/typeMigration/rules/guava/GuavaFluentIterableConversionRule.java index fff0276c2529..ce780e406966 100644 --- a/java/typeMigration/src/com/intellij/refactoring/typeMigration/rules/guava/GuavaFluentIterableConversionRule.java +++ b/java/typeMigration/src/com/intellij/refactoring/typeMigration/rules/guava/GuavaFluentIterableConversionRule.java @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2017 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// 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.refactoring.typeMigration.rules.guava; import com.intellij.codeInspection.java18StreamApi.PseudoLambdaReplaceTemplate; @@ -137,9 +123,9 @@ public class GuavaFluentIterableConversionRule extends BaseGuavaTypeConversionRu PsiType conversionType = null; boolean needSpecifyType = true; if (methodName.equals("of")) { - descriptorBase = new TypeConversionDescriptor("'FluentIterable*.of($arr$)", "java.util.Arrays.stream($arr$)"); + descriptorBase = new TypeConversionDescriptor("'_FluentIterable?.of($arr$)", "java.util.Arrays.stream($arr$)"); } else if (methodName.equals("from")) { - descriptorBase = new TypeConversionDescriptor("'FluentIterable*.from($it$)", null) { + descriptorBase = new TypeConversionDescriptor("'_FluentIterable?.from($it$)", null) { @Override public PsiExpression replace(PsiExpression expression, @NotNull TypeEvaluator evaluator) { final PsiMethodCallExpression methodCall = (PsiMethodCallExpression)expression; @@ -277,7 +263,7 @@ public class GuavaFluentIterableConversionRule extends BaseGuavaTypeConversionRu if (list.getParametersCount() != 1) return null; final PsiType parameterType = list.getParameters()[0].getType(); if (parameterType instanceof PsiEllipsisType) { - return new TypeConversionDescriptor("$q$.append('params*)", "java.util.stream.Stream.concat($q$, java.util.Arrays.asList($params$).stream())"); + return new TypeConversionDescriptor("$q$.append('_params*)", "java.util.stream.Stream.concat($q$, java.util.Arrays.asList($params$).stream())"); } else if (parameterType instanceof PsiClassType) { final PsiClass psiClass = PsiTypesUtil.getPsiClass(parameterType); diff --git a/java/typeMigration/src/com/intellij/refactoring/typeMigration/rules/guava/GuavaOptionalConversionRule.java b/java/typeMigration/src/com/intellij/refactoring/typeMigration/rules/guava/GuavaOptionalConversionRule.java index 7447115681a3..e0117e7a376b 100644 --- a/java/typeMigration/src/com/intellij/refactoring/typeMigration/rules/guava/GuavaOptionalConversionRule.java +++ b/java/typeMigration/src/com/intellij/refactoring/typeMigration/rules/guava/GuavaOptionalConversionRule.java @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2017 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// 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.refactoring.typeMigration.rules.guava; import com.intellij.openapi.diagnostic.Logger; @@ -129,7 +115,7 @@ public class GuavaOptionalConversionRule extends BaseGuavaTypeConversionRule { @Override protected void fillSimpleDescriptors(Map descriptorsMap) { - descriptorsMap.put("absent", new TypeConversionDescriptor("'Optional*.absent()", "java.util.Optional.empty()") { + descriptorsMap.put("absent", new TypeConversionDescriptor("'_Optional?.absent()", "java.util.Optional.empty()") { @Override public PsiExpression replace(PsiExpression expression, @NotNull TypeEvaluator evaluator) { LOG.assertTrue(expression instanceof PsiMethodCallExpression); @@ -146,9 +132,9 @@ public class GuavaOptionalConversionRule extends BaseGuavaTypeConversionRule { }); - descriptorsMap.put("of", new TypeConversionDescriptor("'Optional*.of($ref$)", "java.util.Optional.of($ref$)")); - descriptorsMap.put("fromNullable", new TypeConversionDescriptor("'Optional*.fromNullable($ref$)", "java.util.Optional.ofNullable($ref$)")); - descriptorsMap.put("presentInstances", new TypeConversionDescriptor("'Optional*.presentInstances($it$)", "java.util.stream.StreamSupport.stream($it$.spliterator(), false).map(java.util.Optional::get).collect(java.util.Collectors.toList())")); + descriptorsMap.put("of", new TypeConversionDescriptor("'_Optional?.of($ref$)", "java.util.Optional.of($ref$)")); + descriptorsMap.put("fromNullable", new TypeConversionDescriptor("'_Optional?.fromNullable($ref$)", "java.util.Optional.ofNullable($ref$)")); + descriptorsMap.put("presentInstances", new TypeConversionDescriptor("'_Optional?.presentInstances($it$)", "java.util.stream.StreamSupport.stream($it$.spliterator(), false).map(java.util.Optional::get).collect(java.util.Collectors.toList())")); final TypeConversionDescriptorBase identity = new TypeConversionDescriptorBase(); descriptorsMap.put("get", identity); diff --git a/java/typeMigration/src/com/intellij/refactoring/typeMigration/rules/guava/GuavaPredicatesUtil.java b/java/typeMigration/src/com/intellij/refactoring/typeMigration/rules/guava/GuavaPredicatesUtil.java index 8eafcf3db8e7..d759e21d8af7 100644 --- a/java/typeMigration/src/com/intellij/refactoring/typeMigration/rules/guava/GuavaPredicatesUtil.java +++ b/java/typeMigration/src/com/intellij/refactoring/typeMigration/rules/guava/GuavaPredicatesUtil.java @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2015 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// 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.refactoring.typeMigration.rules.guava; import com.intellij.openapi.diagnostic.Logger; @@ -76,7 +62,7 @@ public class GuavaPredicatesUtil { private final String myReplaceByStringTemplate; TypeConversionDescriptorWithLocalVariable(String methodName, String replaceByString) { - super("'Predicates*." + methodName + "(" + (methodName.equals("equalTo") ? "$v$" : "") + ")", null); + super("'_Predicates?." + methodName + "(" + (methodName.equals("equalTo") ? "$v$" : "") + ")", null); myReplaceByStringTemplate = replaceByString; } diff --git a/java/typeMigration/test/com/intellij/codeInsight/inspections/GuavaInspectionTest.java b/java/typeMigration/test/com/intellij/codeInsight/inspections/GuavaInspectionTest.java index 018e9c0b7a78..a8ae8bfdfa68 100644 --- a/java/typeMigration/test/com/intellij/codeInsight/inspections/GuavaInspectionTest.java +++ b/java/typeMigration/test/com/intellij/codeInsight/inspections/GuavaInspectionTest.java @@ -1,24 +1,9 @@ -/* - * Copyright 2000-2017 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// 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.codeInsight.inspections; import com.intellij.codeInsight.daemon.impl.HighlightInfo; import com.intellij.codeInsight.intention.IntentionAction; import com.intellij.codeInspection.actions.CleanupInspectionIntention; -import com.intellij.openapi.application.PathManager; import com.intellij.openapi.editor.RangeMarker; import com.intellij.openapi.util.Pair; import com.intellij.pom.java.LanguageLevel; @@ -30,7 +15,6 @@ import com.intellij.testFramework.IdeaTestUtil; import com.intellij.testFramework.PlatformTestUtil; import com.intellij.testFramework.builders.JavaModuleFixtureBuilder; import com.intellij.testFramework.fixtures.JavaCodeInsightFixtureTestCase; -import org.junit.Assert; import java.util.Arrays; import java.util.List; diff --git a/platform/structuralsearch/source/com/intellij/structuralsearch/MatchOptions.java b/platform/structuralsearch/source/com/intellij/structuralsearch/MatchOptions.java index 953789714e8a..c43c55796bcc 100644 --- a/platform/structuralsearch/source/com/intellij/structuralsearch/MatchOptions.java +++ b/platform/structuralsearch/source/com/intellij/structuralsearch/MatchOptions.java @@ -146,14 +146,6 @@ public class MatchOptions implements JDOMExternalizable { StringToConstraintsTransformer.transformCriteria(criteria, this); } - public boolean isResultIsContextMatch() { - return resultIsContextMatch; - } - - public void setResultIsContextMatch(boolean resultIsContextMatch) { - this.resultIsContextMatch = resultIsContextMatch; - } - public SearchScope getScope() { return scope; } diff --git a/platform/structuralsearch/source/com/intellij/structuralsearch/MatchResult.java b/platform/structuralsearch/source/com/intellij/structuralsearch/MatchResult.java index c45d0be6485a..710ca970a06b 100644 --- a/platform/structuralsearch/source/com/intellij/structuralsearch/MatchResult.java +++ b/platform/structuralsearch/source/com/intellij/structuralsearch/MatchResult.java @@ -24,5 +24,7 @@ public abstract class MatchResult { public abstract boolean hasChildren(); public abstract boolean isScopeMatch(); public abstract boolean isMultipleMatch(); + + public abstract MatchResult getRoot(); public abstract boolean isTarget(); } diff --git a/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/GlobalMatchingVisitor.java b/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/GlobalMatchingVisitor.java index 884d48985993..5d59759005f4 100644 --- a/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/GlobalMatchingVisitor.java +++ b/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/GlobalMatchingVisitor.java @@ -227,7 +227,7 @@ public class GlobalMatchingVisitor extends AbstractMatchingVisitor { } private void dispatchMatched(final List matchedNodes, MatchResultImpl result) { - if (!matchContext.getOptions().isResultIsContextMatch() && doDispatch(result)) return; + if (doDispatch(result)) return; // There is no substitutions so show the context @@ -260,7 +260,7 @@ public class GlobalMatchingVisitor extends AbstractMatchingVisitor { } else { for (final PsiElement matchStatement : matchedNodes) { - result.addChild(new MatchResultImpl(MatchResult.LINE_MATCH, matchStatement.getText(), new SmartPsiPointer(matchStatement), true)); + result.addChild(new MatchResultImpl(MatchResult.LINE_MATCH, matchStatement.getText(), new SmartPsiPointer(matchStatement), false)); } result.setMatchRef(new SmartPsiPointer(match)); diff --git a/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/MatchResultImpl.java b/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/MatchResultImpl.java index 58bd08e51276..3bcf769b132d 100644 --- a/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/MatchResultImpl.java +++ b/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/MatchResultImpl.java @@ -20,6 +20,7 @@ public final class MatchResultImpl extends MatchResult { private boolean myScopeMatch; private boolean myMultipleMatch; + private MatchResultImpl parent = null; MatchResultImpl() { } @@ -143,9 +144,22 @@ public final class MatchResultImpl extends MatchResult { } public void addChild(MatchResult result) { + if (result instanceof MatchResultImpl) { + ((MatchResultImpl)result).parent = this; + } myChildren.add(result); } + @Override + public MatchResult getRoot() { + if (parent == null) return this; + MatchResultImpl root = parent; + while (root.parent != null) { + root = root.parent; + } + return root; + } + public void setMatchImage(String matchImage) { this.matchImage = matchImage; } diff --git a/platform/structuralsearch/source/com/intellij/structuralsearch/plugin/replace/impl/ReplacementInfoImpl.java b/platform/structuralsearch/source/com/intellij/structuralsearch/plugin/replace/impl/ReplacementInfoImpl.java index ddbbdea1bdf7..341a5ad16f4b 100644 --- a/platform/structuralsearch/source/com/intellij/structuralsearch/plugin/replace/impl/ReplacementInfoImpl.java +++ b/platform/structuralsearch/source/com/intellij/structuralsearch/plugin/replace/impl/ReplacementInfoImpl.java @@ -30,7 +30,7 @@ class ReplacementInfoImpl implements ReplacementInfo { private void init(Project project) { fillPointerList(project); - fillVariableMap(matchResult); + fillVariableMap(matchResult.getRoot()); for(Map.Entry entry : variableMap.entrySet()) { fillElementToVariableNameMap(entry.getKey(), entry.getValue()); } diff --git a/platform/structuralsearch/source/com/intellij/structuralsearch/plugin/replace/impl/Replacer.java b/platform/structuralsearch/source/com/intellij/structuralsearch/plugin/replace/impl/Replacer.java index f35bcf7c6932..7de512590c98 100644 --- a/platform/structuralsearch/source/com/intellij/structuralsearch/plugin/replace/impl/Replacer.java +++ b/platform/structuralsearch/source/com/intellij/structuralsearch/plugin/replace/impl/Replacer.java @@ -116,12 +116,10 @@ public class Replacer { lastElement = parent.getLastChild(); } - matchOptions.setResultIsContextMatch(true); CollectingMatchResultSink sink = new CollectingMatchResultSink(); matcher.testFindMatches(sink, matchOptions); final List resultPtrList = new SmartList<>(); - for (final MatchResult result : sink.getMatches()) { resultPtrList.add(buildReplacement(result)); } @@ -217,7 +215,7 @@ public class Replacer { if (element==null || !element.isWritable() || !element.isValid()) return null; - final PsiElement elementParent = element.getParent(); + final PsiElement elementParent = StructuralSearchUtil.getPresentableElement(element).getParent(); CodeStyleManager.getInstance(project).performActionWithFormatterDisabled( (Runnable)() -> { @@ -353,7 +351,7 @@ public class Replacer { public ReplacementInfo buildReplacement(MatchResult result) { final ReplacementInfoImpl replacementInfo = new ReplacementInfoImpl(result, project); - if (replacementBuilder==null) { + if (replacementBuilder == null) { replacementBuilder = new ReplacementBuilder(project, options); } replacementInfo.setReplacement(replacementBuilder.process(result, replacementInfo, options.getMatchOptions().getFileType())); diff --git a/platform/structuralsearch/testSource/com/intellij/structuralsearch/StructuralReplaceTest.java b/platform/structuralsearch/testSource/com/intellij/structuralsearch/StructuralReplaceTest.java index c25ca31f539f..6b10d0c5facd 100644 --- a/platform/structuralsearch/testSource/com/intellij/structuralsearch/StructuralReplaceTest.java +++ b/platform/structuralsearch/testSource/com/intellij/structuralsearch/StructuralReplaceTest.java @@ -95,7 +95,7 @@ public class StructuralReplaceTest extends StructuralReplaceTestCase { " JOptionPane.showMessageDialog(null, \"MESSAGE\");\n" + " }\n" + "}"; - String s2 = "JOptionPane.'showDialog(null, '_msg);"; + String s2 = "JOptionPane.'_showDialog(null, '_msg);"; String s3 = "//FIXME provide a parent frame\n" + "JOptionPane.$showDialog$(null, $msg$);"; @@ -230,7 +230,7 @@ public class StructuralReplaceTest extends StructuralReplaceTestCase { assertEquals("Replacement of init in definition + empty substitution", expectedResult7, replacer.testReplace(str16,str17,str18,options)); String str19 = " aaa(bbb);"; - String str20 = "'t('_);"; + String str20 = "'_t('_);"; String str21 = "$t$(ccc);"; String expectedResult8 = " aaa(ccc);"; @@ -253,7 +253,7 @@ public class StructuralReplaceTest extends StructuralReplaceTestCase { " });\n" + " }\n" + " });"; - String str26 = " LaterInvocator.invokeLater('Params{1,10});"; + String str26 = " LaterInvocator.invokeLater('_Params{1,10});"; String str27 = " com.intellij.openapi.application.ApplicationManager.getApplication().invokeLater($Params$);"; String expectedResult10 = " com.intellij.openapi.application.ApplicationManager.getApplication().invokeLater(new Runnable() {\n" + " public void run() {\n" + @@ -272,7 +272,7 @@ public class StructuralReplaceTest extends StructuralReplaceTestCase { " processedElement.getTextOffset(), true,\n" + " !myUsageViewDescriptor.toMarkInvalidOrReadonlyUsages(), null);"; String str29 = "new UTElementNode('_param, '_directory, '_null, '_0, '_true, !'_descr.toMarkInvalidOrReadonlyUsages(),\n" + - " 'referencesWord)"; + " '_referencesWord)"; String str30 = "new UTElementNode($param$, $directory$, $null$, $0$, $true$, true,\n" + " $referencesWord$)"; @@ -281,7 +281,7 @@ public class StructuralReplaceTest extends StructuralReplaceTestCase { assertEquals("Replace in def initializer", expectedResult11, replacer.testReplace(str28,str29,str30,options)); String s31 = "a = b; b = c; a=a; c=c;"; - String s32 = "'a = 'a;"; + String s32 = "'_a = '_a;"; String s33 = "1 = 1;"; String expectedResult12 = "a = b; b = c; 1 = 1; 1 = 1;"; @@ -300,7 +300,7 @@ public class StructuralReplaceTest extends StructuralReplaceTestCase { " ParamChecker.isTrue(2==2, \"!!!\");\n" + "} catch(Exception ex) {}"; String s38 = "try {\n" + - " 'Statement{0,100};\n" + + " '_Statement{0,100};\n" + "} catch(Exception ex) {}"; String s39 = "$Statement$;"; @@ -499,7 +499,7 @@ public class StructuralReplaceTest extends StructuralReplaceTestCase { String s4 = "if (true) System.out.println(\"1111\"); else System.out.println(\"2222\");\n" + "while(true) System.out.println(\"1111\");"; - String s5 = "System.out.println('Test);"; + String s5 = "System.out.println('_Test);"; String s6 = "/* System.out.println($Test$); */"; String expectedResult2 = "if (true) /* System.out.println(\"1111\"); */; else /* System.out.println(\"2222\"); */;\n" + "while(true) /* System.out.println(\"1111\"); */;"; @@ -676,10 +676,10 @@ public class StructuralReplaceTest extends StructuralReplaceTestCase { "}"; String s11 = "new Runnable() {\n" + " public void run() {\n" + - " 'l{2,2};\n" + + " '_l{2,2};\n" + " }\n" + " public void run2() {\n" + - " 'l;\n" + + " '_l;\n" + " }\n" + "\n" + " };"; @@ -726,7 +726,7 @@ public class StructuralReplaceTest extends StructuralReplaceTestCase { " abstract void f();\n" + "}"; - assertEquals("same multiple occurences 2 times", expectedResult4, replacer.testReplace(s10,s11,s12,options)); + assertEquals("same multiple occurrences 2 times", expectedResult4, replacer.testReplace(s10,s11,s12,options)); options.setToReformatAccordingToStyle(formatAccordingToStyle); @@ -754,7 +754,7 @@ public class StructuralReplaceTest extends StructuralReplaceTestCase { " }"; String s14 = " PsiLock.LOCK.acquire();\n" + " try {\n" + - " 'T{1,1000};\n" + + " '_T{1,1000};\n" + " }\n" + " finally {\n" + " PsiLock.LOCK.release();\n" + @@ -814,7 +814,7 @@ public class StructuralReplaceTest extends StructuralReplaceTestCase { "}"; String s17 = "synchronized(lock) {\n" + - " 'Statement*;\n" + + " '_Statement*;\n" + "}"; String s18 = "$Statement$;"; @@ -923,8 +923,8 @@ public class StructuralReplaceTest extends StructuralReplaceTestCase { " this.closeOutOnExit = closeOutOnExit;\n" + "}\n" + "}"; - String s14 = "class 'Class extends Thread {\n" + - " 'Class('_ParameterType* '_ParameterName*) {\n" + + String s14 = "class '_Class extends Thread {\n" + + " '_Class('_ParameterType* '_ParameterName*) {\n" + "\t super (CustomThreadGroup.getThreadGroup(), '_superarg* );\n" + " '_Statement*;\n" + " }\n" + @@ -1305,7 +1305,7 @@ public class StructuralReplaceTest extends StructuralReplaceTestCase { public void testReplaceReturnWithArrayInitializer() { String searchIn = "return ( new String[]{CoreVars.CMUAudioPort + \"\"} );"; - String searchFor = "return ( 'A );"; + String searchFor = "return ( '_A );"; String replaceBy = "return $A$;"; String expectedResult = "return new String[]{CoreVars.CMUAudioPort + \"\"};"; @@ -1625,7 +1625,7 @@ public class StructuralReplaceTest extends StructuralReplaceTestCase { public void testUseStaticImport() { final String in = "class X {{ Math.abs(-1); }}"; - final String what = "Math.abs('a)"; + final String what = "Math.abs('_a)"; final String by = "Math.abs($a$)"; final boolean save = options.isToUseStaticImport(); options.setToUseStaticImport(true); @@ -1647,7 +1647,7 @@ public class StructuralReplaceTest extends StructuralReplaceTestCase { " System.out.println(Collections.emptyList());" + " }" + "}"; - final String what3 = "'_q.'_method:[regex( println )]('a)"; + final String what3 = "'_q.'_method:[regex( println )]('_a)"; final String by3 = "$q$.$method$($a$)"; final String expected4 = "import java.util.Collections;" + "import static java.lang.System.out;" + @@ -1899,7 +1899,7 @@ public class StructuralReplaceTest extends StructuralReplaceTestCase { " System.out.println(\"blah\");\n" + "} finally {\n" + "}\n"; - String s2 = "try {\n" + " 'statement*;\n" + "} finally {\n" + " \n" + "}"; + String s2 = "try {\n" + " '_statement*;\n" + "} finally {\n" + " \n" + "}"; String replacement = "$statement$;"; String expected = "String[] a = {\"a\"};\n" + " System.out.println(\"blah\");\n"; @@ -2367,4 +2367,43 @@ public class StructuralReplaceTest extends StructuralReplaceTestCase { "}", replacer.testReplace(in, what, by, options, true)); } + + public void testReplaceTarget() { + String in = "import org.junit.Test;" + + "class Help {" + + " private String s = \"hello\";" + + " @Test" + + " public void testThisThing(){" + + " System.out.println();" + + " System.out.println();" + + " System.out.println();" + + " s = null;" + + " }" + + "}"; + String what = "class 'Class {" + + " '_FieldType '_FieldName;" + + " @'_Annotation" + + " '_MethodType '_MethodName() {" + + " '_Statement*;" + + " '_FieldName = null;" + + " }" + + "}"; + String by = "class $Class$ {" + + " $FieldType$ $FieldName$;" + + " @$Annotation$" + + " $MethodType$ $MethodName$() {" + + " $Statement$;" + + " }" + + "}"; + assertEquals("import org.junit.Test;" + + "class Help {" + + " private String s=\"hello\";" + + " @Test" + + " public void testThisThing() {" + + " System.out.println();" + + " System.out.println();" + + " System.out.println();" + + " }" + + "}", replacer.testReplace(in, what, by, options, true)); + } } \ No newline at end of file diff --git a/platform/structuralsearch/testSource/com/intellij/structuralsearch/XmlStructuralReplaceTest.java b/platform/structuralsearch/testSource/com/intellij/structuralsearch/XmlStructuralReplaceTest.java index 9c8cfd0061e3..b9f4d4e9575c 100644 --- a/platform/structuralsearch/testSource/com/intellij/structuralsearch/XmlStructuralReplaceTest.java +++ b/platform/structuralsearch/testSource/com/intellij/structuralsearch/XmlStructuralReplaceTest.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; import com.intellij.openapi.fileTypes.StdFileTypes; @@ -39,7 +39,7 @@ public class XmlStructuralReplaceTest extends StructuralReplaceTestCase { " \n" + " \n" + ""; - String s5 = ""; + String s5 = ""; String s6 = ""; actualResult = replacer.testReplace(s4,s5,s6,options); @@ -60,7 +60,7 @@ public class XmlStructuralReplaceTest extends StructuralReplaceTestCase { String s7 = "

My titleZZZZ My title 3

\n" + "

My title 2

"; - String s8 = "

'Content*

"; + String s8 = "

'_Content*

"; String s9 = "
$Content$
"; actualResult = replacer.testReplace(s7,s8,s9,options); @@ -77,14 +77,14 @@ public class XmlStructuralReplaceTest extends StructuralReplaceTestCase { "

My title 2

"; assertEquals("Delete tag", expectedResult, replacer.testReplace(s7, s8, "", options)); - String what = "<'H:h4 class=\"a\">'_Content*"; + String what = "<'_H:h4 class=\"a\">'_Content*"; String by = "<$H$>$Content$"; expectedResult = "

My title ZZZZ My title 3

\n" + "

My title 2

"; assertEquals("Replace with variable", expectedResult, replacer.testReplace(s7, what, by, options)); String in = "Cry 'Havoc!', and let slip the
dogs of war
"; - what = "<'Tag:b >'_Content2*"; + what = "<'_Tag:b >'_Content2*"; by = "<$Tag$ id=\"unique\">$Content2$"; expectedResult = "Cry 'Havoc!', and let slip the
dogs of war
"; assertEquals("Replace complex content with variable", expectedResult, replacer.testReplace(in, what, by, options)); @@ -104,7 +104,7 @@ public class XmlStructuralReplaceTest extends StructuralReplaceTestCase { public void testHtmlAddAttribute() { String in = ""; - String what = ""; + String what = ""; String by = ""; String expected = "";