Search in sources :

Example 76 with PsiWhiteSpace

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

the class XmlAttributeDeclImpl method findElementType.

private PsiElement findElementType() {
    final PsiElement elementName = findElementByTokenType(XML_NAME);
    final PsiElement nextSibling = (elementName != null) ? elementName.getNextSibling() : null;
    final PsiElement elementType = (nextSibling instanceof PsiWhiteSpace) ? nextSibling.getNextSibling() : nextSibling;
    return elementType;
}
Also used : PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Example 77 with PsiWhiteSpace

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

the class XmlFormattingModel method replaceWithPsiInLeaf.

@Override
protected String replaceWithPsiInLeaf(final TextRange textRange, String whiteSpace, ASTNode leafElement) {
    if (!myCanModifyAllWhiteSpaces) {
        if (leafElement.getElementType() == TokenType.WHITE_SPACE)
            return null;
        LOG.assertTrue(leafElement.getPsi().isValid());
        ASTNode prevNode = TreeUtil.prevLeaf(leafElement);
        if (prevNode != null) {
            IElementType type = prevNode.getElementType();
            if (type == TokenType.WHITE_SPACE) {
                final String text = prevNode.getText();
                @NonNls final String cdataStartMarker = "<![CDATA[";
                final int cdataPos = text.indexOf(cdataStartMarker);
                if (cdataPos != -1 && whiteSpace.indexOf(cdataStartMarker) == -1) {
                    whiteSpace = mergeWsWithCdataMarker(whiteSpace, text, cdataPos);
                    if (whiteSpace == null)
                        return null;
                }
                prevNode = TreeUtil.prevLeaf(prevNode);
                type = prevNode != null ? prevNode.getElementType() : null;
            }
            @NonNls final String cdataEndMarker = "]]>";
            if (type == XmlTokenType.XML_CDATA_END && whiteSpace.indexOf(cdataEndMarker) == -1) {
                final ASTNode at = findElementAt(prevNode.getStartOffset());
                if (at != null && at.getPsi() instanceof PsiWhiteSpace) {
                    final String s = at.getText();
                    final int cdataEndPos = s.indexOf(cdataEndMarker);
                    whiteSpace = mergeWsWithCdataMarker(whiteSpace, s, cdataEndPos);
                    leafElement = at;
                } else {
                    whiteSpace = null;
                }
                if (whiteSpace == null)
                    return null;
            }
        }
    }
    FormatterUtil.replaceWhiteSpace(whiteSpace, leafElement, TokenType.WHITE_SPACE, textRange);
    return whiteSpace;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) NonNls(org.jetbrains.annotations.NonNls) ASTNode(com.intellij.lang.ASTNode) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Example 78 with PsiWhiteSpace

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

the class XmlBlock method createLeafBlocks.

private void createLeafBlocks(ASTNode node, List<Block> result) {
    if (node instanceof OuterLanguageElement) {
        processChild(result, node, null, null, null);
        return;
    }
    ASTNode child = node.getFirstChildNode();
    if (child == null && !(node instanceof PsiWhiteSpace) && node.getElementType() != TokenType.ERROR_ELEMENT && node.getTextLength() > 0) {
        result.add(new ReadOnlyBlock(node));
        return;
    }
    while (child != null) {
        createLeafBlocks(child, result);
        child = child.getTreeNext();
    }
}
Also used : OuterLanguageElement(com.intellij.psi.templateLanguages.OuterLanguageElement) ASTNode(com.intellij.lang.ASTNode) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Example 79 with PsiWhiteSpace

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

the class PyOverrideImplementUtil method getContextClass.

@Nullable
public static PyClass getContextClass(@NotNull final Editor editor, @NotNull final PsiFile file) {
    int offset = editor.getCaretModel().getOffset();
    PsiElement element = file.findElementAt(offset);
    if (element == null) {
        // are we in whitespace after last class? PY-440
        final PsiElement lastChild = file.getLastChild();
        if (lastChild != null && offset >= lastChild.getTextRange().getStartOffset() && offset <= lastChild.getTextRange().getEndOffset()) {
            element = lastChild;
        }
    }
    final PyClass pyClass = PsiTreeUtil.getParentOfType(element, PyClass.class, false);
    if (pyClass == null && element instanceof PsiWhiteSpace && element.getPrevSibling() instanceof PyClass) {
        return (PyClass) element.getPrevSibling();
    }
    return pyClass;
}
Also used : PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace) Nullable(org.jetbrains.annotations.Nullable)

Example 80 with PsiWhiteSpace

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

the class ConvertFormatOperatorToMethodIntention method collectWhitespace.

private static Pair<String, PsiElement> collectWhitespace(PsiElement start) {
    StringBuilder sb = new StringBuilder();
    PsiElement seeker = start;
    while (seeker != null) {
        seeker = seeker.getNextSibling();
        if (seeker != null && seeker instanceof PsiWhiteSpace)
            sb.append(seeker.getText());
        else
            break;
    }
    return Pair.create(sb.toString(), seeker);
}
Also used : 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