Search in sources :

Example 71 with PsiWhiteSpace

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

the class WhiteSpace method coveredByBlock.

/**
   * Allows to check if {@code 'myInitial'} property value stands for continuous white space text.
   * <p/>
   * The text is considered to be continuous {@code 'white space'} at following cases:
   * <ul>
   *   <li>{@code 'myInitial'} is empty string or string that contains white spaces only;</li>
   *   <li>{@code 'myInitial'} is a {@code CDATA} string which content is empty or consists from white spaces only;</li>
   *   <li>{@code 'myInitial'} string belongs to the same {@link PsiWhiteSpace} element;</li>
   * </ul>
   *
   * @param model     formatting model that is used to provide access to the {@code PSI API} if necessary
   * @return          {@code true} if {@code 'myInitial'} property value stands for white space;
   *                  {@code false} otherwise
   */
private boolean coveredByBlock(final FormattingDocumentModel model) {
    if (myInitial == null)
        return true;
    if (model.containsWhiteSpaceSymbolsOnly(myStart, myEnd))
        return true;
    if (!(model instanceof FormattingDocumentModelImpl))
        return false;
    PsiFile psiFile = ((FormattingDocumentModelImpl) model).getFile();
    if (psiFile == null)
        return false;
    PsiElement start = psiFile.findElementAt(myStart);
    PsiElement end = psiFile.findElementAt(myEnd - 1);
    // there maybe non-white text inside CDATA-encoded injected elements
    return start == end && start instanceof PsiWhiteSpace;
}
Also used : PsiFile(com.intellij.psi.PsiFile) FormattingDocumentModelImpl(com.intellij.psi.formatter.FormattingDocumentModelImpl) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Example 72 with PsiWhiteSpace

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

the class PyForPartImpl method followsNodeOfType.

/**
   * Checks that given node actually follows a node of given type, skipping whitespace.
   * @param node node to check.
   * @param eltType type of a node that must precede the node we're checking.
   * @return true if node is really a next sibling to a node of eltType type.
   */
protected static boolean followsNodeOfType(ASTNode node, PyElementType eltType) {
    if (node != null) {
        PsiElement checker = node.getPsi();
        // step from the source node
        checker = checker.getPrevSibling();
        while (checker != null) {
            ASTNode ch_node = checker.getNode();
            if (ch_node == null)
                return false;
            else {
                if (ch_node.getElementType() == eltType) {
                    return true;
                } else if (!(checker instanceof PsiWhiteSpace)) {
                    return false;
                }
            }
            checker = checker.getPrevSibling();
        }
    }
    return false;
}
Also used : ASTNode(com.intellij.lang.ASTNode) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Example 73 with PsiWhiteSpace

use of com.intellij.psi.PsiWhiteSpace 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 74 with PsiWhiteSpace

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

the class PyBreakContinueGotoProvider method getGotoDeclarationTarget.

@Override
public PsiElement getGotoDeclarationTarget(@Nullable PsiElement source, Editor editor) {
    if (source != null && source.getLanguage() instanceof PythonLanguage) {
        final PyLoopStatement loop = PsiTreeUtil.getParentOfType(source, PyLoopStatement.class, false, PyFunction.class, PyClass.class);
        if (loop != null) {
            ASTNode node = source.getNode();
            if (node != null) {
                IElementType node_type = node.getElementType();
                if (node_type == PyTokenTypes.CONTINUE_KEYWORD) {
                    return loop;
                }
                if (node_type == PyTokenTypes.BREAK_KEYWORD) {
                    PsiElement outer_element = loop;
                    PsiElement after_cycle;
                    while (true) {
                        after_cycle = outer_element.getNextSibling();
                        if (after_cycle != null) {
                            if (after_cycle instanceof PsiWhiteSpace) {
                                after_cycle = after_cycle.getNextSibling();
                            }
                            if (after_cycle instanceof PyStatement)
                                return after_cycle;
                        }
                        outer_element = outer_element.getParent();
                        if (PyUtil.instanceOf(outer_element, PsiFile.class, PyFunction.class, PyClass.class)) {
                            break;
                        }
                    }
                    // cycle is the last statement in the flow of execution. its last element is our best bet.
                    return PsiTreeUtil.getDeepestLast(loop);
                }
            }
        }
    }
    return null;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PythonLanguage(com.jetbrains.python.PythonLanguage) ASTNode(com.intellij.lang.ASTNode) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Example 75 with PsiWhiteSpace

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

the class StatementEffectFunctionCallQuickFix method getNextElement.

private static PsiElement getNextElement(@NotNull final PsiElement expression) {
    final PsiElement whiteSpace = expression.getContainingFile().findElementAt(expression.getTextOffset() + expression.getTextLength());
    PsiElement next = null;
    if (whiteSpace instanceof PsiWhiteSpace) {
        final String whiteSpaceText = whiteSpace.getText();
        if (!whiteSpaceText.contains("\n")) {
            next = whiteSpace.getNextSibling();
            while (next instanceof PsiWhiteSpace && whiteSpaceText.contains("\\")) {
                next = next.getNextSibling();
            }
        }
    } else
        next = whiteSpace;
    RemoveUnnecessaryBackslashQuickFix.removeBackSlash(next);
    if (whiteSpace != null)
        whiteSpace.delete();
    return next;
}
Also used : LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) 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