Search in sources :

Example 51 with LookupElementBuilder

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

the class AndroidCompletionContributor method addAndroidPrefixElement.

private static void addAndroidPrefixElement(PsiElement position, PsiElement parent, CompletionResultSet resultSet) {
    if (position.getText().startsWith(SdkConstants.ANDROID_NS_NAME_PREFIX)) {
        return;
    }
    final PsiElement grandparent = parent.getParent();
    if (!(grandparent instanceof XmlTag)) {
        return;
    }
    final DomElement element = DomManager.getDomManager(grandparent.getProject()).getDomElement((XmlTag) grandparent);
    if (!(element instanceof LayoutElement) && !(element instanceof PreferenceElement)) {
        return;
    }
    final String prefix = ((XmlTag) grandparent).getPrefixByNamespace(SdkConstants.NS_RESOURCES);
    if (prefix == null || prefix.length() < 3) {
        return;
    }
    final LookupElementBuilder e = LookupElementBuilder.create(prefix + ":").withTypeText("[Namespace Prefix]", true);
    resultSet.addElement(PrioritizedLookupElement.withPriority(e, Double.MAX_VALUE));
}
Also used : LayoutElement(org.jetbrains.android.dom.layout.LayoutElement) PreferenceElement(org.jetbrains.android.dom.xml.PreferenceElement) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) PsiElement(com.intellij.psi.PsiElement)

Example 52 with LookupElementBuilder

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

the class AndroidActivityAliasCompletionContributor method fillCompletionVariants.

@Override
public void fillCompletionVariants(@NotNull CompletionParameters parameters, @NotNull CompletionResultSet result) {
    if (parameters.getCompletionType() != CompletionType.BASIC) {
        return;
    }
    final Editor editor = parameters.getEditor();
    LaunchOptionConfigurableContext context = editor.getUserData(LaunchOptionConfigurableContext.KEY);
    if (context == null) {
        return;
    }
    final Module module = context.getModule();
    if (module == null) {
        return;
    }
    final AndroidFacet facet = AndroidFacet.getInstance(module);
    if (facet == null) {
        return;
    }
    final String prefix = parameters.getEditor().getDocument().getText().substring(0, parameters.getOffset());
    result = result.withPrefixMatcher(prefix);
    final PsiClass activityClass = JavaPsiFacade.getInstance(module.getProject()).findClass(AndroidUtils.ACTIVITY_BASE_CLASS_NAME, module.getModuleWithDependenciesAndLibrariesScope(false));
    if (activityClass != null) {
        final CompletionResultSet finalResult = result;
        ClassInheritorsSearch.search(activityClass, module.getModuleWithDependenciesScope(), true, true, false).forEach(new Processor<PsiClass>() {

            @Override
            public boolean process(PsiClass psiClass) {
                final PsiModifierList modifierList = psiClass.getModifierList();
                if (modifierList != null && modifierList.hasModifierProperty(PsiModifier.PUBLIC) && !modifierList.hasModifierProperty(PsiModifier.ABSTRACT)) {
                    final String qName = psiClass.getQualifiedName();
                    if (qName != null && qName.length() > 0) {
                        finalResult.addElement(JavaLookupElementBuilder.forClass(psiClass, qName));
                    }
                }
                return true;
            }
        });
    }
    final Set<String> aliases = collectActivityAliases(facet);
    if (aliases.size() > 0) {
        for (String alias : aliases) {
            LookupElementBuilder element = LookupElementBuilder.create(alias);
            final String shortName = StringUtilRt.getShortName(alias);
            if (!shortName.equals(alias)) {
                element = element.withLookupString(shortName);
            }
            result.addElement(element);
        }
    }
    result.stopHere();
}
Also used : LaunchOptionConfigurableContext(com.android.tools.idea.run.editor.LaunchOptionConfigurableContext) PsiClass(com.intellij.psi.PsiClass) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) Editor(com.intellij.openapi.editor.Editor) Module(com.intellij.openapi.module.Module) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) PsiModifierList(com.intellij.psi.PsiModifierList)

Example 53 with LookupElementBuilder

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

the class LanguageListCompletionContributor method doFillVariants.

private static void doFillVariants(@NotNull CompletionParameters parameters, @NotNull CompletionResultSet result) {
    for (CodeFenceLanguageProvider provider : LanguageGuesser.INSTANCE.getCodeFenceLanguageProviders()) {
        final List<LookupElement> lookups = provider.getCompletionVariantsForInfoString(parameters);
        for (LookupElement lookupElement : lookups) {
            result.addElement(LookupElementDecorator.withInsertHandler(lookupElement, (context, item) -> {
                new MyInsertHandler(parameters).handleInsert(context, item);
                lookupElement.handleInsert(context);
            }));
        }
    }
    for (Map.Entry<String, Language> entry : LanguageGuesser.INSTANCE.getLangToLanguageMap().entrySet()) {
        final Language language = entry.getValue();
        final LookupElementBuilder lookupElementBuilder = LookupElementBuilder.create(entry.getKey()).withIcon(new DeferredIconImpl<>(null, language, true, language1 -> {
            final LanguageFileType fileType = language1.getAssociatedFileType();
            return fileType != null ? fileType.getIcon() : null;
        })).withTypeText(language.getDisplayName(), true).withInsertHandler(new MyInsertHandler(parameters));
        result.addElement(lookupElementBuilder);
    }
}
Also used : Language(com.intellij.lang.Language) MarkdownElementTypes(org.intellij.plugins.markdown.lang.MarkdownElementTypes) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) LookupElement(com.intellij.codeInsight.lookup.LookupElement) MarkdownTokenTypes(org.intellij.plugins.markdown.lang.MarkdownTokenTypes) MarkdownFile(org.intellij.plugins.markdown.lang.psi.impl.MarkdownFile) TextRange(com.intellij.openapi.util.TextRange) DeferredIconImpl(com.intellij.ui.DeferredIconImpl) com.intellij.codeInsight.completion(com.intellij.codeInsight.completion) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) LanguageFileType(com.intellij.openapi.fileTypes.LanguageFileType) PsiUtilCore(com.intellij.psi.util.PsiUtilCore) Map(java.util.Map) PsiElement(com.intellij.psi.PsiElement) LookupElementDecorator(com.intellij.codeInsight.lookup.LookupElementDecorator) NotNull(org.jetbrains.annotations.NotNull) LanguageFileType(com.intellij.openapi.fileTypes.LanguageFileType) Language(com.intellij.lang.Language) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) LookupElement(com.intellij.codeInsight.lookup.LookupElement) Map(java.util.Map)

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