correct formatting after surrounding two statements at the end of a code block

This commit is contained in:
peter
2011-03-29 20:06:46 +02:00
parent a91d56845f
commit 866e810fbe
3 changed files with 16 additions and 26 deletions
@@ -21,11 +21,9 @@ import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElement;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.groovy.lang.parser.GroovyElementTypes;
import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement;
@@ -72,31 +70,10 @@ public abstract class GroovyManyStatementsSurrounder implements Surrounder {
assert newStmt != null;
ASTNode parentNode = element1.getParent().getNode();
for (int i = 0; i < elements.length; i++) {
PsiElement element = elements[i];
if (i == 0) {
parentNode.replaceChild(element1.getNode(), newStmt.getNode());
} else {
if (parentNode != element.getParent().getNode()) return null;
final int endOffset = element.getTextRange().getEndOffset();
final PsiElement semicolon = PsiTreeUtil.findElementOfClassAtOffset(element.getContainingFile(), endOffset, PsiElement.class, false);
if (semicolon != null && ";".equals(semicolon.getText())) {
assert parentNode == semicolon.getParent().getNode();
parentNode.removeChild(semicolon.getNode());
}
final PsiElement newLine = PsiTreeUtil.findElementOfClassAtOffset(element.getContainingFile(), endOffset, PsiElement.class, false);
if (newLine != null && GroovyElementTypes.mNLS.equals(newLine.getNode().getElementType())) {
assert parentNode == newLine.getParent().getNode();
parentNode.removeChild(newLine.getNode());
}
parentNode.removeChild(element.getNode());
}
if (elements.length > 1) {
parentNode.removeRange(element1.getNode().getTreeNext(), elements[elements.length - 1].getNode().getTreeNext());
}
parentNode.replaceChild(element1.getNode(), newStmt.getNode());
return getSurroundSelectionRange(newStmt);
}
@@ -24,6 +24,7 @@ public class SurroundStatementsTest extends SurroundTestCase {
public void testTry_catch1() throws Exception { doTest(new TryCatchSurrounder()); }
public void testTry_catch_finally() throws Exception { doTest(new TryCatchFinallySurrounder()); }
public void testTry_finally1() throws Exception { doTest(new TryFinallySurrounder()); }
public void testTry_finallyFormatting() throws Exception { doTest(new TryFinallySurrounder()); }
public void testWhile1() throws Exception { doTest(new WhileSurrounder()); }
public void testWith2() throws Exception { doTest(new WithStatementsSurrounder()); }
public void testFor1() throws Exception { doTest(new ForSurrounder()); }
@@ -0,0 +1,12 @@
def foo() {
<selection>println hello
println hello</selection>
}
-----
def foo() {
try {
<caret>println hello
println hello
} finally {
}
}