mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
preserve comments: surround with auto-closeable
This commit is contained in:
+5
-3
@@ -190,7 +190,8 @@ public class SurroundAutoCloseableAction extends PsiElementBaseIntentionAction {
|
||||
if (!(child instanceof PsiDeclarationStatement)) continue;
|
||||
|
||||
PsiElement anchor = child;
|
||||
for (PsiElement declared : ((PsiDeclarationStatement)child).getDeclaredElements()) {
|
||||
PsiElement[] declaredElements = ((PsiDeclarationStatement)child).getDeclaredElements();
|
||||
for (PsiElement declared : declaredElements) {
|
||||
if (!(declared instanceof PsiLocalVariable)) continue;
|
||||
|
||||
int endOffset = last.getTextRange().getEndOffset();
|
||||
@@ -204,13 +205,14 @@ public class SurroundAutoCloseableAction extends PsiElementBaseIntentionAction {
|
||||
assert name != null : child.getText();
|
||||
toFormat.add(parent.addBefore(factory.createVariableDeclarationStatement(name, var.getType(), null), statement));
|
||||
|
||||
CommentTracker commentTracker = new CommentTracker();
|
||||
PsiExpression varInit = var.getInitializer();
|
||||
if (varInit != null) {
|
||||
String varAssignText = name + " = " + varInit.getText() + ";";
|
||||
String varAssignText = name + " = " + commentTracker.markUnchanged(varInit).getText() + ";";
|
||||
anchor = parent.addAfter(factory.createStatementFromText(varAssignText, parent), anchor);
|
||||
}
|
||||
|
||||
var.delete();
|
||||
commentTracker.deleteAndRestoreComments(declaredElements.length == 1 ? child : var);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
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
|
||||
String bar = s + fileInputStream.read();
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
class C {
|
||||
void m(File file) throws IOException {
|
||||
try (FileInputStream fileInputStream = new FileInputStream(file)) {
|
||||
String s = "initial value";//Non-NLS
|
||||
String bar = s + fileInputStream.read();
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
@@ -34,6 +34,7 @@ public class SurroundAutoCloseableActionTest extends LightCodeInsightFixtureTest
|
||||
public void testSplitVar() { doTest(); }
|
||||
public void testExpression() { doTest(); }
|
||||
public void testExpressionIncomplete() { doTest(); }
|
||||
public void testCommentsInVarDeclaration() { doTest(); }
|
||||
|
||||
private void doTest() {
|
||||
String name = getTestName(false);
|
||||
|
||||
Reference in New Issue
Block a user