Search in sources :

Example 16 with LookupElement

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

the class GroovySmartCompletionContributor method addExpectedClassMembers.

static void addExpectedClassMembers(CompletionParameters params, final CompletionResultSet result) {
    for (final TypeConstraint info : getExpectedTypeInfos(params)) {
        Consumer<LookupElement> consumer = element -> result.addElement(element);
        PsiType type = info.getType();
        PsiType defType = info.getDefaultType();
        boolean searchInheritors = params.getInvocationCount() > 1;
        if (type instanceof PsiClassType) {
            new GroovyMembersGetter((PsiClassType) type, params).processMembers(searchInheritors, consumer);
        }
        if (!defType.equals(type) && defType instanceof PsiClassType) {
            new GroovyMembersGetter((PsiClassType) defType, params).processMembers(searchInheritors, consumer);
        }
    }
}
Also used : TypeConversionUtil(com.intellij.psi.util.TypeConversionUtil) Arrays(java.util.Arrays) IElementType(com.intellij.psi.tree.IElementType) Document(com.intellij.openapi.editor.Document) THashSet(gnu.trove.THashSet) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement) GrLiteral(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral) AfterNewClassInsertHandler(org.jetbrains.plugins.groovy.lang.completion.handlers.AfterNewClassInsertHandler) TailType(com.intellij.codeInsight.TailType) PlatformPatterns(com.intellij.patterns.PlatformPatterns) TObjectHashingStrategy(gnu.trove.TObjectHashingStrategy) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) ScrollType(com.intellij.openapi.editor.ScrollType) SmartList(com.intellij.util.SmartList) TypeInferenceHelper(org.jetbrains.plugins.groovy.lang.psi.dataFlow.types.TypeInferenceHelper) FeatureUsageTracker(com.intellij.featureStatistics.FeatureUsageTracker) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) CodeStyleSettings(com.intellij.psi.codeStyle.CodeStyleSettings) PsiTypeLookupItem(com.intellij.codeInsight.lookup.PsiTypeLookupItem) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GroovyTokenTypes(org.jetbrains.plugins.groovy.lang.lexer.GroovyTokenTypes) ProcessingContext(com.intellij.util.ProcessingContext) GrAnnotationNameValuePair(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationNameValuePair) GroovyConfigUtils(org.jetbrains.plugins.groovy.config.GroovyConfigUtils) TypeConstraint(org.jetbrains.plugins.groovy.lang.psi.expectedTypes.TypeConstraint) LookupElement(com.intellij.codeInsight.lookup.LookupElement) ChooseTypeExpression(org.jetbrains.plugins.groovy.template.expressions.ChooseTypeExpression) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) Set(java.util.Set) StandardPatterns(com.intellij.patterns.StandardPatterns) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) CodeStyleSettingsManager(com.intellij.psi.codeStyle.CodeStyleSettingsManager) Editor(com.intellij.openapi.editor.Editor) ElementPattern(com.intellij.patterns.ElementPattern) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) com.intellij.codeInsight.completion(com.intellij.codeInsight.completion) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) GrAnnotation(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation) org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions) com.intellij.psi(com.intellij.psi) GroovyExpectedTypesProvider(org.jetbrains.plugins.groovy.lang.psi.expectedTypes.GroovyExpectedTypesProvider) NotNull(org.jetbrains.annotations.NotNull) Consumer(com.intellij.util.Consumer) PsiUtil(org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil) TypeConstraint(org.jetbrains.plugins.groovy.lang.psi.expectedTypes.TypeConstraint) LookupElement(com.intellij.codeInsight.lookup.LookupElement)

Example 17 with LookupElement

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

the class MapArgumentCompletionProvider method addCompletions.

