Search in sources :

Example 56 with GrOpenBlock

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

the class CreateParameterForFieldIntention method addParameter.

private static void addParameter(final GrField selectedValue, final GrMethod constructor, final Project project) {
    List<GrParameterInfo> parameters = new ArrayList<>();
    GrParameter[] constructorParameters = constructor.getParameters();
    for (int i = 0; i < constructorParameters.length; i++) {
        parameters.add(new GrParameterInfo(constructorParameters[i], i));
    }
    final String[] suggestedNames = JavaCodeStyleManager.getInstance(project).suggestVariableName(VariableKind.PARAMETER, selectedValue.getName(), null, null).names;
    final DefaultGroovyVariableNameValidator nameValidator = new DefaultGroovyVariableNameValidator(constructor, Collections.<String>emptyList(), false);
    String parameterName = ContainerUtil.find(suggestedNames, name -> !nameValidator.validateName(name, false).isEmpty());
    if (parameterName == null) {
        parameterName = nameValidator.validateName(suggestedNames[0], true);
    }
    parameters.add(new GrParameterInfo(parameterName, "null", "", selectedValue.getTypeGroovy(), -1, false));
    PsiClassType[] exceptionTypes = constructor.getThrowsList().getReferencedTypes();
    ThrownExceptionInfo[] thrownExceptionInfos = new ThrownExceptionInfo[exceptionTypes.length];
    for (int i = 0; i < exceptionTypes.length; i++) {
        new JavaThrownExceptionInfo(i, exceptionTypes[i]);
    }
    final GrChangeInfoImpl grChangeInfo = new GrChangeInfoImpl(constructor, null, null, constructor.getName(), parameters, thrownExceptionInfos, false);
    final String finalParameterName = parameterName;
    final GrChangeSignatureProcessor processor = new GrChangeSignatureProcessor(project, grChangeInfo) {

        @Override
        protected void performRefactoring(@NotNull UsageInfo[] usages) {
            super.performRefactoring(usages);
            final GrOpenBlock block = constructor.getBlock();
            LOG.assertTrue(block != null);
            final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
            final String text;
            if (StringUtil.equals(selectedValue.getName(), finalParameterName)) {
                text = "this." + selectedValue.getName() + " = " + finalParameterName;
            } else {
                text = selectedValue.getName() + " = " + finalParameterName;
            }
            final GrStatement assignment = factory.createStatementFromText(text);
            final GrStatement statement = block.addStatementBefore(assignment, null);
            final GrReferenceExpression ref = (GrReferenceExpression) ((GrAssignmentExpression) statement).getLValue();
            if (!PsiManager.getInstance(project).areElementsEquivalent(ref.resolve(), selectedValue)) {
                PsiUtil.qualifyMemberReference(ref, selectedValue, selectedValue.getName());
            }
        }
    };
    processor.run();
}
Also used : JavaThrownExceptionInfo(com.intellij.refactoring.changeSignature.JavaThrownExceptionInfo) ArrayList(java.util.ArrayList) DefaultGroovyVariableNameValidator(org.jetbrains.plugins.groovy.refactoring.DefaultGroovyVariableNameValidator) GrChangeInfoImpl(org.jetbrains.plugins.groovy.refactoring.changeSignature.GrChangeInfoImpl) GrChangeSignatureProcessor(org.jetbrains.plugins.groovy.refactoring.changeSignature.GrChangeSignatureProcessor) ThrownExceptionInfo(com.intellij.refactoring.changeSignature.ThrownExceptionInfo) JavaThrownExceptionInfo(com.intellij.refactoring.changeSignature.JavaThrownExceptionInfo) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) NotNull(org.jetbrains.annotations.NotNull) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrParameterInfo(org.jetbrains.plugins.groovy.refactoring.changeSignature.GrParameterInfo) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)

Example 57 with GrOpenBlock

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

the class CreateParameterForFieldIntention method findCandidates.

