Search in sources :

Example 41 with GroovyPsiElement

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

the class GrClosableBlockImpl method getOwner.

private PsiVariable getOwner() {
    return CachedValuesManager.getCachedValue(this, () -> {
        final GroovyPsiElement context = PsiTreeUtil.getParentOfType(this, GrTypeDefinition.class, GrClosableBlock.class, GroovyFile.class);
        final PsiElementFactory factory = JavaPsiFacade.getInstance(getProject()).getElementFactory();
        PsiType type = null;
        if (context instanceof GrTypeDefinition) {
            type = factory.createType((PsiClass) context);
        } else if (context instanceof GrClosableBlock) {
            type = GrClosureType.create((GrClosableBlock) context, true);
        } else if (context instanceof GroovyFile) {
            final PsiClass scriptClass = ((GroovyFile) context).getScriptClass();
            if (scriptClass != null && GroovyNamesUtil.isIdentifier(scriptClass.getName()))
                type = factory.createType(scriptClass);
        }
        if (type == null) {
            type = TypesUtil.getJavaLangObject(this);
        }
        PsiVariable owner = new GrLightVariable(getManager(), OWNER_NAME, type, this);
        return CachedValueProvider.Result.create(owner, PsiModificationTracker.MODIFICATION_COUNT);
    });
}
Also used : GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrLightVariable(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrLightVariable) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Example 42 with GroovyPsiElement

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

the class ResolveUtil method getCallVariants.

public static GroovyResolveResult[] getCallVariants(GroovyPsiElement place) {
    final PsiElement parent = place.getParent();
    GroovyResolveResult[] variants = GroovyResolveResult.EMPTY_ARRAY;
    if (parent instanceof GrCallExpression) {
        variants = ((GrCallExpression) parent).getCallVariants(place instanceof GrExpression ? (GrExpression) place : null);
    } else if (parent instanceof GrConstructorInvocation) {
        final PsiClass clazz = ((GrConstructorInvocation) parent).getDelegatedClass();
        if (clazz != null) {
            final PsiMethod[] constructors = clazz.getConstructors();
            variants = getConstructorResolveResult(constructors, place);
        }
    } else if (parent instanceof GrAnonymousClassDefinition) {
        final PsiElement element = ((GrAnonymousClassDefinition) parent).getBaseClassReferenceGroovy().resolve();
        if (element instanceof PsiClass) {
            final PsiMethod[] constructors = ((PsiClass) element).getConstructors();
            variants = getConstructorResolveResult(constructors, place);
        }
    } else if (place instanceof GrReferenceExpression) {
        variants = ((GrReferenceExpression) place).getSameNameVariants();
    }
    return variants;
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrCallExpression) GrAnonymousClassDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 43 with GroovyPsiElement

use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement in project android by JetBrains.

the class GradleDslElement method delete.

/**
   * Deletes this element and all it's children from the .gradle file.
   */
protected void delete() {
    for (GradleDslElement element : getChildren()) {
        element.delete();
    }
    GroovyPsiElement psiElement = getPsiElement();
    if (psiElement == null || !psiElement.isValid()) {
        return;
    }
    PsiElement parent = psiElement.getParent();
    psiElement.delete();
    if (parent != null) {
        deleteIfEmpty(parent);
    }
    setPsiElement(null);
}
Also used : GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 44 with GroovyPsiElement

use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement in project android by JetBrains.

the class GradleDslElement method create.

/**
   * Creates the {@link GroovyPsiElement} by adding this element to the .gradle file.
   *
   * <p>It creates a new {@link GroovyPsiElement} only when {@link #getPsiElement()} return {@code null}.
   *
   * <p>Returns the final {@link GroovyPsiElement} corresponds to this element or {@code null} when failed to create the
   * {@link GroovyPsiElement}.
   */
