Search in sources :

Example 56 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 57 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 58 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 59 with PsiWhiteSpace

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

the class InspectionUtil method isSuppressedAt.

private static boolean isSuppressedAt(PsiElement anchor, LocalInspectionTool tool) {
    if (anchor == null)
        return false;
    PsiElement prevSibling;
    if (!(anchor instanceof XmlComment)) {
        prevSibling = anchor.getPrevSibling();
        while (prevSibling instanceof PsiWhiteSpace || prevSibling instanceof XmlText) {
            prevSibling = prevSibling.getPrevSibling();
        }
    } else {
        prevSibling = anchor;
    }
    if (prevSibling instanceof XmlProlog) {
        if (prevSibling.getTextLength() > 0 && !"\n".equals(prevSibling.getText())) {
            return isSuppressedAt(prevSibling.getLastChild(), tool);
        } else {
            return isSuppressedAt(prevSibling, tool);
        }
    }
    if (prevSibling instanceof XmlComment) {
        final XmlComment comment = (XmlComment) prevSibling;
        final String text = XmlUtil.getCommentText(comment);
        if (text != null) {
            final Matcher matcher = SUPPRESSION_PATTERN.matcher(text);
            if (matcher.matches()) {
                final String[] strings = matcher.group(1).split(",");
                final String toolId = tool.getID();
                for (String s : strings) {
                    if (s.trim().equals(toolId) || ALL_ID.equals(s.trim()))
                        return true;
                }
            }
        }
    }
    return false;
}
Also used : Matcher(java.util.regex.Matcher) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Example 60 with PsiWhiteSpace

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

the class SuppressInspectionAction method invoke.

public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {
    final XmlTag anchor = getAnchor(element);
    if (anchor == null)
        return;
    PsiElement prevSibling = anchor.getPrevSibling();
    while (prevSibling instanceof PsiWhiteSpace || prevSibling instanceof XmlText) {
        prevSibling = prevSibling.getPrevSibling();
    }
    if (prevSibling instanceof XmlProlog) {
        prevSibling = prevSibling.getLastChild();
        if (prevSibling != null && !(prevSibling instanceof XmlComment)) {
            prevSibling = PsiTreeUtil.getPrevSiblingOfType(prevSibling, XmlComment.class);
        }
    }
    if (prevSibling instanceof XmlComment) {
        final XmlComment comment = (XmlComment) prevSibling;
        final String text = XmlUtil.getCommentText(comment);
        if (text != null && InspectionUtil.SUPPRESSION_PATTERN.matcher(text).matches()) {
            final String s = text.trim() + ", " + myToolId;
            final XmlComment newComment = createComment(project, s);
            CodeStyleManager.getInstance(PsiManager.getInstance(project).getProject()).reformat(comment.replace(newComment));
        } else {
            addNoinspectionComment(project, anchor);
        }
    } else {
        addNoinspectionComment(project, anchor);
    }
}
Also used : XmlComment(com.intellij.psi.xml.XmlComment) XmlText(com.intellij.psi.xml.XmlText) XmlProlog(com.intellij.psi.xml.XmlProlog) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag) 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