Search in sources :

Example 1 with HighlighterIteratorWrapper

use of com.intellij.openapi.editor.ex.util.HighlighterIteratorWrapper in project intellij-community by JetBrains.

the class BraceHighlightingHandler method getLazyParsableHighlighterIfAny.

@NotNull
static EditorHighlighter getLazyParsableHighlighterIfAny(Project project, Editor editor, PsiFile psiFile) {
    if (!PsiDocumentManager.getInstance(project).isCommitted(editor.getDocument())) {
        return ((EditorEx) editor).getHighlighter();
    }
    PsiElement elementAt = psiFile.findElementAt(editor.getCaretModel().getOffset());
    for (PsiElement e : SyntaxTraverser.psiApi().parents(elementAt).takeWhile(Conditions.notEqualTo(psiFile))) {
        if (!(PsiUtilCore.getElementType(e) instanceof ILazyParseableElementType))
            continue;
        Language language = ILazyParseableElementType.LANGUAGE_KEY.get(e.getNode());
        if (language == null)
            continue;
        TextRange range = e.getTextRange();
        final int offset = range.getStartOffset();
        SyntaxHighlighter syntaxHighlighter = SyntaxHighlighterFactory.getSyntaxHighlighter(language, project, psiFile.getVirtualFile());
        LexerEditorHighlighter highlighter = new LexerEditorHighlighter(syntaxHighlighter, editor.getColorsScheme()) {

            @NotNull
            @Override
            public HighlighterIterator createIterator(int startOffset) {
                return new HighlighterIteratorWrapper(super.createIterator(Math.max(startOffset - offset, 0))) {

                    @Override
                    public int getStart() {
                        return super.getStart() + offset;
                    }

                    @Override
                    public int getEnd() {
                        return super.getEnd() + offset;
                    }
                };
            }
        };
        highlighter.setText(editor.getDocument().getText(range));
        return highlighter;
    }
    return ((EditorEx) editor).getHighlighter();
}
Also used : EditorEx(com.intellij.openapi.editor.ex.EditorEx) HighlighterIteratorWrapper(com.intellij.openapi.editor.ex.util.HighlighterIteratorWrapper) Language(com.intellij.lang.Language) ILazyParseableElementType(com.intellij.psi.tree.ILazyParseableElementType) TextRange(com.intellij.openapi.util.TextRange) SyntaxHighlighter(com.intellij.openapi.fileTypes.SyntaxHighlighter) LexerEditorHighlighter(com.intellij.openapi.editor.ex.util.LexerEditorHighlighter) LightweightHint(com.intellij.ui.LightweightHint) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

Language (com.intellij.lang.Language)1 EditorEx (com.intellij.openapi.editor.ex.EditorEx)1 HighlighterIteratorWrapper (com.intellij.openapi.editor.ex.util.HighlighterIteratorWrapper)1 LexerEditorHighlighter (com.intellij.openapi.editor.ex.util.LexerEditorHighlighter)1 SyntaxHighlighter (com.intellij.openapi.fileTypes.SyntaxHighlighter)1 TextRange (com.intellij.openapi.util.TextRange)1 ILazyParseableElementType (com.intellij.psi.tree.ILazyParseableElementType)1 LightweightHint (com.intellij.ui.LightweightHint)1 NotNull (org.jetbrains.annotations.NotNull)1