Search in sources :

Example 86 with GrStatement

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

the class GrCaseSectionImpl method addStatementBefore.

@Override
@NotNull
public GrStatement addStatementBefore(@NotNull GrStatement element, @Nullable GrStatement anchor) throws IncorrectOperationException {
    ASTNode elemNode = element.copy().getNode();
    assert elemNode != null;
    final ASTNode anchorNode = anchor != null ? anchor.getNode() : null;
    getNode().addChild(elemNode, anchorNode);
    if (mayUseNewLinesAsSeparators()) {
        getNode().addLeaf(GroovyTokenTypes.mNLS, "\n", anchorNode);
    } else {
        getNode().addLeaf(GroovyTokenTypes.mSEMI, ";", anchorNode);
    }
    return (GrStatement) elemNode.getPsi();
}
Also used : ASTNode(com.intellij.lang.ASTNode) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) NotNull(org.jetbrains.annotations.NotNull)

Example 87 with GrStatement

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

the class ConditionalUtils method stripBraces.

public static GrStatement stripBraces(GrStatement branch) {
    if (branch instanceof GrBlockStatement) {
        final GrBlockStatement block = (GrBlockStatement) branch;
        final GrOpenBlock codeBlock = block.getBlock();
        final GrStatement[] statements = codeBlock.getStatements();
        if (statements.length == 1) {
            return statements[0];
        } else {
            return block;
        }
    } else {
        return branch;
    }
}
Also used : GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) GrBlockStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 88 with GrStatement

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement 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 89 with GrStatement

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

the class ExpandBooleanIntention method processIntention.

@Override
public void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    final GrStatement containingStatement = (GrStatement) element;
    if (ExpandBooleanPredicate.isBooleanAssignment(containingStatement)) {
        final GrAssignmentExpression assignmentExpression = (GrAssignmentExpression) containingStatement;
        final GrExpression rhs = assignmentExpression.getRValue();
        assert rhs != null;
        final String rhsText = rhs.getText();
        final GrExpression lhs = assignmentExpression.getLValue();
        final String lhsText = lhs.getText();
        @NonNls final String statement = "if(" + rhsText + "){\n" + lhsText + " = true\n}else{\n" + lhsText + " = false\n}";
        PsiImplUtil.replaceStatement(statement, containingStatement);
    } else if (ExpandBooleanPredicate.isBooleanReturn(containingStatement)) {
        final GrReturnStatement returnStatement = (GrReturnStatement) containingStatement;
        final GrExpression returnValue = returnStatement.getReturnValue();
        final String valueText = returnValue.getText();
        @NonNls final String statement = "if(" + valueText + "){\nreturn true\n}else{\nreturn false\n}";
        PsiImplUtil.replaceStatement(statement, containingStatement);
    }
}
Also used : NonNls(org.jetbrains.annotations.NonNls) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrReturnStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 90 with GrStatement

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement 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)

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