mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
PyFormatterModelBuilder always uses PyFile as root block element
The same way FormatterModelBuilders are implemented for Java and JavaScript. It allows to call codeStyleManager.reformat(PsiElement) directly instead of codeStyleManager.reformatText(elt.getContainingFile(), elt.getTextRange().getStartOffset(), elt.getTextRange().getEndOffset());
This commit is contained in:
@@ -46,13 +46,13 @@ public class PythonFormattingModelBuilder implements FormattingModelBuilderEx, C
|
||||
public FormattingModel createModel(@NotNull PsiElement element,
|
||||
@NotNull CodeStyleSettings settings,
|
||||
@NotNull FormattingMode mode) {
|
||||
final ASTNode fileNode = element.getContainingFile().getNode();
|
||||
if (DUMP_FORMATTING_AST) {
|
||||
ASTNode fileNode = element.getContainingFile().getNode();
|
||||
System.out.println("AST tree for " + element.getContainingFile().getName() + ":");
|
||||
printAST(fileNode, 0);
|
||||
}
|
||||
final PyBlockContext context = new PyBlockContext(settings, createSpacingBuilder(settings), mode);
|
||||
final PyBlock block = new PyBlock(null, element.getNode(), null, Indent.getNoneIndent(), null, context);
|
||||
final PyBlock block = new PyBlock(null, fileNode, null, Indent.getNoneIndent(), null, context);
|
||||
if (DUMP_FORMATTING_AST) {
|
||||
FormattingModelDumper.dumpFormattingModel(block, 2, System.out);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
x=[1,2,3]
|
||||
y='spam<caret>'*2
|
||||
@@ -0,0 +1,2 @@
|
||||
x=[1,2,3]
|
||||
y = 'spam' * 2
|
||||
@@ -21,10 +21,12 @@ import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.codeStyle.CodeStyleManager;
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettings;
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.jetbrains.python.fixtures.PyTestCase;
|
||||
import com.jetbrains.python.formatter.PyCodeStyleSettings;
|
||||
import com.jetbrains.python.psi.LanguageLevel;
|
||||
import com.jetbrains.python.psi.PyElementGenerator;
|
||||
import com.jetbrains.python.psi.PyStatement;
|
||||
import com.jetbrains.python.psi.impl.PythonLanguageLevelPusher;
|
||||
|
||||
/**
|
||||
@@ -424,6 +426,26 @@ public class PyFormatterTest extends PyTestCase {
|
||||
myFixture.checkResultByFile("formatter/" + getTestName(true) + "_after.py");
|
||||
}
|
||||
|
||||
/**
|
||||
* This test merely checks that call to {@link com.intellij.psi.codeStyle.CodeStyleManager#reformat(com.intellij.psi.PsiElement)}
|
||||
* is possible for Python sources.
|
||||
*/
|
||||
public void testReformatOfSingleElementPossible() {
|
||||
myFixture.configureByFile("formatter/" + getTestName(true) + ".py");
|
||||
WriteCommandAction.runWriteCommandAction(myFixture.getProject(), new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
final PsiElement elementAtCaret = myFixture.getFile().findElementAt(myFixture.getCaretOffset());
|
||||
assertNotNull(elementAtCaret);
|
||||
final PyStatement statement = PsiTreeUtil.getParentOfType(elementAtCaret, PyStatement.class, false);
|
||||
assertNotNull(statement);
|
||||
final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(myFixture.getProject());
|
||||
codeStyleManager.reformat(statement);
|
||||
}
|
||||
});
|
||||
myFixture.checkResultByFile("formatter/" + getTestName(true) + "_after.py");
|
||||
}
|
||||
|
||||
private CodeStyleSettings settings() {
|
||||
return CodeStyleSettingsManager.getInstance().getSettings(myFixture.getProject());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user