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;
}
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();
}
}
Aggregations