Search in sources :

Example 66 with PsiWhiteSpace

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

the class PsiTreeDebugBuilder method psiToBuffer.

private void psiToBuffer(PsiElement root, int indent, boolean showRanges, boolean showChildrenRanges) {
    if (!myShowWhiteSpaces && root instanceof PsiWhiteSpace)
        return;
    if (!myShowErrorElements && root instanceof PsiErrorElement)
        return;
    for (int i = 0; i < indent; i++) {
        myBuffer.append(' ');
    }
    final String rootStr = root.toString();
    myBuffer.append(rootStr);
    PsiElement child = root.getFirstChild();
    if (child == null) {
        String text = root.getText();
        assert text != null : "text is null for <" + root + ">";
        text = StringUtil.replace(text, "\n", "\\n");
        text = StringUtil.replace(text, "\r", "\\r");
        text = StringUtil.replace(text, "\t", "\\t");
        myBuffer.append("('");
        myBuffer.append(text);
        myBuffer.append("')");
    }
    if (showRanges)
        myBuffer.append(root.getTextRange());
    myBuffer.append("\n");
    while (child != null) {
        psiToBuffer(child, indent + 2, showChildrenRanges, showChildrenRanges);
        child = child.getNextSibling();
    }
}
Also used : PsiErrorElement(com.intellij.psi.PsiErrorElement) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Example 67 with PsiWhiteSpace

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

the class UpdateJavaScriptFileCopyright method scanFile.

protected void scanFile() {
    PsiElement first = getFile().getFirstChild();
    if (first != null) {
        final PsiElement child = first.getFirstChild();
        if (child instanceof PsiComment) {
            first = child;
        }
    }
    PsiElement last = first;
    PsiElement next = first;
    while (next != null) {
        if (next instanceof PsiComment || next instanceof PsiWhiteSpace) {
            next = getNextSibling(next);
        } else {
            break;
        }
        last = next;
    }
    if (first != null) {
        checkComments(first, last, true);
    } else {
        checkComments(null, null, true);
    }
}
Also used : PsiComment(com.intellij.psi.PsiComment) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Example 68 with PsiWhiteSpace

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

the class UpdateJspFileCopyright method scanFile.

protected void scanFile() {
    logger.debug("updating " + getFile().getVirtualFile());
    XmlDocument doc = ((XmlFile) getFile()).getDocument();
    XmlTag root = doc.getRootTag();
    if (root == null) {
        return;
    }
    PsiElement elem = root.getFirstChild();
    PsiElement docTypeStart = null;
    PsiElement docTypeEnd = null;
    PsiElement firstTag = null;
    while (elem != null) {
        if (elem instanceof XmlToken) {
            if ("<!DOCTYPE".equals(elem.getText())) {
                docTypeStart = elem;
                while ((elem = getNextSibling(elem)) != null) {
                    if (elem instanceof PsiWhiteSpace)
                        continue;
                    if (elem instanceof XmlToken) {
                        if (elem.getText().endsWith(">")) {
                            elem = getNextSibling(elem);
                            docTypeEnd = elem;
                            break;
                        } else if (elem.getText().startsWith("<")) {
                            docTypeEnd = elem;
                            break;
                        }
                    } else {
                        break;
                    }
                }
                continue;
            } else {
                firstTag = elem;
                break;
            }
        } else if (elem instanceof XmlTag && !(elem instanceof JspDirective)) {
            firstTag = elem;
            break;
        }
        elem = getNextSibling(elem);
    }
    PsiElement first = root.getFirstChild();
    int location = getLanguageOptions().getFileLocation();
    if (docTypeStart != null) {
        checkComments(first, docTypeStart, location == XmlOptions.LOCATION_BEFORE_DOCTYPE);
        first = docTypeEnd;
    } else if (location == XmlOptions.LOCATION_BEFORE_DOCTYPE) {
        location = XmlOptions.LOCATION_BEFORE_ROOTTAG;
    }
    if (firstTag != null) {
        checkComments(first, firstTag, location == XmlOptions.LOCATION_BEFORE_ROOTTAG);
    } else if (location == XmlOptions.LOCATION_BEFORE_ROOTTAG) {
        // If we get here we have an empty file
        checkComments(first, first, true);
    }
}
Also used : JspDirective(com.intellij.psi.impl.source.jsp.jspXml.JspDirective) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Example 69 with PsiWhiteSpace

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

the class JsonSmartEnterProcessor method terminatedOnCurrentLine.

private static boolean terminatedOnCurrentLine(@NotNull Editor editor, @NotNull PsiElement element) {
    final Document document = editor.getDocument();
    final int caretOffset = editor.getCaretModel().getCurrentCaret().getOffset();
    final int elementEndOffset = element.getTextRange().getEndOffset();
    if (document.getLineNumber(elementEndOffset) != document.getLineNumber(caretOffset)) {
        return false;
    }
    // Skip empty PsiError elements if comma is missing
    PsiElement nextLeaf = PsiTreeUtil.nextLeaf(element, true);
    return nextLeaf == null || (nextLeaf instanceof PsiWhiteSpace && nextLeaf.getText().contains("\n"));
}
Also used : Document(com.intellij.openapi.editor.Document) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Example 70 with PsiWhiteSpace

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

the class JsonSurroundDescriptor method getElementsToSurround.

@NotNull
@Override
public PsiElement[] getElementsToSurround(PsiFile file, int startOffset, int endOffset) {
    PsiElement firstElement = file.findElementAt(startOffset);
    PsiElement lastElement = file.findElementAt(endOffset - 1);
    // Extend selection beyond possible delimiters
    while (firstElement != null && (firstElement instanceof PsiWhiteSpace || firstElement.getNode().getElementType() == JsonElementTypes.COMMA)) {
        firstElement = firstElement.getNextSibling();
    }
    while (lastElement != null && (lastElement instanceof PsiWhiteSpace || lastElement.getNode().getElementType() == JsonElementTypes.COMMA)) {
        lastElement = lastElement.getPrevSibling();
    }
    if (firstElement != null) {
        startOffset = firstElement.getTextRange().getStartOffset();
    }
    if (lastElement != null) {
        endOffset = lastElement.getTextRange().getEndOffset();
    }
    final JsonElement property = PsiTreeUtil.findElementOfClassAtRange(file, startOffset, endOffset, JsonProperty.class);
    if (property != null) {
        final List<JsonElement> properties = ContainerUtil.newArrayList(property);
        PsiElement nextSibling = property.getNextSibling();
        while (nextSibling != null && nextSibling.getTextRange().getEndOffset() <= endOffset) {
            if (nextSibling instanceof JsonProperty) {
                properties.add((JsonProperty) nextSibling);
            }
            nextSibling = nextSibling.getNextSibling();
        }
        return properties.toArray(new PsiElement[properties.size()]);
    }
    final JsonValue value = PsiTreeUtil.findElementOfClassAtRange(file, startOffset, endOffset, JsonValue.class);
    if (value != null) {
        return new PsiElement[] { value };
    }
    return PsiElement.EMPTY_ARRAY;
}
Also used : JsonProperty(com.intellij.json.psi.JsonProperty) JsonElement(com.intellij.json.psi.JsonElement) JsonValue(com.intellij.json.psi.JsonValue) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace) NotNull(org.jetbrains.annotations.NotNull)

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