aboutsummaryrefslogtreecommitdiffstats
path: root/language-web/src/main/js/xtext/XtextClient.ts
diff options
context:
space:
mode:
Diffstat (limited to 'language-web/src/main/js/xtext/XtextClient.ts')
-rw-r--r--language-web/src/main/js/xtext/XtextClient.ts73
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 @@
1import type {
2 CompletionContext,
3 CompletionResult,
4} from '@codemirror/autocomplete';
5import type { Transaction } from '@codemirror/state';
6
7import type { EditorStore } from '../editor/EditorStore';
8import { ContentAssistService } from './ContentAssistService';
9import { getLogger } from '../logging';
10import { UpdateService } from './UpdateService';
11import { ValidationService } from './ValidationService';
12import { XtextWebSocketClient } from './XtextWebSocketClient';
13
14const log = getLogger('xtext.XtextClient');
15
16export 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}