Search in sources :

Example 1 with GrQualifiedReference

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

the class ImportOnDemandIntention method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    if (!(element instanceof GrReferenceElement))
        return;
    final GrReferenceElement ref = (GrReferenceElement) element;
    final PsiElement resolved = ref.resolve();
    if (!(resolved instanceof PsiClass))
        return;
    final String qname = ((PsiClass) resolved).getQualifiedName();
    final GrImportStatement importStatement = GroovyPsiElementFactory.getInstance(project).createImportStatementFromText(qname, true, true, null);
    final PsiFile containingFile = element.getContainingFile();
    if (!(containingFile instanceof GroovyFile))
        return;
    ((GroovyFile) containingFile).addImport(importStatement);
    for (PsiReference reference : ReferencesSearch.search(resolved, new LocalSearchScope(containingFile))) {
        final PsiElement refElement = reference.getElement();
        if (refElement == null)
            continue;
        final PsiElement parent = refElement.getParent();
        if (parent instanceof GrQualifiedReference<?>) {
            org.jetbrains.plugins.groovy.codeStyle.GrReferenceAdjuster.shortenReference((GrQualifiedReference<?>) parent);
        }
    }
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) PsiClass(com.intellij.psi.PsiClass) PsiReference(com.intellij.psi.PsiReference) PsiFile(com.intellij.psi.PsiFile) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement) PsiElement(com.intellij.psi.PsiElement) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) GrQualifiedReference(org.jetbrains.plugins.groovy.lang.psi.GrQualifiedReference)

Example 2 with GrQualifiedReference

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

the class ImportStaticIntention method shortenUsages.

private static boolean shortenUsages(PsiElement resolved, PsiFile containingFile) {
    boolean isAnythingShortened = false;
    for (PsiReference reference : ReferencesSearch.search(resolved, new LocalSearchScope(containingFile))) {
        final PsiElement refElement = reference.getElement();
        if (refElement instanceof GrQualifiedReference<?>) {
            boolean shortened = GrReferenceAdjuster.shortenReference((GrQualifiedReference<?>) refElement);
            isAnythingShortened |= shortened;
        }
    }
    return isAnythingShortened;
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) GrQualifiedReference(org.jetbrains.plugins.groovy.lang.psi.GrQualifiedReference)

Example 3 with GrQualifiedReference

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

the class GrStaticChecker method isInStaticContext.

public static boolean isInStaticContext(@NotNull PsiElement place, @Nullable PsiClass targetClass) {
    if (place instanceof GrReferenceExpression) {
        GrQualifiedReference reference = (GrQualifiedReference) place;
        PsiElement qualifier = reference.getQualifier();
        if (qualifier != null && !PsiUtil.isThisOrSuperRef(reference)) {
            if (PsiUtil.isInstanceThisRef(qualifier) || PsiUtil.isSuperReference(qualifier)) {
                return false;
            } else if (PsiUtil.isThisReference(qualifier)) {
                //instance 'this' already is processed. So it static 'this'
                return true;
            }
            return qualifier instanceof GrQualifiedReference && ResolveUtil.resolvesToClass(qualifier);
        }
        if (PsiUtil.isSuperReference(reference))
            return false;
    //this reference should be checked as all other refs
    }
    PsiElement run = place;
    while (run != null && run != targetClass) {
        if (targetClass == null && run instanceof PsiClass)
            return false;
        if (run instanceof GrClosableBlock)
            return false;
        if (run instanceof PsiModifierListOwner && ((PsiModifierListOwner) run).hasModifierProperty(PsiModifier.STATIC))
            return true;
        run = run.getParent();
    }
    return false;
}
Also used : GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrQualifiedReference(org.jetbrains.plugins.groovy.lang.psi.GrQualifiedReference) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 4 with GrQualifiedReference

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

the class GrIntroduceFieldProcessor method replaceOccurrence.

@NotNull
private PsiElement replaceOccurrence(@NotNull GrVariable field, @NotNull PsiElement occurrence, @NotNull PsiClass containingClass) {
    boolean isOriginal = occurrence == myContext.getExpression();
    final GrReferenceExpression newExpr = createRefExpression(field, occurrence, containingClass);
    final PsiElement replaced;
    if (occurrence instanceof GrExpression) {
        replaced = ((GrExpression) occurrence).replaceWithExpression(newExpr, false);
    } else {
        replaced = occurrence.replace(newExpr);
    }
    if (replaced instanceof GrQualifiedReference<?>) {
        GrReferenceAdjuster.shortenReference((GrQualifiedReference<?>) replaced);
    }
    if (isOriginal) {
        updateCaretPosition(replaced);
    }
    return replaced;
}
Also used : GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrQualifiedReference(org.jetbrains.plugins.groovy.lang.psi.GrQualifiedReference) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with GrQualifiedReference

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

the class GroovyBlockGenerator method addNestedChildren.

private void addNestedChildren(final PsiElement elem, List<Block> list, @Nullable AlignmentProvider.Aligner aligner, final boolean topLevel, Wrap wrap) {
    final List<ASTNode> children = visibleChildren(elem.getNode());
    if (elem instanceof GrMethodCallExpression) {
        GrExpression invokedExpression = ((GrMethodCallExpression) elem).getInvokedExpression();
        if (invokedExpression instanceof GrQualifiedReference) {
            final PsiElement nameElement = ((GrQualifiedReference) invokedExpression).getReferenceNameElement();
            if (nameElement != null) {
                List<ASTNode> grandChildren = visibleChildren(invokedExpression.getNode());
                int i = 0;
                while (i < grandChildren.size() && nameElement != grandChildren.get(i).getPsi()) i++;
                if (i > 0) {
                    processNestedChildrenPrefix(list, aligner, false, grandChildren, i, wrap);
                }
                if (i < grandChildren.size()) {
                    LOG.assertTrue(nameElement == grandChildren.get(i).getPsi());
                    list.add(new MethodCallWithoutQualifierBlock(nameElement, wrap, topLevel, children, elem, myContext));
                }
                return;
            }
        }
    }
    processNestedChildrenPrefix(list, aligner, topLevel, children, children.size(), wrap);
}
Also used : GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) ASTNode(com.intellij.lang.ASTNode) GrQualifiedReference(org.jetbrains.plugins.groovy.lang.psi.GrQualifiedReference) PsiElement(com.intellij.psi.PsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement)

Aggregations

GrQualifiedReference (org.jetbrains.plugins.groovy.lang.psi.GrQualifiedReference)5 PsiElement (com.intellij.psi.PsiElement)2 LocalSearchScope (com.intellij.psi.search.LocalSearchScope)2 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)2 ASTNode (com.intellij.lang.ASTNode)1 PsiClass (com.intellij.psi.PsiClass)1 PsiFile (com.intellij.psi.PsiFile)1 PsiReference (com.intellij.psi.PsiReference)1 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)1 NotNull (org.jetbrains.annotations.NotNull)1 GrReferenceElement (org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement)1 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)1 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)1 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)1 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)1 GrImportStatement (org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement)1