Search in sources :

Example 1 with XmlProlog

use of com.intellij.psi.xml.XmlProlog 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)

Example 2 with XmlProlog

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

the class UpdateJspxFileCopyright method scanFile.

protected void scanFile() {
    logger.debug("updating " + getFile().getVirtualFile());
    XmlDocument doc = ((XmlFile) getFile()).getDocument();
    XmlProlog xmlProlog = doc.getProlog();
    if (xmlProlog == null) {
        return;
    }
    PsiElement elem = xmlProlog.getFirstChild();
    PsiElement docTypeStart = null;
    while (elem != null) {
        if (elem instanceof XmlDoctype) {
            docTypeStart = elem;
            break;
        }
        elem = getNextSibling(elem);
    }
    PsiElement first = xmlProlog.getFirstChild();
    int location = getLanguageOptions().getFileLocation();
    if (docTypeStart != null) {
        final ArrayList<PsiComment> comments = new ArrayList<>();
        collectComments(doc.getFirstChild(), xmlProlog, comments);
        collectComments(first, docTypeStart, comments);
        checkComments(first, location == XmlOptions.LOCATION_BEFORE_DOCTYPE, comments);
        checkComments(docTypeStart, doc.getRootTag(), location == XmlOptions.LOCATION_BEFORE_ROOTTAG);
        return;
    } else if (location == XmlOptions.LOCATION_BEFORE_DOCTYPE) {
        location = XmlOptions.LOCATION_BEFORE_ROOTTAG;
    }
    final ArrayList<PsiComment> comments = new ArrayList<>();
    collectComments(doc.getFirstChild(), xmlProlog, comments);
    collectComments(first, doc.getRootTag(), comments);
    checkComments(doc.getRootTag(), location == XmlOptions.LOCATION_BEFORE_ROOTTAG, comments);
}
Also used : PsiComment(com.intellij.psi.PsiComment) XmlFile(com.intellij.psi.xml.XmlFile) XmlDoctype(com.intellij.psi.xml.XmlDoctype) ArrayList(java.util.ArrayList) XmlDocument(com.intellij.psi.xml.XmlDocument) XmlProlog(com.intellij.psi.xml.XmlProlog) PsiElement(com.intellij.psi.PsiElement)

Example 3 with XmlProlog

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

the class GenerateDTDAction method getHandler.

@Override
@NotNull
protected CodeInsightActionHandler getHandler() {
    return new CodeInsightActionHandler() {

        @Override
        public void invoke(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) {
            final XmlDocument document = findSuitableXmlDocument(file);
            if (document != null) {
                @NonNls final StringBuilder buffer = new StringBuilder();
                buffer.append("<!DOCTYPE " + document.getRootTag().getName() + " [\n");
                buffer.append(XmlUtil.generateDocumentDTD(document, true));
                buffer.append("]>\n");
                XmlFile tempFile;
                try {
                    final XmlProlog prolog = document.getProlog();
                    final PsiElement childOfType = PsiTreeUtil.getChildOfType(prolog, XmlProcessingInstruction.class);
                    if (childOfType != null) {
                        final String text = childOfType.getText();
                        buffer.insert(0, text);
                        final PsiElement nextSibling = childOfType.getNextSibling();
                        if (nextSibling instanceof PsiWhiteSpace) {
                            buffer.insert(text.length(), nextSibling.getText());
                        }
                    }
                    tempFile = (XmlFile) PsiFileFactory.getInstance(file.getProject()).createFileFromText("dummy.xml", buffer.toString());
                    prolog.replace(tempFile.getDocument().getProlog());
                } catch (IncorrectOperationException e) {
                    LOG.error(e);
                }
            }
        }
    };
}
Also used : NonNls(org.jetbrains.annotations.NonNls) XmlFile(com.intellij.psi.xml.XmlFile) CodeInsightActionHandler(com.intellij.codeInsight.CodeInsightActionHandler) XmlDocument(com.intellij.psi.xml.XmlDocument) XmlProlog(com.intellij.psi.xml.XmlProlog) NotNull(org.jetbrains.annotations.NotNull) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Editor(com.intellij.openapi.editor.Editor) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PsiElement (com.intellij.psi.PsiElement)3 XmlProlog (com.intellij.psi.xml.XmlProlog)3 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)2 XmlDocument (com.intellij.psi.xml.XmlDocument)2 XmlFile (com.intellij.psi.xml.XmlFile)2 CodeInsightActionHandler (com.intellij.codeInsight.CodeInsightActionHandler)1 Editor (com.intellij.openapi.editor.Editor)1 Project (com.intellij.openapi.project.Project)1 PsiComment (com.intellij.psi.PsiComment)1 PsiFile (com.intellij.psi.PsiFile)1 XmlComment (com.intellij.psi.xml.XmlComment)1 XmlDoctype (com.intellij.psi.xml.XmlDoctype)1 XmlTag (com.intellij.psi.xml.XmlTag)1 XmlText (com.intellij.psi.xml.XmlText)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 ArrayList (java.util.ArrayList)1 NonNls (org.jetbrains.annotations.NonNls)1 NotNull (org.jetbrains.annotations.NotNull)1