Search in sources :

Example 91 with GroovyFile

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

the class GroovyConfigSlurperCompletionProvider method addCompletions.

@Override
protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet result) {
    PsiFile file = parameters.getOriginalFile();
    if (!(file instanceof GroovyFile))
        return;
    GroovyFile groovyFile = (GroovyFile) file;
    if (!groovyFile.isScript())
        return;
    GrReferenceExpression ref = (GrReferenceExpression) parameters.getPosition().getParent();
    if (ref == null)
        return;
    final Map<String, Boolean> variants = new HashMap<>();
    collectVariants((s, isFinal) -> variants.put(s, isFinal), ref, groovyFile);
    if (variants.isEmpty())
        return;
    // Remove existing variants.
    PsiElement parent = ref.getParent();
    if (parent instanceof GrAssignmentExpression) {
        parent = parent.getParent();
    }
    if (parent == null)
        return;
    Set<String> processedPrefixes = new HashSet<>();
    Set<String> prefixesInMethodCall = new HashSet<>();
    for (PsiElement e = parent.getFirstChild(); e != null; e = e.getNextSibling()) {
        if (e instanceof GrAssignmentExpression) {
            PsiElement left = ((GrAssignmentExpression) e).getLValue();
            if (left instanceof GrReferenceExpression) {
                String s = refToString((GrReferenceExpression) left);
                if (s == null)
                    continue;
                int dotIndex = s.indexOf('.');
                if (dotIndex > 0) {
                    processedPrefixes.add(s.substring(0, dotIndex));
                }
                variants.remove(s);
            }
        } else if (e instanceof GrMethodCall) {
            GrMethodCall call = (GrMethodCall) e;
            if (isPropertyCall(call)) {
                String name = extractPropertyName(call);
                if (name == null)
                    continue;
                processedPrefixes.add(name);
                prefixesInMethodCall.add(name);
                variants.remove(name);
            }
        }
    }
    // Process variants.
    for (Map.Entry<String, Boolean> entry : variants.entrySet()) {
        String variant = entry.getKey();
        int dotIndex = variant.indexOf('.');
        if (dotIndex > 0 && dotIndex < variant.length() - 1) {
            String p = variant.substring(0, dotIndex);
            if (prefixesInMethodCall.contains(p))
                continue;
            if (myAddPrefixes && processedPrefixes.add(p)) {
                result.addElement(LookupElementBuilder.create(p));
            }
        }
        LookupElement lookupElement = LookupElementBuilder.create(variant);
        if (entry.getValue()) {
            lookupElement = TailTypeDecorator.withTail(lookupElement, TailType.EQ);
        }
        result.addElement(lookupElement);
    }
}
Also used : GrMethodCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall) LookupElement(com.intellij.codeInsight.lookup.LookupElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) PsiFile(com.intellij.psi.PsiFile) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) PsiElement(com.intellij.psi.PsiElement)

Aggregations

GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)91 PsiFile (com.intellij.psi.PsiFile)26 PsiElement (com.intellij.psi.PsiElement)21 NotNull (org.jetbrains.annotations.NotNull)17 GrImportStatement (org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement)17 VirtualFile (com.intellij.openapi.vfs.VirtualFile)13 Project (com.intellij.openapi.project.Project)10 GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)10 PsiClass (com.intellij.psi.PsiClass)9 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)9 Nullable (org.jetbrains.annotations.Nullable)8 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)8 GroovyScriptClass (org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass)8 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)7 Module (com.intellij.openapi.module.Module)6 IncorrectOperationException (com.intellij.util.IncorrectOperationException)6 GrReferenceElement (org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement)6 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)6 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)6 ASTNode (com.intellij.lang.ASTNode)5