diff options
author | 2021-10-30 21:56:42 +0200 | |
---|---|---|
committer | 2021-10-31 19:26:15 +0100 | |
commit | 8c90023676fae675e89b1a20c7fc95c63fc1dd5a (patch) | |
tree | cef3c3f6716026d1e9998fec8ca9171c6fe478af /language-web/src/main/js/xtext/OccurrencesService.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/xtext/OccurrencesService.ts')
-rw-r--r-- | language-web/src/main/js/xtext/OccurrencesService.ts | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/language-web/src/main/js/xtext/OccurrencesService.ts b/language-web/src/main/js/xtext/OccurrencesService.ts new file mode 100644 index 00000000..804f5ba2 --- /dev/null +++ b/language-web/src/main/js/xtext/OccurrencesService.ts | |||
@@ -0,0 +1,116 @@ | |||
1 | import { Transaction } from '@codemirror/state'; | ||
2 | |||
3 | import type { EditorStore } from '../editor/EditorStore'; | ||
4 | import type { IOccurrence } from '../editor/findOccurrences'; | ||
5 | import type { UpdateService } from './UpdateService'; | ||
6 | import { getLogger } from '../utils/logger'; | ||
7 | import { Timer } from '../utils/Timer'; | ||
8 | import { XtextWebSocketClient } from './XtextWebSocketClient'; | ||
9 | import { | ||
10 | isOccurrencesResult, | ||
11 | isServiceConflictResult, | ||
12 | ITextRegion, | ||
13 | } from './xtextServiceResults'; | ||
14 | |||
15 | const FIND_OCCURRENCES_TIMEOUT_MS = 1000; | ||
16 | |||
17 | // Must clear occurrences asynchronously from `onTransaction`, | ||
18 | // because we must not emit a conflicting transaction when handling the pending transaction. | ||
19 | const CLEAR_OCCURRENCES_TIMEOUT_MS = 10; | ||
20 | |||
21 | const log = getLogger('xtext.OccurrencesService'); | ||
22 | |||
23 | function transformOccurrences(regions: ITextRegion[]): IOccurrence[] { | ||
24 | const occurrences: IOccurrence[] = []; | ||
25 | regions.forEach(({ offset, length }) => { | ||
26 | if (length > 0) { | ||
27 | occurrences.push({ | ||
28 | from: offset, | ||
29 | to: offset + length, | ||
30 | }); | ||
31 | } | ||
32 | }); | ||
33 | return occurrences; | ||
34 | } | ||
35 | |||
36 | export class OccurrencesService { | ||
37 | private store: EditorStore; | ||
38 | |||
39 | private webSocketClient: XtextWebSocketClient; | ||
40 | |||
41 | private updateService: UpdateService; | ||
42 | |||
43 | private hasOccurrences = false; | ||
44 | |||
45 | private findOccurrencesTimer = new Timer(() => { | ||
46 | this.handleFindOccurrences(); | ||
47 | }, FIND_OCCURRENCES_TIMEOUT_MS); | ||
48 | |||
49 | private clearOccurrencesTimer = new Timer(() => { | ||
50 | this.clearOccurrences(); | ||
51 | }, CLEAR_OCCURRENCES_TIMEOUT_MS); | ||
52 | |||
53 | constructor( | ||
54 | store: EditorStore, | ||
55 | webSocketClient: XtextWebSocketClient, | ||
56 | updateService: UpdateService, | ||
57 | ) { | ||
58 | this.store = store; | ||
59 | this.webSocketClient = webSocketClient; | ||
60 | this.updateService = updateService; | ||
61 | } | ||
62 | |||
63 | onTransaction(transaction: Transaction): void { | ||
64 | if (transaction.docChanged) { | ||
65 | this.clearOccurrencesTimer.schedule(); | ||
66 | this.findOccurrencesTimer.reschedule(); | ||
67 | } | ||
68 | if (transaction.isUserEvent('select')) { | ||
69 | this.findOccurrencesTimer.reschedule(); | ||
70 | } | ||
71 | } | ||
72 | |||
73 | private handleFindOccurrences() { | ||
74 | this.clearOccurrencesTimer.cancel(); | ||
75 | this.updateOccurrences().catch((error) => { | ||
76 | log.error('Unexpected error while updating occurrences', error); | ||
77 | this.clearOccurrences(); | ||
78 | }); | ||
79 | } | ||
80 | |||
81 | private async updateOccurrences() { | ||
82 | await this.updateService.update(); | ||
83 | const result = await this.webSocketClient.send({ | ||
84 | resource: this.updateService.resourceName, | ||
85 | serviceType: 'occurrences', | ||
86 | expectedStateId: this.updateService.xtextStateId, | ||
87 | caretOffset: this.store.state.selection.main.head, | ||
88 | }); | ||
89 | const allChanges = this.updateService.computeChangesSinceLastUpdate(); | ||
90 | if (!allChanges.empty | ||
91 | || (isServiceConflictResult(result) && result.conflict === 'canceled')) { | ||
92 | // Stale occurrences result, the user already made some changes. | ||
93 | // We can safely ignore the occurrences and schedule a new find occurrences call. | ||
94 | this.clearOccurrences(); | ||
95 | this.findOccurrencesTimer.schedule(); | ||
96 | return; | ||
97 | } | ||
98 | if (!isOccurrencesResult(result) || result.stateId !== this.updateService.xtextStateId) { | ||
99 | log.error('Unexpected occurrences result', result); | ||
100 | this.clearOccurrences(); | ||
101 | return; | ||
102 | } | ||
103 | const write = transformOccurrences(result.writeRegions); | ||
104 | const read = transformOccurrences(result.readRegions); | ||
105 | this.hasOccurrences = write.length > 0 || read.length > 0; | ||
106 | log.debug('Found', write.length, 'write and', read.length, 'read occurrences'); | ||
107 | this.store.updateOccurrences(write, read); | ||
108 | } | ||
109 | |||
110 | private clearOccurrences() { | ||
111 | if (this.hasOccurrences) { | ||
112 | this.store.updateOccurrences([], []); | ||
113 | this.hasOccurrences = false; | ||
114 | } | ||
115 | } | ||
116 | } | ||