diff options
author | Kristóf Marussy <kristof@marussy.com> | 2021-10-30 21:56:42 +0200 |
---|---|---|
committer | Kristóf Marussy <kristof@marussy.com> | 2021-10-31 19:26:15 +0100 |
commit | 8c90023676fae675e89b1a20c7fc95c63fc1dd5a (patch) | |
tree | cef3c3f6716026d1e9998fec8ca9171c6fe478af /language-web/src/main/js/editor/findOccurrences.ts | |
parent | feat(web): use 4 space for indentation (diff) | |
download | refinery-8c90023676fae675e89b1a20c7fc95c63fc1dd5a.tar.gz refinery-8c90023676fae675e89b1a20c7fc95c63fc1dd5a.tar.zst refinery-8c90023676fae675e89b1a20c7fc95c63fc1dd5a.zip |
feat(web): find occurrences when idle
Diffstat (limited to 'language-web/src/main/js/editor/findOccurrences.ts')
-rw-r--r-- | language-web/src/main/js/editor/findOccurrences.ts | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/language-web/src/main/js/editor/findOccurrences.ts b/language-web/src/main/js/editor/findOccurrences.ts new file mode 100644 index 00000000..92102746 --- /dev/null +++ b/language-web/src/main/js/editor/findOccurrences.ts | |||
@@ -0,0 +1,35 @@ | |||
1 | import { Range, RangeSet } from '@codemirror/rangeset'; | ||
2 | import type { TransactionSpec } from '@codemirror/state'; | ||
3 | import { Decoration } from '@codemirror/view'; | ||
4 | |||
5 | import { decorationSetExtension } from './decorationSetExtension'; | ||
6 | |||
7 | export interface IOccurrence { | ||
8 | from: number; | ||
9 | |||
10 | to: number; | ||
11 | } | ||
12 | |||
13 | const [setOccurrencesInteral, findOccurrences] = decorationSetExtension(); | ||
14 | |||
15 | const writeDecoration = Decoration.mark({ | ||
16 | class: 'cm-problem-write', | ||
17 | }); | ||
18 | |||
19 | const readDecoration = Decoration.mark({ | ||
20 | class: 'cm-problem-read', | ||
21 | }); | ||
22 | |||
23 | export function setOccurrences(write: IOccurrence[], read: IOccurrence[]): TransactionSpec { | ||
24 | const decorations: Range<Decoration>[] = []; | ||
25 | write.forEach(({ from, to }) => { | ||
26 | decorations.push(writeDecoration.range(from, to)); | ||
27 | }); | ||
28 | read.forEach(({ from, to }) => { | ||
29 | decorations.push(readDecoration.range(from, to)); | ||
30 | }); | ||
31 | const rangeSet = RangeSet.of(decorations, true); | ||
32 | return setOccurrencesInteral(rangeSet); | ||
33 | } | ||
34 | |||
35 | export { findOccurrences }; | ||