Search in sources :

Example 1 with CustomFileTypeLexer

use of com.intellij.ide.highlighter.custom.CustomFileTypeLexer 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 CustomFileTypeLexer

use of com.intellij.ide.highlighter.custom.CustomFileTypeLexer in project intellij-community by JetBrains.

the class CustomFileTypeTokenizer method tokenize.

@Override
public void tokenize(@NotNull PsiElement element, TokenConsumer consumer) {
    CustomFileTypeLexer lexer = new CustomFileTypeLexer(mySyntaxTable);
    String text = element.getText();
    lexer.start(text);
    while (true) {
        IElementType tokenType = lexer.getTokenType();
        if (tokenType == null) {
            break;
        }
        if (!isKeyword(tokenType)) {
            consumer.consumeToken(element, text, false, 0, new TextRange(lexer.getTokenStart(), lexer.getTokenEnd()), PlainTextSplitter.getInstance());
        }
        lexer.advance();
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) CustomFileTypeLexer(com.intellij.ide.highlighter.custom.CustomFileTypeLexer) TextRange(com.intellij.openapi.util.TextRange)

Aggregations

CustomFileTypeLexer (com.intellij.ide.highlighter.custom.CustomFileTypeLexer)2 TextRange (com.intellij.openapi.util.TextRange)2 CustomUncommenter (com.intellij.lang.CustomUncommenter)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 IElementType (com.intellij.psi.tree.IElementType)1 Nullable (org.jetbrains.annotations.Nullable)1