From b9ca9a904c17a66317c4110ea46bfc60e3defbff Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 1 Jul 2010 21:52:35 +0400 Subject: [PATCH] introduce: process parenthesis correctly --- .../src/com/intellij/codeInsight/CodeInsightUtil.java | 9 ++++++++- .../introduceVariable/ParenthizedOccurence1.after.java | 5 +++++ .../introduceVariable/ParenthizedOccurence1.java | 5 +++++ .../com/intellij/refactoring/IntroduceVariableTest.java | 4 ++++ 4 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 java/java-tests/testData/refactoring/introduceVariable/ParenthizedOccurence1.after.java create mode 100644 java/java-tests/testData/refactoring/introduceVariable/ParenthizedOccurence1.java diff --git a/java/java-impl/src/com/intellij/codeInsight/CodeInsightUtil.java b/java/java-impl/src/com/intellij/codeInsight/CodeInsightUtil.java index 5e369700c2c5..e378a4905596 100644 --- a/java/java-impl/src/com/intellij/codeInsight/CodeInsightUtil.java +++ b/java/java-impl/src/com/intellij/codeInsight/CodeInsightUtil.java @@ -171,7 +171,14 @@ public class CodeInsightUtil { public static PsiExpression[] findExpressionOccurrences(PsiElement scope, PsiExpression expr) { List array = new ArrayList(); addExpressionOccurrences(RefactoringUtil.unparenthesizeExpression(expr), array, scope); - if (!array.contains(expr)) array.add(expr); + boolean found = false; + for (PsiExpression psiExpression : array) { + if (areExpressionsEquivalent(RefactoringUtil.unparenthesizeExpression(psiExpression), RefactoringUtil.unparenthesizeExpression(expr))) { + found = true; + break; + } + } + if (!found) array.add(expr); return array.toArray(new PsiExpression[array.size()]); } diff --git a/java/java-tests/testData/refactoring/introduceVariable/ParenthizedOccurence1.after.java b/java/java-tests/testData/refactoring/introduceVariable/ParenthizedOccurence1.after.java new file mode 100644 index 000000000000..b58aaf72e530 --- /dev/null +++ b/java/java-tests/testData/refactoring/introduceVariable/ParenthizedOccurence1.after.java @@ -0,0 +1,5 @@ +public class Introduce { + void foo(String str) { + final String s = (String) str; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/refactoring/introduceVariable/ParenthizedOccurence1.java b/java/java-tests/testData/refactoring/introduceVariable/ParenthizedOccurence1.java new file mode 100644 index 000000000000..5281ba2e18d9 --- /dev/null +++ b/java/java-tests/testData/refactoring/introduceVariable/ParenthizedOccurence1.java @@ -0,0 +1,5 @@ +public class Introduce { + void foo(String str) { + ((String)str) + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/refactoring/IntroduceVariableTest.java b/java/java-tests/testSrc/com/intellij/refactoring/IntroduceVariableTest.java index 84dbb30c6e9c..8264b21c1af2 100644 --- a/java/java-tests/testSrc/com/intellij/refactoring/IntroduceVariableTest.java +++ b/java/java-tests/testSrc/com/intellij/refactoring/IntroduceVariableTest.java @@ -74,6 +74,10 @@ public class IntroduceVariableTest extends LightCodeInsightTestCase { doTest(new MockIntroduceVariableHandler("empty", true, true, true, "boolean")); } + public void testParenthizedOccurence1() throws Exception { + doTest(new MockIntroduceVariableHandler("s", true, true, true, "java.lang.String")); + } + public void testConflictingField() throws Exception { doTest(new MockIntroduceVariableHandler("name", true, false, true, "java.lang.String")); }