Search in sources :

Example 11 with PsiWhiteSpace

use of com.intellij.psi.PsiWhiteSpace in project intellij-community by JetBrains.

the class PyFillParagraphHandler method getIndent.

private static String getIndent(PyStringLiteralExpression stringLiteralExpression) {
    final PyStatementList statementList = PsiTreeUtil.getParentOfType(stringLiteralExpression, PyStatementList.class);
    String indent = "";
    if (statementList != null) {
        final PsiElement whiteSpace = statementList.getPrevSibling();
        if (whiteSpace instanceof PsiWhiteSpace)
            indent = whiteSpace.getText();
        else
            indent = "\n";
    }
    return indent;
}
Also used : PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Example 12 with PsiWhiteSpace

use of com.intellij.psi.PsiWhiteSpace in project intellij-community by JetBrains.

the class PyCommentBreakerEnterProcessor method doEnter.

public boolean doEnter(Editor editor, PsiElement psiElement, boolean isModified) {
    if (isModified) {
        return false;
    }
    final CaretModel caretModel = editor.getCaretModel();
    PsiElement atCaret = psiElement.getContainingFile().findElementAt(caretModel.getOffset());
    if (atCaret instanceof PsiWhiteSpace) {
        atCaret = atCaret.getPrevSibling();
    }
    final PsiElement comment = PsiTreeUtil.getParentOfType(atCaret, PsiComment.class, false);
    if (comment != null) {
        SmartEnterUtil.plainEnter(editor);
        editor.getDocument().insertString(caretModel.getOffset(), "# ");
        caretModel.moveToOffset(caretModel.getOffset() + 2);
        return true;
    }
    return false;
}
Also used : CaretModel(com.intellij.openapi.editor.CaretModel) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Example 13 with PsiWhiteSpace

use of com.intellij.psi.PsiWhiteSpace in project intellij-community by JetBrains.

the class PyCodeBlockProvider method getCodeBlockRange.

@Nullable
@Override
public TextRange getCodeBlockRange(Editor editor, PsiFile psiFile) {
    int caretOffset = editor.getCaretModel().getOffset();
    PsiElement element = psiFile.findElementAt(caretOffset);
    if (element == null) {
        return null;
    }
    while (caretOffset > 0 && element instanceof PsiWhiteSpace) {
        caretOffset--;
        element = psiFile.findElementAt(caretOffset);
    }
    PyStatement statement = PsiTreeUtil.getParentOfType(element, PyStatement.class);
    if (statement != null) {
        PyStatementList statementList = PsiTreeUtil.findChildOfType(statement, PyStatementList.class);
        // that statement list
        if (statementList == null) {
            statementList = PsiTreeUtil.getParentOfType(statement, PyStatementList.class);
            if (statementList != null) {
                statement = PsiTreeUtil.getParentOfType(statementList, PyStatement.class);
            }
        }
        if (statement != null) {
            // if we're in the beginning of the statement already, pressing Ctrl-[ again should move the caret one statement higher
            final int statementStart = statement.getTextRange().getStartOffset();
            int statementEnd = statement.getTextRange().getEndOffset();
            while (statementEnd > statementStart && psiFile.findElementAt(statementEnd) instanceof PsiWhiteSpace) {
                statementEnd--;
            }
            if (caretOffset == statementStart || caretOffset == statementEnd) {
                final PyStatement statementAbove = PsiTreeUtil.getParentOfType(statement, PyStatement.class);
                if (statementAbove != null) {
                    if (caretOffset == statementStart) {
                        return new TextRange(statementAbove.getTextRange().getStartOffset(), statementEnd);
                    } else {
                        return new TextRange(statementStart, statementAbove.getTextRange().getEndOffset());
                    }
                }
            }
            return statement.getTextRange();
        }
    }
    return null;
}
Also used : PyStatement(com.jetbrains.python.psi.PyStatement) TextRange(com.intellij.openapi.util.TextRange) PyStatementList(com.jetbrains.python.psi.PyStatementList) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace) Nullable(org.jetbrains.annotations.Nullable)

Example 14 with PsiWhiteSpace

