-
Notifications
You must be signed in to change notification settings - Fork 61
/
main-browser.ts
40 lines (34 loc) · 1.9 KB
/
main-browser.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/******************************************************************************
* Copyright 2022 TypeFox GmbH
* This program and the accompanying materials are made available under the
* terms of the MIT License, which is available in the project root.
******************************************************************************/
import type { Diagnostic } from 'vscode-languageserver/browser.js';
import { startLanguageServer, EmptyFileSystem, DocumentState } from 'langium';
import { BrowserMessageReader, BrowserMessageWriter, createConnection, NotificationType } from 'vscode-languageserver/browser.js';
import { createDomainModelServices } from './domain-model-module.js';
/* browser specific setup code */
const messageReader = new BrowserMessageReader(self);
const messageWriter = new BrowserMessageWriter(self);
const connection = createConnection(messageReader, messageWriter);
// Inject the shared services and language-specific services
const { shared, domainmodel } = createDomainModelServices({ connection, ...EmptyFileSystem });
// Start the language server with the shared services
startLanguageServer(shared);
// Send a notification with the serialized AST after every document change
type DocumentChange = { uri: string, content: string, diagnostics: Diagnostic[] };
const documentChangeNotification = new NotificationType<DocumentChange>('browser/DocumentChange');
const jsonSerializer = domainmodel.serializer.JsonSerializer;
shared.workspace.DocumentBuilder.onBuildPhase(DocumentState.Validated, documents => {
for (const document of documents) {
const json = jsonSerializer.serialize(document.parseResult.value, {
sourceText: true,
textRegions: true,
});
connection.sendNotification(documentChangeNotification, {
uri: document.uri.toString(),
content: json,
diagnostics: document.diagnostics ?? []
});
}
});