mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-13 21:55:01 +07:00
surround with closeable: don't include unrelated variables (IDEA-159434)
This commit is contained in:
+4
-1
@@ -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) {
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
class C {
|
||||
void m(File file) throws IOException {
|
||||
<caret>FileInputStream fileInputStream = new FileInputStream(file);
|
||||
String s = "initial value";//Non-NLS
|
||||
s = s + fileInputStream.read();
|
||||
s += "end";
|
||||
int time = 0;
|
||||
System.out.println(time);
|
||||
}
|
||||
}
|
||||
+17
@@ -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);
|
||||
}
|
||||
}
|
||||
+1
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user