Search in sources :

Example 56 with GroovyPsiElementFactory

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

the class Intention method replaceExpressionWithNegatedExpressionString.

protected static void replaceExpressionWithNegatedExpressionString(@NotNull String newExpression, @NotNull GrExpression expression) throws IncorrectOperationException {
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(expression.getProject());
    GrExpression expressionToReplace = expression;
    final String expString;
    if (BoolUtils.isNegated(expression)) {
        expressionToReplace = BoolUtils.findNegation(expression);
        expString = newExpression;
    } else {
        expString = "!(" + newExpression + ')';
    }
    final GrExpression newCall = factory.createExpressionFromText(expString);
    assert expressionToReplace != null;
    expressionToReplace.replaceWithExpression(newCall, true);
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)

Example 57 with GroovyPsiElementFactory

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

the class GrDocInlinedTagImpl method setName.

@Override
public PsiElement setName(@NonNls @NotNull String name) throws IncorrectOperationException {
    final PsiElement nameElement = getNameElement();
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(getProject());
    final GrDocComment comment = factory.createDocCommentFromText("/** {@" + name + "}*/");
    nameElement.replace(comment.getTags()[0].getNameElement());
    return this;
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) PsiElement(com.intellij.psi.PsiElement) GrDocComment(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment)

Example 58 with GroovyPsiElementFactory

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

the class GroovyFileImpl method setPackageName.

@Override
public void setPackageName(String packageName) {
    final ASTNode fileNode = getNode();
    final GrPackageDefinition currentPackage = getPackageDefinition();
    if (packageName == null || packageName.isEmpty()) {
        if (currentPackage != null) {
            final ASTNode currNode = currentPackage.getNode();
            fileNode.removeChild(currNode);
        }
        return;
    }
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(getProject());
    final GrPackageDefinition newPackage = (GrPackageDefinition) factory.createTopElementFromText("package " + packageName);
    if (currentPackage != null) {
        final GrCodeReferenceElement packageReference = currentPackage.getPackageReference();
        if (packageReference != null) {
            GrCodeReferenceElement ref = newPackage.getPackageReference();
            if (ref != null) {
                packageReference.replace(ref);
            }
            return;
        }
    }
    final ASTNode newNode = newPackage.getNode();
    if (currentPackage != null) {
        final ASTNode currNode = currentPackage.getNode();
        fileNode.replaceChild(currNode, newNode);
    } else {
        ASTNode anchor = fileNode.getFirstChildNode();
        if (anchor != null && anchor.getElementType() == GroovyTokenTypes.mSH_COMMENT) {
            anchor = anchor.getTreeNext();
            fileNode.addLeaf(GroovyTokenTypes.mNLS, "\n", anchor);
        }
        fileNode.addChild(newNode, anchor);
        if (anchor != null && !anchor.getText().startsWith("\n\n")) {
            fileNode.addLeaf(GroovyTokenTypes.mNLS, "\n", anchor);
        }
    }
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) ASTNode(com.intellij.lang.ASTNode) GrPackageDefinition(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition)

Example 59 with GroovyPsiElementFactory

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

the class GrReferenceElementImpl method handleElementRename.

@Override
public PsiElement handleElementRename(String newElementName) throws IncorrectOperationException {
    PsiElement nameElement = getReferenceNameElement();
    if (nameElement != null) {
        ASTNode node = nameElement.getNode();
        GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(getProject());
        ASTNode newNameNode;
        try {
            newNameNode = factory.createReferenceNameFromText(newElementName).getNode();
        } catch (IncorrectOperationException e) {
            newNameNode = factory.createLiteralFromValue(newElementName).getFirstChild().getNode();
        }
        assert newNameNode != null && node != null;
        getNode().replaceChild(node, newNameNode);
    }
    return this;
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) ASTNode(com.intellij.lang.ASTNode) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 60 with GroovyPsiElementFactory

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

the class GrModifierListImpl method addAnnotation.

@Override
@NotNull
public GrAnnotation addAnnotation(@NotNull @NonNls String qualifiedName) {
    final PsiClass psiClass = JavaPsiFacade.getInstance(getProject()).findClass(qualifiedName, getResolveScope());
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(getProject());
    GrAnnotation annotation;
    if (psiClass != null && psiClass.isAnnotationType()) {
        annotation = (GrAnnotation) addAfter(factory.createModifierFromText("@xxx"), null);
        annotation.getClassReference().bindToElement(psiClass);
    } else {
        annotation = (GrAnnotation) addAfter(factory.createModifierFromText("@" + qualifiedName), null);
    }
    final PsiElement parent = getParent();
    if (!(parent instanceof GrParameter)) {
        final ASTNode node = annotation.getNode();
        final ASTNode treeNext = node.getTreeNext();
        if (treeNext != null) {
            getNode().addLeaf(TokenType.WHITE_SPACE, "\n", treeNext);
        } else {
            parent.getNode().addLeaf(TokenType.WHITE_SPACE, "\n", getNode().getTreeNext());
        }
    }
    return annotation;
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrAnnotation(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation) ASTNode(com.intellij.lang.ASTNode) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) 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