Search in sources :

Example 76 with GrStatement

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

the class SwitchStatementGenerator method generateCaseSection.

private static void generateCaseSection(@NotNull StringBuilder builder, @NotNull ExpressionContext context, @NotNull ExpressionContext innerContext, @NotNull GrCaseSection section) {
    for (GrCaseLabel label : section.getCaseLabels()) {
        writeLabel(builder, context, label);
    }
    final GrStatement[] statements = section.getStatements();
    CodeBlockGenerator generator = new CodeBlockGenerator(builder, innerContext);
    for (GrStatement statement : statements) {
        statement.accept(generator);
        builder.append("\n");
    }
}
Also used : GrCaseLabel(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseLabel) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 77 with GrStatement

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

the class ClassItemGeneratorImpl method generateDelegateCall.

private StringBuilder generateDelegateCall(GrReflectedMethod method) {
    final GrParameter[] actualParams = method.getParameterList().getParameters();
    final GrParameter[] parameters = method.getBaseMethod().getParameters();
    Set<String> actual = new HashSet<>(actualParams.length);
    for (GrParameter param : actualParams) {
        actual.add(param.getName());
    }
    StringBuilder builder = new StringBuilder();
    if (method.isConstructor()) {
        builder.append("this");
    } else {
        if (!PsiType.VOID.equals(context.typeProvider.getReturnType(method))) {
            builder.append("return ");
        }
        builder.append(method.getName());
    }
    builder.append('(');
    for (GrParameter parameter : parameters) {
        if (actual.contains(parameter.getName())) {
            builder.append(parameter.getName());
        } else {
            LOG.assertTrue(parameter.isOptional());
            final GrExpression initializer = parameter.getInitializerGroovy();
            LOG.assertTrue(initializer != null);
            builder.append(initializer.getText());
        }
        builder.append(", ");
    }
    builder.delete(builder.length() - 2, builder.length());
    //builder.removeFromTheEnd(2);
    builder.append(')');
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(context.project);
    final GrStatement delegateCall;
    if (method.isConstructor()) {
        delegateCall = factory.createConstructorInvocation(builder.toString(), method);
    } else {
        delegateCall = factory.createStatementFromText(builder.toString(), method);
    }
    final StringBuilder result = new StringBuilder();
    delegateCall.accept(new CodeBlockGenerator(result, this.context.extend()));
    return result;
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) HashSet(com.intellij.util.containers.HashSet) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 78 with GrStatement

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

the class GroovyExtractChooser method isLastStatementOfMethodOrClosure.

private static boolean isLastStatementOfMethodOrClosure(GrStatement[] statements) {
    final GrStatement statement0 = statements[0];
    PsiElement returnFrom = PsiTreeUtil.getParentOfType(statement0, GrMethod.class, GrClosableBlock.class, GroovyFile.class);
    if (returnFrom instanceof GrMethod) {
        returnFrom = ((GrMethod) returnFrom).getBlock();
    }
    LOG.assertTrue(returnFrom instanceof GrStatementOwner);
    final GrStatement[] blockStatements = ((GrStatementOwner) returnFrom).getStatements();
    final GrStatement lastFromBlock = ArrayUtil.getLastElement(blockStatements);
    final GrStatement lastStatement = ArrayUtil.getLastElement(statements);
    return statement0.getManager().areElementsEquivalent(lastFromBlock, lastStatement);
}
Also used : GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrStatementOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 79 with GrStatement

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

the class ExtractClosureFromClosureProcessor method preprocessUsages.

