Search in sources :

Example 11 with GrImportStatement

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

the class GroovyImportOptimizerRefactoringHelper method performOperation.

@Override
public void performOperation(final Project project, final Set<GroovyFile> files) {
    final ProgressManager progressManager = ProgressManager.getInstance();
    final Map<GroovyFile, Pair<List<GrImportStatement>, Set<GrImportStatement>>> redundants = new HashMap<>();
    final Runnable findUnusedImports = () -> {
        final ProgressIndicator progressIndicator = progressManager.getProgressIndicator();
        final int total = files.size();
        int i = 0;
        for (final GroovyFile file : files) {
            if (!file.isValid())
                continue;
            final VirtualFile virtualFile = file.getVirtualFile();
            if (!ProjectRootManager.getInstance(project).getFileIndex().isInSource(virtualFile)) {
                continue;
            }
            if (progressIndicator != null) {
                progressIndicator.setText2(virtualFile.getPresentableUrl());
                progressIndicator.setFraction((double) i++ / total);
            }
            ApplicationManager.getApplication().runReadAction(() -> {
                final Set<GrImportStatement> usedImports = GroovyImportUtil.findUsedImports(file);
                final List<GrImportStatement> validImports = PsiUtil.getValidImportStatements(file);
                redundants.put(file, Pair.create(validImports, usedImports));
            });
        }
    };
    if (!progressManager.runProcessWithProgressSynchronously(findUnusedImports, "Optimizing imports (Groovy) ... ", false, project)) {
        return;
    }
    WriteAction.run(() -> {
        for (GroovyFile groovyFile : redundants.keySet()) {
            if (!groovyFile.isValid())
                continue;
            final Pair<List<GrImportStatement>, Set<GrImportStatement>> pair = redundants.get(groovyFile);
            final List<GrImportStatement> validImports = pair.getFirst();
            final Set<GrImportStatement> usedImports = pair.getSecond();
            for (GrImportStatement importStatement : validImports) {
                if (!usedImports.contains(importStatement)) {
                    groovyFile.removeImport(importStatement);
                }
            }
        }
    });
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Set(java.util.Set) HashSet(com.intellij.util.containers.hash.HashSet) HashMap(java.util.HashMap) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) ProgressManager(com.intellij.openapi.progress.ProgressManager) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) List(java.util.List) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) Pair(com.intellij.openapi.util.Pair)

Example 12 with GrImportStatement

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

the class GroovyRefactoringSupportProvider method isMemberInplaceRenameAvailable.

@Override
public boolean isMemberInplaceRenameAvailable(@NotNull PsiElement element, @Nullable PsiElement context) {
    if (context == null || context.getContainingFile() instanceof GroovyFile)
        return false;
    PsiElement parent = context.getParent();
    //don't try to inplace rename aliased imported references
    if (parent instanceof GrReferenceElement) {
        GroovyResolveResult result = ((GrReferenceElement) parent).advancedResolve();
        PsiElement fileResolveContext = result.getCurrentFileResolveContext();
        if (fileResolveContext instanceof GrImportStatement && ((GrImportStatement) fileResolveContext).isAliasedImport()) {
            return false;
        }
    }
    return element instanceof GrMember;
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrMember(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMember) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) PsiElement(com.intellij.psi.PsiElement) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement)

Example 13 with GrImportStatement

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

the class GroovyCompletionData method suggestPrimitiveTypes.

