Search in sources :

Example 86 with LookupElement

use of com.intellij.codeInsight.lookup.LookupElement in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoCompletionTestBase method selectLookupItem.

protected void selectLookupItem(@NotNull String selectItem) {
    LookupElement[] lookupElements = myFixture.getLookupElements();
    assertNotNull("Lookup is empty", lookupElements);
    LookupElement toSelect = null;
    for (LookupElement lookupElement : lookupElements) {
        if (selectItem.equals(lookupElement.getLookupString())) {
            toSelect = lookupElement;
            break;
        }
    }
    assertNotNull(selectItem + " not found in lookup", toSelect);
    myFixture.getLookup().setCurrentItem(toSelect);
    myFixture.type(Lookup.NORMAL_SELECT_CHAR);
}
Also used : LookupElement(com.intellij.codeInsight.lookup.LookupElement)

Example 87 with LookupElement

use of com.intellij.codeInsight.lookup.LookupElement in project smali by JesusFreke.

the class SmaliCodeFragmentFactoryTest method assertCompletionContains.

private void assertCompletionContains(String completionText, PsiElement context, String[] expectedItems, String[] disallowedItems) {
    SmaliCodeFragmentFactory codeFragmentFactory = new SmaliCodeFragmentFactory();
    JavaCodeFragment fragment = codeFragmentFactory.createCodeFragment(new TextWithImportsImpl(CodeFragmentKind.EXPRESSION, completionText), context, getProject());
    Editor editor = createEditor(fragment.getVirtualFile());
    editor.getCaretModel().moveToOffset(completionText.length());
    new CodeCompletionHandlerBase(CompletionType.BASIC).invokeCompletion(getProject(), editor);
    List<LookupElement> elements = LookupManager.getInstance(getProject()).getActiveLookup().getItems();
    HashSet expectedSet = Sets.newHashSet(expectedItems);
    HashSet disallowedSet = Sets.newHashSet(disallowedItems);
    for (LookupElement element : elements) {
        expectedSet.remove(element.toString());
        Assert.assertFalse(disallowedSet.contains(element.toString()));
    }
    Assert.assertTrue(expectedSet.size() == 0);
}
Also used : SmaliCodeFragmentFactory(org.jf.smalidea.debugging.SmaliCodeFragmentFactory) TextWithImportsImpl(com.intellij.debugger.engine.evaluation.TextWithImportsImpl) CodeCompletionHandlerBase(com.intellij.codeInsight.completion.CodeCompletionHandlerBase) Editor(com.intellij.openapi.editor.Editor) LookupElement(com.intellij.codeInsight.lookup.LookupElement) JavaCodeFragment(com.intellij.psi.JavaCodeFragment) HashSet(java.util.HashSet)

Example 88 with LookupElement

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

the class MethodsChainLookupRangingHelper method chainsToWeightableLookupElements.

public static List<LookupElement> chainsToWeightableLookupElements(final List<MethodsChain> chains, final ChainCompletionContext context) {
    final CachedRelevantStaticMethodSearcher staticMethodSearcher = new CachedRelevantStaticMethodSearcher(context);
    final List<LookupElement> lookupElements = new ArrayList<>(chains.size());
    for (final MethodsChain chain : chains) {
        final LookupElement lookupElement = chainToWeightableLookupElement(chain, context, staticMethodSearcher);
        if (lookupElement != null) {
            lookupElements.add(lookupElement);
        }
    }
    return lookupElements;
}
Also used : ArrayList(java.util.ArrayList) JavaChainLookupElement(com.intellij.codeInsight.completion.JavaChainLookupElement) SubLookupElement(com.intellij.compiler.classFilesIndex.chainsSearch.completion.lookup.sub.SubLookupElement) LookupElement(com.intellij.codeInsight.lookup.LookupElement) ChainCompletionLookupElementUtil.createLookupElement(com.intellij.compiler.classFilesIndex.chainsSearch.completion.lookup.ChainCompletionLookupElementUtil.createLookupElement) WeightableChainLookupElement(com.intellij.compiler.classFilesIndex.chainsSearch.completion.lookup.WeightableChainLookupElement) ChainCompletionNewVariableLookupElement(com.intellij.compiler.classFilesIndex.chainsSearch.completion.lookup.ChainCompletionNewVariableLookupElement) GetterLookupSubLookupElement(com.intellij.compiler.classFilesIndex.chainsSearch.completion.lookup.sub.GetterLookupSubLookupElement) VariableSubLookupElement(com.intellij.compiler.classFilesIndex.chainsSearch.completion.lookup.sub.VariableSubLookupElement)

Example 89 with LookupElement

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

the class ThemeEditorGuiTestUtils method getCompletionPopup.

/**
   * Returns a {@link JListFixture} for the auto-completion popup
   */
@NotNull
public static JListFixture getCompletionPopup(@NotNull Robot robot) {
    JBList list = GuiTests.waitUntilFound(robot, new GenericTypeMatcher<JBList>(JBList.class) {

        @Override
        protected boolean isMatching(@NotNull JBList component) {
            ListModel listModel = component.getModel();
            return listModel.getSize() > 0 && listModel.getElementAt(0) instanceof LookupElement;
        }
    });
    JListFixture listFixture = new JListFixture(robot, list);
    listFixture.replaceCellReader((jList, index) -> ((LookupElement) jList.getModel().getElementAt(index)).getLookupString());
    return listFixture;
}
Also used : JListFixture(org.fest.swing.fixture.JListFixture) JBList(com.intellij.ui.components.JBList) LookupElement(com.intellij.codeInsight.lookup.LookupElement) NotNull(org.jetbrains.annotations.NotNull)

