Search in sources :

Example 1 with GroovyResolveResult

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

the class GrAliasImportIntention method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    GrImportStatement context;
    final PsiMember resolved;
    if (element instanceof GrReferenceExpression) {
        GrReferenceExpression ref = (GrReferenceExpression) element;
        GroovyResolveResult result = ref.advancedResolve();
        context = (GrImportStatement) result.getCurrentFileResolveContext();
        assert context != null;
        resolved = (PsiMember) result.getElement();
    } else if (element instanceof GrImportStatement) {
        context = (GrImportStatement) element;
        resolved = (PsiMember) context.getImportReference().resolve();
    } else {
        return;
    }
    assert resolved != null;
    doRefactoring(project, context, resolved);
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 2 with GroovyResolveResult

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

the class GrAliasImportIntention method getElementPredicate.

@NotNull
@Override
protected PsiElementPredicate getElementPredicate() {
    return new PsiElementPredicate() {

        @Override
        public boolean satisfiedBy(PsiElement element) {
            if (element instanceof GrReferenceExpression) {
                GroovyResolveResult result = ((GrReferenceExpression) element).advancedResolve();
                PsiElement context = result.getCurrentFileResolveContext();
                if (!(context instanceof GrImportStatement))
                    return false;
                GrImportStatement importStatement = (GrImportStatement) context;
                if (!importStatement.isStatic() || importStatement.isAliasedImport())
                    return false;
                return true;
            } else if (element instanceof GrImportStatement) {
                final GrImportStatement importStatement = (GrImportStatement) element;
                if (!importStatement.isStatic())
                    return false;
                if (importStatement.isOnDemand())
                    return false;
                if (importStatement.isAliasedImport())
                    return false;
                return true;
            }
            return false;
        }
    };
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) PsiElementPredicate(org.jetbrains.plugins.groovy.intentions.base.PsiElementPredicate) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with GroovyResolveResult

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

the class MoveGroovyClassHandler method preprocessUsages.

@Override
public void preprocessUsages(Collection<UsageInfo> results) {
    //remove all alias-imported usages from collection
    for (Iterator<UsageInfo> iterator = results.iterator(); iterator.hasNext(); ) {
        UsageInfo info = iterator.next();
        if (info == null) {
            LOG.debug("info==null");
            continue;
        }
        final PsiReference ref = info.getReference();
        if (ref == null)
            continue;
        final PsiElement element = ref.getElement();
        if (!(element instanceof GrReferenceElement))
            continue;
        final GroovyResolveResult resolveResult = ((GrReferenceElement) element).advancedResolve();
        final PsiElement context = resolveResult.getCurrentFileResolveContext();
        if (!(context instanceof GrImportStatement))
            continue;
        if (!((GrImportStatement) context).isAliasedImport())
            continue;
        iterator.remove();
    }
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) UsageInfo(com.intellij.usageView.UsageInfo) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement)

Example 4 with GroovyResolveResult

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

the class GrIntroduceClosureParameterProcessor method removeParametersFromCall.

private static void removeParametersFromCall(GrMethodCallExpression methodCall, GrIntroduceParameterSettings settings) {
    final GroovyResolveResult resolveResult = methodCall.advancedResolve();
    final PsiElement resolved = resolveResult.getElement();
    LOG.assertTrue(resolved instanceof PsiMethod);
    final GrClosureSignature signature = GrClosureSignatureUtil.createSignature((PsiMethod) resolved, resolveResult.getSubstitutor());
    final GrClosureSignatureUtil.ArgInfo<PsiElement>[] argInfos = GrClosureSignatureUtil.mapParametersToArguments(signature, methodCall);
    LOG.assertTrue(argInfos != null);
    settings.parametersToRemove().forEach(new TIntProcedure() {

        @Override
        public boolean execute(int value) {
            final List<PsiElement> args = argInfos[value].args;
            for (PsiElement arg : args) {
                arg.delete();
            }
            return true;
        }
    });
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) TIntProcedure(gnu.trove.TIntProcedure) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) List(java.util.List) ArrayList(java.util.ArrayList) GrParameterList(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList)

Example 5 with GroovyResolveResult

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

the class RenameAliasedUsagesUtil method findAliasedName.

private static String findAliasedName(Map<GroovyFile, String> map, GroovyFile containingFile, PsiElement elementToResolve) {
    final String s = map.get(containingFile);
    if (s != null)
        return s;
    final GrImportStatement[] imports = containingFile.getImportStatements();
    final PsiManager manager = elementToResolve.getManager();
    for (GrImportStatement anImport : imports) {
        if (anImport.isAliasedImport()) {
            final ResolverProcessor processor = getProcessor(elementToResolve, containingFile);
            anImport.processDeclarations(processor, ResolveState.initial(), null, containingFile);
            final GroovyResolveResult[] results = processor.getCandidates();
            for (GroovyResolveResult result : results) {
                if (manager.areElementsEquivalent(elementToResolve, result.getElement())) {
                    final String importedName = anImport.getImportedName();
                    if (importedName != null) {
                        map.put(containingFile, importedName);
                        return importedName;
                    }
                }
            }
        }
    }
    map.put(containingFile, EMPTY_ALIAS);
    return EMPTY_ALIAS;
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) MethodResolverProcessor(org.jetbrains.plugins.groovy.lang.resolve.processors.MethodResolverProcessor) PropertyResolverProcessor(org.jetbrains.plugins.groovy.lang.resolve.processors.PropertyResolverProcessor) ClassResolverProcessor(org.jetbrains.plugins.groovy.lang.resolve.processors.ClassResolverProcessor) ResolverProcessor(org.jetbrains.plugins.groovy.lang.resolve.processors.ResolverProcessor) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement)

Aggregations

GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)132 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)33 Nullable (org.jetbrains.annotations.Nullable)29 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)29 NotNull (org.jetbrains.annotations.NotNull)25 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)23 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)17 GrImportStatement (org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement)14 GrClosureSignature (org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature)13 GrCodeReferenceElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement)12 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)11 PsiElement (com.intellij.psi.PsiElement)10 ArrayList (java.util.ArrayList)8 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)8 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)8 IElementType (com.intellij.psi.tree.IElementType)7 GrReferenceElement (org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement)7 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)7 GrCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall)7 Project (com.intellij.openapi.project.Project)6