Search in sources :

Example 11 with GroovyPsiElementFactory

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

the class GroovyBraceEnforcer method replaceWithBlock.

private void replaceWithBlock(@NotNull GrStatement statement, GrStatement blockCandidate) {
    if (!statement.isValid()) {
        LOG.assertTrue(false);
    }
    if (!checkRangeContainsElement(blockCandidate))
        return;
    final PsiManager manager = statement.getManager();
    LOG.assertTrue(manager != null);
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(manager.getProject());
    String oldText = blockCandidate.getText();
    // There is a possible case that target block to wrap ends with single-line comment. Example:
    //     if (true) i = 1; // Cool assignment
    // We can't just surround target block of code with curly braces because the closing one will be treated as comment as well.
    // Hence, we perform a check if we have such situation at the moment and insert new line before the closing brace.
    StringBuilder buf = new StringBuilder(oldText.length() + 5);
    buf.append("{\n").append(oldText);
    buf.append("\n}");
    final int oldTextLength = statement.getTextLength();
    try {
        ASTNode newChild = SourceTreeToPsiMap.psiElementToTree(factory.createBlockStatementFromText(buf.toString(), null));
        ASTNode parent = SourceTreeToPsiMap.psiElementToTree(statement);
        ASTNode childToReplace = SourceTreeToPsiMap.psiElementToTree(blockCandidate);
        CodeEditUtil.replaceChild(parent, childToReplace, newChild);
        removeTailSemicolon(newChild, parent);
        CodeStyleManager.getInstance(statement.getProject()).reformat(statement, true);
    } catch (IncorrectOperationException e) {
        LOG.error(e);
    } finally {
        updateResultRange(oldTextLength, statement.getTextLength());
    }
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) ASTNode(com.intellij.lang.ASTNode) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 12 with GroovyPsiElementFactory

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

the class GrCreateMissingSwitchBranchesIntention method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    if (!(element instanceof GrSwitchStatement))
        return;
    final List<PsiEnumConstant> constants = findUnusedConstants((GrSwitchStatement) element);
    if (constants.isEmpty())
        return;
    final PsiEnumConstant first = constants.get(0);
    final PsiClass aClass = first.getContainingClass();
    if (aClass == null)
        return;
    String qName = aClass.getQualifiedName();
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
    PsiElement anchor = findAnchor(element);
    for (PsiEnumConstant constant : constants) {
        final GrCaseSection section = factory.createSwitchSection("case " + qName + "." + constant.getName() + ":\n break");
        final PsiElement added = element.addBefore(section, anchor);
        element.addBefore(factory.createLineTerminator(1), anchor);
        JavaCodeStyleManager.getInstance(project).shortenClassReferences(added);
    }
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrCaseSection(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection) GrSwitchStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrSwitchStatement)

Example 13 with GroovyPsiElementFactory

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

the class GrSplitDeclarationIntention method processSingleVar.

private static void processSingleVar(Project project, GrVariableDeclaration declaration, GrVariable variable) {
    GrExpression initializer = variable.getInitializerGroovy();
    if (initializer != null) {
        GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
        GrExpression assignment = factory.createExpressionFromText(variable.getName() + " = " + initializer.getText());
        initializer.delete();
        declaration = GroovyRefactoringUtil.addBlockIntoParent(declaration);
        declaration.getParent().addAfter(assignment, declaration);
    }
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)

Example 14 with GroovyPsiElementFactory

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

the class GroovyExtractMethodHandler method renameParameterOccurrences.

private static void renameParameterOccurrences(GrMethod method, ExtractMethodInfoHelper helper) throws IncorrectOperationException {
    GrOpenBlock block = method.getBlock();
    if (block == null)
        return;
    GrStatement[] statements = block.getStatements();
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(helper.getProject());
    for (ParameterInfo info : helper.getParameterInfos()) {
        final String oldName = info.getOriginalName();
        final String newName = info.getName();
        final ArrayList<GrExpression> result = new ArrayList<>();
        if (!oldName.equals(newName)) {
            for (final GrStatement statement : statements) {
                statement.accept(new PsiRecursiveElementVisitor() {

                    @Override
                    public void visitElement(final PsiElement element) {
                        super.visitElement(element);
                        if (element instanceof GrReferenceExpression) {
                            GrReferenceExpression expr = (GrReferenceExpression) element;
                            if (!expr.isQualified() && oldName.equals(expr.getReferenceName())) {
                                result.add(expr);
                            }
                        }
                    }
                });
                for (GrExpression expr : result) {
                    expr.replaceWithExpression(factory.createExpressionFromText(newName), false);
                }
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) ParameterInfo(org.jetbrains.plugins.groovy.refactoring.extract.ParameterInfo) 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) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)

Example 15 with GroovyPsiElementFactory

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

the class GroovyMethodInliner method prepareNewMethod.

/**
   * Prepare temporary method with non-conflicting local names
   */
@NotNull
private static GrMethod prepareNewMethod(@NotNull GrCallExpression call, @NotNull GrMethod method, @Nullable GrExpression qualifier) throws IncorrectOperationException {
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(method.getProject());
    if (method instanceof GrReflectedMethod) {
        method = ((GrReflectedMethod) method).getBaseMethod();
    }
    GrMethod newMethod = factory.createMethodFromText(method.getText(), call);
    if (qualifier != null) {
        Collection<GroovyInlineMethodUtil.ReferenceExpressionInfo> infos = GroovyInlineMethodUtil.collectReferenceInfo(method);
        GroovyInlineMethodUtil.addQualifiersToInnerReferences(newMethod, infos, qualifier);
    }
    ArrayList<PsiNamedElement> innerDefinitions = new ArrayList<>();
    collectInnerDefinitions(newMethod.getBlock(), innerDefinitions);
    // there are only local variables and parameters (possible of inner closures)
    for (PsiNamedElement namedElement : innerDefinitions) {
        String name = namedElement.getName();
        if (name != null) {
            String newName = qualifier instanceof GrReferenceExpression ? InlineMethodConflictSolver.suggestNewName(name, method, call, ((GrReferenceExpression) qualifier).getReferenceName()) : InlineMethodConflictSolver.suggestNewName(name, method, call);
            if (!newName.equals(namedElement.getName())) {
                final Collection<PsiReference> refs = ReferencesSearch.search(namedElement).findAll();
                for (PsiReference ref : refs) {
                    PsiElement element = ref.getElement();
                    if (element instanceof GrReferenceExpression) {
                        GrExpression newExpr = factory.createExpressionFromText(newName);
                        ((GrReferenceExpression) element).replaceWithExpression(newExpr, false);
                    }
                }
                namedElement.setName(newName);
            }
        }
    }
    GroovyInlineMethodUtil.replaceParametersWithArguments(call, newMethod);
    return newMethod;
}
Also used : GrReflectedMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrReflectedMethod) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) ArrayList(java.util.ArrayList) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)159 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)42 PsiElement (com.intellij.psi.PsiElement)30 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)23 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)22 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)21 IncorrectOperationException (com.intellij.util.IncorrectOperationException)20 Nullable (org.jetbrains.annotations.Nullable)20 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)18 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)18 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)17 NotNull (org.jetbrains.annotations.NotNull)16 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)15 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)14 Project (com.intellij.openapi.project.Project)8 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)8 ASTNode (com.intellij.lang.ASTNode)7 ArrayList (java.util.ArrayList)7 GrCodeReferenceElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement)7 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)6