private static boolean suggestPrimitiveTypes(PsiElement context) {
    if (isInfixOperatorPosition(context))
        return false;
    if (isAfterForParameter(context))
        return false;
    final PsiElement parent = context.getParent();
    if (parent == null)
        return false;
    PsiElement previous = PsiImplUtil.realPrevious(parent.getPrevSibling());
    if (parent instanceof GrReferenceElement && parent.getParent() instanceof GrArgumentList) {
        PsiElement prevSibling = context.getPrevSibling();
        if (prevSibling != null && prevSibling.getNode() != null) {
            if (!TokenSets.DOTS.contains(prevSibling.getNode().getElementType())) {
                return true;
            }
        } else if (!(previous != null && GroovyTokenTypes.mAT.equals(previous.getNode().getElementType()))) {
            return true;
        }
    }
    if (GroovyCompletionUtil.isTupleVarNameWithoutTypeDeclared(context))
        return true;
    if (previous != null && GroovyTokenTypes.mAT.equals(previous.getNode().getElementType())) {
        return false;
    }
    if (GroovyCompletionUtil.asSimpleVariable(context) || GroovyCompletionUtil.asTypedMethod(context) || GroovyCompletionUtil.asVariableInBlock(context) || asVariableAfterModifiers(context)) {
        return true;
    }
    if ((parent instanceof GrParameter && ((GrParameter) parent).getTypeElementGroovy() == null) || parent instanceof GrReferenceElement && !(parent.getParent() instanceof GrImportStatement) && !(parent.getParent() instanceof GrPackageDefinition) && !(parent.getParent() instanceof GrArgumentList)) {
        PsiElement prevSibling = context.getPrevSibling();
        if (parent instanceof GrReferenceElement && prevSibling != null && prevSibling.getNode() != null) {
            ASTNode node = prevSibling.getNode();
            return !TokenSets.DOTS.contains(node.getElementType());
        } else {
            return true;
        }
    }
    if (PsiImplUtil.realPrevious(parent.getPrevSibling()) instanceof GrModifierList) {
        return true;
    }
    if (PsiImplUtil.realPrevious(context.getPrevSibling()) instanceof GrModifierList) {
        return true;
    }
    return parent instanceof GrExpression && parent.getParent() instanceof GroovyFile && GroovyCompletionUtil.isNewStatement(context, false);
}
Also used : GrModifierList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) ASTNode(com.intellij.lang.ASTNode) GrPackageDefinition(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Example 14 with GrImportStatement

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

the class GroovyCompletionUtil method createLookupElements.

public static List<? extends LookupElement> createLookupElements(@NotNull GroovyResolveResult candidate, boolean afterNew, @NotNull PrefixMatcher matcher, @Nullable PsiElement position) {
    final PsiElement element = candidate.getElement();
    final PsiElement context = candidate.getCurrentFileResolveContext();
    if (context instanceof GrImportStatement && element != null) {
        if (element instanceof PsiPackage) {
            return Collections.emptyList();
        }
        final String importedName = ((GrImportStatement) context).getImportedName();
        if (importedName != null) {
            if (!(matcher.prefixMatches(importedName) || element instanceof PsiMethod && getterMatches(matcher, (PsiMethod) element, importedName) || element instanceof PsiMethod && setterMatches(matcher, (PsiMethod) element, importedName))) {
                return Collections.emptyList();
            }
            final GrCodeReferenceElement importReference = ((GrImportStatement) context).getImportReference();
            if (importReference != null) {
                boolean alias = ((GrImportStatement) context).isAliasedImport();
                for (GroovyResolveResult r : importReference.multiResolve(false)) {
                    final PsiElement resolved = r.getElement();
                    if (context.getManager().areElementsEquivalent(resolved, element) && (alias || !(element instanceof PsiClass))) {
                        return generateLookupForImportedElement(candidate, importedName);
                    } else {
                        if (resolved instanceof PsiField && element instanceof PsiMethod && GroovyPropertyUtils.isAccessorFor((PsiMethod) element, (PsiField) resolved)) {
                            return generateLookupForImportedElement(candidate, GroovyPropertyUtils.getAccessorPrefix((PsiMethod) element) + GroovyPropertyUtils.capitalize(importedName));
                        }
                    }
                }
            }
        }
    }
    String name = element instanceof PsiNamedElement ? ((PsiNamedElement) element).getName() : element.getText();
    if (name == null || !matcher.prefixMatches(name)) {
        return Collections.emptyList();
    }
    if (element instanceof PsiClass) {
        return JavaClassNameCompletionContributor.createClassLookupItems((PsiClass) element, afterNew, new GroovyClassNameInsertHandler(), Conditions.<PsiClass>alwaysTrue());
    }
    LookupElementBuilder builder = LookupElementBuilder.create(element instanceof PsiPackage ? element : candidate, name);
    return Arrays.asList(setupLookupBuilder(element, candidate.getSubstitutor(), builder, position));
}
Also used : GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement)

Example 15 with GrImportStatement

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

the class GroovyFindJarFix method getFqns.

@Override
protected Collection<String> getFqns(@NotNull GrReferenceElement ref) {
    GrImportStatement importStatement = PsiTreeUtil.getParentOfType(ref.getElement(), GrImportStatement.class);
    //from static imports
    if (importStatement != null) {
        GrCodeReferenceElement reference = importStatement.getImportReference();
        if (reference != null) {
            return Collections.singleton(reference.getText());
        }
        return Collections.emptyList();
    }
    if (ref.getQualifier() != null)
        return Collections.emptyList();
    final String className = ref.getReferenceName();
    if (className == null)
        return Collections.emptyList();
    PsiFile file = ref.getContainingFile().getOriginalFile();
    if (!(file instanceof GroovyFile))
        return Collections.emptyList();
    GrImportStatement[] importList = ((GroovyFile) file).getImportStatements();
    for (GrImportStatement imp : importList) {
        if (className.equals(imp.getImportedName())) {
            GrCodeReferenceElement importReference = imp.getImportReference();
            if (importReference == null)
                return Collections.emptyList();
            return Collections.singleton(importReference.getText());
        }
    }
    return Collections.emptyList();
}
Also used : GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) PsiFile(com.intellij.psi.PsiFile) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Aggregations

GrImportStatement (org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement)49 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)15 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)11 NotNull (org.jetbrains.annotations.NotNull)9 GrReferenceElement (org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement)9 PsiElement (com.intellij.psi.PsiElement)7 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)6 GrPackageDefinition (org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition)6 TextRange (com.intellij.openapi.util.TextRange)4 VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 UsageInfo (com.intellij.usageView.UsageInfo)4 GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)4 GrTopStatement (org.jetbrains.plugins.groovy.lang.psi.api.toplevel.GrTopStatement)4 GrCodeReferenceElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement)4 ASTNode (com.intellij.lang.ASTNode)3 Document (com.intellij.openapi.editor.Document)3 PsiFile (com.intellij.psi.PsiFile)3 GroovyScriptClass (org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass)3 DaemonCodeAnalyzerImpl (com.intellij.codeInsight.daemon.impl.DaemonCodeAnalyzerImpl)2 IntentionAction (com.intellij.codeInsight.intention.IntentionAction)2