mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-75548 Groovy surrounding should work correctly with comments
This commit is contained in:
+1
-4
@@ -79,10 +79,7 @@ public abstract class GroovyManyStatementsSurrounder implements Surrounder {
|
||||
}
|
||||
|
||||
protected static void addStatements(GrCodeBlock block, PsiElement[] elements) throws IncorrectOperationException {
|
||||
for (PsiElement element : elements) {
|
||||
final GrStatement statement = (GrStatement)element;
|
||||
block.addStatementBefore(statement, null);
|
||||
}
|
||||
block.addRangeBefore(elements[0], elements[elements.length - 1], block.getRBrace());
|
||||
}
|
||||
|
||||
protected abstract GroovyPsiElement doSurroundElements(PsiElement[] elements, PsiElement context) throws IncorrectOperationException;
|
||||
|
||||
+2
-55
@@ -15,21 +15,12 @@
|
||||
*/
|
||||
package org.jetbrains.plugins.groovy.lang.surroundWith;
|
||||
|
||||
import com.intellij.lang.ASTNode;
|
||||
import com.intellij.lang.surroundWith.SurroundDescriptor;
|
||||
import com.intellij.lang.surroundWith.Surrounder;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.PsiWhiteSpace;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.plugins.groovy.lang.lexer.GroovyTokenTypes;
|
||||
import org.jetbrains.plugins.groovy.lang.lexer.TokenSets;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import org.jetbrains.plugins.groovy.refactoring.GroovyRefactoringUtil;
|
||||
|
||||
/**
|
||||
* User: Dmitry.Krasilschikov
|
||||
@@ -70,51 +61,7 @@ public class GroovySurroundDescriptor implements SurroundDescriptor {
|
||||
|
||||
@NotNull
|
||||
public PsiElement[] getElementsToSurround(PsiFile file, int startOffset, int endOffset) {
|
||||
GrStatement[] statements = findStatementsInRange(file, startOffset, endOffset);
|
||||
|
||||
if (statements == null) return PsiElement.EMPTY_ARRAY;
|
||||
return statements;
|
||||
return GroovyRefactoringUtil.findStatementsInRange(file, startOffset, endOffset, true);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static GrStatement[] findStatementsInRange(PsiFile file, int startOffset, int endOffset) {
|
||||
|
||||
GrStatement statement;
|
||||
int endOffsetLocal = endOffset;
|
||||
int startOffsetLocal = startOffset;
|
||||
|
||||
List<GrStatement> statements = new ArrayList<GrStatement>();
|
||||
do {
|
||||
PsiElement element1 = file.findElementAt(startOffsetLocal);
|
||||
PsiElement element2 = file.findElementAt(endOffsetLocal - 1);
|
||||
|
||||
if (element1 == null) break;
|
||||
ASTNode node1 = element1.getNode();
|
||||
assert node1 != null;
|
||||
if (element1 instanceof PsiWhiteSpace || TokenSets.WHITE_SPACE_TOKEN_SET.contains(node1.getElementType()) || GroovyTokenTypes.mNLS.equals(node1.getElementType())) {
|
||||
startOffsetLocal = element1.getTextRange().getEndOffset();
|
||||
}
|
||||
|
||||
if (element2 == null) break;
|
||||
ASTNode node2 = element2.getNode();
|
||||
assert node2 != null;
|
||||
if (element2 instanceof PsiWhiteSpace || TokenSets.WHITE_SPACE_TOKEN_SET.contains(node2.getElementType()) || GroovyTokenTypes.mNLS.equals(node2.getElementType())) {
|
||||
endOffsetLocal = element2.getTextRange().getStartOffset();
|
||||
}
|
||||
|
||||
if (";".equals(element2.getText())) endOffsetLocal = endOffsetLocal - 1;
|
||||
|
||||
statement = PsiTreeUtil.findElementOfClassAtRange(file, startOffsetLocal, endOffsetLocal, GrStatement.class);
|
||||
|
||||
if (statement == null) break;
|
||||
statements.add(statement);
|
||||
|
||||
startOffsetLocal = statement.getTextRange().getEndOffset();
|
||||
final PsiElement endSemicolon = file.findElementAt(startOffsetLocal);
|
||||
|
||||
if (endSemicolon != null && ";".equals(endSemicolon.getText())) startOffsetLocal = startOffsetLocal + 1;
|
||||
} while (true);
|
||||
|
||||
return statements.toArray(new GrStatement[0]);
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -46,7 +46,7 @@ public class SurrounderByClosure extends GroovyManyStatementsSurrounder {
|
||||
}
|
||||
|
||||
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(elements[0].getProject());
|
||||
final GrMethodCallExpression call = (GrMethodCallExpression) factory.createExpressionFromText("{ -> }.call()", context);
|
||||
final GrMethodCallExpression call = (GrMethodCallExpression) factory.createExpressionFromText("{ -> \n}.call()", context);
|
||||
final GrClosableBlock closure = (GrClosableBlock) ((GrReferenceExpression) call.getInvokedExpression()).getQualifierExpression();
|
||||
addStatements(closure, elements);
|
||||
return call;
|
||||
|
||||
+1
-1
@@ -267,7 +267,7 @@ public abstract class GroovyRefactoringUtil {
|
||||
editor.getSelectionModel().setSelection(start, end);
|
||||
}
|
||||
|
||||
public static PsiElement[] findStatementsInRange(PsiFile file, int startOffset, int endOffset, boolean strict) {
|
||||
@NotNull public static PsiElement[] findStatementsInRange(PsiFile file, int startOffset, int endOffset, boolean strict) {
|
||||
if (!(file instanceof GroovyFileBase)) return PsiElement.EMPTY_ARRAY;
|
||||
Language language = GroovyFileType.GROOVY_FILE_TYPE.getLanguage();
|
||||
PsiElement element1 = file.getViewProvider().findElementAt(startOffset, language);
|
||||
|
||||
+1
@@ -28,5 +28,6 @@ public class SurroundStatementsTest extends SurroundTestCase {
|
||||
public void testWhile1() throws Exception { doTest(new WhileSurrounder()); }
|
||||
public void testWith2() throws Exception { doTest(new WithStatementsSurrounder()); }
|
||||
public void testFor1() throws Exception { doTest(new ForSurrounder()); }
|
||||
public void testIfComments() throws Exception { doTest(new IfSurrounder()); }
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
def foo() {
|
||||
<selection>println 'hi' //foo
|
||||
println 'hi' //foo</selection>
|
||||
}
|
||||
-----
|
||||
def foo() {
|
||||
if (<caret>) {
|
||||
println 'hi' //foo
|
||||
println 'hi' //foo
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user