Search in sources :

Example 1 with CompletionResultSet

use of com.intellij.codeInsight.completion.CompletionResultSet in project intellij-community by JetBrains.

the class MvcRunTargetDialog method createUIComponents.

private void createUIComponents() {
    myTargetField = new ComboBox(MvcRunTargetHistoryService.getInstance().getHistory());
    myTargetField.setLightWeightPopupEnabled(false);
    EditorComboBoxEditor editor = new StringComboboxEditor(myModule.getProject(), PlainTextFileType.INSTANCE, myTargetField);
    myTargetField.setRenderer(new EditorComboBoxRenderer(editor));
    myTargetField.setEditable(true);
    myTargetField.setEditor(editor);
    EditorTextField editorTextField = editor.getEditorComponent();
    myFakePanel = new JPanel(new BorderLayout());
    myFakePanel.add(myTargetField, BorderLayout.CENTER);
    TextFieldCompletionProvider vmOptionCompletionProvider = new TextFieldCompletionProviderDumbAware() {

        @NotNull
        @Override
        protected String getPrefix(@NotNull String currentTextPrefix) {
            return MvcRunTargetDialog.getPrefix(currentTextPrefix);
        }

        @Override
        protected void addCompletionVariants(@NotNull String text, int offset, @NotNull String prefix, @NotNull CompletionResultSet result) {
            if (prefix.endsWith("-D")) {
                result.addAllElements(MvcTargetDialogCompletionUtils.getSystemPropertiesVariants());
            }
        }
    };
    myVmOptionsField = vmOptionCompletionProvider.createEditor(myModule.getProject());
    new TextFieldCompletionProviderDumbAware() {

        @NotNull
        @Override
        protected String getPrefix(@NotNull String currentTextPrefix) {
            return MvcRunTargetDialog.getPrefix(currentTextPrefix);
        }

        @Override
        protected void addCompletionVariants(@NotNull String text, int offset, @NotNull String prefix, @NotNull CompletionResultSet result) {
            for (LookupElement variant : MvcTargetDialogCompletionUtils.collectVariants(myModule, text, offset, prefix)) {
                result.addElement(variant);
            }
        }
    }.apply(editorTextField);
    editorTextField.getDocument().addDocumentListener(new DocumentAdapter() {

        @Override
        public void documentChanged(DocumentEvent e) {
            setOKActionEnabled(!StringUtil.isEmptyOrSpaces(e.getDocument().getText()));
        }
    });
    setOKActionEnabled(false);
}
Also used : EditorComboBoxEditor(com.intellij.ui.EditorComboBoxEditor) StringComboboxEditor(com.intellij.ui.StringComboboxEditor) ComboBox(com.intellij.openapi.ui.ComboBox) ModulesComboBox(com.intellij.application.options.ModulesComboBox) CompletionResultSet(com.intellij.codeInsight.completion.CompletionResultSet) DocumentAdapter(com.intellij.openapi.editor.event.DocumentAdapter) LookupElement(com.intellij.codeInsight.lookup.LookupElement) DocumentEvent(com.intellij.openapi.editor.event.DocumentEvent) NotNull(org.jetbrains.annotations.NotNull) TextFieldCompletionProviderDumbAware(com.intellij.util.TextFieldCompletionProviderDumbAware) EditorTextField(com.intellij.ui.EditorTextField) TextFieldCompletionProvider(com.intellij.util.TextFieldCompletionProvider) EditorComboBoxRenderer(com.intellij.ui.EditorComboBoxRenderer)

Example 2 with CompletionResultSet

use of com.intellij.codeInsight.completion.CompletionResultSet 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)

Example 3 with CompletionResultSet

use of com.intellij.codeInsight.completion.CompletionResultSet in project intellij-community by JetBrains.

the class ChangeListCompletionContributor method fillCompletionVariants.

@Override
public void fillCompletionVariants(@NotNull final CompletionParameters parameters, @NotNull final CompletionResultSet result) {
    final PsiFile file = parameters.getOriginalFile();
    final Document document = PsiDocumentManager.getInstance(file.getProject()).getCachedDocument(file);
    if (document == null)
        return;
    ComboBox comboBox = document.getUserData(COMBO_BOX_KEY);
    if (comboBox == null)
        return;
    final CompletionResultSet resultSet = result.withPrefixMatcher(new PlainPrefixMatcher(document.getText()));
    for (int i = 0; i < comboBox.getItemCount(); i++) {
        resultSet.addElement(LookupElementBuilder.create(comboBox.getItemAt(i)));
    }
}
Also used : ComboBox(com.intellij.openapi.ui.ComboBox) PlainPrefixMatcher(com.intellij.codeInsight.completion.PlainPrefixMatcher) CompletionResultSet(com.intellij.codeInsight.completion.CompletionResultSet) PsiFile(com.intellij.psi.PsiFile) Document(com.intellij.openapi.editor.Document)

Example 4 with CompletionResultSet

use of com.intellij.codeInsight.completion.CompletionResultSet in project intellij-community by JetBrains.

the class ExternalProjectPathField method createTextField.