@Override
protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet result) {
    PsiElement mapOrArgumentList = findMapOrArgumentList(parameters);
    if (mapOrArgumentList == null) {
        return;
    }
    if (isMapKeyCompletion(parameters)) {
        result.stopHere();
    }
    final Map<String, NamedArgumentDescriptor> map = ContainerUtil.newHashMap(calculateNamedArguments(mapOrArgumentList));
    for (GrNamedArgument argument : getSiblingNamedArguments(mapOrArgumentList)) {
        map.remove(argument.getLabelName());
    }
    for (Map.Entry<String, NamedArgumentDescriptor> entry : map.entrySet()) {
        LookupElementBuilder lookup = LookupElementBuilder.create(entry.getValue(), entry.getKey()).withInsertHandler(NamedArgumentInsertHandler.INSTANCE).withTailText(":");
        if (entry.getValue().getPriority() == NamedArgumentDescriptor.Priority.UNLIKELY) {
            lookup = lookup.withItemTextForeground(GroovySyntaxHighlighter.MAP_KEY.getDefaultAttributes().getForegroundColor());
        } else {
            lookup = lookup.withIcon(JetgroovyIcons.Groovy.DynamicProperty);
        }
        LookupElement customized = entry.getValue().customizeLookupElement(lookup);
        result.addElement(customized == null ? lookup : customized);
    }
}
Also used : NamedArgumentDescriptor(org.jetbrains.plugins.groovy.extensions.NamedArgumentDescriptor) GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) LookupElement(com.intellij.codeInsight.lookup.LookupElement) HashMap(java.util.HashMap) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) Map(java.util.Map) PsiElement(com.intellij.psi.PsiElement)

Example 18 with LookupElement

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

the class ChooseTypeExpression method calculateLookupItems.

@Override
public LookupElement[] calculateLookupItems(ExpressionContext context) {
    List<LookupElement> result = ContainerUtil.newArrayList();
    for (SmartTypePointer item : myItems) {
        PsiType type = TypesUtil.unboxPrimitiveTypeWrapper(item.getType());
        if (type == null)
            continue;
        PsiTypeLookupItem lookupItem = PsiTypeLookupItem.createLookupItem(type, null, PsiTypeLookupItem.isDiamond(type), IMPORT_FIXER);
        result.add(lookupItem);
    }
    if (myForGroovy) {
        LookupElementBuilder def = LookupElementBuilder.create(GrModifier.DEF).bold();
        if (mySelectDef) {
            result.add(0, def);
        } else {
            result.add(def);
        }
    }
    return result.toArray(new LookupElement[result.size()]);
}
Also used : PsiTypeLookupItem(com.intellij.codeInsight.lookup.PsiTypeLookupItem) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) LookupElement(com.intellij.codeInsight.lookup.LookupElement)

Example 19 with LookupElement

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

the class TestUtils method checkCompletionContains.

public static void checkCompletionContains(JavaCodeInsightTestFixture fixture, String... expectedVariants) {
    LookupElement[] lookupElements = fixture.completeBasic();
    Assert.assertNotNull(lookupElements);
    Set<String> missedVariants = ContainerUtil.newHashSet(expectedVariants);
    for (LookupElement lookupElement : lookupElements) {
        String lookupString = lookupElement.getLookupString();
        missedVariants.remove(lookupString);
        Object object = lookupElement.getObject();
        if (object instanceof ResolveResult) {
            object = ((ResolveResult) object).getElement();
        }
        if (object instanceof PsiMethod) {
            missedVariants.remove(lookupString + "()");
        } else if (object instanceof PsiVariable) {
            missedVariants.remove('@' + lookupString);
        } else if (object instanceof NamedArgumentDescriptor) {
            missedVariants.remove(lookupString + ':');
        }
    }
    if (missedVariants.size() > 0) {
        Assert.assertTrue("Some completion variants are missed " + missedVariants, false);
    }
}
Also used : NamedArgumentDescriptor(org.jetbrains.plugins.groovy.extensions.NamedArgumentDescriptor) LookupElement(com.intellij.codeInsight.lookup.LookupElement)

Example 20 with LookupElement

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

the class MavenVersionCompletionContributor method fillCompletionVariants.

