From 16afb2dde6f8b33c20987f838f1c2dfe5626841e Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Fri, 23 Feb 2018 14:48:42 +0100 Subject: [PATCH] SSR: fix multiple target check --- .../JavaPredefinedConfigurations.java | 4 +-- .../StringToConstraintsTransformer.java | 12 ++++----- .../StructuralReplaceTest.java | 6 ++--- .../StringToConstraintsTransformerTest.java | 26 ++++++++----------- 4 files changed, 22 insertions(+), 26 deletions(-) diff --git a/java/structuralsearch-java/src/com/intellij/structuralsearch/JavaPredefinedConfigurations.java b/java/structuralsearch-java/src/com/intellij/structuralsearch/JavaPredefinedConfigurations.java index a49c59bc92fe..4642bd837a88 100644 --- a/java/structuralsearch-java/src/com/intellij/structuralsearch/JavaPredefinedConfigurations.java +++ b/java/structuralsearch-java/src/com/intellij/structuralsearch/JavaPredefinedConfigurations.java @@ -210,8 +210,8 @@ class JavaPredefinedConfigurations { "@interface 'Interface {}", METADATA_TYPE), // J2EE templates - createSearchTemplateInfoSimple(SSRBundle.message("predefined.configuration.struts.1.1.actions"),"public class 'StrutsActionClass extends '_ParentClass*:Action {\n" + - " public ActionForward 'AnActionMethod:*execute (ActionMapping '_action,\n" + + createSearchTemplateInfoSimple(SSRBundle.message("predefined.configuration.struts.1.1.actions"),"public class '_StrutsActionClass extends '_ParentClass*:Action {\n" + + " public ActionForward '_AnActionMethod:*execute (ActionMapping '_action,\n" + " ActionForm '_form,\n" + " HttpServletRequest '_request,\n" + " HttpServletResponse '_response);\n" + diff --git a/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/compiler/StringToConstraintsTransformer.java b/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/compiler/StringToConstraintsTransformer.java index 753c9587a1c1..10d262fda9b8 100644 --- a/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/compiler/StringToConstraintsTransformer.java +++ b/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/compiler/StringToConstraintsTransformer.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.impl.matcher.compiler; import com.intellij.openapi.util.text.StringUtil; @@ -56,10 +56,10 @@ public class StringToConstraintsTransformer { while (endIndex < length && Character.isJavaIdentifierPart(criteria.charAt(endIndex))) endIndex++; if (endIndex == index) throw new MalformedPatternException(SSRBundle.message("error.expected.character")); - boolean anonymous = false; + boolean target = true; final String typedVar; if (criteria.charAt(index)=='_') { - anonymous = true; + target = false; if(endIndex == index + 1) { // anonymous var, make it unique for the case of constraints @@ -143,11 +143,11 @@ public class StringToConstraintsTransformer { constraint.setMinCount(minOccurs); constraint.setMaxCount(maxOccurs); constraint.setGreedy(greedy); - constraint.setPartOfSearchResults(!anonymous); - if (targetFound && !anonymous) { + constraint.setPartOfSearchResults(target); + if (targetFound && target) { throw new MalformedPatternException(SSRBundle.message("error.only.one.target.allowed")); } - targetFound = !anonymous; + targetFound |= target; } else if (savedIndex != index) { throw new MalformedPatternException(SSRBundle.message("error.condition.only.on.first.variable.reference")); diff --git a/platform/structuralsearch/testSource/com/intellij/structuralsearch/StructuralReplaceTest.java b/platform/structuralsearch/testSource/com/intellij/structuralsearch/StructuralReplaceTest.java index 79d59428039f..c25ca31f539f 100644 --- a/platform/structuralsearch/testSource/com/intellij/structuralsearch/StructuralReplaceTest.java +++ b/platform/structuralsearch/testSource/com/intellij/structuralsearch/StructuralReplaceTest.java @@ -196,7 +196,7 @@ public class StructuralReplaceTest extends StructuralReplaceTestCase { assertEquals("Expression replacement", expectedResult3, replacer.testReplace(expectedResult1,str5,str6,options)); String str7 = "try { a.doSomething(); b.doSomething(); } catch(IOException ex) { ex.printStackTrace(); throw new RuntimeException(ex); }"; - String str8 = "try { 'Statements+; } catch('_ '_) { 'HandlerStatements+; }"; + String str8 = "try { '_Statements+; } catch('_ '_) { '_HandlerStatements+; }"; String str9 = "$Statements$;"; String expectedResult4 = "a.doSomething(); b.doSomething();"; @@ -1056,9 +1056,9 @@ public class StructuralReplaceTest extends StructuralReplaceTestCase { "\n" + "}"; - String s35 = "public interface 'MessageInterface {\n" + + String s35 = "public interface '_MessageInterface {\n" + " public static final String '_X = '_VALUE;\n" + - " 'blah*" + + " '_blah*" + "}"; String s36 = "public interface $MessageInterface$ {\n" + " public static final String HEADER = $VALUE$;\n" + diff --git a/platform/structuralsearch/testSource/com/intellij/structuralsearch/impl/matcher/compiler/StringToConstraintsTransformerTest.java b/platform/structuralsearch/testSource/com/intellij/structuralsearch/impl/matcher/compiler/StringToConstraintsTransformerTest.java index 3e4f32ba61c2..1e13707c3951 100644 --- a/platform/structuralsearch/testSource/com/intellij/structuralsearch/impl/matcher/compiler/StringToConstraintsTransformerTest.java +++ b/platform/structuralsearch/testSource/com/intellij/structuralsearch/impl/matcher/compiler/StringToConstraintsTransformerTest.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.structuralsearch.impl.matcher.compiler; import com.intellij.structuralsearch.MalformedPatternException; @@ -266,6 +252,16 @@ public class StringToConstraintsTransformerTest { assertEquals("a", constraint.getRegExp()); } + @Test(expected = MalformedPatternException.class) + public void testMultipleTargets() { + test("try { 'Statements+; } catch('_ '_) { 'HandlerStatements+; }"); + } + + @Test + public void testSameTargetMultipleTimes() { + test("'a = 'a;"); + } + private void test(String criteria) { StringToConstraintsTransformer.transformCriteria(criteria, myOptions); }