@Nullable
public GroovyPsiElement create() {
    GroovyPsiElement psiElement = getPsiElement();
    if (psiElement != null) {
        return psiElement;
    }
    if (myParent == null) {
        return null;
    }
    GroovyPsiElement parentPsiElement = myParent.create();
    if (parentPsiElement == null) {
        return null;
    }
    Project project = parentPsiElement.getProject();
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
    if (isNewEmptyBlockElement()) {
        // Avoid creation of an empty block statement.
        return null;
    }
    String statementText = myName + (isBlockElement() ? " {\n}\n" : " \"abc\", \"xyz\"");
    GrStatement statement = factory.createStatementFromText(statementText);
    if (statement instanceof GrApplicationStatement) {
        // Workaround to create an application statement.
        ((GrApplicationStatement) statement).getArgumentList().delete();
    }
    PsiElement lineTerminator = factory.createLineTerminator(1);
    PsiElement addedElement;
    if (parentPsiElement instanceof GroovyFile) {
        addedElement = parentPsiElement.addAfter(statement, parentPsiElement.getLastChild());
        parentPsiElement.addBefore(lineTerminator, addedElement);
    } else {
        addedElement = parentPsiElement.addBefore(statement, parentPsiElement.getLastChild());
        parentPsiElement.addAfter(lineTerminator, addedElement);
    }
    if (isBlockElement()) {
        GrClosableBlock closableBlock = getClosableBlock(addedElement);
        if (closableBlock != null) {
            setPsiElement(closableBlock);
        }
    } else {
        if (addedElement instanceof GrApplicationStatement) {
            setPsiElement((GrApplicationStatement) addedElement);
        }
    }
    return getPsiElement();
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) Project(com.intellij.openapi.project.Project) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrApplicationStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrApplicationStatement) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) Nullable(org.jetbrains.annotations.Nullable)

Example 45 with GroovyPsiElement

use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement in project android by JetBrains.

the class GradleDslExpressionList method create.

@Override
@Nullable
public GroovyPsiElement create() {
    GroovyPsiElement psiElement = getPsiElement();
    if (psiElement == null) {
        if (myParent instanceof GradleDslExpressionMap) {
            // This is a list in the map element and we need to create a named argument for it.
            return createNamedArgumentList();
        }
        psiElement = super.create();
    }
    if (psiElement == null) {
        return null;
    }
    if (psiElement instanceof GrListOrMap) {
        return psiElement;
    }
    if (psiElement instanceof GrArgumentList) {
        if (!myToBeAddedExpressions.isEmpty() && ((GrArgumentList) psiElement).getAllArguments().length == 1 && !myAppendToArgumentListWithOneElement) {
            // Sometimes it's not possible to append to the arguments list with one item. eg. proguardFile "xyz".
            // Set the psiElement to null and create a new psiElement of an empty application statement.
            setPsiElement(null);
            psiElement = super.create();
        } else {
            return psiElement;
        }
    }
    if (psiElement instanceof GrApplicationStatement) {
        GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(psiElement.getProject());
        GrArgumentList argumentList = factory.createArgumentListFromText("xyz");
        // Workaround to get an empty argument list.
        argumentList.getFirstChild().delete();
        PsiElement added = psiElement.addAfter(argumentList, psiElement.getLastChild());
        if (added instanceof GrArgumentList) {
            GrArgumentList addedArgumentList = (GrArgumentList) added;
            setPsiElement(addedArgumentList);
            return addedArgumentList;
        }
    }
    return null;
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) GrApplicationStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrApplicationStatement) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)98 PsiElement (com.intellij.psi.PsiElement)34 Nullable (org.jetbrains.annotations.Nullable)17 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)17 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)16 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)13 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)12 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)11 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)9 GrListOrMap (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap)8 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)8 GrApplicationStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrApplicationStatement)8 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)8 GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)8 Project (com.intellij.openapi.project.Project)7 TextRange (com.intellij.openapi.util.TextRange)7 NotNull (org.jetbrains.annotations.NotNull)7 GroovyRecursiveElementVisitor (org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor)7 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)7 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)6