Search in sources :

Example 16 with LookupElementBuilder

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

the class JavaLookupElementBuilder method forClass.

public static LookupElementBuilder forClass(@NotNull PsiClass psiClass, final String lookupString, final boolean withLocation) {
    LookupElementBuilder builder = LookupElementBuilder.create(psiClass, lookupString).withIcon(psiClass.getIcon(Iconable.ICON_FLAG_VISIBILITY));
    String name = psiClass.getName();
    if (StringUtil.isNotEmpty(name)) {
        builder = builder.withLookupString(name);
    }
    if (withLocation) {
        return builder.withTailText(" (" + PsiFormatUtil.getPackageDisplayName(psiClass) + ")", true);
    }
    return builder;
}
Also used : LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder)

Example 17 with LookupElementBuilder

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

the class JavaLookupElementBuilder method forMethod.

public static LookupElementBuilder forMethod(@NotNull PsiMethod method, @NotNull String lookupString, @NotNull final PsiSubstitutor substitutor, @Nullable PsiClass qualifierClass) {
    LookupElementBuilder builder = LookupElementBuilder.create(method, lookupString).withIcon(method.getIcon(Iconable.ICON_FLAG_VISIBILITY)).withPresentableText(method.getName()).withTailText(PsiFormatUtil.formatMethod(method, substitutor, PsiFormatUtilBase.SHOW_PARAMETERS, PsiFormatUtilBase.SHOW_NAME | PsiFormatUtilBase.SHOW_TYPE));
    final PsiType returnType = method.getReturnType();
    if (returnType != null) {
        builder = builder.withTypeText(substitutor.substitute(returnType).getPresentableText());
    }
    builder = setBoldIfInClass(method, qualifierClass, builder);
    return builder;
}
Also used : LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder)

Example 18 with LookupElementBuilder

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

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

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

the class JavaChangeSignatureDialog method createParametersListTable.

