Search in sources :

Example 21 with LookupElement

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

the class PyDunderAllReference method getVariants.

@NotNull
@Override
public Object[] getVariants() {
    final List<LookupElement> result = new ArrayList<>();
    PyFile containingFile = (PyFile) getElement().getContainingFile().getOriginalFile();
    final List<String> dunderAll = containingFile.getDunderAll();
    final Set<String> seenNames = new HashSet<>();
    if (dunderAll != null) {
        seenNames.addAll(dunderAll);
    }
    containingFile.processDeclarations(new PsiScopeProcessor() {

        @Override
        public boolean execute(@NotNull PsiElement element, @NotNull ResolveState state) {
            if (element instanceof PsiNamedElement && !(element instanceof LightNamedElement)) {
                final String name = ((PsiNamedElement) element).getName();
                if (name != null && PyUtil.getInitialUnderscores(name) == 0 && !seenNames.contains(name)) {
                    seenNames.add(name);
                    result.add(LookupElementBuilder.createWithSmartPointer(name, element).withIcon(element.getIcon(0)));
                }
            } else if (element instanceof PyImportElement) {
                final String visibleName = ((PyImportElement) element).getVisibleName();
                if (visibleName != null && !seenNames.contains(visibleName)) {
                    seenNames.add(visibleName);
                    result.add(LookupElementBuilder.createWithSmartPointer(visibleName, element));
                }
            }
            return true;
        }

        @Override
        public <T> T getHint(@NotNull Key<T> hintKey) {
            return null;
        }

        @Override
        public void handleEvent(@NotNull Event event, @Nullable Object associated) {
        }
    }, ResolveState.initial(), null, containingFile);
    return ArrayUtil.toObjectArray(result);
}
Also used : PsiNamedElement(com.intellij.psi.PsiNamedElement) PyImportElement(com.jetbrains.python.psi.PyImportElement) ArrayList(java.util.ArrayList) PsiScopeProcessor(com.intellij.psi.scope.PsiScopeProcessor) PyFile(com.jetbrains.python.psi.PyFile) LookupElement(com.intellij.codeInsight.lookup.LookupElement) ResolveState(com.intellij.psi.ResolveState) LightNamedElement(com.jetbrains.python.psi.impl.LightNamedElement) PsiElement(com.intellij.psi.PsiElement) HashSet(java.util.HashSet) NotNull(org.jetbrains.annotations.NotNull)

Example 22 with LookupElement

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

the class GroovyDocCompletionProvider method addCompletions.

@Override
protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet result) {
    final PsiElement position = parameters.getPosition();
    GrDocMemberReference reference = PsiTreeUtil.getParentOfType(position, GrDocMemberReference.class);
    if (reference == null)
        return;
    GrDocReferenceElement holder = reference.getReferenceHolder();
    PsiElement resolved;
    if (holder != null) {
        GrCodeReferenceElement referenceElement = holder.getReferenceElement();
        resolved = referenceElement != null ? referenceElement.resolve() : null;
    } else {
        resolved = PsiUtil.getContextClass(reference);
    }
    if (resolved instanceof PsiClass) {
        ResolverProcessor propertyProcessor = CompletionProcessor.createPropertyCompletionProcessor(reference);
        resolved.processDeclarations(propertyProcessor, ResolveState.initial(), null, reference);
        PsiElement[] propertyCandidates = ResolveUtil.mapToElements(propertyProcessor.getCandidates());
        ResolverProcessor methodProcessor = CompletionProcessor.createPropertyCompletionProcessor(reference);
        resolved.processDeclarations(methodProcessor, ResolveState.initial(), null, reference);
        PsiElement[] methodCandidates = ResolveUtil.mapToElements(methodProcessor.getCandidates());
        PsiElement[] elements = ArrayUtil.mergeArrays(propertyCandidates, methodCandidates);
        for (PsiElement psiElement : elements) {
            LookupElement element = GroovyCompletionUtil.createLookupElement((PsiNamedElement) psiElement);
            if (psiElement instanceof PsiMethod) {
                element = ((LookupElementBuilder) element).withInsertHandler(new GroovyMethodSignatureInsertHandler());
            }
            result.addElement(element);
        }
    }
}
Also used : GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) ResolverProcessor(org.jetbrains.plugins.groovy.lang.resolve.processors.ResolverProcessor) GroovyMethodSignatureInsertHandler(org.jetbrains.plugins.groovy.lang.completion.handlers.GroovyMethodSignatureInsertHandler) GrDocReferenceElement(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocReferenceElement) GrDocMemberReference(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocMemberReference) LookupElement(com.intellij.codeInsight.lookup.LookupElement)

Example 23 with LookupElement

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

the class GroovyReferenceCharFilter method acceptChar.

@Override
@Nullable
public Result acceptChar(char c, int prefixLength, Lookup lookup) {
    final PsiFile psiFile = lookup.getPsiFile();
    if (psiFile != null && !psiFile.getViewProvider().getLanguages().contains(GroovyLanguage.INSTANCE))
        return null;
    LookupElement item = lookup.getCurrentItem();
    if (item == null)
        return null;
    if (Character.isJavaIdentifierPart(c) || c == '\'') {
        return Result.ADD_TO_PREFIX;
    }
    int caret = lookup.getEditor().getCaretModel().getOffset();
    if (c == '.' && prefixLength == 0 && !lookup.isSelectionTouched() && caret > 0 && lookup.getEditor().getDocument().getCharsSequence().charAt(caret - 1) == '.') {
        return Result.HIDE_LOOKUP;
    }
    if (c == ':') {
        PsiFile file = lookup.getPsiFile();
        PsiDocumentManager.getInstance(file.getProject()).commitDocument(lookup.getEditor().getDocument());
        PsiElement element = file.findElementAt(Math.max(caret - 1, 0));
        if (PsiJavaPatterns.psiElement().withParent(PsiJavaPatterns.psiElement(GrReferenceExpression.class).withParent(StandardPatterns.or(PsiJavaPatterns.psiElement(GrCaseLabel.class), PsiJavaPatterns.psiElement(GrConditionalExpression.class)))).accepts(element)) {
            return Result.SELECT_ITEM_AND_FINISH_LOOKUP;
        }
        if (item.getObject() instanceof NamedArgumentDescriptor && (MapArgumentCompletionProvider.IN_ARGUMENT_LIST_OF_CALL.accepts(element) || MapArgumentCompletionProvider.IN_LABEL.accepts(element))) {
            return Result.SELECT_ITEM_AND_FINISH_LOOKUP;
        }
        return Result.HIDE_LOOKUP;
    }
    if (c == '[' || c == ']' || c == ')' || c == '>')
        return CharFilter.Result.SELECT_ITEM_AND_FINISH_LOOKUP;
    if (c == '<' && item.getObject() instanceof PsiClass)
        return Result.SELECT_ITEM_AND_FINISH_LOOKUP;
    if (c == '(' && PsiKeyword.RETURN.equals(item.getLookupString())) {
        return Result.HIDE_LOOKUP;
    }
    return null;
}
Also used : NamedArgumentDescriptor(org.jetbrains.plugins.groovy.extensions.NamedArgumentDescriptor) LookupElement(com.intellij.codeInsight.lookup.LookupElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) Nullable(org.jetbrains.annotations.Nullable)

Example 24 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 25 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)

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