blob: 04690cc6fa205db3a5549d9daa5559358491ac4c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
/*
* Copyright 2000-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.usages.impl;
import com.intellij.lexer.LayeredLexer;
import com.intellij.lexer.Lexer;
import com.intellij.openapi.editor.colors.TextAttributesKey;
import com.intellij.openapi.editor.ex.util.LayeredHighlighterIterator;
import com.intellij.openapi.editor.ex.util.LayeredLexerEditorHighlighter;
import com.intellij.openapi.editor.highlighter.EditorHighlighter;
import com.intellij.openapi.editor.highlighter.EditorHighlighterFactory;
import com.intellij.openapi.editor.highlighter.HighlighterIterator;
import com.intellij.openapi.fileTypes.PlainSyntaxHighlighter;
import com.intellij.openapi.fileTypes.PlainTextFileType;
import com.intellij.openapi.fileTypes.SyntaxHighlighter;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.impl.search.LexerEditorHighlighterLexer;
import com.intellij.psi.tree.IElementType;
import org.jetbrains.annotations.NotNull;
/**
* Created by Maxim.Mossienko on 7/31/2014.
*/
public class SyntaxHighlighterOverEditorHighlighter implements SyntaxHighlighter {
private final Lexer lexer;
private LayeredHighlighterIterator layeredHighlighterIterator = null;
private final SyntaxHighlighter highlighter;
public SyntaxHighlighterOverEditorHighlighter(SyntaxHighlighter _highlighter, VirtualFile file, Project project) {
if (file.getFileType() == PlainTextFileType.INSTANCE) { // optimization for large files, PlainTextSyntaxHighlighterFactory is slow
highlighter = new PlainSyntaxHighlighter();
lexer = highlighter.getHighlightingLexer();
} else {
highlighter = _highlighter;
LayeredLexer.ourDisableLayersFlag.set(Boolean.TRUE);
EditorHighlighter editorHighlighter = EditorHighlighterFactory.getInstance().createEditorHighlighter(project, file);
try {
if (editorHighlighter instanceof LayeredLexerEditorHighlighter) {
lexer = new LexerEditorHighlighterLexer(editorHighlighter, false);
}
else {
lexer = highlighter.getHighlightingLexer();
}
}
finally {
LayeredLexer.ourDisableLayersFlag.set(null);
}
}
}
@NotNull
@Override
public Lexer getHighlightingLexer() {
return lexer;
}
@NotNull
@Override
public TextAttributesKey[] getTokenHighlights(IElementType tokenType) {
final SyntaxHighlighter activeSyntaxHighlighter =
layeredHighlighterIterator != null ? layeredHighlighterIterator.getActiveSyntaxHighlighter() : highlighter;
return activeSyntaxHighlighter.getTokenHighlights(tokenType);
}
public void restart(@NotNull CharSequence text) {
lexer.start(text);
if (lexer instanceof LexerEditorHighlighterLexer) {
HighlighterIterator iterator = ((LexerEditorHighlighterLexer)lexer).getHighlighterIterator();
if (iterator instanceof LayeredHighlighterIterator) {
layeredHighlighterIterator = (LayeredHighlighterIterator)iterator;
} else {
layeredHighlighterIterator = null;
}
}
}
public void resetPosition(int startOffset) {
if (lexer instanceof LexerEditorHighlighterLexer) {
((LexerEditorHighlighterLexer)lexer).resetPosition(startOffset);
HighlighterIterator iterator = ((LexerEditorHighlighterLexer)lexer).getHighlighterIterator();
if (iterator instanceof LayeredHighlighterIterator) {
layeredHighlighterIterator = (LayeredHighlighterIterator)iterator;
} else {
layeredHighlighterIterator = null;
}
} else {
CharSequence text = lexer.getBufferSequence();
lexer.start(text, startOffset, text.length());
}
}
}
|