Search in sources :

Example 1 with PythonIndentingLexer

use of com.jetbrains.python.lexer.PythonIndentingLexer 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

Lexer (com.intellij.lexer.Lexer)1 PsiElement (com.intellij.psi.PsiElement)1 PsiManager (com.intellij.psi.PsiManager)1 PythonIndentingLexer (com.jetbrains.python.lexer.PythonIndentingLexer)1 PyStatement (com.jetbrains.python.psi.PyStatement)1