Search in sources :

Example 1 with CustomUncommenter

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

the class CommentByBlockCommentHandler method findCommentedRange.

@Nullable
private TextRange findCommentedRange(final Commenter commenter) {
    final CharSequence text = myDocument.getCharsSequence();
    final FileType fileType = myFile.getFileType();
    if (fileType instanceof CustomSyntaxTableFileType) {
        Lexer lexer = new CustomFileTypeLexer(((CustomSyntaxTableFileType) fileType).getSyntaxTable());
        final int caretOffset = myCaret.getOffset();
        int commentStart = CharArrayUtil.lastIndexOf(text, commenter.getBlockCommentPrefix(), caretOffset);
        if (commentStart == -1)
            return null;
        lexer.start(text, commentStart, text.length());
        if (lexer.getTokenType() == CustomHighlighterTokenType.MULTI_LINE_COMMENT && lexer.getTokenEnd() >= caretOffset) {
            return new TextRange(commentStart, lexer.getTokenEnd());
        }
        return null;
    }
    final String prefix;
    final String suffix;
    // Custom uncommenter is able to find commented block inside of selected text
    final String selectedText = myCaret.getSelectedText();
    if ((commenter instanceof CustomUncommenter) && selectedText != null) {
        final TextRange commentedRange = ((CustomUncommenter) commenter).findMaximumCommentedRange(selectedText);
        if (commentedRange == null) {
            return null;
        }
        // Uncommenter returns range relative to text start, so we need to shift it to make abosolute.
        return commentedRange.shiftRight(myCaret.getSelectionStart());
    }
    if (commenter instanceof SelfManagingCommenter) {
        SelfManagingCommenter selfManagingCommenter = (SelfManagingCommenter) commenter;
        prefix = selfManagingCommenter.getBlockCommentPrefix(myCaret.getSelectionStart(), myDocument, mySelfManagedCommenterData);
        suffix = selfManagingCommenter.getBlockCommentSuffix(myCaret.getSelectionEnd(), myDocument, mySelfManagedCommenterData);
    } else {
        prefix = trim(commenter.getBlockCommentPrefix());
        suffix = trim(commenter.getBlockCommentSuffix());
    }
    if (prefix == null || suffix == null)
        return null;
    TextRange commentedRange;
    if (commenter instanceof SelfManagingCommenter) {
        commentedRange = ((SelfManagingCommenter) commenter).getBlockCommentRange(myCaret.getSelectionStart(), myCaret.getSelectionEnd(), myDocument, mySelfManagedCommenterData);
    } else {
        if (!testSelectionForNonComments()) {
            return null;
        }
        commentedRange = getSelectedComments(text, prefix, suffix);
    }
    if (commentedRange == null) {
        PsiElement comment = findCommentAtCaret();
        if (comment != null) {
            String commentText = comment.getText();
            if (commentText.startsWith(prefix) && commentText.endsWith(suffix)) {
                commentedRange = comment.getTextRange();
            }
        }
    }
    return commentedRange;
}
Also used : CustomFileTypeLexer(com.intellij.ide.highlighter.custom.CustomFileTypeLexer) Lexer(com.intellij.lexer.Lexer) CustomFileTypeLexer(com.intellij.ide.highlighter.custom.CustomFileTypeLexer) FileType(com.intellij.openapi.fileTypes.FileType) AbstractFileType(com.intellij.openapi.fileTypes.impl.AbstractFileType) CustomSyntaxTableFileType(com.intellij.openapi.fileTypes.impl.CustomSyntaxTableFileType) CustomUncommenter(com.intellij.lang.CustomUncommenter) TextRange(com.intellij.openapi.util.TextRange) CustomSyntaxTableFileType(com.intellij.openapi.fileTypes.impl.CustomSyntaxTableFileType) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with CustomUncommenter

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

the class CommentByBlockCommentHandler method uncommentRange.

