Search in sources :

Example 26 with LookupElement

use of com.intellij.codeInsight.lookup.LookupElement in project intellij-community by JetBrains.

the class GenericDomValueReference method getVariants.

@Override
@NotNull
public Object[] getVariants() {
    final Converter<T> converter = getConverter();
    if (converter instanceof EnumConverter || converter == ResolvingConverter.BOOLEAN_CONVERTER) {
        if (DomCompletionContributor.isSchemaEnumerated(getElement()))
            return ArrayUtil.EMPTY_OBJECT_ARRAY;
    }
    if (converter instanceof ResolvingConverter) {
        final ResolvingConverter<T> resolvingConverter = (ResolvingConverter<T>) converter;
        ArrayList<Object> result = new ArrayList<>();
        final ConvertContext convertContext = getConvertContext();
        for (T variant : resolvingConverter.getVariants(convertContext)) {
            LookupElement lookupElement = resolvingConverter.createLookupElement(variant);
            if (lookupElement != null) {
                result.add(lookupElement);
                continue;
            }
            String name = converter.toString(variant, convertContext);
            if (name != null) {
                result.add(ElementPresentationManager.getInstance().createVariant(variant, name, ((ResolvingConverter) converter).getPsiElement(variant)));
            }
        }
        for (final String string : resolvingConverter.getAdditionalVariants(convertContext)) {
            result.add(LookupValueFactory.createLookupValue(string, null));
        }
        return result.toArray();
    }
    return ArrayUtil.EMPTY_OBJECT_ARRAY;
}
Also used : ArrayList(java.util.ArrayList) LookupElement(com.intellij.codeInsight.lookup.LookupElement) NotNull(org.jetbrains.annotations.NotNull)

Example 27 with LookupElement

use of com.intellij.codeInsight.lookup.LookupElement in project intellij-community by JetBrains.

the class MavenUtil method getPhaseVariants.

public static List<LookupElement> getPhaseVariants(MavenProjectsManager manager) {
    Set<String> goals = new HashSet<>();
    goals.addAll(MavenConstants.PHASES);
    for (MavenProject mavenProject : manager.getProjects()) {
        for (MavenPlugin plugin : mavenProject.getPlugins()) {
            MavenPluginInfo pluginInfo = MavenArtifactUtil.readPluginInfo(manager.getLocalRepository(), plugin.getMavenId());
            if (pluginInfo != null) {
                for (MavenPluginInfo.Mojo mojo : pluginInfo.getMojos()) {
                    goals.add(mojo.getDisplayName());
                }
            }
        }
    }
    List<LookupElement> res = new ArrayList<>(goals.size());
    for (String goal : goals) {
        res.add(LookupElementBuilder.create(goal).withIcon(MavenIcons.Phase));
    }
    return res;
}
Also used : MavenProject(org.jetbrains.idea.maven.project.MavenProject) MavenPlugin(org.jetbrains.idea.maven.model.MavenPlugin) LookupElement(com.intellij.codeInsight.lookup.LookupElement) THashSet(gnu.trove.THashSet)

Example 28 with LookupElement

use of com.intellij.codeInsight.lookup.LookupElement in project intellij-community by JetBrains.

the class PyModuleType method getCompletionVariantsAsLookupElements.

