Search in sources :

Example 16 with VariableKind

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

the class ChangeMethodSignatureFromUsageFix method suggestUniqueParameterName.

static String suggestUniqueParameterName(JavaCodeStyleManager codeStyleManager, PsiExpression expression, PsiType exprType, Set<String> existingNames) {
    SuggestedNameInfo nameInfo = codeStyleManager.suggestVariableName(VariableKind.PARAMETER, null, expression, exprType);
    @NonNls String[] names = nameInfo.names;
    if (expression instanceof PsiReferenceExpression) {
        final PsiElement resolve = ((PsiReferenceExpression) expression).resolve();
        if (resolve instanceof PsiVariable) {
            final VariableKind variableKind = codeStyleManager.getVariableKind((PsiVariable) resolve);
            final String propertyName = codeStyleManager.variableNameToPropertyName(((PsiVariable) resolve).getName(), variableKind);
            final String parameterName = codeStyleManager.propertyNameToVariableName(propertyName, VariableKind.PARAMETER);
            names = ArrayUtil.mergeArrays(new String[] { parameterName }, names);
        }
    }
    if (names.length == 0)
        names = new String[] { "param" };
    int suffix = 0;
    while (true) {
        for (String name : names) {
            String suggested = name + (suffix == 0 ? "" : String.valueOf(suffix));
            if (existingNames.add(suggested)) {
                return suggested;
            }
        }
        suffix++;
    }
}
Also used : NonNls(org.jetbrains.annotations.NonNls) SuggestedNameInfo(com.intellij.psi.codeStyle.SuggestedNameInfo) VariableKind(com.intellij.psi.codeStyle.VariableKind)

Example 17 with VariableKind

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

the class CreatePropertyFromUsageFix method getVariableName.

private static String getVariableName(PsiMethodCallExpression methodCall, boolean isStatic) {
    JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(methodCall.getProject());
    String methodName = methodCall.getMethodExpression().getReferenceName();
    String propertyName = PropertyUtil.getPropertyName(methodName);
    if (propertyName != null && !propertyName.isEmpty()) {
        VariableKind kind = isStatic ? VariableKind.STATIC_FIELD : VariableKind.FIELD;
        return codeStyleManager.propertyNameToVariableName(propertyName, kind);
    }
    return null;
}
Also used : JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) VariableKind(com.intellij.psi.codeStyle.VariableKind)

Example 18 with VariableKind

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

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

the class InputVariables method getParameterName.

private String getParameterName(PsiVariable var) {
    String name = var.getName();
    if (!(var instanceof PsiParameter)) {
        JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(myProject);
        VariableKind kind = codeStyleManager.getVariableKind(var);
        name = codeStyleManager.variableNameToPropertyName(name, kind);
        name = codeStyleManager.propertyNameToVariableName(name, VariableKind.PARAMETER);
    }
    return name;
}
Also used : JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) VariableKind(com.intellij.psi.codeStyle.VariableKind)

Example 20 with VariableKind

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

the class JavaStatisticsManager method getAllVariableNamesUsed.

public static String[] getAllVariableNamesUsed(VariableKind variableKind, String propertyName, PsiType type) {
    StatisticsInfo[] keys2 = StatisticsManager.getInstance().getAllValues(getVariableNameUseKey1(propertyName, type));
    ArrayList<String> list = new ArrayList<>();
    for (StatisticsInfo key2 : keys2) {
        VariableKind variableKind1 = getVariableKindFromKey2(key2.getValue());
        if (variableKind1 != variableKind)
            continue;
        String name = getVariableNameFromKey2(key2.getValue());
        list.add(name);
    }
    return ArrayUtil.toStringArray(list);
}
Also used : ArrayList(java.util.ArrayList) VariableKind(com.intellij.psi.codeStyle.VariableKind)

Aggregations

VariableKind (com.intellij.psi.codeStyle.VariableKind)21 JavaCodeStyleManager (com.intellij.psi.codeStyle.JavaCodeStyleManager)18 SuggestedNameInfo (com.intellij.psi.codeStyle.SuggestedNameInfo)9 Nullable (org.jetbrains.annotations.Nullable)7 Project (com.intellij.openapi.project.Project)4 IncorrectOperationException (com.intellij.util.IncorrectOperationException)4 NonNls (org.jetbrains.annotations.NonNls)3 ArrayList (java.util.ArrayList)2 GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)2 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)2 IntentionAction (com.intellij.codeInsight.intention.IntentionAction)1 EnumMap (java.util.EnumMap)1 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)1 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)1 GrCodeBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)1 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)1 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)1 DefaultGroovyVariableNameValidator (org.jetbrains.plugins.groovy.refactoring.DefaultGroovyVariableNameValidator)1