[PsiBasedFormattingModel] Perform whitespace modification with disabled automatic formatting of changed psi elements. Changed whitespace would not be formatted twice, which will prevent from reformatting whitespaces of aligned blocks and producing inconsistent result

This commit is contained in:
Yaroslav Lepenkin
2015-05-14 15:19:38 +03:00
parent 8374d6a540
commit 1bcff41926
5 changed files with 36 additions and 8 deletions
@@ -27,6 +27,7 @@ import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.TokenType;
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.psi.impl.source.SourceTreeToPsiMap;
import com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil;
import org.jetbrains.annotations.NotNull;
@@ -36,18 +37,19 @@ public class PsiBasedFormattingModel implements FormattingModelEx {
private static final Logger LOG = Logger.getInstance("#com.intellij.psi.formatter.PsiBasedFormattingModel");
private final Project myProject;
private final ASTNode myASTNode;
private final FormattingDocumentModelImpl myDocumentModel;
@NotNull private final Block myRootBlock;
protected boolean myCanModifyAllWhiteSpaces = false;
public PsiBasedFormattingModel(final PsiFile file,
@NotNull final Block rootBlock,
final FormattingDocumentModelImpl documentModel) {
myASTNode = SourceTreeToPsiMap.psiElementToTree(file);
myDocumentModel = documentModel;
myRootBlock = rootBlock;
myProject = file.getProject();
}
@@ -81,7 +83,7 @@ public class PsiBasedFormattingModel implements FormattingModelEx {
@Nullable
private String replaceWithPSI(final TextRange textRange, String whiteSpace) {
private String replaceWithPSI(final TextRange textRange, final String whiteSpace) {
final int offset = textRange.getEndOffset();
ASTNode leafElement = findElementAt(offset);
@@ -101,7 +103,14 @@ public class PsiBasedFormattingModel implements FormattingModelEx {
return replaceWithPsiInLeaf(textRange, whiteSpace, leafElement);
}
} else if (textRange.getEndOffset() == myASTNode.getTextLength()){
FormatterUtil.replaceLastWhiteSpace(myASTNode, whiteSpace, textRange);
CodeStyleManager.getInstance(myProject).performActionWithFormatterDisabled(new Runnable() {
@Override
public void run() {
FormatterUtil.replaceLastWhiteSpace(myASTNode, whiteSpace, textRange);
}
});
return whiteSpace;
} else {
return null;
@@ -109,12 +118,18 @@ public class PsiBasedFormattingModel implements FormattingModelEx {
}
@Nullable
protected String replaceWithPsiInLeaf(final TextRange textRange, String whiteSpace, ASTNode leafElement) {
protected String replaceWithPsiInLeaf(final TextRange textRange, final String whiteSpace, final ASTNode leafElement) {
if (!myCanModifyAllWhiteSpaces) {
if (leafElement.getElementType() == TokenType.WHITE_SPACE) return null;
}
FormatterUtil.replaceWhiteSpace(whiteSpace, leafElement, TokenType.WHITE_SPACE, textRange);
CodeStyleManager.getInstance(myProject).performActionWithFormatterDisabled(new Runnable() {
@Override
public void run() {
FormatterUtil.replaceWhiteSpace(whiteSpace, leafElement, TokenType.WHITE_SPACE, textRange);
}
});
return whiteSpace;
}
@@ -1,6 +1,6 @@
{
"a": 1,
"bbb": [
"a": 1,
"bbb": [
2
],
"bbbbb": 3
@@ -0,0 +1,4 @@
{
"aaaaa": 1,
"bbb aaaaaaa": 2
}
@@ -0,0 +1,4 @@
{
"aaaaa" : 1,
"bbb aaaaaaa": 2
}
@@ -601,6 +601,11 @@ public class PyFormatterTest extends PyTestCase {
doTest();
}
public void testDoNotDestroyAlignment_OnPostponedFormatting() throws Exception {
getCustomSettings().DICT_ALIGNMENT = PyCodeStyleSettings.DICT_ALIGNMENT_ON_COLON;
doTest();
}
public void testAlignmentOfEmptyCollectionLiterals() {
doTest();
}