use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock in project intellij-community by JetBrains.
the class GroovyBlockStatementsSelectioner method select.
@Override
public List<TextRange> select(PsiElement e, CharSequence editorText, int cursorOffset, Editor editor) {
List<TextRange> result = super.select(e, editorText, cursorOffset, editor);
if (e instanceof GrCodeBlock) {
GrCodeBlock block = ((GrCodeBlock) e);
int startOffset = findOpeningBrace(block);
int endOffset = findClosingBrace(block, startOffset);
TextRange range = new TextRange(startOffset, endOffset);
result.addAll(expandToWholeLine(editorText, range));
}
return result;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock in project intellij-community by JetBrains.
the class GrChangeSignatureUsageProcessor method generateDelegate.
private static boolean generateDelegate(JavaChangeInfo grInfo) {
final GrMethod method = (GrMethod) grInfo.getMethod();
final PsiClass psiClass = method.getContainingClass();
GrMethod newMethod = (GrMethod) method.copy();
newMethod = (GrMethod) psiClass.addAfter(newMethod, method);
StringBuilder buffer = new StringBuilder();
buffer.append("\n");
if (method.isConstructor()) {
buffer.append("this");
} else {
if (!PsiType.VOID.equals(method.getReturnType())) {
buffer.append("return ");
}
buffer.append(GrChangeSignatureUtil.getNameWithQuotesIfNeeded(grInfo.getNewName(), method.getProject()));
}
generateParametersForDelegateCall(grInfo, method, buffer);
final GrCodeBlock codeBlock = GroovyPsiElementFactory.getInstance(method.getProject()).createMethodBodyFromText(buffer.toString());
newMethod.setBlock(codeBlock);
newMethod.getModifierList().setModifierProperty(PsiModifier.ABSTRACT, false);
CodeStyleManager.getInstance(method.getProject()).reformat(newMethod);
return processPrimaryMethodInner(grInfo, method, null);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock in project intellij-community by JetBrains.
the class GroovyRefactoringUtil method findStatementsInRange.
@NotNull
public static PsiElement[] findStatementsInRange(PsiFile file, int startOffset, int endOffset, boolean strict) {
if (!(file instanceof GroovyFileBase))
return PsiElement.EMPTY_ARRAY;
Language language = GroovyLanguage.INSTANCE;
PsiElement element1 = file.getViewProvider().findElementAt(startOffset, language);
PsiElement element2 = file.getViewProvider().findElementAt(endOffset - 1, language);
if (element1 instanceof PsiWhiteSpace || org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil.isNewLine(element1)) {
startOffset = element1.getTextRange().getEndOffset();
element1 = file.findElementAt(startOffset);
}
if (element2 instanceof PsiWhiteSpace || org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil.isNewLine(element2)) {
endOffset = element2.getTextRange().getStartOffset();
element2 = file.findElementAt(endOffset - 1);
}
if (element1 == null || element2 == null)
return PsiElement.EMPTY_ARRAY;
PsiElement parent = PsiTreeUtil.findCommonParent(element1, element2);
if (parent == null)
return PsiElement.EMPTY_ARRAY;
while (true) {
if (parent instanceof GrCodeBlock)
break;
if (parent instanceof GroovyFileBase)
break;
if (parent instanceof GrCaseSection)
break;
if (parent instanceof GrStatement) {
parent = parent.getParent();
break;
}
if (parent == null)
return PsiElement.EMPTY_ARRAY;
final PsiElement prev = parent;
parent = parent.getParent();
if (parent instanceof GrCodeBlock && prev instanceof LeafPsiElement) {
//braces
parent = parent.getParent();
}
}
if (!parent.equals(element1)) {
while (!parent.equals(element1.getParent())) {
element1 = element1.getParent();
}
}
if (startOffset != element1.getTextRange().getStartOffset() && strict)
return PsiElement.EMPTY_ARRAY;
if (!parent.equals(element2)) {
while (!parent.equals(element2.getParent())) {
element2 = element2.getParent();
}
}
if (endOffset != element2.getTextRange().getEndOffset() && strict)
return PsiElement.EMPTY_ARRAY;
if (parent instanceof GrCodeBlock && parent.getParent() instanceof GrBlockStatement && element1 == ((GrCodeBlock) parent).getLBrace() && element2 == ((GrCodeBlock) parent).getRBrace()) {
return new PsiElement[] { parent.getParent() };
}
// calculate children
PsiElement[] children = PsiElement.EMPTY_ARRAY;
PsiElement psiChild = parent.getFirstChild();
if (psiChild != null) {
List<PsiElement> result = new ArrayList<>();
while (psiChild != null) {
result.add(psiChild);
psiChild = psiChild.getNextSibling();
}
children = PsiUtilCore.toPsiElementArray(result);
}
ArrayList<PsiElement> possibleStatements = new ArrayList<>();
boolean flag = false;
for (PsiElement child : children) {
if (child == element1) {
flag = true;
}
if (flag) {
possibleStatements.add(child);
}
if (child == element2) {
break;
}
}
for (PsiElement element : possibleStatements) {
if (!(element instanceof GrStatement || element instanceof PsiWhiteSpace || element instanceof PsiComment || TokenSets.SEPARATORS.contains(element.getNode().getElementType()))) {
return PsiElement.EMPTY_ARRAY;
}
}
return PsiUtilCore.toPsiElementArray(possibleStatements);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock in project intellij-community by JetBrains.
the class RecursionUtils method tryStatementDefinitelyRecurses.
private static boolean tryStatementDefinitelyRecurses(GrTryCatchStatement tryStatement, GrMethod method) {
final GrCodeBlock tryBlock = tryStatement.getTryBlock();
if (codeBlockDefinitelyRecurses(tryBlock, method)) {
return true;
}
final GrFinallyClause finallyBlock = tryStatement.getFinallyClause();
if (finallyBlock == null) {
return false;
}
return codeBlockDefinitelyRecurses(finallyBlock.getBody(), method);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock in project intellij-community by JetBrains.
the class RecursionUtils method statementDefinitelyRecurses.
private static boolean statementDefinitelyRecurses(@Nullable GrStatement statement, GrMethod method) {
if (statement == null) {
return false;
}
if (statement instanceof GrBreakStatement || statement instanceof GrContinueStatement || statement instanceof GrThrowStatement || statement instanceof GrAssertStatement) {
return false;
} else if (statement instanceof GrExpression) {
final GrExpression expression = (GrExpression) statement;
return expressionDefinitelyRecurses(expression, method);
} else if (statement instanceof GrVariableDeclaration) {
final GrVariableDeclaration declaration = (GrVariableDeclaration) statement;
final GrVariable[] declaredElements = declaration.getVariables();
for (final GrVariable variable : declaredElements) {
final GrExpression initializer = (GrExpression) variable.getInitializer();
if (expressionDefinitelyRecurses(initializer, method)) {
return true;
}
}
return false;
} else if (statement instanceof GrReturnStatement) {
final GrReturnStatement returnStatement = (GrReturnStatement) statement;
final GrExpression returnValue = returnStatement.getReturnValue();
if (returnValue != null) {
if (expressionDefinitelyRecurses(returnValue, method)) {
return true;
}
}
return false;
} else if (statement instanceof GrForStatement) {
return forStatementDefinitelyRecurses((GrForStatement) statement, method);
} else if (statement instanceof GrWhileStatement) {
return whileStatementDefinitelyRecurses((GrWhileStatement) statement, method);
} else if (statement instanceof GrSynchronizedStatement) {
final GrCodeBlock body = ((GrSynchronizedStatement) statement).getBody();
return codeBlockDefinitelyRecurses(body, method);
} else if (statement instanceof GrBlockStatement) {
final GrCodeBlock codeBlock = ((GrBlockStatement) statement).getBlock();
return codeBlockDefinitelyRecurses(codeBlock, method);
} else if (statement instanceof GrIfStatement) {
return ifStatementDefinitelyRecurses((GrIfStatement) statement, method);
} else if (statement instanceof GrTryCatchStatement) {
return tryStatementDefinitelyRecurses((GrTryCatchStatement) statement, method);
} else if (statement instanceof GrSwitchStatement) {
return switchStatementDefinitelyRecurses((GrSwitchStatement) statement, method);
} else {
// unknown statement type
return false;
}
}
Aggregations