Search in sources :

Example 51 with JavaCodeStyleManager

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

the class RenameJavaVariableProcessor method prepareFieldRenaming.

private static void prepareFieldRenaming(PsiField field, String newName, final Map<PsiElement, String> allRenames) {
    // search for getters/setters
    PsiClass aClass = field.getContainingClass();
    Project project = field.getProject();
    final JavaCodeStyleManager manager = JavaCodeStyleManager.getInstance(project);
    final String propertyName = PropertyUtil.suggestPropertyName(field, field.getName());
    final String newPropertyName = PropertyUtil.suggestPropertyName(field, newName);
    boolean isStatic = field.hasModifierProperty(PsiModifier.STATIC);
    PsiMethod[] getters = GetterSetterPrototypeProvider.findGetters(aClass, propertyName, isStatic);
    PsiMethod setter = PropertyUtil.findPropertySetter(aClass, propertyName, isStatic, false);
    boolean shouldRenameSetterParameter = false;
    if (setter != null) {
        shouldRenameSetterParameter = shouldRenameSetterParameter(manager, propertyName, setter);
    }
    if (getters != null) {
        List<PsiMethod> validGetters = new ArrayList<>();
        for (PsiMethod getter : getters) {
            String newGetterName = GetterSetterPrototypeProvider.suggestNewGetterName(propertyName, newPropertyName, getter);
            String getterId = null;
            if (newGetterName == null) {
                getterId = getter.getName();
                newGetterName = PropertyUtil.suggestGetterName(newPropertyName, field.getType(), getterId);
            }
            if (newGetterName.equals(getterId)) {
                continue;
            } else {
                boolean valid = true;
                for (PsiMethod method : getter.findDeepestSuperMethods()) {
                    if (method instanceof PsiCompiledElement) {
                        valid = false;
                        break;
                    }
                }
                if (!valid)
                    continue;
            }
            validGetters.add(getter);
        }
        getters = validGetters.isEmpty() ? null : validGetters.toArray(new PsiMethod[validGetters.size()]);
    }
    String newSetterName = "";
    if (setter != null) {
        newSetterName = PropertyUtil.suggestSetterName(newPropertyName);
        final String newSetterParameterName = manager.propertyNameToVariableName(newPropertyName, VariableKind.PARAMETER);
        if (newSetterName.equals(setter.getName())) {
            setter = null;
            newSetterName = null;
            shouldRenameSetterParameter = false;
        } else if (newSetterParameterName.equals(setter.getParameterList().getParameters()[0].getName())) {
            shouldRenameSetterParameter = false;
        } else {
            for (PsiMethod method : setter.findDeepestSuperMethods()) {
                if (method instanceof PsiCompiledElement) {
                    setter = null;
                    shouldRenameSetterParameter = false;
                    break;
                }
            }
        }
    }
    if ((getters != null || setter != null) && askToRenameAccesors(getters != null ? getters[0] : null, setter, newName, project)) {
        getters = null;
        setter = null;
        shouldRenameSetterParameter = false;
    }
    if (getters != null) {
        for (PsiMethod getter : getters) {
            String newGetterName = GetterSetterPrototypeProvider.suggestNewGetterName(propertyName, newPropertyName, getter);
            if (newGetterName == null) {
                newGetterName = PropertyUtil.suggestGetterName(newPropertyName, field.getType(), getter.getName());
            }
            addOverriddenAndImplemented(getter, newGetterName, null, propertyName, manager, allRenames);
        }
    }
    if (setter != null) {
        addOverriddenAndImplemented(setter, newSetterName, shouldRenameSetterParameter ? newPropertyName : null, propertyName, manager, allRenames);
    }
}
Also used : Project(com.intellij.openapi.project.Project) JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) ArrayList(java.util.ArrayList)

Example 52 with JavaCodeStyleManager

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

the class AutomaticVariableRenamer method nameToCanonicalName.

public String nameToCanonicalName(@NotNull String name, PsiNamedElement psiVariable) {
    final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(psiVariable.getProject());
    final String propertyName = codeStyleManager.variableNameToPropertyName(name, codeStyleManager.getVariableKind((PsiVariable) psiVariable));
    if (myToUnpluralize.contains(psiVariable)) {
        final String singular = StringUtil.unpluralize(propertyName);
        if (singular != null)
            return singular;
        // no need to pluralize since it was initially in singular form
        myToUnpluralize.remove(psiVariable);
    }
    return propertyName;
}
Also used : JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager)

