use of com.intellij.psi.codeStyle.CodeStyleManager in project intellij-community by JetBrains.
the class ReturnStatementReturnValue method createReplacement.
public PsiStatement createReplacement(final PsiMethod extractedMethod, final PsiMethodCallExpression methodCallExpression) throws IncorrectOperationException {
final PsiElementFactory elementFactory = JavaPsiFacade.getInstance(methodCallExpression.getProject()).getElementFactory();
final CodeStyleManager styleManager = CodeStyleManager.getInstance(methodCallExpression.getProject());
PsiReturnStatement returnStatement = (PsiReturnStatement) elementFactory.createStatementFromText("return x;", null);
returnStatement = (PsiReturnStatement) styleManager.reformat(returnStatement);
returnStatement.getReturnValue().replace(methodCallExpression);
return returnStatement;
}
use of com.intellij.psi.codeStyle.CodeStyleManager in project intellij-community by JetBrains.
the class PsiAdapter method addOrReplaceJavadoc.
/**
* Add or replaces the javadoc comment to the given method.
*
* @param method the method the javadoc should be added/set to.
* @param javadoc the javadoc comment.
* @param replace true if any existing javadoc should be replaced. false will not replace any existing javadoc and thus leave the javadoc untouched.
* @return the added/replace javadoc comment, null if the was an existing javadoc and it should <b>not</b> be replaced.
* @throws IncorrectOperationException is thrown if error adding/replacing the javadoc comment.
*/
@Nullable
public static PsiComment addOrReplaceJavadoc(PsiMethod method, String javadoc, boolean replace) {
final Project project = method.getProject();
PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory();
PsiComment comment = factory.createCommentFromText(javadoc, null);
// does a method already exists?
PsiDocComment doc = method.getDocComment();
if (doc != null) {
if (replace) {
// javadoc already exists, so replace
doc.replace(comment);
final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
// to reformat javadoc
codeStyleManager.reformat(method);
return comment;
} else {
// do not replace existing javadoc
return null;
}
} else {
// add new javadoc
method.addBefore(comment, method.getFirstChild());
final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
// to reformat javadoc
codeStyleManager.reformat(method);
return comment;
}
}
use of com.intellij.psi.codeStyle.CodeStyleManager in project intellij-community by JetBrains.
the class MakeClassStaticProcessor method changeInternalUsage.
protected void changeInternalUsage(final InternalUsageInfo usage, final PsiElementFactory factory) throws IncorrectOperationException {
if (!mySettings.isChangeSignature())
return;
PsiElement element = usage.getElement();
if (element instanceof PsiReferenceExpression) {
PsiReferenceExpression newRef = null;
if (mySettings.isMakeFieldParameters()) {
PsiElement resolved = ((PsiReferenceExpression) element).resolve();
if (resolved instanceof PsiField) {
String name = mySettings.getNameForField((PsiField) resolved);
if (name != null) {
name = convertToFieldName(name);
if (name != null) {
newRef = (PsiReferenceExpression) factory.createExpressionFromText(name, null);
}
}
}
}
if (newRef == null && mySettings.isMakeClassParameter()) {
newRef = (PsiReferenceExpression) factory.createExpressionFromText(convertToFieldName(mySettings.getClassParameterName()) + "." + element.getText(), null);
}
if (newRef != null) {
CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(myProject);
newRef = (PsiReferenceExpression) codeStyleManager.reformat(newRef);
element.replace(newRef);
}
} else if (mySettings.isMakeClassParameter() && (element instanceof PsiThisExpression || element instanceof PsiSuperExpression)) {
final PsiElement replace = element.replace(factory.createExpressionFromText(convertToFieldName(mySettings.getClassParameterName()), null));
final PsiField field = PsiTreeUtil.getParentOfType(replace, PsiField.class);
if (field != null) {
myFieldsToSplit.add(field);
}
} else if (element instanceof PsiNewExpression && mySettings.isMakeClassParameter()) {
final PsiNewExpression newExpression = ((PsiNewExpression) element);
LOG.assertTrue(newExpression.getQualifier() == null);
final String newText = convertToFieldName(mySettings.getClassParameterName()) + "." + newExpression.getText();
final PsiExpression expr = factory.createExpressionFromText(newText, null);
element.replace(expr);
}
}
use of com.intellij.psi.codeStyle.CodeStyleManager in project intellij-community by JetBrains.
the class RefactoringChangeUtil method qualifyReference.
public static PsiReferenceExpression qualifyReference(@NotNull PsiReferenceExpression referenceExpression, @NotNull PsiMember member, @Nullable final PsiClass qualifyingClass) throws IncorrectOperationException {
PsiManager manager = referenceExpression.getManager();
PsiMethodCallExpression methodCallExpression = PsiTreeUtil.getParentOfType(referenceExpression, PsiMethodCallExpression.class, true);
while (methodCallExpression != null) {
if (isSuperOrThisMethodCall(methodCallExpression)) {
return referenceExpression;
}
methodCallExpression = PsiTreeUtil.getParentOfType(methodCallExpression, PsiMethodCallExpression.class, true);
}
PsiReferenceExpression expressionFromText;
final PsiElementFactory factory = JavaPsiFacade.getInstance(manager.getProject()).getElementFactory();
if (qualifyingClass == null) {
PsiClass parentClass = PsiTreeUtil.getParentOfType(referenceExpression, PsiClass.class);
final PsiClass containingClass = member.getContainingClass();
if (parentClass != null && !InheritanceUtil.isInheritorOrSelf(parentClass, containingClass, true)) {
while (parentClass != null && !InheritanceUtil.isInheritorOrSelf(parentClass, containingClass, true)) {
parentClass = PsiTreeUtil.getParentOfType(parentClass, PsiClass.class, true);
}
LOG.assertTrue(parentClass != null);
expressionFromText = (PsiReferenceExpression) factory.createExpressionFromText("A.this." + member.getName(), null);
((PsiThisExpression) expressionFromText.getQualifierExpression()).getQualifier().replace(factory.createClassReferenceElement(parentClass));
} else {
final PsiModifierListOwner staticElement = PsiUtil.getEnclosingStaticElement(referenceExpression, null);
if (staticElement != null && containingClass != null && !PsiTreeUtil.isAncestor(staticElement, containingClass, false)) {
return referenceExpression;
} else {
expressionFromText = (PsiReferenceExpression) factory.createExpressionFromText("this." + member.getName(), null);
}
}
} else {
expressionFromText = (PsiReferenceExpression) factory.createExpressionFromText("A." + member.getName(), null);
expressionFromText.setQualifierExpression(factory.createReferenceExpression(qualifyingClass));
}
CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(manager.getProject());
expressionFromText = (PsiReferenceExpression) codeStyleManager.reformat(expressionFromText);
return (PsiReferenceExpression) referenceExpression.replace(expressionFromText);
}
use of com.intellij.psi.codeStyle.CodeStyleManager in project intellij-community by JetBrains.
the class FieldConflictsResolver method qualifyReference.
public static GrReferenceExpression qualifyReference(GrReferenceExpression referenceExpression, final PsiMember member, @Nullable final PsiClass qualifyingClass) throws IncorrectOperationException {
PsiManager manager = referenceExpression.getManager();
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(manager.getProject());
GrReferenceExpression expressionFromText;
if (qualifyingClass == null) {
final PsiClass parentClass = PsiTreeUtil.getParentOfType(referenceExpression, PsiClass.class);
final PsiClass containingClass = member.getContainingClass();
if (parentClass != null && !InheritanceUtil.isInheritorOrSelf(parentClass, containingClass, true)) {
expressionFromText = (GrReferenceExpression) factory.createExpressionFromText(containingClass.getQualifiedName() + ".this." + member.getName());
} else {
expressionFromText = (GrReferenceExpression) factory.createExpressionFromText("this." + member.getName());
}
} else {
expressionFromText = (GrReferenceExpression) factory.createExpressionFromText(qualifyingClass.getQualifiedName() + '.' + member.getName());
}
CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(manager.getProject());
expressionFromText = (GrReferenceExpression) codeStyleManager.reformat(expressionFromText);
return (GrReferenceExpression) referenceExpression.replace(expressionFromText);
}
Aggregations