forked from jupyter-lsp/jupyterlab-lsp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyntax_highlighting.ts
159 lines (135 loc) · 4.77 KB
/
syntax_highlighting.ts
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import {
JupyterFrontEnd,
JupyterFrontEndPlugin
} from '@jupyterlab/application';
import {
IEditorMimeTypeService,
IEditorServices
} from '@jupyterlab/codeeditor';
import { CodeMirrorEditor, ICodeMirror } from '@jupyterlab/codemirror';
import { ISettingRegistry } from '@jupyterlab/settingregistry';
import { ITranslator } from '@jupyterlab/translation';
import { LabIcon } from '@jupyterlab/ui-components';
import syntaxSvg from '../../style/icons/syntax-highlight.svg';
import { CodeSyntax as LSPSyntaxHighlightingSettings } from '../_syntax_highlighting';
import { CodeMirrorIntegration } from '../editor_integration/codemirror';
import {
FeatureSettings,
IEditorIntegrationOptions,
IFeatureLabIntegration,
IFeatureSettings
} from '../feature';
import { ILSPFeatureManager, PLUGIN_ID } from '../tokens';
export const syntaxHighlightingIcon = new LabIcon({
name: 'lsp:syntax-highlighting',
svgstr: syntaxSvg
});
const FEATURE_ID = PLUGIN_ID + ':syntax_highlighting';
export class CMSyntaxHighlighting extends CodeMirrorIntegration {
editors_with_active_highlight: Set<CodeMirrorEditor>;
constructor(options: IEditorIntegrationOptions) {
super(options);
this.virtual_document.changed.connect(this.update_mode.bind(this), this);
this.editors_with_active_highlight = new Set();
}
get lab_integration() {
return super.lab_integration as SyntaxLabIntegration;
}
get settings() {
return super.settings as IFeatureSettings<LSPSyntaxHighlightingSettings>;
}
private get_mode(language: string) {
let mimetype = this.lab_integration.mimeTypeService.getMimeTypeByLanguage({
name: language
});
if (!mimetype || mimetype == 'text/plain') {
// if a mimetype cannot be found it will be 'text/plain', therefore do
// not change mode to text/plain, as this could be a step backwards for
// the user experience
return;
}
return this.lab_integration.codeMirror.CodeMirror.findModeByMIME(mimetype);
}
update_mode() {
let root = this.virtual_document;
let editors_with_current_highlight = new Set<CodeMirrorEditor>();
for (let map of root.foreign_document_maps) {
for (let [range, block] of map.entries()) {
let ce_editor = block.editor as CodeMirrorEditor;
let editor = ce_editor.editor;
let lines = editor.getValue('\n');
let total_area = lines.concat('').length;
let covered_area =
ce_editor.getOffsetAt(range.end) - ce_editor.getOffsetAt(range.start);
let coverage = covered_area / total_area;
let language = block.virtual_document.language;
let mode = this.get_mode(language);
// if not highlighting mode available, skip this editor
if (typeof mode === 'undefined') {
continue;
}
// change the mode if the majority of the code is the foreign code
if (coverage > this.settings.composite.foreignCodeThreshold) {
editors_with_current_highlight.add(ce_editor);
let old_mode = editor.getOption('mode');
if (old_mode != mode.mime) {
editor.setOption('mode', mode.mime);
}
}
}
}
if (editors_with_current_highlight != this.editors_with_active_highlight) {
for (let ce_editor of this.editors_with_active_highlight) {
if (!editors_with_current_highlight.has(ce_editor)) {
ce_editor.editor.setOption('mode', ce_editor.model.mimeType);
}
}
}
this.editors_with_active_highlight = editors_with_current_highlight;
}
}
class SyntaxLabIntegration implements IFeatureLabIntegration {
// TODO: we could accept custom mimetype mapping from settings
settings: IFeatureSettings<LSPSyntaxHighlightingSettings>;
constructor(
public mimeTypeService: IEditorMimeTypeService,
public codeMirror: ICodeMirror
) {}
}
export const SYNTAX_HIGHLIGHTING_PLUGIN: JupyterFrontEndPlugin<void> = {
id: FEATURE_ID,
requires: [
ILSPFeatureManager,
IEditorServices,
ISettingRegistry,
ICodeMirror,
ITranslator
],
autoStart: true,
activate: (
app: JupyterFrontEnd,
featureManager: ILSPFeatureManager,
editorServices: IEditorServices,
settingRegistry: ISettingRegistry,
codeMirror: ICodeMirror,
translator: ITranslator
) => {
const settings = new FeatureSettings(settingRegistry, FEATURE_ID);
const trans = translator.load('jupyterlab-lsp');
featureManager.register({
feature: {
editorIntegrationFactory: new Map([
['CodeMirrorEditor', CMSyntaxHighlighting]
]),
commands: [],
id: FEATURE_ID,
name: trans.__('Syntax highlighting'),
labIntegration: new SyntaxLabIntegration(
editorServices.mimeTypeService,
codeMirror
),
settings: settings
}
});
}
};