Search in sources :

Example 16 with Commenter

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

the class CommentByLineCommentHandler method getBlockSuitableCommenter.

private static Commenter getBlockSuitableCommenter(final PsiFile file, int offset, int endOffset) {
    final Language languageSuitableForCompleteFragment;
    if (offset >= endOffset) {
        // we are on empty line
        PsiElement element = file.findElementAt(offset);
        if (element != null)
            languageSuitableForCompleteFragment = element.getParent().getLanguage();
        else
            languageSuitableForCompleteFragment = null;
    } else {
        languageSuitableForCompleteFragment = PsiUtilBase.reallyEvaluateLanguageInRange(offset, endOffset, file);
    }
    Commenter blockSuitableCommenter = languageSuitableForCompleteFragment == null ? LanguageCommenters.INSTANCE.forLanguage(file.getLanguage()) : null;
    if (blockSuitableCommenter == null && file.getFileType() instanceof CustomSyntaxTableFileType) {
        blockSuitableCommenter = new Commenter() {

            final SyntaxTable mySyntaxTable = ((CustomSyntaxTableFileType) file.getFileType()).getSyntaxTable();

            @Override
            @Nullable
            public String getLineCommentPrefix() {
                return mySyntaxTable.getLineComment();
            }

            @Override
            @Nullable
            public String getBlockCommentPrefix() {
                return mySyntaxTable.getStartComment();
            }

            @Override
            @Nullable
            public String getBlockCommentSuffix() {
                return mySyntaxTable.getEndComment();
            }

            @Override
            public String getCommentedBlockCommentPrefix() {
                return null;
            }

            @Override
            public String getCommentedBlockCommentSuffix() {
                return null;
            }
        };
    }
    return blockSuitableCommenter;
}
Also used : SyntaxTable(com.intellij.ide.highlighter.custom.SyntaxTable) Language(com.intellij.lang.Language) Commenter(com.intellij.lang.Commenter) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable) CustomSyntaxTableFileType(com.intellij.openapi.fileTypes.impl.CustomSyntaxTableFileType)

Example 17 with Commenter

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

the class CommentByLineCommentHandler method uncommentLine.

private static void uncommentLine(Block block, int line, boolean removeSpace) {
    Document document = block.editor.getDocument();
    Commenter commenter = block.commenters[line - block.startLine];
    if (commenter == null)
        commenter = findCommenter(block.editor, block.psiFile, line);
    if (commenter == null)
        return;
    final int startOffset = block.startOffsets[line - block.startLine];
    if (commenter instanceof SelfManagingCommenter) {
        final SelfManagingCommenter selfManagingCommenter = (SelfManagingCommenter) commenter;
        selfManagingCommenter.uncommentLine(line, startOffset, document, block.commenterStateMap.get(selfManagingCommenter));
        return;
    }
    final int endOffset = block.endOffsets[line - block.startLine];
    if (startOffset == endOffset) {
        return;
    }
    RangeMarker marker = endOffset > startOffset ? block.editor.getDocument().createRangeMarker(startOffset, endOffset) : null;
    try {
        if (doUncommentLine(line, document, commenter, startOffset, endOffset, removeSpace))
            return;
        if (marker != null) {
            CommentByBlockCommentHandler.processDocument(document, marker, commenter, false);
        }
    } finally {
        if (marker != null) {
            marker.dispose();
        }
    }
}
Also used : Commenter(com.intellij.lang.Commenter)

Example 18 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 19 with Commenter

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

the class BlockMarkerCommentsInspection method buildVisitor.

@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, final boolean isOnTheFly) {
    return new PsiElementVisitor() {

        @Override
        public void visitComment(final PsiComment element) {
            final IElementType tokenType = element.getTokenType();
            if (!(tokenType.equals(JavaTokenType.END_OF_LINE_COMMENT))) {
                return;
            }
            final Commenter commenter = LanguageCommenters.INSTANCE.forLanguage(element.getLanguage());
            String rawCommentText = element.getText();
            final String prefix = commenter.getLineCommentPrefix();
            if (prefix != null && rawCommentText.startsWith(prefix)) {
                rawCommentText = rawCommentText.substring(prefix.length());
            }
            final String commentText = rawCommentText.trim().toLowerCase();
            if (!commentText.startsWith(END_WORD) || StringUtil.split(commentText, " ").size() > 3) {
                return;
            }
            if (MARKER_PATTERN.accepts(element)) {
                holder.registerProblem(element, "Redundant block marker", new LocalQuickFix() {

                    @NotNull
                    @Override
                    public String getFamilyName() {
                        return "Remove block marker comments";
                    }

                    @Override
                    public void applyFix(@NotNull final Project project, @NotNull final ProblemDescriptor descriptor) {
                        descriptor.getPsiElement().delete();
                    }
                });
            }
        }
    };
}
Also used : IElementType(com.intellij.psi.tree.IElementType) Project(com.intellij.openapi.project.Project) Commenter(com.intellij.lang.Commenter) NotNull(org.jetbrains.annotations.NotNull) NotNull(org.jetbrains.annotations.NotNull)

Example 20 with Commenter

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

the class PsiParserFacadeImpl method createBlockCommentFromText.

@NotNull
@Override
public PsiComment createBlockCommentFromText(@NotNull Language language, @NotNull String text) throws IncorrectOperationException {
    Commenter commenter = LanguageCommenters.INSTANCE.forLanguage(language);
    assert commenter != null : language;
    final String blockCommentPrefix = commenter.getBlockCommentPrefix();
    final String blockCommentSuffix = commenter.getBlockCommentSuffix();
    PsiFile aFile = PsiFileFactory.getInstance(myManager.getProject()).createFileFromText("_Dummy_", language, (blockCommentPrefix + text + blockCommentSuffix));
    return findPsiCommentChild(aFile);
}
Also used : Commenter(com.intellij.lang.Commenter) NotNull(org.jetbrains.annotations.NotNull)

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