Example 90 with LookupElement

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

the class ResourceTypeCompletionContributor method fillCompletionVariants.

@Override
public void fillCompletionVariants(@NotNull final CompletionParameters parameters, @NotNull final CompletionResultSet result) {
    //if (parameters.getCompletionType() != CompletionType.SMART) return;
    PsiElement pos = parameters.getPosition();
    if (JavaKeywordCompletion.AFTER_DOT.accepts(pos)) {
        return;
    }
    AndroidFacet facet = AndroidFacet.getInstance(pos);
    if (facet == null) {
        return;
    }
    ResourceTypeInspection.Constraints allowedValues = getAllowedValues(pos);
    if (allowedValues == null)
        return;
    final Set<PsiElement> allowed = new THashSet<PsiElement>(new TObjectHashingStrategy<PsiElement>() {

        @Override
        public int computeHashCode(PsiElement object) {
            return 0;
        }

        @Override
        public boolean equals(PsiElement o1, PsiElement o2) {
            return parameters.getOriginalFile().getManager().areElementsEquivalent(o1, o2);
        }
    });
    // Suggest resource types
    if (allowedValues instanceof ResourceTypeInspection.ResourceTypeAllowedValues) {
        for (ResourceType resourceType : ((ResourceTypeInspection.ResourceTypeAllowedValues) allowedValues).types) {
            // are markers for @ColorInt and @Px
            if (resourceType == COLOR_INT_MARKER_TYPE || resourceType == DIMENSION_MARKER_TYPE) {
                continue;
            }
            PsiElementFactory factory = JavaPsiFacade.getElementFactory(pos.getProject());
            String code = "R." + resourceType.getName();
            // Look up the fully qualified name of the application package
            String fqcn = MergedManifest.get(facet).getPackage();
            String qualifiedCode = fqcn + "." + code;
            Project project = facet.getModule().getProject();
            PsiClass cls = JavaPsiFacade.getInstance(project).findClass(qualifiedCode, GlobalSearchScope.allScope(project));
            if (cls != null) {
                result.addElement(new JavaPsiClassReferenceElement(cls));
            } else {
                PsiExpression type = factory.createExpressionFromText(code, pos);
                result.addElement(PrioritizedLookupElement.withPriority(LookupElementBuilder.create(type, code), PRIORITY - 1));
                allowed.add(type);
            }
        }
    } else if (allowedValues instanceof ResourceTypeInspection.AllowedValues) {
        ResourceTypeInspection.AllowedValues a = (ResourceTypeInspection.AllowedValues) allowedValues;
        if (a.canBeOred) {
            PsiElementFactory factory = JavaPsiFacade.getElementFactory(pos.getProject());
            PsiExpression zero = factory.createExpressionFromText("0", pos);
            result.addElement(PrioritizedLookupElement.withPriority(LookupElementBuilder.create(zero, "0"), PRIORITY - 1));
            PsiExpression minusOne = factory.createExpressionFromText("-1", pos);
            result.addElement(PrioritizedLookupElement.withPriority(LookupElementBuilder.create(minusOne, "-1"), PRIORITY - 1));
            allowed.add(zero);
            allowed.add(minusOne);
        }
        List<ExpectedTypeInfo> types = Arrays.asList(JavaSmartCompletionContributor.getExpectedTypes(parameters));
        for (PsiAnnotationMemberValue value : a.values) {
            if (value instanceof PsiReference) {
                PsiElement resolved = ((PsiReference) value).resolve();
                if (resolved instanceof PsiNamedElement) {
                    LookupElement lookupElement = LookupItemUtil.objectToLookupItem(resolved);
                    if (lookupElement instanceof VariableLookupItem) {
                        ((VariableLookupItem) lookupElement).setSubstitutor(PsiSubstitutor.EMPTY);
                    }
                    LookupElement element = PrioritizedLookupElement.withPriority(lookupElement, PRIORITY);
                    element = decorate(parameters, types, element);
                    result.addElement(element);
                    allowed.add(resolved);
                    continue;
                }
            }
            LookupElement element = LookupElementBuilder.create(value, value.getText());
            element = decorate(parameters, types, element);
            result.addElement(element);
            allowed.add(value);
        }
    }
    result.runRemainingContributors(parameters, new Consumer<CompletionResult>() {

        @Override
        public void consume(CompletionResult completionResult) {
            LookupElement element = completionResult.getLookupElement();
            Object object = element.getObject();
            if (object instanceof PsiElement && allowed.contains(object)) {
                return;
            }
            result.passResult(completionResult);
        }
    });
}
Also used : VariableLookupItem(com.intellij.codeInsight.lookup.VariableLookupItem) List(java.util.List) ResourceType(com.android.resources.ResourceType) LookupElement(com.intellij.codeInsight.lookup.LookupElement) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) THashSet(gnu.trove.THashSet) Project(com.intellij.openapi.project.Project)

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