Search in sources :

Example 31 with SuggestedNameInfo

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

the class IntroduceFieldDialog method createGenerator.

static NameSuggestionsGenerator createGenerator(final boolean willBeDeclaredStatic, final PsiLocalVariable localVariable, final PsiExpression initializerExpression, final boolean isInvokedOnDeclaration, @Nullable final String enteredName, final PsiClass parentClass, final Project project) {
    return new NameSuggestionsGenerator() {

        private final JavaCodeStyleManager myCodeStyleManager = JavaCodeStyleManager.getInstance(project);

        public SuggestedNameInfo getSuggestedNameInfo(PsiType type) {
            VariableKind variableKind = willBeDeclaredStatic ? VariableKind.STATIC_FIELD : VariableKind.FIELD;
            String propertyName = null;
            if (isInvokedOnDeclaration) {
                propertyName = myCodeStyleManager.variableNameToPropertyName(localVariable.getName(), VariableKind.LOCAL_VARIABLE);
            }
            final SuggestedNameInfo nameInfo = myCodeStyleManager.suggestVariableName(variableKind, propertyName, initializerExpression, type);
            if (initializerExpression != null) {
                String[] names = nameInfo.names;
                for (int i = 0, namesLength = names.length; i < namesLength; i++) {
                    String name = names[i];
                    if (parentClass.findFieldByName(name, false) != null) {
                        names[i] = myCodeStyleManager.suggestUniqueVariableName(name, initializerExpression, true);
                    }
                }
            }
            final String[] strings = AbstractJavaInplaceIntroducer.appendUnresolvedExprName(JavaCompletionUtil.completeVariableNameForRefactoring(myCodeStyleManager, type, VariableKind.LOCAL_VARIABLE, nameInfo), initializerExpression);
            return new SuggestedNameInfo.Delegate(enteredName != null ? ArrayUtil.mergeArrays(new String[] { enteredName }, strings) : strings, nameInfo);
        }
    };
}
Also used : JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) SuggestedNameInfo(com.intellij.psi.codeStyle.SuggestedNameInfo) VariableKind(com.intellij.psi.codeStyle.VariableKind)

Example 32 with SuggestedNameInfo

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

the class IntroduceVariableBase method getSettings.

public IntroduceVariableSettings getSettings(Project project, Editor editor, PsiExpression expr, PsiExpression[] occurrences, final TypeSelectorManagerImpl typeSelectorManager, boolean declareFinalIfAll, boolean anyAssignmentLHS, final InputValidator validator, PsiElement anchor, final JavaReplaceChoice replaceChoice) {
    final boolean replaceAll = replaceChoice.isMultiple();
    final SuggestedNameInfo suggestedName = getSuggestedName(typeSelectorManager.getDefaultType(), expr, anchor);
    final String variableName = suggestedName.names.length > 0 ? suggestedName.names[0] : "";
    final boolean declareFinal = replaceAll && declareFinalIfAll || !anyAssignmentLHS && createFinals(project);
    final boolean replaceWrite = anyAssignmentLHS && replaceChoice.isAll();
    return new IntroduceVariableSettings() {

        @Override
        public String getEnteredName() {
            return variableName;
        }

        @Override
        public boolean isReplaceAllOccurrences() {
            return replaceAll;
        }

        @Override
        public boolean isDeclareFinal() {
            return declareFinal;
        }

        @Override
        public boolean isReplaceLValues() {
            return replaceWrite;
        }

        @Override
        public PsiType getSelectedType() {
            final PsiType selectedType = typeSelectorManager.getTypeSelector().getSelectedType();
            return selectedType != null ? selectedType : typeSelectorManager.getDefaultType();
        }

        @Override
        public boolean isOK() {
            return true;
        }
    };
}
Also used : SuggestedNameInfo(com.intellij.psi.codeStyle.SuggestedNameInfo)

Example 33 with SuggestedNameInfo

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

