Use reformat with context on postprocess reformat

This commit is contained in:
Yaroslav Lepenkin
2015-07-07 17:24:08 +03:00
parent f3107a404d
commit 59a1e0c715
4 changed files with 1 additions and 74 deletions
@@ -249,40 +249,6 @@ public class PsiTypeElementImpl extends CompositePsiElement implements PsiTypeEl
PsiImplUtil.markTypeAnnotations(this);
PsiElement result = super.replace(newElement);
PsiImplUtil.deleteTypeAnnotations((PsiTypeElement)result);
// We want to reformat method call arguments on method return type change because there is a possible situation that they are aligned
// and the change breaks the alignment.
// Example:
// Object test(1,
// 2) {}
// Suppose we're changing return type to 'MyCustomClass'. We get the following if parameter list is not reformatted:
// MyCustomClass test(1,
// 2) {}
PsiElement parent = result.getParent();
if (parent instanceof PsiMethod) {
PsiMethod method = (PsiMethod)parent;
CodeEditUtil.markToReformat(method.getParameterList().getNode(), true);
}
// We cover situation like below here:
// int test(int i, int j) {}
// ...
// int i = test(1,
// 2);
// I.e. the point is to avoid code like below during changing 'test()' return type from 'int' to 'long':
// long i = test(1,
// 2);
else if (parent instanceof PsiVariable) {
PsiVariable variable = (PsiVariable)parent;
if (variable.hasInitializer()) {
PsiExpression methodCallCandidate = variable.getInitializer();
if (methodCallCandidate instanceof PsiMethodCallExpression) {
PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression)methodCallCandidate;
CodeEditUtil.markToReformat(methodCallExpression.getArgumentList().getNode(), true);
}
}
}
return result;
}
@@ -43,27 +43,6 @@ public class PsiIdentifierImpl extends LeafPsiElement implements PsiIdentifier,
}
}
@Override
public PsiElement replace(@NotNull PsiElement newElement) throws IncorrectOperationException {
PsiElement result = super.replace(newElement);
// We want to reformat method parameters on method name change as well because there is a possible situation that they are aligned
// and method name change breaks the alignment.
// Example:
// public void test(int i,
// int j) {}
// Suppose we're renaming the method to test123. We get the following if parameter list is not reformatted:
// public void test123(int i,
// int j) {}
PsiElement methodCandidate = result.getParent();
if (methodCandidate instanceof PsiMethod) {
PsiMethod method = (PsiMethod)methodCandidate;
CodeEditUtil.markToReformat(method.getParameterList().getNode(), true);
}
return result;
}
public String toString(){
return "PsiIdentifier:" + getText();
}
@@ -717,25 +717,6 @@ public class PsiReferenceExpressionImpl extends PsiReferenceExpressionBase imple
return getChildRole(getFirstChildNode()) == ChildRole.QUALIFIER;
}
@Override
public void subtreeChanged() {
super.subtreeChanged();
// We want to reformat method call arguments on method name change because there is a possible situation that they are aligned
// and method change breaks the alignment.
// Example:
// test(1,
// 2);
// Suppose we're renaming the method to test123. We get the following if parameter list is not reformatted:
// test123(1,
// 2);
PsiElement methodCallCandidate = getParent();
if (methodCallCandidate instanceof PsiMethodCallExpression) {
PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression)methodCallCandidate;
CodeEditUtil.markToReformat(methodCallExpression.getArgumentList().getNode(), true);
}
}
private String getCachedNormalizedText() {
String whiteSpaceAndComments = myCachedNormalizedText;
if (whiteSpaceAndComments == null) {
@@ -745,6 +745,7 @@ public class PostprocessReformattingAspect implements PomModelAspect {
@Override
public void execute(@NotNull FileViewProvider viewProvider) {
final CodeFormatterFacade codeFormatter = getFormatterFacade(viewProvider);
codeFormatter.setReformatContext(true);
codeFormatter.processText(viewProvider.getPsi(viewProvider.getBaseLanguage()), myRanges.ensureNonEmpty(), false);
}