-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathCm6_ViewPlugin.ts
More file actions
301 lines (257 loc) · 8.6 KB
/
Cm6_ViewPlugin.ts
File metadata and controls
301 lines (257 loc) · 8.6 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import type ShikiPlugin from 'src/main';
import { SHIKI_INLINE_REGEX } from 'src/main';
import { Decoration, type DecorationSet, type EditorView, ViewPlugin, type ViewUpdate } from '@codemirror/view';
import { type EditorState, type Range } from '@codemirror/state';
import { type SyntaxNode } from '@lezer/common';
import { syntaxTree } from '@codemirror/language';
import { Cm6_Util } from 'src/codemirror/Cm6_Util';
import { type ThemedToken } from 'shiki';
import { editorLivePreviewField } from 'obsidian';
enum DecorationUpdateType {
Insert,
Remove,
}
type DecorationUpdate = InsertDecoration | RemoveDecoration;
interface InsertDecoration {
type: DecorationUpdateType.Insert;
from: number;
to: number;
lang: string;
content: string;
hideLang?: boolean;
hideTo?: number;
}
interface RemoveDecoration {
type: DecorationUpdateType.Remove;
from: number;
to: number;
}
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export function createCm6Plugin(plugin: ShikiPlugin) {
return ViewPlugin.fromClass(
class Cm6ViewPlugin {
decorations: DecorationSet;
view: EditorView;
constructor(view: EditorView) {
this.view = view;
this.decorations = Decoration.none;
void this.updateWidgets(view);
plugin.updateCm6Plugin = (): Promise<void> => {
return this.updateWidgets(this.view);
};
}
/**
* Triggered by codemirror when the view updates.
* Depending on the update type, the decorations are either updated or recreated.
*
* @param update
*/
update(update: ViewUpdate): void {
try {
this.decorations = this.decorations.map(update.changes);
} catch (e) {
// Decorations may have stale positions if the document changed while an async
// updateWidgets call was in flight. Reset them so the next update can rebuild.
this.decorations = Decoration.none;
console.warn('Resetting decorations due to error:', e);
}
// we handle doc changes and selection changes here
if (update.docChanged || update.selectionSet) {
this.view = update.view;
void this.updateWidgets(update.view, update.docChanged);
}
}
isLivePreview(state: EditorState): boolean {
// @ts-ignore some strange private field not being assignable
return state.field(editorLivePreviewField);
}
/**
* Updates all the widgets by traversing the syntax tree.
*
* @param view
* @param docChanged
*/
async updateWidgets(view: EditorView, docChanged: boolean = true): Promise<void> {
let lang = '';
let state: SyntaxNode[] = [];
const decorationUpdates: DecorationUpdate[] = [];
// Capture the state at the time of the syntax tree traversal so we can
// detect if the document changed while async decoration building was in flight.
const capturedState = view.state;
// const t1 = performance.now();
syntaxTree(view.state).iterate({
enter: nodeRef => {
const node = nodeRef.node;
const props: Set<string> = new Set<string>(node.type.name?.split('_'));
if (props.has('formatting')) {
return;
}
if (props.has('inline-code')) {
const content = Cm6_Util.getContent(view.state, node.from, node.to);
if (content.startsWith('{') && plugin.settings.inlineHighlighting) {
const match = content.match(SHIKI_INLINE_REGEX); // format: `{lang} code`
if (match) {
const hasSelectionOverlap = Cm6_Util.checkSelectionAndRangeOverlap(view.state.selection, node.from - 1, node.to + 1);
decorationUpdates.push({
type: DecorationUpdateType.Insert,
from: node.from,
to: node.to,
lang: match[1],
content: match[2],
hideLang: this.isLivePreview(view.state) && !hasSelectionOverlap,
hideTo: node.from + match[1].length + 3, // hide `{lang} `
});
}
} else {
// we don't want to highlight normal inline code blocks, thus we remove any of our decorations
// we could check if we even have any decorations at this node, but it's not necessary
this.removeDecoration(node.from, node.to);
}
return;
}
// if !docChanged, then this change was a selection change.
// We only care about inline code blocks in this case, so we can skip the rest.
if (!docChanged) {
return;
}
if (props.has('HyperMD-codeblock') && !props.has('HyperMD-codeblock-begin') && !props.has('HyperMD-codeblock-end')) {
state.push(node);
return;
}
if (props.has('HyperMD-codeblock-begin')) {
const content = Cm6_Util.getContent(view.state, node.from, node.to);
lang = /^```\s*(\S+)/.exec(content)?.[1] ?? '';
}
if (props.has('HyperMD-codeblock-end')) {
if (state.length > 0 && lang !== '') {
const start = state[0].from;
const end = state[state.length - 1].to;
decorationUpdates.push({
type: DecorationUpdateType.Insert,
from: start,
to: end,
lang,
content: Cm6_Util.getContent(view.state, start, end),
});
}
if (state.length > 0 && lang === '') {
const start = state[0].from;
const end = state[state.length - 1].to;
decorationUpdates.push({
type: DecorationUpdateType.Remove,
from: start,
to: end,
});
}
lang = '';
state = [];
}
},
});
for (const node of decorationUpdates) {
try {
if (node.type === DecorationUpdateType.Remove) {
this.removeDecoration(node.from, node.to);
} else if (node.type === DecorationUpdateType.Insert) {
const decorations = await this.buildDecorations(node.hideTo ?? node.from, node.to, node.lang, node.content);
// If the document changed while we were awaiting, the positions we captured
// from the syntax tree are stale. Abort to avoid applying out-of-range decorations.
if (this.view.state !== capturedState) {
return;
}
this.removeDecoration(node.from, node.to);
if (node.hideLang) {
// add the decoration that hides the language tag
decorations.unshift(Decoration.replace({}).range(node.from, node.hideTo));
}
// add the highlight decorations
this.addDecoration(node.from, node.to, decorations);
}
} catch (e) {
console.error(e);
}
}
if (decorationUpdates.length > 0) {
this.view.dispatch(this.view.state.update({}));
}
// console.log('Traversed syntax tree in', performance.now() - t1, 'ms');
}
/**
* Removes all decorations at a given node.
*
* @param from
* @param to
*/
removeDecoration(from: number, to: number): void {
this.decorations = this.decorations.update({
filterFrom: from,
filterTo: to,
filter: (_from3, _to3, _decoration) => {
return false;
},
});
}
/**
* Adds a widget at a given node if it does not exist yet.
*
* @param from
* @param to
* @param newDecorations
*/
addDecoration(from: number, to: number, newDecorations: Range<Decoration>[]): void {
// check if the decoration already exists and only add it if it does not exist
if (Cm6_Util.existsDecorationBetween(this.decorations, from, to)) {
return;
}
if (newDecorations.length === 0) {
return;
}
this.decorations = this.decorations.update({
add: newDecorations,
});
}
/**
* Builds mark decorations for a given range, laguage and content.
*
* @param from
* @param to
* @param language
* @param content
*/
async buildDecorations(from: number, to: number, language: string, content: string): Promise<Range<Decoration>[]> {
if (language === '') {
return [];
}
const highlight = await plugin.highlighter.getHighlightTokens(content, language.toLowerCase());
if (!highlight) {
return [];
}
const tokens = highlight.tokens.flat(1);
const decorations: Range<Decoration>[] = [];
for (let i = 0; i < tokens.length; i++) {
const token = tokens[i];
const nextToken: ThemedToken | undefined = tokens[i + 1];
const tokenStyle = plugin.highlighter.getTokenStyle(token);
decorations.push(
Decoration.mark({
attributes: {
style: tokenStyle.style,
class: tokenStyle.classes.join(' '),
},
}).range(from + token.offset, nextToken ? from + nextToken.offset : to),
);
}
return decorations;
}
/**
* Triggered by codemirror when the view plugin is destroyed.
*/
destroy(): void {
this.decorations = Decoration.none;
}
},
{
decorations: v => v.decorations,
},
);
}