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/decorationSetExtension.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/decorationSetExtension.ts')
-rw-r--r-- | language-web/src/main/js/editor/decorationSetExtension.ts | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/language-web/src/main/js/editor/decorationSetExtension.ts b/language-web/src/main/js/editor/decorationSetExtension.ts new file mode 100644 index 00000000..2d630c20 --- /dev/null +++ b/language-web/src/main/js/editor/decorationSetExtension.ts | |||
@@ -0,0 +1,39 @@ | |||
1 | import { StateEffect, StateField, TransactionSpec } from '@codemirror/state'; | ||
2 | import { EditorView, Decoration, DecorationSet } from '@codemirror/view'; | ||
3 | |||
4 | export type TransactionSpecFactory = (decorations: DecorationSet) => TransactionSpec; | ||
5 | |||
6 | export function decorationSetExtension(): [TransactionSpecFactory, StateField<DecorationSet>] { | ||
7 | const setEffect = StateEffect.define<DecorationSet>(); | ||
8 | const field = StateField.define<DecorationSet>({ | ||
9 | create() { | ||
10 | return Decoration.none; | ||
11 | }, | ||
12 | update(currentDecorations, transaction) { | ||
13 | let newDecorations: DecorationSet | null = null; | ||
14 | transaction.effects.forEach((effect) => { | ||
15 | if (effect.is(setEffect)) { | ||
16 | newDecorations = effect.value; | ||
17 | } | ||
18 | }); | ||
19 | if (newDecorations === null) { | ||
20 | if (transaction.docChanged) { | ||
21 | return currentDecorations.map(transaction.changes); | ||
22 | } | ||
23 | return currentDecorations; | ||
24 | } | ||
25 | return newDecorations; | ||
26 | }, | ||
27 | provide: (f) => EditorView.decorations.from(f), | ||
28 | }); | ||
29 | |||
30 | function transactionSpecFactory(decorations: DecorationSet) { | ||
31 | return { | ||
32 | effects: [ | ||
33 | setEffect.of(decorations), | ||
34 | ], | ||
35 | }; | ||
36 | } | ||
37 | |||
38 | return [transactionSpecFactory, field]; | ||
39 | } | ||