Search in sources :

Example 6 with SuggestedNameInfo

use of com.intellij.psi.codeStyle.SuggestedNameInfo in project intellij-community by JetBrains.

the class RenameDialog method getSuggestedNames.

public String[] getSuggestedNames() {
    final LinkedHashSet<String> result = new LinkedHashSet<>();
    final String initialName = VariableInplaceRenameHandler.getInitialName();
    if (initialName != null) {
        result.add(initialName);
    }
    result.add(UsageViewUtil.getShortName(myPsiElement));
    final NameSuggestionProvider[] providers = Extensions.getExtensions(NameSuggestionProvider.EP_NAME);
    for (NameSuggestionProvider provider : providers) {
        SuggestedNameInfo info = provider.getSuggestedNames(myPsiElement, myNameSuggestionContext, result);
        if (info != null) {
            mySuggestedNameInfo = info;
            if (provider instanceof PreferrableNameSuggestionProvider && !((PreferrableNameSuggestionProvider) provider).shouldCheckOthers())
                break;
        }
    }
    return ArrayUtil.toStringArray(result);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SuggestedNameInfo(com.intellij.psi.codeStyle.SuggestedNameInfo)

Example 7 with SuggestedNameInfo

use of com.intellij.psi.codeStyle.SuggestedNameInfo in project intellij-community by JetBrains.

the class JavaMemberNameCompletionContributor method completeFieldName.

private static void completeFieldName(Set<LookupElement> set, PsiField var, final PrefixMatcher matcher, boolean includeOverlapped) {
    FeatureUsageTracker.getInstance().triggerFeatureUsed("editing.completion.variable.name");
    Project project = var.getProject();
    JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project);
    final VariableKind variableKind = JavaCodeStyleManager.getInstance(project).getVariableKind(var);
    final String prefix = matcher.getPrefix();
    if (PsiType.VOID.equals(var.getType()) || psiField().inClass(psiClass().isInterface().andNot(psiClass().isAnnotationType())).accepts(var)) {
        completeVariableNameForRefactoring(project, set, matcher, var.getType(), variableKind, includeOverlapped, true);
        return;
    }
    SuggestedNameInfo suggestedNameInfo = codeStyleManager.suggestVariableName(variableKind, null, null, var.getType());
    final String[] suggestedNames = suggestedNameInfo.names;
    addLookupItems(set, suggestedNameInfo, matcher, project, suggestedNames);
    if (!hasStartMatches(set, matcher) && includeOverlapped) {
        // use suggested names as suffixes
        final String requiredSuffix = codeStyleManager.getSuffixByVariableKind(variableKind);
        if (variableKind != VariableKind.STATIC_FINAL_FIELD) {
            for (int i = 0; i < suggestedNames.length; i++) suggestedNames[i] = codeStyleManager.variableNameToPropertyName(suggestedNames[i], variableKind);
        }
        addLookupItems(set, null, matcher, project, getOverlappedNameVersions(prefix, suggestedNames, requiredSuffix));
    }
    addLookupItems(set, suggestedNameInfo, matcher, project, getUnresolvedReferences(var.getParent(), false));
    PsiExpression initializer = var.getInitializer();
    PsiClass containingClass = var.getContainingClass();
    if (initializer != null && containingClass != null) {
        SuggestedNameInfo initializerSuggestions = InplaceIntroduceFieldPopup.suggestFieldName(var.getType(), null, initializer, var.hasModifierProperty(PsiModifier.STATIC), containingClass);
        addLookupItems(set, initializerSuggestions, matcher, project, initializerSuggestions.names);
    }
}
Also used : Project(com.intellij.openapi.project.Project) JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) SuggestedNameInfo(com.intellij.psi.codeStyle.SuggestedNameInfo) VariableKind(com.intellij.psi.codeStyle.VariableKind)

Example 8 with SuggestedNameInfo

use of com.intellij.psi.codeStyle.SuggestedNameInfo in project intellij-community by JetBrains.

the class JavaMemberNameCompletionContributor method completeVariableNameForRefactoring.

public static void completeVariableNameForRefactoring(Project project, Set<LookupElement> set, PrefixMatcher matcher, PsiType varType, VariableKind varKind, final boolean includeOverlapped, final boolean methodPrefix) {
    JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project);
    SuggestedNameInfo suggestedNameInfo = codeStyleManager.suggestVariableName(varKind, null, null, varType);
    final String[] strings = completeVariableNameForRefactoring(codeStyleManager, matcher, varType, varKind, suggestedNameInfo, includeOverlapped, methodPrefix);
    addLookupItems(set, suggestedNameInfo, matcher, project, strings);
}
Also used : JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) SuggestedNameInfo(com.intellij.psi.codeStyle.SuggestedNameInfo)

Example 9 with SuggestedNameInfo

use of com.intellij.psi.codeStyle.SuggestedNameInfo 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 10 with SuggestedNameInfo

use of com.intellij.psi.codeStyle.SuggestedNameInfo in project intellij-community by JetBrains.

the class ClassMappingNameConverter method getVariants.

@NotNull
@Override
public Collection<String> getVariants(ConvertContext context) {
    DomElement parent = context.getInvocationElement().getParent();
    assert parent != null;
    List<DomElement> children = DomUtil.getDefinedChildren(parent, true, true);
    DomElement classElement = ContainerUtil.find(children, domElement -> domElement.getAnnotation(MappingClass.class) != null);
    if (classElement == null)
        return Collections.emptyList();
    Object value = ((GenericDomValue) classElement).getValue();
    if (value == null)
        return Collections.emptyList();
    PsiType type;
    if (value instanceof PsiType) {
        type = (PsiType) value;
    } else if (value instanceof PsiClass) {
        type = PsiTypesUtil.getClassType((PsiClass) value);
    } else {
        LOG.error("wrong type: " + value.getClass());
        return Collections.emptyList();
    }
    JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(context.getProject());
    SuggestedNameInfo info = codeStyleManager.suggestVariableName(VariableKind.LOCAL_VARIABLE, null, null, type);
    return Arrays.asList(info.names);
}
Also used : JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) PsiClass(com.intellij.psi.PsiClass) SuggestedNameInfo(com.intellij.psi.codeStyle.SuggestedNameInfo) PsiType(com.intellij.psi.PsiType) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

SuggestedNameInfo (com.intellij.psi.codeStyle.SuggestedNameInfo)40 JavaCodeStyleManager (com.intellij.psi.codeStyle.JavaCodeStyleManager)22 VariableKind (com.intellij.psi.codeStyle.VariableKind)11 Nullable (org.jetbrains.annotations.Nullable)7 Project (com.intellij.openapi.project.Project)6 NotNull (org.jetbrains.annotations.NotNull)6 ArrayList (java.util.ArrayList)4 LinkedHashSet (java.util.LinkedHashSet)4 NonNls (org.jetbrains.annotations.NonNls)4 LookupElement (com.intellij.codeInsight.lookup.LookupElement)3 Editor (com.intellij.openapi.editor.Editor)3 PsiType (com.intellij.psi.PsiType)3 PreferrableNameSuggestionProvider (com.intellij.refactoring.rename.PreferrableNameSuggestionProvider)3 IncorrectOperationException (com.intellij.util.IncorrectOperationException)3 List (java.util.List)3 ApplicationManager (com.intellij.openapi.application.ApplicationManager)2 Logger (com.intellij.openapi.diagnostic.Logger)2 com.intellij.psi (com.intellij.psi)2 THashSet (gnu.trove.THashSet)2 JavaPsiEquivalenceUtil (com.intellij.codeInsight.JavaPsiEquivalenceUtil)1