Preserve comment right after the loop

GitOrigin-RevId: e096580618c805f0e1f5c8e1a878b5e3321174c7
This commit is contained in:
Tagir Valeev
2020-06-23 17:07:46 +07:00
committed by intellij-monorepo-bot
parent b2c6daba06
commit 1f6ad2d2bb
3 changed files with 9 additions and 3 deletions

View File

@@ -206,7 +206,6 @@ public class UnrollLoopAction extends PsiElementBaseIntentionAction {
CommentTracker ct = new CommentTracker();
PsiElement anchor = loop;
for (PsiExpression expression : expressions) {
ct.markUnchanged(loop.getBody());
PsiLoopStatement copy = (PsiLoopStatement)factory.createStatementFromText(loop.getText(), element);
PsiVariable variable = Objects.requireNonNull(getVariable(copy));
for (PsiReference reference : ReferencesSearch.search(variable, new LocalSearchScope(copy))) {
@@ -220,10 +219,17 @@ public class UnrollLoopAction extends PsiElementBaseIntentionAction {
assert body != null;
PsiElement[] children;
if (body instanceof PsiBlockStatement) {
PsiCodeBlock block = ((PsiBlockStatement)Objects.requireNonNull(loop.getBody())).getCodeBlock();
PsiElement firstBodyElement = block.getFirstBodyElement();
PsiElement lastBodyElement = block.getLastBodyElement();
if (firstBodyElement != null && lastBodyElement != null) {
ct.markRangeUnchanged(firstBodyElement, lastBodyElement);
}
children = ((PsiBlockStatement)body).getCodeBlock().getChildren();
// Skip {braces}
children = Arrays.copyOfRange(children, 1, children.length-1);
} else {
ct.markUnchanged(loop.getBody());
children = new PsiElement[]{body};
}
for(PsiElement child : children) {

View File

@@ -12,7 +12,7 @@ class Test {
}
}
}
}
}//Comment
}
void foo(boolean b) {}

View File

@@ -4,7 +4,7 @@ class Test {
fo<caret>r(Object x : new Object[] {"one", 1, 1.0, 1.0f}) {
if(Math.random() > 0.5) break;
System.out.println(x);
}
}//Comment
}
void foo(boolean b) {}