Search in sources :

Example 1 with LookupElementBuilder

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

the class PydevConsoleReference method getVariants.

@NotNull
public Object[] getVariants() {
    Map<String, LookupElement> variants = Maps.newHashMap();
    try {
        final List<PydevCompletionVariant> completions = myCommunication.getCompletions(getText(), myPrefix);
        for (PydevCompletionVariant completion : completions) {
            final PsiManager manager = myElement.getManager();
            final String name = completion.getName();
            final int type = completion.getType();
            LookupElementBuilder builder = LookupElementBuilder.create(new PydevConsoleElement(manager, name, completion.getDescription())).withIcon(PyCodeCompletionImages.getImageForType(type));
            String args = completion.getArgs();
            if (args.equals("(%)")) {
                builder.withPresentableText("%" + completion.getName());
                builder = builder.withInsertHandler(new InsertHandler<LookupElement>() {

                    @Override
                    public void handleInsert(InsertionContext context, LookupElement item) {
                        final Editor editor = context.getEditor();
                        final Document document = editor.getDocument();
                        int offset = context.getStartOffset();
                        if (offset == 0 || !"%".equals(document.getText(TextRange.from(offset - 1, 1)))) {
                            document.insertString(offset, "%");
                        }
                    }
                });
                args = "";
            } else if (!StringUtil.isEmptyOrSpaces(args)) {
                builder = builder.withTailText(args);
            }
            // Set function insert handler
            if (type == IToken.TYPE_FUNCTION || args.endsWith(")")) {
                builder = builder.withInsertHandler(ParenthesesInsertHandler.WITH_PARAMETERS);
            }
            variants.put(name, builder);
        }
    } catch (Exception e) {
    //LOG.error(e);
    }
    return variants.values().toArray();
}
Also used : LookupElement(com.intellij.codeInsight.lookup.LookupElement) InsertionContext(com.intellij.codeInsight.completion.InsertionContext) Document(com.intellij.openapi.editor.Document) PydevCompletionVariant(com.jetbrains.python.console.pydev.PydevCompletionVariant) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) InsertHandler(com.intellij.codeInsight.completion.InsertHandler) ParenthesesInsertHandler(com.intellij.codeInsight.completion.util.ParenthesesInsertHandler) Editor(com.intellij.openapi.editor.Editor) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with LookupElementBuilder

use of com.intellij.codeInsight.lookup.LookupElementBuilder 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 3 with LookupElementBuilder

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

the class CompleteReferenceExpression method createPropertyLookupElement.

@Nullable
public static LookupElementBuilder createPropertyLookupElement(@NotNull PsiMethod accessor, @Nullable GroovyResolveResult resolveResult, @Nullable PrefixMatcher matcher) {
    String propName;
    PsiType propType;
    final boolean getter = GroovyPropertyUtils.isSimplePropertyGetter(accessor, null);
    if (getter) {
        propName = GroovyPropertyUtils.getPropertyNameByGetter(accessor);
    } else if (GroovyPropertyUtils.isSimplePropertySetter(accessor, null)) {
        propName = GroovyPropertyUtils.getPropertyNameBySetter(accessor);
    } else {
        return null;
    }
    assert propName != null;
    if (!PsiUtil.isValidReferenceName(propName)) {
        propName = "'" + propName + "'";
    }
    if (matcher != null && !matcher.prefixMatches(propName)) {
        return null;
    }
    if (getter) {
        propType = PsiUtil.getSmartReturnType(accessor);
    } else {
        propType = accessor.getParameterList().getParameters()[0].getType();
    }
    final PsiType substituted = resolveResult != null ? resolveResult.getSubstitutor().substitute(propType) : propType;
    LookupElementBuilder builder = LookupElementBuilder.create(generatePropertyResolveResult(propName, accessor, propType, resolveResult), propName).withIcon(JetgroovyIcons.Groovy.Property);
    if (substituted != null) {
        builder = builder.withTypeText(substituted.getPresentableText());
    }
    return builder;
}
Also used : LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with LookupElementBuilder

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

the class ResourceBundleReference method getVariants.

@Override
@NotNull
public Object[] getVariants() {
    final ProjectFileIndex projectFileIndex = ProjectFileIndex.SERVICE.getInstance(getElement().getProject());
    final PropertiesReferenceManager referenceManager = PropertiesReferenceManager.getInstance(getElement().getProject());
    final Set<String> bundleNames = new HashSet<>();
    final List<LookupElement> variants = new SmartList<>();
    PropertiesFileProcessor processor = new PropertiesFileProcessor() {

        @Override
        public boolean process(String baseName, PropertiesFile propertiesFile) {
            if (!bundleNames.add(baseName))
                return true;
            final LookupElementBuilder builder = LookupElementBuilder.create(baseName).withIcon(AllIcons.Nodes.ResourceBundle);
            boolean isInContent = projectFileIndex.isInContent(propertiesFile.getVirtualFile());
            variants.add(isInContent ? PrioritizedLookupElement.withPriority(builder, Double.MAX_VALUE) : builder);
            return true;
        }
    };
    referenceManager.processPropertiesFiles(myElement.getResolveScope(), processor, this);
    return variants.toArray(new LookupElement[variants.size()]);
}
Also used : ProjectFileIndex(com.intellij.openapi.roots.ProjectFileIndex) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) SmartList(com.intellij.util.SmartList) LookupElement(com.intellij.codeInsight.lookup.LookupElement) PrioritizedLookupElement(com.intellij.codeInsight.completion.PrioritizedLookupElement) HashSet(java.util.HashSet) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with LookupElementBuilder

use of com.intellij.codeInsight.lookup.LookupElementBuilder 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

LookupElementBuilder (com.intellij.codeInsight.lookup.LookupElementBuilder)53 LookupElement (com.intellij.codeInsight.lookup.LookupElement)19 NotNull (org.jetbrains.annotations.NotNull)16 PsiElement (com.intellij.psi.PsiElement)12 Nullable (org.jetbrains.annotations.Nullable)9 PsiFile (com.intellij.psi.PsiFile)6 Document (com.intellij.openapi.editor.Document)5 ArrayList (java.util.ArrayList)4 PrioritizedLookupElement (com.intellij.codeInsight.completion.PrioritizedLookupElement)3 Editor (com.intellij.openapi.editor.Editor)3 TextRange (com.intellij.openapi.util.TextRange)3 PsiPresentableMetaData (com.intellij.psi.meta.PsiPresentableMetaData)3 XmlFile (com.intellij.psi.xml.XmlFile)3 XmlExtension (com.intellij.xml.XmlExtension)3 com.intellij.codeInsight.completion (com.intellij.codeInsight.completion)2 CompletionResultSet (com.intellij.codeInsight.completion.CompletionResultSet)2 ParenthesesInsertHandler (com.intellij.codeInsight.completion.util.ParenthesesInsertHandler)2 AutoCompletionPolicy (com.intellij.codeInsight.lookup.AutoCompletionPolicy)2 AllIcons (com.intellij.icons.AllIcons)2 FileType (com.intellij.openapi.fileTypes.FileType)2