use of com.intellij.psi.PsiWhiteSpace in project intellij-community by JetBrains.

the class PythonFoldingBuilder method foldSequentialComments.

private static void foldSequentialComments(ASTNode node, List<FoldingDescriptor> descriptors) {
    //do not start folded comments from custom region
    if (isCustomRegionElement(node.getPsi())) {
        return;
    }
    //need to skip previous comments in sequence
    ASTNode curNode = node.getTreePrev();
    while (curNode != null) {
        if (curNode.getElementType() == PyTokenTypes.END_OF_LINE_COMMENT) {
            if (isCustomRegionElement(curNode.getPsi())) {
                break;
            }
            return;
        }
        curNode = curNode.getPsi() instanceof PsiWhiteSpace ? curNode.getTreePrev() : null;
    }
    //fold sequence comments in one block
    curNode = node.getTreeNext();
    ASTNode lastCommentNode = node;
    while (curNode != null) {
        if (curNode.getElementType() == PyTokenTypes.END_OF_LINE_COMMENT) {
            //do not end folded comments with custom region
            if (isCustomRegionElement(curNode.getPsi())) {
                break;
            }
            lastCommentNode = curNode;
            curNode = curNode.getTreeNext();
            continue;
        }
        curNode = curNode.getPsi() instanceof PsiWhiteSpace ? curNode.getTreeNext() : null;
    }
    if (lastCommentNode != node) {
        descriptors.add(new FoldingDescriptor(node, TextRange.create(node.getStartOffset(), lastCommentNode.getTextRange().getEndOffset())));
    }
}
Also used : FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) ASTNode(com.intellij.lang.ASTNode) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Example 15 with PsiWhiteSpace

use of com.intellij.psi.PsiWhiteSpace in project intellij-community by JetBrains.

the class XmlAutoPopupHandler method isLanguageRelevant.

private static boolean isLanguageRelevant(final PsiElement element, final PsiFile file, final Ref<Boolean> isRelevantLanguage, final Ref<Boolean> isAnt) {
    Boolean isAntFile = isAnt.get();
    if (isAntFile == null) {
        isAntFile = XmlUtil.isAntFile(file);
        isAnt.set(isAntFile);
    }
    Boolean result = isRelevantLanguage.get();
    if (result == null) {
        Language language = element.getLanguage();
        PsiElement parent = element.getParent();
        if (element instanceof PsiWhiteSpace && parent != null) {
            language = parent.getLanguage();
        }
        result = language instanceof XMLLanguage || HtmlUtil.supportsXmlTypedHandlers(file) || isAntFile.booleanValue();
        isRelevantLanguage.set(result);
    }
    return result.booleanValue();
}
Also used : Language(com.intellij.lang.Language) XMLLanguage(com.intellij.lang.xml.XMLLanguage) XMLLanguage(com.intellij.lang.xml.XMLLanguage) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Aggregations

PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)90 PsiElement (com.intellij.psi.PsiElement)82 TextRange (com.intellij.openapi.util.TextRange)23 ASTNode (com.intellij.lang.ASTNode)15 PsiFile (com.intellij.psi.PsiFile)15 NotNull (org.jetbrains.annotations.NotNull)15 Nullable (org.jetbrains.annotations.Nullable)14 PsiComment (com.intellij.psi.PsiComment)13 IElementType (com.intellij.psi.tree.IElementType)13 Document (com.intellij.openapi.editor.Document)12 Project (com.intellij.openapi.project.Project)7 ArrayList (java.util.ArrayList)6 FoldingDescriptor (com.intellij.lang.folding.FoldingDescriptor)5 Editor (com.intellij.openapi.editor.Editor)5 Language (com.intellij.lang.Language)4 UnfairTextRange (com.intellij.openapi.util.UnfairTextRange)4 XmlTag (com.intellij.psi.xml.XmlTag)4 XMLLanguage (com.intellij.lang.xml.XMLLanguage)3 PsiDocumentManager (com.intellij.psi.PsiDocumentManager)3 XmlFile (com.intellij.psi.xml.XmlFile)3