Search in sources :

Example 16 with GrCodeBlock

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;
}
Also used : TextRange(com.intellij.openapi.util.TextRange) GrCodeBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)

Example 17 with GrCodeBlock

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);
}
Also used : GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrCodeBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)

Example 18 with GrCodeBlock

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);
}
Also used : Language(com.intellij.lang.Language) GroovyLanguage(org.jetbrains.plugins.groovy.GroovyLanguage) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) GrCaseSection(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) GrCodeBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock) NotNull(org.jetbrains.annotations.NotNull)

Example 19 with GrCodeBlock

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);
}
Also used : GrCodeBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)

Example 20 with GrCodeBlock

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;
    }
}
Also used : GrCodeBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)

Aggregations

GrCodeBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)22 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)10 PsiElement (com.intellij.psi.PsiElement)6 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)5 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)5 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)4 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)4 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)3 IncorrectOperationException (com.intellij.util.IncorrectOperationException)3 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)3 GrCaseSection (org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection)3 ASTNode (com.intellij.lang.ASTNode)2 ArrayList (java.util.ArrayList)2 GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)2 GrArgumentLabel (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentLabel)2 GrString (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString)2 GrTypeDefinitionBody (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinitionBody)2 GrStatementOwner (org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner)2 LineRange (com.intellij.codeInsight.editorActions.moveUpDown.LineRange)1 FileTemplate (com.intellij.ide.fileTemplates.FileTemplate)1