@Override
protected ParametersListTable createParametersListTable() {
    return new ParametersListTable() {

        private final EditorTextFieldJBTableRowRenderer myRowRenderer = new EditorTextFieldJBTableRowRenderer(getProject(), JavaChangeSignatureDialog.this.getFileType(), myDisposable) {

            @Override
            protected String getText(JTable table, int row) {
                ParameterTableModelItemBase<ParameterInfoImpl> item = getRowItem(row);
                final String typeText = item.typeCodeFragment.getText();
                final String separator = StringUtil.repeatSymbol(' ', getTypesMaxLength() - typeText.length() + 1);
                String text = typeText + separator + item.parameter.getName();
                final String defaultValue = item.defaultValueCodeFragment.getText();
                String tail = "";
                if (StringUtil.isNotEmpty(defaultValue)) {
                    tail += " default value = " + defaultValue;
                }
                if (item.parameter.isUseAnySingleVariable()) {
                    if (StringUtil.isNotEmpty(defaultValue)) {
                        tail += ";";
                    }
                    tail += " Use any var.";
                }
                if (!StringUtil.isEmpty(tail)) {
                    text += " //" + tail;
                }
                return " " + text;
            }
        };

        @Override
        protected JBTableRowRenderer getRowRenderer(int row) {
            return myRowRenderer;
        }

        @NotNull
        @Override
        protected JBTableRowEditor getRowEditor(final ParameterTableModelItemBase<ParameterInfoImpl> item) {
            return new JBTableRowEditor() {

                private EditorTextField myTypeEditor;

                private EditorTextField myNameEditor;

                private EditorTextField myDefaultValueEditor;

                private JCheckBox myAnyVar;

                @Override
                public void prepareEditor(JTable table, int row) {
                    setLayout(new BorderLayout());
                    final Document document = PsiDocumentManager.getInstance(getProject()).getDocument(item.typeCodeFragment);
                    myTypeEditor = new EditorTextField(document, getProject(), getFileType());
                    myTypeEditor.addDocumentListener(mySignatureUpdater);
                    myTypeEditor.setPreferredWidth(getTable().getWidth() / 2);
                    myTypeEditor.addDocumentListener(new RowEditorChangeListener(0));
                    add(createLabeledPanel("Type:", myTypeEditor), BorderLayout.WEST);
                    myNameEditor = new EditorTextField(item.parameter.getName(), getProject(), getFileType());
                    myNameEditor.addDocumentListener(mySignatureUpdater);
                    myNameEditor.addDocumentListener(new RowEditorChangeListener(1));
                    add(createLabeledPanel("Name:", myNameEditor), BorderLayout.CENTER);
                    new TextFieldCompletionProvider() {

                        @Override
                        protected void addCompletionVariants(@NotNull String text, int offset, @NotNull String prefix, @NotNull CompletionResultSet result) {
                            final PsiCodeFragment fragment = item.typeCodeFragment;
                            if (fragment instanceof PsiTypeCodeFragment) {
                                final PsiType type;
                                try {
                                    type = ((PsiTypeCodeFragment) fragment).getType();
                                } catch (Exception e) {
                                    return;
                                }
                                final SuggestedNameInfo info = JavaCodeStyleManager.getInstance(myProject).suggestVariableName(VariableKind.PARAMETER, null, null, type);
                                for (String completionVariant : info.names) {
                                    final LookupElementBuilder element = LookupElementBuilder.create(completionVariant);
                                    result.addElement(element.withLookupString(completionVariant.toLowerCase(Locale.ENGLISH)));
                                }
                            }
                        }
                    }.apply(myNameEditor, item.parameter.getName());
                    if (!item.isEllipsisType() && item.parameter.getOldIndex() == -1) {
                        final JPanel additionalPanel = new JPanel(new BorderLayout());
                        final Document doc = PsiDocumentManager.getInstance(getProject()).getDocument(item.defaultValueCodeFragment);
                        myDefaultValueEditor = new EditorTextField(doc, getProject(), getFileType());
                        ((PsiExpressionCodeFragment) item.defaultValueCodeFragment).setExpectedType(getRowType(item));
                        myDefaultValueEditor.setPreferredWidth(getTable().getWidth() / 2);
                        myDefaultValueEditor.addDocumentListener(new RowEditorChangeListener(2));
                        additionalPanel.add(createLabeledPanel("Default value:", myDefaultValueEditor), BorderLayout.WEST);
                        if (!isGenerateDelegate()) {
                            myAnyVar = new JCheckBox("&Use Any Var");
                            UIUtil.applyStyle(UIUtil.ComponentStyle.SMALL, myAnyVar);
                            DialogUtil.registerMnemonic(myAnyVar, '&');
                            myAnyVar.addActionListener(new ActionListener() {

                                @Override
                                public void actionPerformed(ActionEvent e) {
                                    item.parameter.setUseAnySingleVariable(myAnyVar.isSelected());
                                }
                            });
                            final JPanel anyVarPanel = new JPanel(new BorderLayout());
                            anyVarPanel.add(myAnyVar, BorderLayout.SOUTH);
                            UIUtil.addInsets(anyVarPanel, JBUI.insetsBottom(8));
                            additionalPanel.add(anyVarPanel, BorderLayout.CENTER);
                        //additionalPanel.setPreferredSize(new Dimension(t.getWidth() / 3, -1));
                        }
                        add(additionalPanel, BorderLayout.SOUTH);
                    }
                }

                @Override
                public JBTableRow getValue() {
                    return new JBTableRow() {

                        @Override
                        public Object getValueAt(int column) {
                            switch(column) {
                                case 0:
                                    return item.typeCodeFragment;
                                case 1:
                                    return myNameEditor.getText().trim();
                                case 2:
                                    return item.defaultValueCodeFragment;
                                case 3:
                                    return myAnyVar != null && myAnyVar.isSelected();
                            }
                            return null;
                        }
                    };
                }

                @Override
                public JComponent getPreferredFocusedComponent() {
                    final MouseEvent me = getMouseEvent();
                    if (me == null) {
                        return myTypeEditor.getFocusTarget();
                    }
                    final double x = me.getPoint().getX();
                    return x <= getTypesColumnWidth() ? myTypeEditor.getFocusTarget() : myDefaultValueEditor == null || x <= getNamesColumnWidth() ? myNameEditor.getFocusTarget() : myDefaultValueEditor.getFocusTarget();
                }

                @Override
                public JComponent[] getFocusableComponents() {
                    final List<JComponent> focusable = new ArrayList<>();
                    focusable.add(myTypeEditor.getFocusTarget());
                    focusable.add(myNameEditor.getFocusTarget());
                    if (myDefaultValueEditor != null) {
                        focusable.add(myDefaultValueEditor.getFocusTarget());
                    }
                    if (myAnyVar != null) {
                        focusable.add(myAnyVar);
                    }
                    return focusable.toArray(new JComponent[focusable.size()]);
                }
            };
        }

        @Override
        protected boolean isRowEmpty(int row) {
            ParameterInfoImpl parameter = getRowItem(row).parameter;
            return StringUtil.isEmpty(parameter.getName()) && StringUtil.isEmpty(parameter.getTypeText());
        }
    };
}
Also used : AnActionEvent(com.intellij.openapi.actionSystem.AnActionEvent) ActionEvent(java.awt.event.ActionEvent) ArrayList(java.util.ArrayList) Document(com.intellij.openapi.editor.Document) EditorTextFieldJBTableRowRenderer(com.intellij.util.ui.table.EditorTextFieldJBTableRowRenderer) SuggestedNameInfo(com.intellij.psi.codeStyle.SuggestedNameInfo) JBTableRow(com.intellij.util.ui.table.JBTableRow) MouseEvent(java.awt.event.MouseEvent) CompletionResultSet(com.intellij.codeInsight.completion.CompletionResultSet) JBTableRowEditor(com.intellij.util.ui.table.JBTableRowEditor) ActionListener(java.awt.event.ActionListener) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder)

Aggregations

LookupElementBuilder (com.intellij.codeInsight.lookup.LookupElementBuilder)57 LookupElement (com.intellij.codeInsight.lookup.LookupElement)20 NotNull (org.jetbrains.annotations.NotNull)16 PsiElement (com.intellij.psi.PsiElement)13 Nullable (org.jetbrains.annotations.Nullable)9 PsiFile (com.intellij.psi.PsiFile)7 Document (com.intellij.openapi.editor.Document)5 ArrayList (java.util.ArrayList)5 PlainPrefixMatcher (com.intellij.codeInsight.completion.PlainPrefixMatcher)3 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 InsertionContext (com.intellij.codeInsight.completion.InsertionContext)2 ParenthesesInsertHandler (com.intellij.codeInsight.completion.util.ParenthesesInsertHandler)2 AutoCompletionPolicy (com.intellij.codeInsight.lookup.AutoCompletionPolicy)2