use of com.intellij.psi.codeStyle.JavaCodeStyleManager in project intellij-community by JetBrains.
the class InlineUtil method solveVariableNameConflicts.
public static void solveVariableNameConflicts(final PsiElement scope, final PsiElement placeToInsert, final PsiElement renameScope) throws IncorrectOperationException {
if (scope instanceof PsiVariable) {
PsiVariable var = (PsiVariable) scope;
String name = var.getName();
String oldName = name;
final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(scope.getProject());
while (true) {
String newName = codeStyleManager.suggestUniqueVariableName(name, placeToInsert, true);
if (newName.equals(name))
break;
name = newName;
newName = codeStyleManager.suggestUniqueVariableName(name, var, true);
if (newName.equals(name))
break;
name = newName;
}
if (!name.equals(oldName)) {
RefactoringUtil.renameVariableReferences(var, name, new LocalSearchScope(renameScope), true);
var.getNameIdentifier().replace(JavaPsiFacade.getElementFactory(scope.getProject()).createIdentifier(name));
}
}
PsiElement[] children = scope.getChildren();
for (PsiElement child : children) {
solveVariableNameConflicts(child, placeToInsert, renameScope);
}
}
use of com.intellij.psi.codeStyle.JavaCodeStyleManager in project intellij-community by JetBrains.
the class ReplaceConstructorWithBuilderDialog method applyNewSetterPrefix.
private void applyNewSetterPrefix() {
final String setterPrefix = Messages.showInputDialog(myTable, "New setter prefix:", "Rename Setters Prefix", null, mySetterPrefix, new MySetterPrefixInputValidator());
if (setterPrefix != null) {
mySetterPrefix = setterPrefix;
PropertiesComponent.getInstance(myProject).setValue(SETTER_PREFIX_KEY, setterPrefix);
final JavaCodeStyleManager javaCodeStyleManager = JavaCodeStyleManager.getInstance(myProject);
for (String paramName : myParametersMap.keySet()) {
final ParameterData data = myParametersMap.get(paramName);
paramName = data.getParamName();
final String propertyName = javaCodeStyleManager.variableNameToPropertyName(paramName, VariableKind.PARAMETER);
data.setSetterName(PropertyUtil.suggestSetterName(propertyName, setterPrefix));
}
myTable.revalidate();
myTable.repaint();
}
}
use of com.intellij.psi.codeStyle.JavaCodeStyleManager in project intellij-community by JetBrains.
the class ReplaceConstructorWithBuilderProcessor method createMethodSignature.
private PsiMethod createMethodSignature(String createMethodName) {
JavaCodeStyleManager styleManager = JavaCodeStyleManager.getInstance(myProject);
final StringBuffer buf = new StringBuffer();
final PsiMethod constructor = getWorkingConstructor();
for (PsiParameter parameter : constructor.getParameterList().getParameters()) {
final String pureParamName = styleManager.variableNameToPropertyName(parameter.getName(), VariableKind.PARAMETER);
if (buf.length() > 0)
buf.append(", ");
buf.append(myParametersMap.get(pureParamName).getFieldName());
}
return myElementFactory.createMethodFromText("public " + constructor.getName() + " " + createMethodName + "(){\n return new " + constructor.getName() + "(" + buf.toString() + ")" + ";\n}", constructor);
}
use of com.intellij.psi.codeStyle.JavaCodeStyleManager in project intellij-community by JetBrains.
the class ReplaceConstructorWithSettersChainInfo method fixUsage.
public void fixUsage() throws IncorrectOperationException {
final PsiNewExpression expr = (PsiNewExpression) getElement();
if (expr != null) {
final PsiElementFactory elementFactory = JavaPsiFacade.getInstance(expr.getProject()).getElementFactory();
final PsiMethod constructor = expr.resolveConstructor();
if (constructor != null) {
StringBuffer buf = new StringBuffer();
final PsiExpressionList argumentList = expr.getArgumentList();
if (argumentList != null) {
final PsiExpression[] args = argumentList.getExpressions();
final PsiParameter[] parameters = constructor.getParameterList().getParameters();
final JavaCodeStyleManager styleManager = JavaCodeStyleManager.getInstance(constructor.getProject());
for (int i = 0; i < Math.min(constructor.getParameterList().getParametersCount(), args.length); i++) {
String arg = args[i].getText();
if (parameters[i].isVarArgs()) {
for (int ia = i + 1; ia < args.length; ia++) {
arg += ", " + args[ia].getText();
}
}
final String pureParamName = styleManager.variableNameToPropertyName(parameters[i].getName(), VariableKind.PARAMETER);
final ParameterData data = myParametersMap.get(pureParamName);
if (!Comparing.strEqual(arg, data.getDefaultValue()) || data.isInsertSetter()) {
buf.append(data.getSetterName()).append("(").append(arg).append(").");
}
}
final PsiExpression settersChain = elementFactory.createExpressionFromText("new " + myBuilderClass + "()." + buf.toString() + "create" + StringUtil.capitalize(constructor.getName()) + "()", null);
styleManager.shortenClassReferences(expr.replace(settersChain));
}
}
}
}
use of com.intellij.psi.codeStyle.JavaCodeStyleManager in project intellij-community by JetBrains.
the class PsiJavaCodeReferenceElementImpl method bindToClass.
private PsiElement bindToClass(@NotNull PsiClass aClass, @NotNull PsiFile containingFile) throws IncorrectOperationException {
String qName = aClass.getQualifiedName();
Project project = containingFile.getProject();
boolean preserveQualification = JavaCodeStyleSettingsFacade.getInstance(project).useFQClassNames() && isFullyQualified(containingFile);
JavaPsiFacade facade = JavaPsiFacade.getInstance(project);
if (qName == null) {
qName = aClass.getName();
assert qName != null : aClass;
PsiClass psiClass = facade.getResolveHelper().resolveReferencedClass(qName, this);
if (!getManager().areElementsEquivalent(psiClass, aClass)) {
throw cannotBindError(aClass);
}
} else if (facade.findClass(qName, getResolveScope()) == null && !preserveQualification) {
return this;
}
String text = qName;
PsiReferenceParameterList parameterList = getParameterList();
if (parameterList != null) {
text += parameterList.getText();
}
PsiJavaCodeReferenceElement ref;
try {
ref = facade.getParserFacade().createReferenceFromText(text, getParent());
} catch (IncorrectOperationException e) {
throw new IncorrectOperationException(e.getMessage() + " [qname=" + qName + " class=" + aClass + ";" + aClass.getClass().getName() + "]");
}
getTreeParent().replaceChildInternal(this, (TreeElement) ref.getNode());
if (!preserveQualification) {
JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project);
ref = (PsiJavaCodeReferenceElement) codeStyleManager.shortenClassReferences(ref, JavaCodeStyleManager.INCOMPLETE_CODE);
}
return ref;
}
Aggregations