public void uncommentRange(TextRange range, String commentPrefix, String commentSuffix, Commenter commenter) {
    if (commenter instanceof SelfManagingCommenter) {
        final SelfManagingCommenter selfManagingCommenter = (SelfManagingCommenter) commenter;
        selfManagingCommenter.uncommentBlockComment(range.getStartOffset(), range.getEndOffset(), myDocument, mySelfManagedCommenterData);
        return;
    }
    String text = myDocument.getCharsSequence().subSequence(range.getStartOffset(), range.getEndOffset()).toString();
    int startOffset = range.getStartOffset();
    //boolean endsProperly = CharArrayUtil.regionMatches(chars, range.getEndOffset() - commentSuffix.length(), commentSuffix);
    List<Couple<TextRange>> ranges = new ArrayList<>();
    if (commenter instanceof CustomUncommenter) {
        /*
        In case of custom uncommenter, we need to ask it for list of [commentOpen-start,commentOpen-end], [commentClose-start,commentClose-end]
        and shift if according to current offset
       */
        CustomUncommenter customUncommenter = (CustomUncommenter) commenter;
        for (Couple<TextRange> coupleFromCommenter : customUncommenter.getCommentRangesToDelete(text)) {
            TextRange openComment = coupleFromCommenter.first.shiftRight(startOffset);
            TextRange closeComment = coupleFromCommenter.second.shiftRight(startOffset);
            ranges.add(Couple.of(openComment, closeComment));
        }
    } else {
        // If commenter is not custom, we need to get this list by our selves
        int position = 0;
        while (true) {
            int start = getNearest(text, commentPrefix, position);
            if (start == text.length()) {
                break;
            }
            position = start;
            int end = getNearest(text, commentSuffix, position + commentPrefix.length()) + commentSuffix.length();
            position = end;
            Couple<TextRange> pair = findCommentBlock(new TextRange(start + startOffset, end + startOffset), commentPrefix, commentSuffix);
            ranges.add(pair);
        }
    }
    RangeMarker marker = myDocument.createRangeMarker(range);
    try {
        for (int i = ranges.size() - 1; i >= 0; i--) {
            Couple<TextRange> toDelete = ranges.get(i);
            myDocument.deleteString(toDelete.first.getStartOffset(), toDelete.first.getEndOffset());
            int shift = toDelete.first.getEndOffset() - toDelete.first.getStartOffset();
            myDocument.deleteString(toDelete.second.getStartOffset() - shift, toDelete.second.getEndOffset() - shift);
            if (commenter.getCommentedBlockCommentPrefix() != null) {
                commentNestedComments(myDocument, new TextRange(toDelete.first.getEndOffset() - shift, toDelete.second.getStartOffset() - shift), commenter);
            }
        }
        processDocument(myDocument, marker, commenter, false);
    } finally {
        marker.dispose();
    }
}
Also used : CustomUncommenter(com.intellij.lang.CustomUncommenter) Couple(com.intellij.openapi.util.Couple) IntArrayList(com.intellij.util.containers.IntArrayList) ArrayList(java.util.ArrayList) TextRange(com.intellij.openapi.util.TextRange)

Aggregations

CustomUncommenter (com.intellij.lang.CustomUncommenter)2 TextRange (com.intellij.openapi.util.TextRange)2 CustomFileTypeLexer (com.intellij.ide.highlighter.custom.CustomFileTypeLexer)1 Lexer (com.intellij.lexer.Lexer)1 FileType (com.intellij.openapi.fileTypes.FileType)1 AbstractFileType (com.intellij.openapi.fileTypes.impl.AbstractFileType)1 CustomSyntaxTableFileType (com.intellij.openapi.fileTypes.impl.CustomSyntaxTableFileType)1 Couple (com.intellij.openapi.util.Couple)1 IntArrayList (com.intellij.util.containers.IntArrayList)1 ArrayList (java.util.ArrayList)1 Nullable (org.jetbrains.annotations.Nullable)1