public List<LookupElement> getCompletionVariantsAsLookupElements(PsiElement location, ProcessingContext context, boolean wantAllSubmodules, boolean suppressParentheses) {
    List<LookupElement> result = new ArrayList<>();
    Set<String> namesAlready = context.get(CTX_NAMES);
    PointInImport point = ResolveImportUtil.getPointInImport(location);
    for (PyModuleMembersProvider provider : Extensions.getExtensions(PyModuleMembersProvider.EP_NAME)) {
        for (PyCustomMember member : provider.getMembers(myModule, point)) {
            final String name = member.getName();
            if (namesAlready != null) {
                namesAlready.add(name);
            }
            if (PyUtil.isClassPrivateName(name)) {
                continue;
            }
            final CompletionVariantsProcessor processor = createCompletionVariantsProcessor(location, suppressParentheses, point);
            final PsiElement resolved = member.resolve(location);
            if (resolved != null) {
                processor.execute(resolved, ResolveState.initial());
                final List<LookupElement> lookupList = processor.getResultList();
                if (!lookupList.isEmpty()) {
                    final LookupElement element = lookupList.get(0);
                    if (name.equals(element.getLookupString())) {
                        result.add(element);
                        continue;
                    }
                }
            }
            result.add(LookupElementBuilder.create(name).withIcon(member.getIcon()).withTypeText(member.getShortType()));
        }
    }
    if (point == PointInImport.NONE || point == PointInImport.AS_NAME) {
        // when not imported from, add regular attributes
        final CompletionVariantsProcessor processor = createCompletionVariantsProcessor(location, suppressParentheses, point);
        myModule.processDeclarations(processor, ResolveState.initial(), null, location);
        if (namesAlready != null) {
            for (LookupElement le : processor.getResultList()) {
                String name = le.getLookupString();
                if (!namesAlready.contains(name)) {
                    result.add(le);
                    namesAlready.add(name);
                }
            }
        } else {
            result.addAll(processor.getResultList());
        }
    }
    if (PyUtil.isPackage(myModule)) {
        // our module is a dir, not a single file
        if (point == PointInImport.AS_MODULE || point == PointInImport.AS_NAME || wantAllSubmodules) {
            // when imported from somehow, add submodules
            result.addAll(getSubModuleVariants(myModule.getContainingDirectory(), location, namesAlready));
        } else {
            result.addAll(collectImportedSubmodulesAsLookupElements(myModule, location, namesAlready));
        }
    }
    return result;
}
Also used : PyCustomMember(com.jetbrains.python.codeInsight.PyCustomMember) ArrayList(java.util.ArrayList) LookupElement(com.intellij.codeInsight.lookup.LookupElement)

Example 29 with LookupElement

use of com.intellij.codeInsight.lookup.LookupElement in project intellij-community by JetBrains.

the class MapKeysCompletionProvider method addCompletions.

@Override
protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet result) {
    PsiElement element = parameters.getPosition();
    GrReferenceExpression expression = (GrReferenceExpression) element.getParent();
    GrExpression qualifierExpression = expression.getQualifierExpression();
    if (qualifierExpression == null)
        return;
    PsiType mapType = qualifierExpression.getType();
    if (!InheritanceUtil.isInheritor(mapType, CommonClassNames.JAVA_UTIL_MAP)) {
        return;
    }
    PsiElement resolve = null;
    if (qualifierExpression instanceof GrMethodCall) {
        resolve = ((GrMethodCall) qualifierExpression).resolveMethod();
    } else if (qualifierExpression instanceof GrReferenceExpression) {
        resolve = ((GrReferenceExpression) qualifierExpression).resolve();
    }
    for (GroovyMapContentProvider provider : GroovyMapContentProvider.EP_NAME.getExtensions()) {
        GroovyMapCompletionUtil.addKeyVariants(provider, qualifierExpression, resolve, result);
    }
    if (mapType instanceof GrMapType) {
        for (String key : ((GrMapType) mapType).getStringKeys()) {
            LookupElement lookup = LookupElementBuilder.create(key);
            lookup = PrioritizedLookupElement.withPriority(lookup, 1);
            result.addElement(lookup);
        }
    }
}
Also used : GrMapType(org.jetbrains.plugins.groovy.lang.psi.impl.GrMapType) GrMethodCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GroovyMapContentProvider(org.jetbrains.plugins.groovy.extensions.GroovyMapContentProvider) LookupElement(com.intellij.codeInsight.lookup.LookupElement) PsiElement(com.intellij.psi.PsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) PsiType(com.intellij.psi.PsiType)

Example 30 with LookupElement

use of com.intellij.codeInsight.lookup.LookupElement in project intellij-community by JetBrains.

the class GrMainCompletionProvider method completeReference.

