use of com.jetbrains.python.psi.PyStatement in project intellij-community by JetBrains.
the class PyStatementListImpl method addInternal.
@Override
public ASTNode addInternal(ASTNode first, ASTNode last, ASTNode anchor, Boolean before) {
if (first.getPsi() instanceof PyStatement && getStatements().length == 1) {
ASTNode treePrev = getNode().getTreePrev();
if (treePrev != null && treePrev.getElementType() == TokenType.WHITE_SPACE && !treePrev.textContains('\n')) {
ASTNode lineBreak = ASTFactory.whitespace("\n");
treePrev.getTreeParent().replaceChild(treePrev, lineBreak);
}
}
return super.addInternal(first, last, anchor, before);
}
use of com.jetbrains.python.psi.PyStatement in project intellij-community by JetBrains.
the class PyFormatterTest method testReformatOfSingleElementPossible.
/**
* 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(), () -> {
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");
}
use of com.jetbrains.python.psi.PyStatement in project intellij-community by JetBrains.
the class PySmartEnterProcessor method getStatementAtCaret.
@Nullable
protected PsiElement getStatementAtCaret(Editor editor, PsiFile psiFile) {
PsiElement statementAtCaret = super.getStatementAtCaret(editor, psiFile);
if (statementAtCaret instanceof PsiWhiteSpace) {
return null;
}
if (statementAtCaret == null) {
return null;
}
final PyStatementList statementList = PsiTreeUtil.getParentOfType(statementAtCaret, PyStatementList.class, false);
if (statementList != null) {
for (PyStatement statement : statementList.getStatements()) {
if (PsiTreeUtil.isAncestor(statement, statementAtCaret, true)) {
return statement;
}
}
} else {
final PyFile file = PsiTreeUtil.getParentOfType(statementAtCaret, PyFile.class, false);
if (file != null) {
for (PyStatement statement : file.getStatements()) {
if (PsiTreeUtil.isAncestor(statement, statementAtCaret, true)) {
return statement;
}
}
}
}
return null;
}
use of com.jetbrains.python.psi.PyStatement in project intellij-community by JetBrains.
the class PyTokenSeparatorGenerator method generateWhitespaceBetweenTokens.
public ASTNode generateWhitespaceBetweenTokens(ASTNode left, ASTNode right) {
PsiManager manager = right.getTreeParent().getPsi().getManager();
if (left.getElementType() == PyTokenTypes.END_OF_LINE_COMMENT) {
return createLineBreak(manager);
}
if (left.getPsi().isValid() && right.getPsi().isValid()) {
final PsiElement commonParent = PsiTreeUtil.findCommonParent(left.getPsi(), right.getPsi());
if (commonParent == null)
return null;
final PsiElement leftPrevAncestor = PsiTreeUtil.findPrevParent(commonParent, left.getPsi());
final PsiElement rightPrevAncestor = PsiTreeUtil.findPrevParent(commonParent, right.getPsi());
if (isStatementOrFunction(leftPrevAncestor) && isStatementOrFunction(rightPrevAncestor)) {
int leftIndent = PyPsiUtils.getElementIndentation(leftPrevAncestor);
int rightIndent = PyPsiUtils.getElementIndentation(rightPrevAncestor);
int maxIndent = Math.max(leftIndent, rightIndent);
return createWhitespace(manager, "\n" + StringUtil.repeatSymbol(' ', maxIndent));
}
}
if (right.getElementType() == PyTokenTypes.DEF_KEYWORD || right.getElementType() == PyTokenTypes.CLASS_KEYWORD) {
return createLineBreak(manager);
}
if (left.getElementType() == TokenType.WHITE_SPACE || right.getElementType() == TokenType.WHITE_SPACE) {
return null;
}
final PyStatement leftStatement = PsiTreeUtil.getParentOfType(left.getPsi(), PyStatement.class);
if (leftStatement != null && !PsiTreeUtil.isAncestor(leftStatement, right.getPsi(), false)) {
return createLineBreak(manager);
}
final Lexer lexer = new PythonIndentingLexer();
if (LanguageUtil.canStickTokensTogetherByLexer(left, right, lexer) == ParserDefinition.SpaceRequirements.MUST) {
return createSpace(manager);
}
return null;
}
Aggregations