private static List<GrField> findCandidates(PsiMethod constructor, final GrTypeDefinition clazz) {
    final List<GrField> usedFields = new ArrayList<>();
    final GrOpenBlock block = constructor instanceof GrMethod ? ((GrMethod) constructor).getBlock() : null;
    if (block == null) {
        return usedFields;
    }
    final PsiManager manager = clazz.getManager();
    block.accept(new GroovyRecursiveElementVisitor() {

        @Override
        public void visitReferenceExpression(@NotNull GrReferenceExpression referenceExpression) {
            super.visitReferenceExpression(referenceExpression);
            final PsiElement resolved = referenceExpression.resolve();
            if (resolved instanceof GrField && manager.areElementsEquivalent(((GrField) resolved).getContainingClass(), clazz) && PsiUtil.isAccessedForWriting(referenceExpression)) {
                usedFields.add((GrField) resolved);
            }
        }

        @Override
        public void visitTypeDefinition(@NotNull GrTypeDefinition typeDefinition) {
        }

        @Override
        public void visitClosure(@NotNull GrClosableBlock closure) {
        }
    });
    List<GrField> fields = new ArrayList<>();
    for (final GrField field : clazz.getFields()) {
        if (field.getInitializerGroovy() != null)
            continue;
        if (ContainerUtil.find(usedFields, new Condition<PsiField>() {

            @Override
            public boolean value(PsiField o) {
                return manager.areElementsEquivalent(o, field);
            }
        }) == null) {
            fields.add(field);
        }
    }
    return fields;
}
Also used : Condition(com.intellij.openapi.util.Condition) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) ArrayList(java.util.ArrayList) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GroovyRecursiveElementVisitor(org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)

Example 58 with GrOpenBlock

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

the class GrRedundantElseIntention method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    final PsiElement parent = element.getParent();
    if (!(parent instanceof GrIfStatement))
        return;
    final GrIfStatement ifStatement = GroovyRefactoringUtil.addBlockIntoParent((GrIfStatement) parent);
    assert ifStatement.getParent() instanceof GrStatementOwner;
    final PsiElement statementOwner = ifStatement.getParent();
    final GrStatement branch = ifStatement.getElseBranch();
    if (branch == null)
        return;
    final PsiElement pos;
    if (branch instanceof GrBlockStatement) {
        final GrOpenBlock block = ((GrBlockStatement) branch).getBlock();
        final PsiElement first = inferFirst(block.getLBrace());
        final PsiElement last = inferLast(block.getRBrace());
        pos = statementOwner.addRangeAfter(first, last, ifStatement);
    } else {
        pos = statementOwner.addAfter(branch, ifStatement);
    }
    branch.delete();
    editor.getCaretModel().moveToOffset(pos.getTextRange().getStartOffset());
}
Also used : GrIfStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement) GrStatementOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) GrBlockStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement) PsiElement(com.intellij.psi.PsiElement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 59 with GrOpenBlock

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

the class InvertIfIntention method generateElseBranchTextAndRemoveTailStatements.

private static void generateElseBranchTextAndRemoveTailStatements(@NotNull GrIfStatement ifStatement, @NotNull GrIfStatement newIf) {
    final GrStatement thenBranch = newIf.getThenBranch();
    assert thenBranch != null;
    GrStatement elseBranch = ifStatement.getElseBranch();
    if (elseBranch != null) {
        thenBranch.replaceWithStatement(elseBranch);
        return;
    }
    PsiElement parent = ifStatement.getParent();
    if (!(parent instanceof GrStatementOwner))
        return;
    if (!isTailAfterIf(ifStatement, ((GrStatementOwner) parent)))
        return;
    final PsiElement start = ifStatement.getNextSibling();
    PsiElement end = parent instanceof GrCodeBlock ? ((GrCodeBlock) parent).getRBrace().getPrevSibling() : parent.getLastChild();
    final GrOpenBlock block = ((GrBlockStatement) thenBranch).getBlock();
    block.addRangeAfter(start, end, block.getLBrace());
    parent.deleteChildRange(start, end);
}
Also used : GrStatementOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) GrBlockStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement) PsiElement(com.intellij.psi.PsiElement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) GrCodeBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)

Example 60 with GrOpenBlock

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

the class MergeElseIfPredicate method satisfiedBy.

@Override
public boolean satisfiedBy(PsiElement element) {
    if (!(element instanceof GrIfStatement)) {
        return false;
    }
    final GrIfStatement ifStatement = (GrIfStatement) element;
    final GrStatement thenBranch = ifStatement.getThenBranch();
    if (thenBranch == null) {
        return false;
    }
    final GrStatement elseBranch = ifStatement.getElseBranch();
    if (!(elseBranch instanceof GrBlockStatement)) {
        return false;
    }
    final GrOpenBlock block = ((GrBlockStatement) elseBranch).getBlock();
    final GrStatement[] statements = block.getStatements();
    return statements.length == 1 && statements[0] instanceof GrIfStatement;
}
Also used : GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)

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