aboutsummaryrefslogtreecommitdiffstats
path: root/language-web/src/main/js/editor/semanticHighlighting.ts
diff options
context:
space:
mode:
Diffstat (limited to 'language-web/src/main/js/editor/semanticHighlighting.ts')
-rw-r--r--language-web/src/main/js/editor/semanticHighlighting.ts50
1 files changed, 20 insertions, 30 deletions
diff --git a/language-web/src/main/js/editor/semanticHighlighting.ts b/language-web/src/main/js/editor/semanticHighlighting.ts
index 2d6804f8..2aed421b 100644
--- a/language-web/src/main/js/editor/semanticHighlighting.ts
+++ b/language-web/src/main/js/editor/semanticHighlighting.ts
@@ -1,34 +1,24 @@
1import { StateEffect, StateField, TransactionSpec } from '@codemirror/state'; 1import { RangeSet } from '@codemirror/rangeset';
2import { EditorView, Decoration, DecorationSet } from '@codemirror/view'; 2import type { TransactionSpec } from '@codemirror/state';
3import { Decoration } from '@codemirror/view';
3 4
4const setSemanticHighlightingEffect = StateEffect.define<DecorationSet>(); 5import { decorationSetExtension } from './decorationSetExtension';
5 6
6export function setSemanticHighlighting(decorations: DecorationSet): TransactionSpec { 7export interface IHighlightRange {
7 return { 8 from: number;
8 effects: [ 9
9 setSemanticHighlightingEffect.of(decorations), 10 to: number;
10 ], 11
11 }; 12 classes: string[];
13}
14
15const [setSemanticHighlightingInternal, semanticHighlighting] = decorationSetExtension();
16
17export function setSemanticHighlighting(ranges: IHighlightRange[]): TransactionSpec {
18 const rangeSet = RangeSet.of(ranges.map(({ from, to, classes }) => Decoration.mark({
19 class: classes.map((c) => `cmt-problem-${c}`).join(' '),
20 }).range(from, to)), true);
21 return setSemanticHighlightingInternal(rangeSet);
12} 22}
13 23
14export const semanticHighlighting = StateField.define<DecorationSet>({ 24export { semanticHighlighting };
15 create() {
16 return Decoration.none;
17 },
18 update(currentDecorations, transaction) {
19 let newDecorations: DecorationSet | null = null;
20 transaction.effects.forEach((effect) => {
21 if (effect.is(setSemanticHighlightingEffect)) {
22 newDecorations = effect.value;
23 }
24 });
25 if (newDecorations === null) {
26 if (transaction.docChanged) {
27 return currentDecorations.map(transaction.changes);
28 }
29 return currentDecorations;
30 }
31 return newDecorations;
32 },
33 provide: (f) => EditorView.decorations.from(f),
34});