Search in sources :

Example 21 with Commenter

use of com.intellij.lang.Commenter in project intellij-community by JetBrains.

the class CommentByBlockCommentAction method isValidFor.

@Override
protected boolean isValidFor(@NotNull Project project, @NotNull Editor editor, @NotNull Caret caret, @NotNull final PsiFile file) {
    final FileType fileType = file.getFileType();
    if (fileType instanceof AbstractFileType) {
        return ((AbstractFileType) fileType).getCommenter() != null;
    }
    Commenter commenter = LanguageCommenters.INSTANCE.forLanguage(file.getLanguage());
    if (commenter == null)
        commenter = LanguageCommenters.INSTANCE.forLanguage(file.getViewProvider().getBaseLanguage());
    if (commenter == null)
        return false;
    return commenter.getBlockCommentPrefix() != null && commenter.getBlockCommentSuffix() != null;
}
Also used : AbstractFileType(com.intellij.openapi.fileTypes.impl.AbstractFileType) FileType(com.intellij.openapi.fileTypes.FileType) AbstractFileType(com.intellij.openapi.fileTypes.impl.AbstractFileType) Commenter(com.intellij.lang.Commenter)

Example 22 with Commenter

use of com.intellij.lang.Commenter in project intellij-community by JetBrains.

the class XmlDeclareIdInCommentAction method applyFix.

@Override
public void applyFix(@NotNull final Project project, @NotNull final ProblemDescriptor descriptor) {
    final PsiElement psiElement = descriptor.getPsiElement();
    final PsiFile psiFile = psiElement.getContainingFile();
    new WriteCommandAction(project, psiFile) {

        @Override
        protected void run(@NotNull final Result result) throws Throwable {
            final XmlTag tag = PsiTreeUtil.getParentOfType(psiElement, XmlTag.class);
            if (tag == null)
                return;
            final Language language = psiFile.getViewProvider().getBaseLanguage();
            final Commenter commenter = LanguageCommenters.INSTANCE.forLanguage(language);
            if (commenter == null)
                return;
            final PsiFile tempFile = PsiFileFactory.getInstance(project).createFileFromText("dummy", language.getAssociatedFileType(), commenter.getBlockCommentPrefix() + "@declare id=\"" + myId + "\"" + commenter.getBlockCommentSuffix() + "\n");
            final XmlTag parent = tag.getParentTag();
            if (parent != null && parent.isValid()) {
                final XmlTag[] tags = parent.getSubTags();
                if (tags.length > 0) {
                    final PsiFile psi = tempFile.getViewProvider().getPsi(language);
                    if (psi != null) {
                        final PsiElement element = psi.findElementAt(1);
                        if (element instanceof PsiComment) {
                            parent.getNode().addChild(element.getNode(), tags[0].getNode());
                        }
                    }
                }
            }
        }
    }.execute();
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) PsiComment(com.intellij.psi.PsiComment) Language(com.intellij.lang.Language) Commenter(com.intellij.lang.Commenter) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) Result(com.intellij.openapi.application.Result) XmlTag(com.intellij.psi.xml.XmlTag)

Aggregations

Commenter (com.intellij.lang.Commenter)22 NotNull (org.jetbrains.annotations.NotNull)6 Language (com.intellij.lang.Language)4 PsiFile (com.intellij.psi.PsiFile)4 Nullable (org.jetbrains.annotations.Nullable)3 CodeDocumentationAwareCommenter (com.intellij.lang.CodeDocumentationAwareCommenter)2 Project (com.intellij.openapi.project.Project)2 TextRange (com.intellij.openapi.util.TextRange)2 PsiComment (com.intellij.psi.PsiComment)2 PsiElement (com.intellij.psi.PsiElement)2 IncorrectOperationException (com.intellij.util.IncorrectOperationException)2 SyntaxTable (com.intellij.ide.highlighter.custom.SyntaxTable)1 InjectedCaret (com.intellij.injected.editor.InjectedCaret)1 Result (com.intellij.openapi.application.Result)1 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)1 FileType (com.intellij.openapi.fileTypes.FileType)1 AbstractFileType (com.intellij.openapi.fileTypes.impl.AbstractFileType)1 CustomSyntaxTableFileType (com.intellij.openapi.fileTypes.impl.CustomSyntaxTableFileType)1 IElementType (com.intellij.psi.tree.IElementType)1 XmlTag (com.intellij.psi.xml.XmlTag)1