aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/frontend/src/editor/semanticHighlighting.ts
blob: 1f2e564c193b4e297442f480ebe2d4698f8928ed (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
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*
 * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */

import { RangeSet, type TransactionSpec } from '@codemirror/state';
import { Decoration } from '@codemirror/view';

import defineDecorationSetExtension from './defineDecorationSetExtension';

export interface IHighlightRange {
  from: number;

  to: number;

  classes: string[];
}

const [setSemanticHighlightingInternal, semanticHighlighting] =
  defineDecorationSetExtension();

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 default semanticHighlighting;