@NotNull
static Runnable completeReference(final CompletionParameters parameters, final GrReferenceElement reference, final JavaCompletionSession inheritorsHolder, final PrefixMatcher matcher, final Consumer<LookupElement> _consumer) {
    final Consumer<LookupElement> consumer = new Consumer<LookupElement>() {

        final Set<LookupElement> added = ContainerUtil.newHashSet();

        @Override
        public void consume(LookupElement element) {
            if (added.add(element)) {
                _consumer.consume(element);
            }
        }
    };
    final Map<PsiModifierListOwner, LookupElement> staticMembers = ContainerUtil.newHashMap();
    final PsiElement qualifier = reference.getQualifier();
    final PsiType qualifierType = GroovyCompletionUtil.getQualifierType(qualifier);
    if (reference instanceof GrReferenceExpression && (qualifier instanceof GrExpression || qualifier == null)) {
        for (String string : CompleteReferencesWithSameQualifier.getVariantsWithSameQualifier((GrReferenceExpression) reference, matcher, (GrExpression) qualifier)) {
            consumer.consume(LookupElementBuilder.create(string).withItemTextUnderlined(true));
        }
        if (parameters.getInvocationCount() < 2 && qualifier != null && qualifierType == null && !(qualifier instanceof GrReferenceExpression && ((GrReferenceExpression) qualifier).resolve() instanceof PsiPackage)) {
            if (parameters.getInvocationCount() == 1) {
                showInfo();
            }
            return EmptyRunnable.INSTANCE;
        }
    }
    final List<LookupElement> zeroPriority = ContainerUtil.newArrayList();
    PsiClass qualifierClass = com.intellij.psi.util.PsiUtil.resolveClassInClassTypeOnly(qualifierType);
    final boolean honorExcludes = qualifierClass == null || !JavaCompletionUtil.isInExcludedPackage(qualifierClass, false);
    GroovyCompletionUtil.processVariants(reference, matcher, parameters, lookupElement -> {
        Object object = lookupElement.getObject();
        if (object instanceof GroovyResolveResult) {
            object = ((GroovyResolveResult) object).getElement();
        }
        if (isLightElementDeclaredDuringCompletion(object)) {
            return;
        }
        if (!(lookupElement instanceof LookupElementBuilder) && inheritorsHolder.alreadyProcessed(lookupElement)) {
            return;
        }
        if (honorExcludes && object instanceof PsiMember && JavaCompletionUtil.isInExcludedPackage((PsiMember) object, true)) {
            return;
        }
        if (!(object instanceof PsiClass)) {
            int priority = assignPriority(lookupElement, qualifierType);
            lookupElement = JavaCompletionUtil.highlightIfNeeded(qualifierType, PrioritizedLookupElement.withPriority(lookupElement, priority), object, reference);
        }
        if ((object instanceof PsiMethod || object instanceof PsiField) && ((PsiModifierListOwner) object).hasModifierProperty(PsiModifier.STATIC)) {
            if (lookupElement.getLookupString().equals(((PsiMember) object).getName())) {
                staticMembers.put(CompletionUtil.getOriginalOrSelf((PsiModifierListOwner) object), lookupElement);
            }
        }
        PrioritizedLookupElement prio = lookupElement.as(PrioritizedLookupElement.CLASS_CONDITION_KEY);
        if (prio == null || prio.getPriority() == 0) {
            zeroPriority.add(lookupElement);
        } else {
            consumer.consume(lookupElement);
        }
    });
    for (LookupElement element : zeroPriority) {
        consumer.consume(element);
    }
    if (qualifier == null) {
        return addStaticMembers(parameters, matcher, staticMembers, consumer);
    }
    return EmptyRunnable.INSTANCE;
}
Also used : Set(java.util.Set) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) LookupElement(com.intellij.codeInsight.lookup.LookupElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) PairConsumer(com.intellij.util.PairConsumer) Consumer(com.intellij.util.Consumer) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

LookupElement (com.intellij.codeInsight.lookup.LookupElement)182 PsiElement (com.intellij.psi.PsiElement)32 NotNull (org.jetbrains.annotations.NotNull)32 LookupElementBuilder (com.intellij.codeInsight.lookup.LookupElementBuilder)19 Nullable (org.jetbrains.annotations.Nullable)17 ArrayList (java.util.ArrayList)14 Project (com.intellij.openapi.project.Project)12 LookupElementPresentation (com.intellij.codeInsight.lookup.LookupElementPresentation)10 Document (com.intellij.openapi.editor.Document)10 PsiFile (com.intellij.psi.PsiFile)10 Editor (com.intellij.openapi.editor.Editor)9 THashSet (gnu.trove.THashSet)8 LinkedHashSet (java.util.LinkedHashSet)8 PrioritizedLookupElement (com.intellij.codeInsight.completion.PrioritizedLookupElement)7 XmlTag (com.intellij.psi.xml.XmlTag)7 Consumer (com.intellij.util.Consumer)7 HashSet (java.util.HashSet)7 PsiTypeLookupItem (com.intellij.codeInsight.lookup.PsiTypeLookupItem)6 VirtualFile (com.intellij.openapi.vfs.VirtualFile)6 com.intellij.psi (com.intellij.psi)6