diff options
author | 2021-10-30 13:48:52 +0200 | |
---|---|---|
committer | 2021-10-31 19:26:13 +0100 | |
commit | cdb493b0a47bcf64e8e670b94fa399fcd731f531 (patch) | |
tree | b6b03aec77ef87a2dda7585be7884a30c65d93f5 /language-web/src/main/js/xtext/XtextClient.ts | |
parent | feat(web): add xtext content assist (diff) | |
download | refinery-cdb493b0a47bcf64e8e670b94fa399fcd731f531.tar.gz refinery-cdb493b0a47bcf64e8e670b94fa399fcd731f531.tar.zst refinery-cdb493b0a47bcf64e8e670b94fa399fcd731f531.zip |
chore(web): refactor xtext client
Diffstat (limited to 'language-web/src/main/js/xtext/XtextClient.ts')
-rw-r--r-- | language-web/src/main/js/xtext/XtextClient.ts | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/language-web/src/main/js/xtext/XtextClient.ts b/language-web/src/main/js/xtext/XtextClient.ts new file mode 100644 index 00000000..f8b06258 --- /dev/null +++ b/language-web/src/main/js/xtext/XtextClient.ts | |||
@@ -0,0 +1,73 @@ | |||
1 | import type { | ||
2 | CompletionContext, | ||
3 | CompletionResult, | ||
4 | } from '@codemirror/autocomplete'; | ||
5 | import type { Transaction } from '@codemirror/state'; | ||
6 | |||
7 | import type { EditorStore } from '../editor/EditorStore'; | ||
8 | import { ContentAssistService } from './ContentAssistService'; | ||
9 | import { getLogger } from '../logging'; | ||
10 | import { UpdateService } from './UpdateService'; | ||
11 | import { ValidationService } from './ValidationService'; | ||
12 | import { XtextWebSocketClient } from './XtextWebSocketClient'; | ||
13 | |||
14 | const log = getLogger('xtext.XtextClient'); | ||
15 | |||
16 | export class XtextClient { | ||
17 | webSocketClient: XtextWebSocketClient; | ||
18 | |||
19 | updateService: UpdateService; | ||
20 | |||
21 | contentAssistService: ContentAssistService; | ||
22 | |||
23 | validationService: ValidationService; | ||
24 | |||
25 | constructor(store: EditorStore) { | ||
26 | this.webSocketClient = new XtextWebSocketClient( | ||
27 | async () => { | ||
28 | this.updateService.xtextStateId = null; | ||
29 | await this.updateService.updateFullText(); | ||
30 | }, | ||
31 | async (resource, stateId, service, push) => { | ||
32 | await this.onPush(resource, stateId, service, push); | ||
33 | }, | ||
34 | ); | ||
35 | this.updateService = new UpdateService(store, this.webSocketClient); | ||
36 | this.contentAssistService = new ContentAssistService(this.updateService); | ||
37 | this.validationService = new ValidationService(store, this.updateService); | ||
38 | } | ||
39 | |||
40 | onTransaction(transaction: Transaction): void { | ||
41 | // `ContentAssistService.prototype.onTransaction` needs the dirty change desc | ||
42 | // _before_ the current edit, so we call it before `updateService`. | ||
43 | this.contentAssistService.onTransaction(transaction); | ||
44 | this.updateService.onTransaction(transaction); | ||
45 | } | ||
46 | |||
47 | private async onPush(resource: string, stateId: string, service: string, push: unknown) { | ||
48 | const { resourceName, xtextStateId } = this.updateService; | ||
49 | if (resource !== resourceName) { | ||
50 | log.error('Unknown resource name: expected:', resourceName, 'got:', resource); | ||
51 | return; | ||
52 | } | ||
53 | if (stateId !== xtextStateId) { | ||
54 | log.error('Unexpected xtext state id: expected:', xtextStateId, 'got:', resource); | ||
55 | await this.updateService.updateFullText(); | ||
56 | } | ||
57 | switch (service) { | ||
58 | case 'validate': | ||
59 | this.validationService.onPush(push); | ||
60 | return; | ||
61 | case 'highlight': | ||
62 | // TODO | ||
63 | return; | ||
64 | default: | ||
65 | log.error('Unknown push service:', service); | ||
66 | break; | ||
67 | } | ||
68 | } | ||
69 | |||
70 | contentAssist(context: CompletionContext): Promise<CompletionResult> { | ||
71 | return this.contentAssistService.contentAssist(context); | ||
72 | } | ||
73 | } | ||