Search in sources :

Example 6 with PyStatement

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);
}
Also used : PyStatement(com.jetbrains.python.psi.PyStatement) ASTNode(com.intellij.lang.ASTNode)

Example 7 with PyStatement

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");
}
Also used : PyStatement(com.jetbrains.python.psi.PyStatement) CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager) PsiElement(com.intellij.psi.PsiElement)

Example 8 with PyStatement

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;
}
Also used : PyStatement(com.jetbrains.python.psi.PyStatement) PyStatementList(com.jetbrains.python.psi.PyStatementList) PyFile(com.jetbrains.python.psi.PyFile) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace) Nullable(org.jetbrains.annotations.Nullable)

Example 9 with PyStatement

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;
}
Also used : PyStatement(com.jetbrains.python.psi.PyStatement) Lexer(com.intellij.lexer.Lexer) PythonIndentingLexer(com.jetbrains.python.lexer.PythonIndentingLexer) PythonIndentingLexer(com.jetbrains.python.lexer.PythonIndentingLexer) PsiManager(com.intellij.psi.PsiManager) PsiElement(com.intellij.psi.PsiElement)

Aggregations

PyStatement (com.jetbrains.python.psi.PyStatement)9 PsiElement (com.intellij.psi.PsiElement)8 PyFile (com.jetbrains.python.psi.PyFile)3 Sdk (com.intellij.openapi.projectRoots.Sdk)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 PsiDirectory (com.intellij.psi.PsiDirectory)2 PsiFileSystemItem (com.intellij.psi.PsiFileSystemItem)2 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)2 PyStatementList (com.jetbrains.python.psi.PyStatementList)2 Nullable (org.jetbrains.annotations.Nullable)2 Location (com.intellij.execution.Location)1 ASTNode (com.intellij.lang.ASTNode)1 Lexer (com.intellij.lexer.Lexer)1 Module (com.intellij.openapi.module.Module)1 TextRange (com.intellij.openapi.util.TextRange)1 PsiManager (com.intellij.psi.PsiManager)1 CodeStyleManager (com.intellij.psi.codeStyle.CodeStyleManager)1 PythonIndentingLexer (com.jetbrains.python.lexer.PythonIndentingLexer)1 File (java.io.File)1