Search in sources :

Example 1 with XmlComment

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

the class SuppressInspectionAction method createComment.

@NotNull
private static XmlComment createComment(Project project, String s) throws IncorrectOperationException {
    final XmlTag element = XmlElementFactory.getInstance(project).createTagFromText("<foo><!-- " + s + " --></foo>", XMLLanguage.INSTANCE);
    final XmlComment newComment = PsiTreeUtil.getChildOfType(element, XmlComment.class);
    assert newComment != null;
    return newComment;
}
Also used : XmlComment(com.intellij.psi.xml.XmlComment) XmlTag(com.intellij.psi.xml.XmlTag) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with XmlComment

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

the class SuppressInspectionAction method addNoinspectionComment.

private void addNoinspectionComment(Project project, XmlTag anchor) throws IncorrectOperationException {
    final XmlComment newComment = createComment(project, "noinspection " + myToolId);
    PsiElement parent = anchor.getParentTag();
    if (parent == null) {
        parent = PsiTreeUtil.getPrevSiblingOfType(anchor, XmlProlog.class);
        if (parent != null) {
            CodeStyleManager.getInstance(PsiManager.getInstance(project).getProject()).reformat(parent.add(newComment));
        }
    } else {
        CodeStyleManager.getInstance(PsiManager.getInstance(project).getProject()).reformat(parent.addBefore(newComment, anchor));
    }
}
Also used : XmlComment(com.intellij.psi.xml.XmlComment) XmlProlog(com.intellij.psi.xml.XmlProlog) PsiElement(com.intellij.psi.PsiElement)

Example 3 with XmlComment

use of com.intellij.psi.xml.XmlComment in project intellij-plugins by JetBrains.

the class MxmlEnterHandler method preprocessEnter.

public Result preprocessEnter(@NotNull PsiFile file, @NotNull Editor editor, @NotNull Ref<Integer> caretOffset, @NotNull Ref<Integer> caretAdvance, @NotNull DataContext dataContext, EditorActionHandler originalHandler) {
    int offset = caretOffset.get().intValue();
    if (file instanceof JSFile) {
        PsiElement context = InjectedLanguageManager.getInstance(file.getProject()).getInjectionHost(file);
        if (context instanceof XmlComment) {
            file = context.getContainingFile();
            editor = ((EditorWindow) editor).getDelegate();
            offset = editor.getCaretModel().getOffset();
        }
    }
    if (!JavaScriptSupportLoader.isFlexMxmFile(file))
        return Result.Continue;
    if (CodeInsightSettings.getInstance().INSERT_BRACE_ON_ENTER && isAfterUnmatchedMxmlComment(editor, file, offset)) {
        String indent = "";
        CharSequence buffer = editor.getDocument().getCharsSequence();
        int lineStart = CharArrayUtil.shiftBackwardUntil(buffer, offset - 1, "\n") + 1;
        int current = lineStart;
        while (current < offset && Character.isWhitespace(buffer.charAt(current))) ++current;
        if (current > lineStart) {
            indent = buffer.subSequence(lineStart, current).toString();
        }
        editor.getDocument().insertString(offset, "\n" + indent + "-->");
        originalHandler.execute(editor, dataContext);
        return Result.Stop;
    }
    return Result.Continue;
}
Also used : XmlComment(com.intellij.psi.xml.XmlComment) JSFile(com.intellij.lang.javascript.psi.JSFile) PsiElement(com.intellij.psi.PsiElement)

Example 4 with XmlComment

use of com.intellij.psi.xml.XmlComment in project android by JetBrains.

the class AttributeDefinitionsImpl method getCommentBeforeEatComment.

@Nullable
private static String getCommentBeforeEatComment(XmlTag tag) {
    PsiElement comment = XmlDocumentationProvider.findPreviousComment(tag);
    for (int i = 0; i < 5; ++i) {
        if (comment == null) {
            break;
        }
        String value = StringUtil.trim(XmlUtil.getCommentText((XmlComment) comment));
        //  <!-- ============== -->
        if (!StringUtil.isEmpty(value) && value.charAt(0) != '*' && value.charAt(0) != '=') {
            return value;
        }
        comment = XmlDocumentationProvider.findPreviousComment(comment.getPrevSibling());
    }
    return null;
}
Also used : XmlComment(com.intellij.psi.xml.XmlComment) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with XmlComment

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

XmlComment (com.intellij.psi.xml.XmlComment)6 PsiElement (com.intellij.psi.PsiElement)4 XmlProlog (com.intellij.psi.xml.XmlProlog)2 XmlTag (com.intellij.psi.xml.XmlTag)2 Nullable (org.jetbrains.annotations.Nullable)2 ASTNode (com.intellij.lang.ASTNode)1 JSFile (com.intellij.lang.javascript.psi.JSFile)1 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)1 XmlText (com.intellij.psi.xml.XmlText)1 NotNull (org.jetbrains.annotations.NotNull)1