Search in sources :

Example 21 with HighlighterIterator

use of com.intellij.openapi.editor.highlighter.HighlighterIterator in project intellij-community by JetBrains.

the class EnterInStringLiteralHandler method isInStringLiteral.

private static boolean isInStringLiteral(@NotNull Editor editor, @NotNull DataContext dataContext, int offset) {
    Language language = EnterHandler.getLanguage(dataContext);
    if (offset > 0 && language != null) {
        QuoteHandler quoteHandler = TypedHandler.getLanguageQuoteHandler(language);
        if (quoteHandler == null) {
            FileType fileType = language.getAssociatedFileType();
            quoteHandler = fileType != null ? TypedHandler.getQuoteHandlerForType(fileType) : null;
        }
        if (quoteHandler != null) {
            EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter();
            HighlighterIterator iterator = highlighter.createIterator(offset - 1);
            return StringEscapesTokenTypes.STRING_LITERAL_ESCAPES.contains(iterator.getTokenType()) || quoteHandler.isInsideLiteral(iterator);
        }
    }
    return false;
}
Also used : EditorEx(com.intellij.openapi.editor.ex.EditorEx) Language(com.intellij.lang.Language) FileType(com.intellij.openapi.fileTypes.FileType) JavaLikeQuoteHandler(com.intellij.codeInsight.editorActions.JavaLikeQuoteHandler) QuoteHandler(com.intellij.codeInsight.editorActions.QuoteHandler) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter)

Example 22 with HighlighterIterator

use of com.intellij.openapi.editor.highlighter.HighlighterIterator in project intellij-community by JetBrains.

the class BraceHighlightingHandler method doHighlight.

private void doHighlight(int offset, int originalOffset, @NotNull FileType fileType) {
    if (myEditor.getFoldingModel().isOffsetCollapsed(offset))
        return;
    HighlighterIterator iterator = getEditorHighlighter().createIterator(offset);
    final CharSequence chars = myDocument.getCharsSequence();
    if (BraceMatchingUtil.isLBraceToken(iterator, chars, fileType)) {
        IElementType tokenType = iterator.getTokenType();
        iterator.advance();
        if (!iterator.atEnd() && BraceMatchingUtil.isRBraceToken(iterator, chars, fileType)) {
            if (BraceMatchingUtil.isPairBraces(tokenType, iterator.getTokenType(), fileType) && originalOffset == iterator.getStart())
                return;
        }
        iterator.retreat();
        highlightLeftBrace(iterator, false, fileType);
        if (offset > 0) {
            iterator = getEditorHighlighter().createIterator(offset - 1);
            if (BraceMatchingUtil.isRBraceToken(iterator, chars, fileType)) {
                highlightRightBrace(iterator, fileType);
            }
        }
    } else if (BraceMatchingUtil.isRBraceToken(iterator, chars, fileType)) {
        highlightRightBrace(iterator, fileType);
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator)

Example 23 with HighlighterIterator

use of com.intellij.openapi.editor.highlighter.HighlighterIterator in project intellij-community by JetBrains.

the class BraceHighlightingHandler method highlightScope.

private void highlightScope(int offset, @NotNull FileType fileType) {
    if (myEditor.getFoldingModel().isOffsetCollapsed(offset))
        return;
    if (myEditor.getDocument().getTextLength() <= offset)
        return;
    HighlighterIterator iterator = getEditorHighlighter().createIterator(offset);
    final CharSequence chars = myDocument.getCharsSequence();
    if (!BraceMatchingUtil.isStructuralBraceToken(fileType, iterator, chars)) {
    //      if (BraceMatchingUtil.isRBraceTokenToHighlight(myFileType, iterator) || BraceMatchingUtil.isLBraceTokenToHighlight(myFileType, iterator)) return;
    } else {
        if (BraceMatchingUtil.isRBraceToken(iterator, chars, fileType) || BraceMatchingUtil.isLBraceToken(iterator, chars, fileType))
            return;
    }
    if (!BraceMatchingUtil.findStructuralLeftBrace(fileType, iterator, chars)) {
        removeLineMarkers();
        return;
    }
    highlightLeftBrace(iterator, true, fileType);
}
Also used : HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator)

Example 24 with HighlighterIterator

use of com.intellij.openapi.editor.highlighter.HighlighterIterator in project intellij-community by JetBrains.

the class CustomFileTypeFoldingBuilder method buildBraceMatcherBasedFolding.

