Search in sources :

Example 66 with GrOpenBlock

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock in project intellij-community by JetBrains.

the class GrCreateFieldForParameterIntention method performRefactoring.

@Override
protected void performRefactoring(Project project, PsiClass targetClass, PsiMethod method, PsiParameter myParameter, PsiType type, String fieldName, boolean methodStatic, boolean isFinal) {
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
    if (targetClass.findFieldByName(fieldName, false) == null) {
        String[] modifiers = getModifiers(methodStatic, isFinal);
        GrVariableDeclaration fieldDeclaration = factory.createFieldDeclaration(modifiers, fieldName, null, type);
        GrVariableDeclaration inserted = (GrVariableDeclaration) targetClass.add(fieldDeclaration);
        JavaCodeStyleManager.getInstance(project).shortenClassReferences(inserted);
    }
    GrOpenBlock block = ((GrMethod) method).getBlock();
    if (block == null)
        return;
    GrAssignmentExpression assignment = createAssignment(targetClass, myParameter, fieldName, methodStatic, factory);
    GrStatement anchor = getAnchor(block);
    GrStatement statement = block.addStatementBefore(assignment, anchor);
    JavaCodeStyleManager.getInstance(project).shortenClassReferences(statement);
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 67 with GrOpenBlock

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock in project intellij-community by JetBrains.

the class ControlFlowBuilderUtil method isCertainlyReturnStatement.

/**
   * check whether statement is return (the statement which provides return value) statement of method or closure.
   *
   * @param st
   * @return
   */
public static boolean isCertainlyReturnStatement(GrStatement st) {
    final PsiElement parent = st.getParent();
    if (parent instanceof GrOpenBlock) {
        if (st != ArrayUtil.getLastElement(((GrOpenBlock) parent).getStatements()))
            return false;
        PsiElement pparent = parent.getParent();
        if (pparent instanceof GrMethod) {
            return true;
        }
        if (pparent instanceof GrBlockStatement || pparent instanceof GrCatchClause || pparent instanceof GrLabeledStatement) {
            pparent = pparent.getParent();
        }
        if (pparent instanceof GrIfStatement || pparent instanceof GrControlStatement || pparent instanceof GrTryCatchStatement) {
            return isCertainlyReturnStatement((GrStatement) pparent);
        }
    } else if (parent instanceof GrClosableBlock) {
        return st == ArrayUtil.getLastElement(((GrClosableBlock) parent).getStatements());
    } else if (parent instanceof GroovyFileBase) {
        return st == ArrayUtil.getLastElement(((GroovyFileBase) parent).getStatements());
    } else if (parent instanceof GrForStatement || parent instanceof GrIfStatement && st != ((GrIfStatement) parent).getCondition() || parent instanceof GrSynchronizedStatement && st != ((GrSynchronizedStatement) parent).getMonitor() || parent instanceof GrWhileStatement && st != ((GrWhileStatement) parent).getCondition() || parent instanceof GrConditionalExpression && st != ((GrConditionalExpression) parent).getCondition() || parent instanceof GrElvisExpression) {
        return isCertainlyReturnStatement((GrStatement) parent);
    } else if (parent instanceof GrCaseSection) {
        final GrStatement[] statements = ((GrCaseSection) parent).getStatements();
        final GrStatement last = ArrayUtil.getLastElement(statements);
        final GrSwitchStatement switchStatement = (GrSwitchStatement) parent.getParent();
        if (last instanceof GrBreakStatement && statements.length > 1 && statements[statements.length - 2] == st) {
            return isCertainlyReturnStatement(switchStatement);
        } else if (st == last) {
            if (st instanceof GrBreakStatement || isLastStatementInCaseSection((GrCaseSection) parent, switchStatement)) {
                return isCertainlyReturnStatement(switchStatement);
            }
        }
    }
    return false;
}
Also used : GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrBreakStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrBreakStatement) GrCaseSection(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) GrControlStatement(org.jetbrains.plugins.groovy.lang.psi.api.formatter.GrControlStatement) PsiElement(com.intellij.psi.PsiElement)

Example 68 with GrOpenBlock

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock in project intellij-community by JetBrains.

the class GroovyConfigSlurperCompletionProvider method getPrefix.

@Nullable
public static List<String> getPrefix(GrReferenceExpression ref) {
    List<String> res = new ArrayList<>();
    GrExpression qualifier = ref.getQualifierExpression();
    while (qualifier != null) {
        if (!(qualifier instanceof GrReferenceExpression))
            return null;
        GrReferenceExpression r = (GrReferenceExpression) qualifier;
        String name = r.getReferenceName();
        if (name == null)
            return null;
        res.add(name);
        qualifier = r.getQualifierExpression();
    }
    PsiElement e = ref.getParent();
    if (e instanceof GrAssignmentExpression) {
        GrAssignmentExpression assignmentExpression = (GrAssignmentExpression) e;
        if (assignmentExpression.getLValue() != ref)
            return null;
        e = assignmentExpression.getParent();
    }
    while (true) {
        if (e instanceof PsiFile) {
            break;
        } else if (e instanceof GrClosableBlock) {
            PsiElement eCall = e.getParent();
            if (!(eCall instanceof GrMethodCall))
                return null;
            GrMethodCall call = (GrMethodCall) eCall;
            if (!isPropertyCall(call))
                return null;
            String name = extractPropertyName(call);
            if (name == null)
                return null;
            res.add(name);
            e = call.getParent();
        } else if (e instanceof GrBlockStatement || e instanceof GrOpenBlock || e instanceof GrIfStatement || e instanceof GrForStatement || e instanceof GrWhileStatement || e instanceof GrTryCatchStatement) {
            e = e.getParent();
        } else {
            return null;
        }
    }
    Collections.reverse(res);
    return res;
}
Also used : GrMethodCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) PsiFile(com.intellij.psi.PsiFile) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)68 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)24 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)21 PsiElement (com.intellij.psi.PsiElement)13 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)10 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)10 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)9 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)9 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)8 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)8 GrReturnStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement)7 Nullable (org.jetbrains.annotations.Nullable)6 NotNull (org.jetbrains.annotations.NotNull)5 GrBlockStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement)5 Instruction (org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction)5 ReadWriteVariableInstruction (org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction)5 TextRange (com.intellij.openapi.util.TextRange)4 GroovyRecursiveElementVisitor (org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor)4 GrCodeBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)4 UsageInfo (com.intellij.usageView.UsageInfo)3