/****************************************************************************** * 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'; import { EmptyFileSystem, DocumentState } from 'langium'; import { startLanguageServer } from 'langium/lsp'; import { BrowserMessageReader, BrowserMessageWriter, createConnection, NotificationType } from 'vscode-languageserver/browser'; 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('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 ?? [] }); } });