@NotNull
private static EditorTextField createTextField(@NotNull final Project project, @NotNull final ProjectSystemId externalSystemId) {
    ExternalSystemManager<?, ?, ?, ?, ?> manager = ExternalSystemApiUtil.getManager(externalSystemId);
    assert manager != null;
    final AbstractExternalSystemLocalSettings settings = manager.getLocalSettingsProvider().fun(project);
    final ExternalSystemUiAware uiAware = ExternalSystemUiUtil.getUiAware(externalSystemId);
    TextFieldCompletionProvider provider = new TextFieldCompletionProviderDumbAware() {

        @Override
        protected void addCompletionVariants(@NotNull String text, int offset, @NotNull String prefix, @NotNull CompletionResultSet result) {
            for (Map.Entry<ExternalProjectPojo, Collection<ExternalProjectPojo>> entry : settings.getAvailableProjects().entrySet()) {
                String rootProjectPath = entry.getKey().getPath();
                String rootProjectName = uiAware.getProjectRepresentationName(rootProjectPath, null);
                ExternalProjectPathLookupElement rootProjectElement = new ExternalProjectPathLookupElement(rootProjectName, rootProjectPath);
                result.addElement(rootProjectElement);
                for (ExternalProjectPojo subProject : entry.getValue()) {
                    String p = subProject.getPath();
                    if (rootProjectPath.equals(p)) {
                        continue;
                    }
                    String subProjectName = uiAware.getProjectRepresentationName(p, rootProjectPath);
                    ExternalProjectPathLookupElement subProjectElement = new ExternalProjectPathLookupElement(subProjectName, p);
                    result.addElement(subProjectElement);
                }
            }
            result.stopHere();
        }
    };
    EditorTextField result = provider.createEditor(project, false, editor -> {
        collapseIfPossible(editor, externalSystemId, project);
        editor.getSettings().setShowIntentionBulb(false);
    });
    result.setBorder(UIUtil.getTextFieldBorder());
    result.setOneLineMode(true);
    result.setOpaque(true);
    result.setBackground(UIUtil.getTextFieldBackground());
    return result;
}
Also used : AbstractExternalSystemLocalSettings(com.intellij.openapi.externalSystem.settings.AbstractExternalSystemLocalSettings) CompletionResultSet(com.intellij.codeInsight.completion.CompletionResultSet) NotNull(org.jetbrains.annotations.NotNull) ExternalSystemUiAware(com.intellij.openapi.externalSystem.ExternalSystemUiAware) TextFieldCompletionProviderDumbAware(com.intellij.util.TextFieldCompletionProviderDumbAware) EditorTextField(com.intellij.ui.EditorTextField) TextFieldCompletionProvider(com.intellij.util.TextFieldCompletionProvider) Collection(java.util.Collection) Map(java.util.Map) ExternalProjectPojo(com.intellij.openapi.externalSystem.model.project.ExternalProjectPojo) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with CompletionResultSet

use of com.intellij.codeInsight.completion.CompletionResultSet in project intellij-community by JetBrains.

the class TextCompletionContributor method fillCompletionVariants.

@Override
public void fillCompletionVariants(@NotNull CompletionParameters parameters, @NotNull CompletionResultSet result) {
    PsiFile file = parameters.getOriginalFile();
    TextCompletionProvider provider = TextCompletionUtil.getProvider(file);
    if (provider == null)
        return;
    if (parameters.getInvocationCount() == 0 && !Boolean.TRUE.equals(file.getUserData(TextCompletionUtil.AUTO_POPUP_KEY))) {
        return;
    }
    String advertisement = provider.getAdvertisement();
    if (advertisement != null) {
        result.addLookupAdvertisement(advertisement);
    }
    String text = file.getText();
    int offset = Math.min(text.length(), parameters.getOffset());
    String prefix = provider.getPrefix(text, offset);
    if (prefix == null)
        return;
    CompletionResultSet activeResult = provider.applyPrefixMatcher(result, prefix);
    provider.fillCompletionVariants(parameters, prefix, activeResult);
}
Also used : CompletionResultSet(com.intellij.codeInsight.completion.CompletionResultSet) PsiFile(com.intellij.psi.PsiFile)

Aggregations

CompletionResultSet (com.intellij.codeInsight.completion.CompletionResultSet)7 PsiFile (com.intellij.psi.PsiFile)4 LookupElementBuilder (com.intellij.codeInsight.lookup.LookupElementBuilder)3 Document (com.intellij.openapi.editor.Document)3 NotNull (org.jetbrains.annotations.NotNull)3 ComboBox (com.intellij.openapi.ui.ComboBox)2 EditorTextField (com.intellij.ui.EditorTextField)2 TextFieldCompletionProvider (com.intellij.util.TextFieldCompletionProvider)2 TextFieldCompletionProviderDumbAware (com.intellij.util.TextFieldCompletionProviderDumbAware)2 ModulesComboBox (com.intellij.application.options.ModulesComboBox)1 CompletionParameters (com.intellij.codeInsight.completion.CompletionParameters)1 CompletionProvider (com.intellij.codeInsight.completion.CompletionProvider)1 PlainPrefixMatcher (com.intellij.codeInsight.completion.PlainPrefixMatcher)1 CamelHumpMatcher (com.intellij.codeInsight.completion.impl.CamelHumpMatcher)1 LookupElement (com.intellij.codeInsight.lookup.LookupElement)1 CfmlUtil (com.intellij.coldFusion.model.CfmlUtil)1 CfmlFileViewProvider (com.intellij.coldFusion.model.files.CfmlFileViewProvider)1 CfmlImport (com.intellij.coldFusion.model.psi.CfmlImport)1 CfmlIndex (com.intellij.coldFusion.model.psi.stubs.CfmlIndex)1 AnActionEvent (com.intellij.openapi.actionSystem.AnActionEvent)1