Search in sources :

Example 71 with LookupElement

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

the class FunctionalExpressionCompletionProvider method addFunctionalVariants.

static void addFunctionalVariants(@NotNull CompletionParameters parameters, boolean smart, boolean addInheritors, CompletionResultSet result) {
    if (!PsiUtil.isLanguageLevel8OrHigher(parameters.getOriginalFile()) || !isLambdaContext(parameters.getPosition()))
        return;
    ExpectedTypeInfo[] expectedTypes = JavaSmartCompletionContributor.getExpectedTypes(parameters);
    for (ExpectedTypeInfo expectedType : expectedTypes) {
        final PsiType defaultType = expectedType.getDefaultType();
        if (LambdaUtil.isFunctionalType(defaultType)) {
            final PsiType functionalInterfaceType = FunctionalInterfaceParameterizationUtil.getGroundTargetType(defaultType);
            final PsiMethod functionalInterfaceMethod = LambdaUtil.getFunctionalInterfaceMethod(functionalInterfaceType);
            if (functionalInterfaceMethod != null) {
                PsiParameter[] params = PsiParameter.EMPTY_ARRAY;
                final PsiElement originalPosition = parameters.getPosition();
                final PsiSubstitutor substitutor = LambdaUtil.getSubstitutor(functionalInterfaceMethod, PsiUtil.resolveGenericsClassInType(functionalInterfaceType));
                if (!functionalInterfaceMethod.hasTypeParameters()) {
                    params = functionalInterfaceMethod.getParameterList().getParameters();
                    final Project project = functionalInterfaceMethod.getProject();
                    final JVMElementFactory jvmElementFactory = JVMElementFactories.getFactory(originalPosition.getLanguage(), project);
                    final JavaCodeStyleManager javaCodeStyleManager = JavaCodeStyleManager.getInstance(project);
                    if (jvmElementFactory != null) {
                        params = GenerateMembersUtil.overriddenParameters(params, jvmElementFactory, javaCodeStyleManager, substitutor, originalPosition);
                    }
                    String paramsString = params.length == 1 ? getParamName(params[0], originalPosition) : "(" + StringUtil.join(params, parameter -> getParamName(parameter, originalPosition), ",") + ")";
                    final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
                    PsiLambdaExpression lambdaExpression = (PsiLambdaExpression) JavaPsiFacade.getElementFactory(project).createExpressionFromText(paramsString + " -> {}", null);
                    lambdaExpression = (PsiLambdaExpression) codeStyleManager.reformat(lambdaExpression);
                    paramsString = lambdaExpression.getParameterList().getText();
                    final LookupElementBuilder builder = LookupElementBuilder.create(functionalInterfaceMethod, paramsString + " -> ").withPresentableText(paramsString + " -> {}").withTypeText(functionalInterfaceType.getPresentableText()).withIcon(AllIcons.Nodes.Function);
                    LookupElement lambdaElement = builder.withAutoCompletionPolicy(AutoCompletionPolicy.NEVER_AUTOCOMPLETE);
                    result.addElement(smart ? lambdaElement : PrioritizedLookupElement.withPriority(lambdaElement, 1));
                }
                addMethodReferenceVariants(smart, addInheritors, parameters, result.getPrefixMatcher(), functionalInterfaceType, functionalInterfaceMethod, params, originalPosition, substitutor, element -> result.addElement(smart ? JavaSmartCompletionContributor.decorate(element, Arrays.asList(expectedTypes)) : element));
            }
        }
    }
}
Also used : TypeConversionUtil(com.intellij.psi.util.TypeConversionUtil) java.util(java.util) AllIcons(com.intellij.icons.AllIcons) JBIterable(com.intellij.util.containers.JBIterable) ExpectedTypeInfo(com.intellij.codeInsight.ExpectedTypeInfo) JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) Comparing(com.intellij.openapi.util.Comparing) Project(com.intellij.openapi.project.Project) JavaResolveUtil(com.intellij.psi.impl.source.resolve.JavaResolveUtil) PsiUtil(com.intellij.psi.util.PsiUtil) ProcessingContext(com.intellij.util.ProcessingContext) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) LookupElement(com.intellij.codeInsight.lookup.LookupElement) StringUtil(com.intellij.openapi.util.text.StringUtil) GenerateMembersUtil(com.intellij.codeInsight.generation.GenerateMembersUtil) AutoCompletionPolicy(com.intellij.codeInsight.lookup.AutoCompletionPolicy) MethodReferenceResolver(com.intellij.psi.impl.source.tree.java.MethodReferenceResolver) Nullable(org.jetbrains.annotations.Nullable) CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager) FunctionalInterfaceParameterizationUtil(com.intellij.psi.impl.source.resolve.graphInference.FunctionalInterfaceParameterizationUtil) ObjectUtils(com.intellij.util.ObjectUtils) com.intellij.psi(com.intellij.psi) NotNull(org.jetbrains.annotations.NotNull) Consumer(com.intellij.util.Consumer) JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager) LookupElement(com.intellij.codeInsight.lookup.LookupElement) Project(com.intellij.openapi.project.Project) ExpectedTypeInfo(com.intellij.codeInsight.ExpectedTypeInfo) JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder)