Example 53 with JavaCodeStyleManager

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

the class AutomaticVariableRenamer method canonicalNameToName.

public String canonicalNameToName(String canonicalName, PsiNamedElement psiVariable) {
    final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(psiVariable.getProject());
    final String variableName = codeStyleManager.propertyNameToVariableName(canonicalName, codeStyleManager.getVariableKind((PsiVariable) psiVariable));
    if (myToUnpluralize.contains(psiVariable))
        return StringUtil.pluralize(variableName);
    return variableName;
}
Also used : JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager)

Example 54 with JavaCodeStyleManager

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

the class ConstructorParameterOnFieldRenameRenamer method nameToCanonicalName.

protected String nameToCanonicalName(@NonNls final String name, final PsiNamedElement element) {
    final JavaCodeStyleManager javaCodeStyleManager = JavaCodeStyleManager.getInstance(element.getProject());
    final VariableKind variableKind = element instanceof PsiVariable ? javaCodeStyleManager.getVariableKind((PsiVariable) element) : VariableKind.FIELD;
    return javaCodeStyleManager.variableNameToPropertyName(name, variableKind);
}
Also used : JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) VariableKind(com.intellij.psi.codeStyle.VariableKind)

Example 55 with JavaCodeStyleManager

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

the class ParameterData method initParameterData.

private static ParameterData initParameterData(PsiParameter parameter, String setterPrefix, Map<String, ParameterData> result) {
    JavaCodeStyleManager styleManager = JavaCodeStyleManager.getInstance(parameter.getProject());
    final String paramName = parameter.getName();
    final String pureParamName = styleManager.variableNameToPropertyName(paramName, VariableKind.PARAMETER);
    String uniqueParamName = pureParamName;
    ParameterData parameterData = result.get(uniqueParamName);
    int i = 0;
    while (parameterData != null) {
        if (!Comparing.equal(parameter.getType(), parameterData.getType())) {
            uniqueParamName = pureParamName + i++;
            parameterData = result.get(uniqueParamName);
        } else {
            break;
        }
    }
    if (parameterData == null) {
        parameterData = new ParameterData(paramName, parameter.getType());
        parameterData.setFieldName(styleManager.suggestVariableName(VariableKind.FIELD, uniqueParamName, null, parameter.getType()).names[0]);
        parameterData.setSetterName(PropertyUtil.suggestSetterName(uniqueParamName, setterPrefix));
        result.put(uniqueParamName, parameterData);
    }
    return parameterData;
}
Also used : JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager)

Aggregations

JavaCodeStyleManager (com.intellij.psi.codeStyle.JavaCodeStyleManager)96 Project (com.intellij.openapi.project.Project)27 SuggestedNameInfo (com.intellij.psi.codeStyle.SuggestedNameInfo)21 VariableKind (com.intellij.psi.codeStyle.VariableKind)19 IncorrectOperationException (com.intellij.util.IncorrectOperationException)15 NotNull (org.jetbrains.annotations.NotNull)13 CodeStyleManager (com.intellij.psi.codeStyle.CodeStyleManager)12 Nullable (org.jetbrains.annotations.Nullable)12 ArrayList (java.util.ArrayList)10 NonNls (org.jetbrains.annotations.NonNls)8 StringUtil (com.intellij.openapi.util.text.StringUtil)3 UniqueNameGenerator (com.intellij.util.text.UniqueNameGenerator)3 HashSet (java.util.HashSet)3 JavaLanguage (com.intellij.lang.java.JavaLanguage)2 Logger (com.intellij.openapi.diagnostic.Logger)2 Editor (com.intellij.openapi.editor.Editor)2 Comparing (com.intellij.openapi.util.Comparing)2 com.intellij.psi (com.intellij.psi)2 CodeStyleSettings (com.intellij.psi.codeStyle.CodeStyleSettings)2 FunctionalInterfaceParameterizationUtil (com.intellij.psi.impl.source.resolve.graphInference.FunctionalInterfaceParameterizationUtil)2