the class ParameterNameExpression method calculateLookupItems.

@Override
public LookupElement[] calculateLookupItems(ExpressionContext context) {
    SuggestedNameInfo info = getNameInfo(context);
    if (info == null)
        return null;
    LookupElement[] result = new LookupElement[info.names.length];
    int i = 0;
    for (String name : info.names) {
        result[i++] = LookupElementBuilder.create(name);
    }
    return result;
}
Also used : SuggestedNameInfo(com.intellij.psi.codeStyle.SuggestedNameInfo) LookupElement(com.intellij.codeInsight.lookup.LookupElement)

Example 34 with SuggestedNameInfo

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

the class GrAliasImportIntention method getSuggestedNames.

public static LinkedHashSet<String> getSuggestedNames(PsiElement psiElement, final PsiElement nameSuggestionContext) {
    final LinkedHashSet<String> result = new LinkedHashSet<>();
    result.add(UsageViewUtil.getShortName(psiElement));
    final NameSuggestionProvider[] providers = Extensions.getExtensions(NameSuggestionProvider.EP_NAME);
    for (NameSuggestionProvider provider : providers) {
        SuggestedNameInfo info = provider.getSuggestedNames(psiElement, nameSuggestionContext, result);
        if (info != null) {
            if (provider instanceof PreferrableNameSuggestionProvider && !((PreferrableNameSuggestionProvider) provider).shouldCheckOthers()) {
                break;
            }
        }
    }
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) PreferrableNameSuggestionProvider(com.intellij.refactoring.rename.PreferrableNameSuggestionProvider) NameSuggestionProvider(com.intellij.refactoring.rename.NameSuggestionProvider) SuggestedNameInfo(com.intellij.psi.codeStyle.SuggestedNameInfo) PreferrableNameSuggestionProvider(com.intellij.refactoring.rename.PreferrableNameSuggestionProvider)

Example 35 with SuggestedNameInfo

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

the class GrMainCompletionProvider method suggestVariableNames.

private static void suggestVariableNames(PsiElement context, CompletionResultSet result) {
    final PsiElement parent = context.getParent();
    if (GroovyCompletionUtil.isWildcardCompletion(context))
        return;
    if (parent instanceof GrVariable) {
        final GrVariable variable = (GrVariable) parent;
        if (context.equals(variable.getNameIdentifierGroovy())) {
            final PsiType type = variable.getTypeGroovy();
            if (type != null) {
                final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(context.getProject());
                VariableKind kind = variable instanceof GrParameter ? VariableKind.PARAMETER : variable instanceof GrField ? VariableKind.FIELD : VariableKind.LOCAL_VARIABLE;
                SuggestedNameInfo suggestedNameInfo = codeStyleManager.suggestVariableName(kind, null, null, type);
                String[] names = suggestedNameInfo.names;
                if (names.length > 0) {
                    String name = names[0];
                    String newName = InlineMethodConflictSolver.suggestNewName(name, null, parent);
                    if (!name.equals(newName)) {
                        result.addElement(LookupElementBuilder.create(newName));
                        return;
                    }
                }
                for (String name : names) {
                    result.addElement(LookupElementBuilder.create(name));
                }
            }
            GrExpression initializer = variable.getInitializerGroovy();
            if (initializer != null) {
                for (String name : GroovyNameSuggestionUtil.suggestVariableNames(initializer, new DefaultGroovyVariableNameValidator(variable), variable.hasModifierProperty(PsiModifier.STATIC))) {
                    result.addElement(LookupElementBuilder.create(name));
                }
            }
        }
    }
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) DefaultGroovyVariableNameValidator(org.jetbrains.plugins.groovy.refactoring.DefaultGroovyVariableNameValidator) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) SuggestedNameInfo(com.intellij.psi.codeStyle.SuggestedNameInfo) VariableKind(com.intellij.psi.codeStyle.VariableKind)

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