Example 72 with LookupElement

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

the class SameSignatureCallParametersProvider method createParametersLookupElement.

private static LookupElement createParametersLookupElement(final PsiMethod takeParametersFrom, PsiElement call, PsiMethod invoked) {
    final PsiParameter[] parameters = takeParametersFrom.getParameterList().getParameters();
    final String lookupString = StringUtil.join(parameters, psiParameter -> psiParameter.getName(), ", ");
    final int w = PlatformIcons.PARAMETER_ICON.getIconWidth();
    LayeredIcon icon = new LayeredIcon(2);
    icon.setIcon(PlatformIcons.PARAMETER_ICON, 0, 2 * w / 5, 0);
    icon.setIcon(PlatformIcons.PARAMETER_ICON, 1);
    LookupElementBuilder element = LookupElementBuilder.create(lookupString).withIcon(icon);
    if (PsiTreeUtil.isAncestor(takeParametersFrom, call, true)) {
        element = element.withInsertHandler(new InsertHandler<LookupElement>() {

            @Override
            public void handleInsert(InsertionContext context, LookupElement item) {
                context.commitDocument();
                for (PsiParameter parameter : CompletionUtil.getOriginalOrSelf(takeParametersFrom).getParameterList().getParameters()) {
                    VariableLookupItem.makeFinalIfNeeded(context, parameter);
                }
            }
        });
    }
    element.putUserData(JavaCompletionUtil.SUPER_METHOD_PARAMETERS, Boolean.TRUE);
    return TailTypeDecorator.withTail(element, ExpectedTypesProvider.getFinalCallParameterTailType(call, invoked.getReturnType(), invoked));
}
Also used : LayeredIcon(com.intellij.ui.LayeredIcon) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) LookupElement(com.intellij.codeInsight.lookup.LookupElement)

Example 73 with LookupElement

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

the class ListTemplateActionTest method doTest.

private void doTest(@NotNull String lookupText) {
    myFixture.configureByFile(getTestName(false) + ".java");
    new ListTemplatesAction().actionPerformedImpl(myFixture.getProject(), myFixture.getEditor());
    LookupElement[] elements = myFixture.getLookupElements();
    assertNotNull(elements);
    for (LookupElement element : elements) {
        if (lookupText.equals(element.getLookupString())) {
            myFixture.getLookup().setCurrentItem(element);
            myFixture.finishLookup(Lookup.NORMAL_SELECT_CHAR);
            myFixture.checkResultByFile(getTestName(false) + "_after.java");
            return;
        }
    }
    //noinspection ConstantConditions
    fail("Lookup element with text '" + lookupText + "' not found:\n" + StringUtil.join(myFixture.getLookupElementStrings(), "\n"));
}
Also used : LookupElement(com.intellij.codeInsight.lookup.LookupElement) ListTemplatesAction(com.intellij.codeInsight.template.impl.actions.ListTemplatesAction)

Example 74 with LookupElement

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

the class TemplatesCompletionTest method doAutoPopupTest.

private void doAutoPopupTest(@NotNull String textToType, @Nullable Class<? extends PostfixTemplate> expectedClass) {
    configureByFile();
    type(textToType);
    LookupImpl lookup = getLookup();
    if (expectedClass != null) {
        assertNotNull(lookup);
        LookupElement item = lookup.getCurrentItem();
        assertNotNull(item);
        assertInstanceOf(item, PostfixTemplateLookupElement.class);
        assertInstanceOf(((PostfixTemplateLookupElement) item).getPostfixTemplate(), expectedClass);
    } else {
        assertNull(lookup);
    }
}
Also used : LookupImpl(com.intellij.codeInsight.lookup.impl.LookupImpl) LookupElement(com.intellij.codeInsight.lookup.LookupElement)

Example 75 with LookupElement

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

the class TemplatesCompletionTest method testRestartCompletionForExactMatch.

public void testRestartCompletionForExactMatch() {
    configureByFile();
    type("not");
    LookupElement currentItem = getLookup().getCurrentItem();
    assertNotNull(currentItem);
    assertInstanceOf(currentItem, PostfixTemplateLookupElement.class);
    assertEquals(".not", currentItem.getLookupString());
    type("null");
    currentItem = getLookup().getCurrentItem();
    assertNotNull(currentItem);
    assertInstanceOf(currentItem, PostfixTemplateLookupElement.class);
    assertEquals(".notnull", currentItem.getLookupString());
}
Also used : LookupElement(com.intellij.codeInsight.lookup.LookupElement)

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