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);
}
}
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;
}
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;
}
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);
}
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;
}
Aggregations