@Override
protected boolean preprocessUsages(@NotNull Ref<UsageInfo[]> refUsages) {
    UsageInfo[] usagesIn = refUsages.get();
    MultiMap<PsiElement, String> conflicts = new MultiMap<>();
    if (!myHelper.generateDelegate()) {
        for (GrStatement statement : myHelper.getStatements()) {
            GroovyIntroduceParameterUtil.detectAccessibilityConflicts(statement, usagesIn, conflicts, myHelper.replaceFieldsWithGetters() != IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, myProject);
        }
    }
    return showConflicts(conflicts, usagesIn);
}
Also used : MultiMap(com.intellij.util.containers.MultiMap) ExternalUsageInfo(com.intellij.refactoring.introduceParameter.ExternalUsageInfo) UsageInfo(com.intellij.usageView.UsageInfo) PsiElement(com.intellij.psi.PsiElement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 80 with GrStatement

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

the class ExtractClosureFromMethodProcessor method performRefactoring.

@Override
protected void performRefactoring(@NotNull UsageInfo[] usages) {
    final IntroduceParameterData data = new IntroduceParameterDataAdapter();
    IntroduceParameterUtil.processUsages(usages, data);
    final PsiMethod toSearchFor = (PsiMethod) myHelper.getToSearchFor();
    final boolean methodsToProcessAreDifferent = myMethod != toSearchFor;
    if (myHelper.generateDelegate()) {
        GroovyIntroduceParameterUtil.generateDelegate(myMethod, data.getParameterInitializer(), myProject);
        if (methodsToProcessAreDifferent) {
            final GrMethod method = GroovyIntroduceParameterUtil.generateDelegate(toSearchFor, data.getParameterInitializer(), myProject);
            final PsiClass containingClass = method.getContainingClass();
            if (containingClass != null && containingClass.isInterface()) {
                final GrOpenBlock block = method.getBlock();
                if (block != null) {
                    block.delete();
                }
            }
        }
    }
    // Changing signature of initial method
    // (signature of myMethodToReplaceIn will be either changed now or have already been changed)
    final FieldConflictsResolver fieldConflictsResolver = new FieldConflictsResolver(myHelper.getName(), myMethod.getBlock());
    IntroduceParameterUtil.changeMethodSignatureAndResolveFieldConflicts(new UsageInfo(myMethod), usages, data);
    if (methodsToProcessAreDifferent) {
        IntroduceParameterUtil.changeMethodSignatureAndResolveFieldConflicts(new UsageInfo(toSearchFor), usages, data);
    }
    // Replacing expression occurrences
    for (UsageInfo usage : usages) {
        if (usage instanceof ChangedMethodCallInfo) {
            PsiElement element = usage.getElement();
            GroovyIntroduceParameterUtil.processChangedMethodCall(element, myHelper, myProject);
        }
    }
    final GrStatement newStatement = ExtractUtil.replaceStatement(myDeclarationOwner, myHelper);
    final Editor editor = PsiUtilBase.findEditor(newStatement);
    if (editor != null) {
        PsiDocumentManager.getInstance(myProject).commitDocument(editor.getDocument());
        editor.getSelectionModel().removeSelection();
        editor.getCaretModel().moveToOffset(newStatement.getTextRange().getEndOffset());
    }
    fieldConflictsResolver.fix();
}
Also used : GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) FieldConflictsResolver(org.jetbrains.plugins.groovy.refactoring.introduce.parameter.FieldConflictsResolver) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) Editor(com.intellij.openapi.editor.Editor) UsageInfo(com.intellij.usageView.UsageInfo) NoConstructorClassUsageInfo(com.intellij.refactoring.util.usageInfo.NoConstructorClassUsageInfo) DefaultConstructorImplicitUsageInfo(com.intellij.refactoring.util.usageInfo.DefaultConstructorImplicitUsageInfo)

Aggregations

GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)113 PsiElement (com.intellij.psi.PsiElement)36 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)26 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)22 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)21 GrIfStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement)17 TextRange (com.intellij.openapi.util.TextRange)14 Nullable (org.jetbrains.annotations.Nullable)14 GrBlockStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement)13 NotNull (org.jetbrains.annotations.NotNull)12 GrReturnStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement)12 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)12 GrStatementOwner (org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner)11 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)10 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)10 GrVariableDeclaration (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration)9 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)9 GrAssignmentExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression)8 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)8 Document (com.intellij.openapi.editor.Document)6