public static void buildBraceMatcherBasedFolding(List<FoldingDescriptor> descriptors, PsiElement root, Document document, SyntaxHighlighter highlighter) {
    LexerEditorHighlighter editorHighlighter = new LexerEditorHighlighter(highlighter, EditorColorsManager.getInstance().getGlobalScheme());
    editorHighlighter.setText(document.getText());
    FileType fileType = root.getContainingFile().getFileType();
    BraceMatcher braceMatcher = BraceMatchingUtil.getBraceMatcher(fileType, root.getLanguage());
    TextRange totalRange = root.getTextRange();
    final HighlighterIterator iterator = editorHighlighter.createIterator(totalRange.getStartOffset());
    final LinkedList<Trinity<Integer, Integer, IElementType>> stack = new LinkedList<>();
    String editorText = document.getText();
    while (!iterator.atEnd() && iterator.getStart() < totalRange.getEndOffset()) {
        final Trinity<Integer, Integer, IElementType> last;
        if (braceMatcher.isLBraceToken(iterator, editorText, fileType) && braceMatcher.isStructuralBrace(iterator, editorText, fileType)) {
            stack.addLast(Trinity.create(iterator.getStart(), iterator.getEnd(), iterator.getTokenType()));
        } else if (braceMatcher.isRBraceToken(iterator, editorText, fileType) && braceMatcher.isStructuralBrace(iterator, editorText, fileType) && !stack.isEmpty() && braceMatcher.isPairBraces((last = stack.getLast()).third, iterator.getTokenType())) {
            stack.removeLast();
            TextRange range = new TextRange(last.first, iterator.getEnd());
            if (StringUtil.countChars(document.getText(range), '\n') >= 3) {
                descriptors.add(new FoldingDescriptor(root, range));
            }
        }
        iterator.advance();
    }
}
Also used : FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) BraceMatcher(com.intellij.codeInsight.highlighting.BraceMatcher) Trinity(com.intellij.openapi.util.Trinity) TextRange(com.intellij.openapi.util.TextRange) LexerEditorHighlighter(com.intellij.openapi.editor.ex.util.LexerEditorHighlighter) LinkedList(java.util.LinkedList) IElementType(com.intellij.psi.tree.IElementType) FileType(com.intellij.openapi.fileTypes.FileType) CustomSyntaxTableFileType(com.intellij.openapi.fileTypes.impl.CustomSyntaxTableFileType) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator)

Example 25 with HighlighterIterator

use of com.intellij.openapi.editor.highlighter.HighlighterIterator in project intellij-community by JetBrains.

the class CustomFileTypeSelectWordHandler method select.

@Override
public List<TextRange> select(PsiElement e, CharSequence editorText, int cursorOffset, Editor editor) {
    List<TextRange> superResult = super.select(e, editorText, cursorOffset, editor);
    HighlighterIterator iterator = ((EditorEx) editor).getHighlighter().createIterator(cursorOffset);
    if (CustomFileTypeQuoteHandler.isQuotedToken(iterator.getTokenType())) {
        List<TextRange> result = ContainerUtil.newArrayList();
        int start = iterator.getStart();
        int end = iterator.getEnd();
        if (end - start > 2) {
            result.add(new TextRange(start + 1, end - 1));
        }
        result.add(new TextRange(start, end));
        if (superResult != null) {
            result.addAll(superResult);
        }
        return result;
    }
    return superResult;
}
Also used : TextRange(com.intellij.openapi.util.TextRange) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator)

Aggregations

HighlighterIterator (com.intellij.openapi.editor.highlighter.HighlighterIterator)83 EditorHighlighter (com.intellij.openapi.editor.highlighter.EditorHighlighter)36 EditorEx (com.intellij.openapi.editor.ex.EditorEx)33 IElementType (com.intellij.psi.tree.IElementType)33 Document (com.intellij.openapi.editor.Document)12 FileType (com.intellij.openapi.fileTypes.FileType)12 BraceMatcher (com.intellij.codeInsight.highlighting.BraceMatcher)9 TextRange (com.intellij.openapi.util.TextRange)8 Editor (com.intellij.openapi.editor.Editor)7 PsiElement (com.intellij.psi.PsiElement)6 TextAttributes (com.intellij.openapi.editor.markup.TextAttributes)5 PsiFile (com.intellij.psi.PsiFile)5 Language (com.intellij.lang.Language)4 CaretModel (com.intellij.openapi.editor.CaretModel)4 LexerEditorHighlighter (com.intellij.openapi.editor.ex.util.LexerEditorHighlighter)4 Project (com.intellij.openapi.project.Project)4 NontrivialBraceMatcher (com.intellij.codeInsight.highlighting.NontrivialBraceMatcher)3 Trinity (com.intellij.openapi.util.Trinity)3 ArrayList (java.util.ArrayList)3 LineMarkerInfo (com.intellij.codeInsight.daemon.LineMarkerInfo)2