@Override
public void fillCompletionVariants(@NotNull CompletionParameters parameters, @NotNull CompletionResultSet result) {
    if (parameters.getCompletionType() != CompletionType.BASIC)
        return;
    PsiElement element = parameters.getPosition();
    PsiElement xmlText = element.getParent();
    if (!(xmlText instanceof XmlText))
        return;
    PsiElement tagElement = xmlText.getParent();
    if (!(tagElement instanceof XmlTag))
        return;
    XmlTag tag = (XmlTag) tagElement;
    Project project = element.getProject();
    DomElement domElement = DomManager.getDomManager(project).getDomElement(tag);
    if (!(domElement instanceof GenericDomValue))
        return;
    DomElement parent = domElement.getParent();
    if (parent instanceof MavenDomArtifactCoordinates && ((GenericDomValue) domElement).getConverter() instanceof MavenArtifactCoordinatesVersionConverter) {
        MavenDomArtifactCoordinates coordinates = (MavenDomArtifactCoordinates) parent;
        String groupId = coordinates.getGroupId().getStringValue();
        String artifactId = coordinates.getArtifactId().getStringValue();
        if (StringUtil.isEmptyOrSpaces(artifactId))
            return;
        CompletionResultSet newResultSet = result.withRelevanceSorter(CompletionService.getCompletionService().emptySorter().weigh(new LookupElementWeigher("mavenVersionWeigher") {

            @Nullable
            @Override
            public Comparable weigh(@NotNull LookupElement element) {
                return new NegatingComparable(new MavenVersionComparable(element.getLookupString()));
            }
        }));
        MavenProjectIndicesManager indicesManager = MavenProjectIndicesManager.getInstance(project);
        Set<String> versions;
        if (StringUtil.isEmptyOrSpaces(groupId)) {
            if (!(coordinates instanceof MavenDomPlugin))
                return;
            versions = indicesManager.getVersions(MavenArtifactUtil.DEFAULT_GROUPS[0], artifactId);
            for (int i = 0; i < MavenArtifactUtil.DEFAULT_GROUPS.length; i++) {
                versions = Sets.union(versions, indicesManager.getVersions(MavenArtifactUtil.DEFAULT_GROUPS[i], artifactId));
            }
        } else {
            versions = indicesManager.getVersions(groupId, artifactId);
        }
        for (String version : versions) {
            newResultSet.addElement(LookupElementBuilder.create(version));
        }
        newResultSet.addElement(LookupElementBuilder.create(RepositoryUtils.ReleaseVersionId));
        newResultSet.addElement(LookupElementBuilder.create(RepositoryUtils.LatestVersionId));
    }
}
Also used : NegatingComparable(com.intellij.codeInsight.completion.impl.NegatingComparable) MavenProjectIndicesManager(org.jetbrains.idea.maven.indices.MavenProjectIndicesManager) MavenVersionComparable(org.jetbrains.idea.maven.dom.MavenVersionComparable) MavenDomPlugin(org.jetbrains.idea.maven.dom.model.MavenDomPlugin) MavenArtifactCoordinatesVersionConverter(org.jetbrains.idea.maven.dom.converters.MavenArtifactCoordinatesVersionConverter) LookupElement(com.intellij.codeInsight.lookup.LookupElement) NotNull(org.jetbrains.annotations.NotNull) Project(com.intellij.openapi.project.Project) DomElement(com.intellij.util.xml.DomElement) LookupElementWeigher(com.intellij.codeInsight.lookup.LookupElementWeigher) XmlText(com.intellij.psi.xml.XmlText) MavenDomArtifactCoordinates(org.jetbrains.idea.maven.dom.model.MavenDomArtifactCoordinates) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag) GenericDomValue(com.intellij.util.xml.GenericDomValue)

Aggregations

LookupElement (com.intellij.codeInsight.lookup.LookupElement)183 PsiElement (com.intellij.psi.PsiElement)33 NotNull (org.jetbrains.annotations.NotNull)32 LookupElementBuilder (com.intellij.codeInsight.lookup.LookupElementBuilder)20 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 InsertionContext (com.intellij.codeInsight.completion.InsertionContext)6 PsiTypeLookupItem (com.intellij.codeInsight.lookup.PsiTypeLookupItem)6 VirtualFile (com.intellij.openapi.vfs.VirtualFile)6