From d9e6f8d96edaa3eb4615354ff4a4ad8bede7b9bb Mon Sep 17 00:00:00 2001 From: "Anna.Kozlova" Date: Wed, 20 Dec 2017 18:18:28 +0100 Subject: [PATCH] surround with closeable: don't include unrelated variables (IDEA-159434) --- .../impl/SurroundAutoCloseableAction.java | 5 ++++- .../UnrelatedVariable.java | 14 ++++++++++++++ .../UnrelatedVariable_after.java | 17 +++++++++++++++++ .../SurroundAutoCloseableActionTest.java | 1 + 4 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 java/java-tests/testData/codeInsight/surroundAutoCloseable/UnrelatedVariable.java create mode 100644 java/java-tests/testData/codeInsight/surroundAutoCloseable/UnrelatedVariable_after.java diff --git a/java/java-impl/src/com/intellij/codeInsight/intention/impl/SurroundAutoCloseableAction.java b/java/java-impl/src/com/intellij/codeInsight/intention/impl/SurroundAutoCloseableAction.java index 88a35f376472..5ba141974e61 100644 --- a/java/java-impl/src/com/intellij/codeInsight/intention/impl/SurroundAutoCloseableAction.java +++ b/java/java-impl/src/com/intellij/codeInsight/intention/impl/SurroundAutoCloseableAction.java @@ -188,13 +188,16 @@ public class SurroundAutoCloseableAction extends PsiElementBaseIntentionAction { i = PsiTreeUtil.skipWhitespacesAndCommentsForward(i); if (!(child instanceof PsiDeclarationStatement)) continue; + int endOffset = last.getTextRange().getEndOffset(); + //declared after last usage + if (child.getTextOffset() > endOffset) break; PsiElement anchor = child; PsiElement[] declaredElements = ((PsiDeclarationStatement)child).getDeclaredElements(); for (PsiElement declared : declaredElements) { if (!(declared instanceof PsiLocalVariable)) continue; - int endOffset = last.getTextRange().getEndOffset(); + boolean contained = ReferencesSearch.search(declared, scope).forEach(ref -> ref.getElement().getTextOffset() <= endOffset); if (!contained) { diff --git a/java/java-tests/testData/codeInsight/surroundAutoCloseable/UnrelatedVariable.java b/java/java-tests/testData/codeInsight/surroundAutoCloseable/UnrelatedVariable.java new file mode 100644 index 000000000000..e8a23dcfbc75 --- /dev/null +++ b/java/java-tests/testData/codeInsight/surroundAutoCloseable/UnrelatedVariable.java @@ -0,0 +1,14 @@ +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; + +class C { + void m(File file) throws IOException { + FileInputStream fileInputStream = new FileInputStream(file); + String s = "initial value";//Non-NLS + s = s + fileInputStream.read(); + s += "end"; + int time = 0; + System.out.println(time); + } +} diff --git a/java/java-tests/testData/codeInsight/surroundAutoCloseable/UnrelatedVariable_after.java b/java/java-tests/testData/codeInsight/surroundAutoCloseable/UnrelatedVariable_after.java new file mode 100644 index 000000000000..56dcf902e31b --- /dev/null +++ b/java/java-tests/testData/codeInsight/surroundAutoCloseable/UnrelatedVariable_after.java @@ -0,0 +1,17 @@ +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; + +class C { + void m(File file) throws IOException { + String s; + try (FileInputStream fileInputStream = new FileInputStream(file)) { + //Non-NLS + s = "initial value"; + s = s + fileInputStream.read(); + } + s += "end"; + int time = 0; + System.out.println(time); + } +} diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/intention/SurroundAutoCloseableActionTest.java b/java/java-tests/testSrc/com/intellij/java/codeInsight/intention/SurroundAutoCloseableActionTest.java index 8f088d62bc3b..7c98141535dd 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInsight/intention/SurroundAutoCloseableActionTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/intention/SurroundAutoCloseableActionTest.java @@ -35,6 +35,7 @@ public class SurroundAutoCloseableActionTest extends LightCodeInsightFixtureTest public void testSplitVar() { doTest(); } public void testExpression() { doTest(); } public void testExpressionIncomplete() { doTest(); } + public void testUnrelatedVariable() { doTest(); } public void testCommentsInVarDeclaration() { JavaCodeStyleSettings styleSettings = JavaCodeStyleSettings.getInstance(getProject()); boolean finalLocals = styleSettings.GENERATE_FINAL_LOCALS;