diff options
Diffstat (limited to 'language-web/src/main/js/editor/semanticHighlighting.ts')
-rw-r--r-- | language-web/src/main/js/editor/semanticHighlighting.ts | 50 |
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 @@ | |||
1 | import { StateEffect, StateField, TransactionSpec } from '@codemirror/state'; | 1 | import { RangeSet } from '@codemirror/rangeset'; |
2 | import { EditorView, Decoration, DecorationSet } from '@codemirror/view'; | 2 | import type { TransactionSpec } from '@codemirror/state'; |
3 | import { Decoration } from '@codemirror/view'; | ||
3 | 4 | ||
4 | const setSemanticHighlightingEffect = StateEffect.define<DecorationSet>(); | 5 | import { decorationSetExtension } from './decorationSetExtension'; |
5 | 6 | ||
6 | export function setSemanticHighlighting(decorations: DecorationSet): TransactionSpec { | 7 | export 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 | |||
15 | const [setSemanticHighlightingInternal, semanticHighlighting] = decorationSetExtension(); | ||
16 | |||
17 | export 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 | ||
14 | export const semanticHighlighting = StateField.define<DecorationSet>({ | 24 | export { 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 | }); | ||