aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/frontend/src/editor/semanticHighlighting.ts
blob: a5d0af7a0741ab57f084fa212c2da1a21fac7358 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import { RangeSet, type TransactionSpec } from '@codemirror/state';
import { Decoration } from '@codemirror/view';

import { decorationSetExtension } from './decorationSetExtension';

export interface IHighlightRange {
  from: number;

  to: number;

  classes: string[];
}

const [setSemanticHighlightingInternal, semanticHighlighting] = decorationSetExtension();

export function setSemanticHighlighting(ranges: IHighlightRange[]): TransactionSpec {
  const rangeSet = RangeSet.of(ranges.map(({ from, to, classes }) => Decoration.mark({
    class: classes.map((c) => `tok-problem-${c}`).join(' '),
  }).range(from, to)), true);
  return setSemanticHighlightingInternal(rangeSet);
}

export { semanticHighlighting };