Skip to content

Commit

Permalink
Moved build step to initialized step
Browse files Browse the repository at this point in the history
  • Loading branch information
dhuebner committed Jul 1, 2022
1 parent 7233a47 commit 86b0ea6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 17 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"langium.build.skipFolder": "node_modules, out, langium-template"
}
43 changes: 27 additions & 16 deletions packages/langium/src/lsp/language-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import {
CancellationToken, ClientCapabilities, Connection, DidChangeConfigurationNotification, FileChangeType, HandlerResult, InitializeResult,
LSPErrorCodes, RequestHandler, ResponseError, ServerRequestHandler, TextDocumentIdentifier, TextDocumentSyncKind
LSPErrorCodes, RequestHandler, ResponseError, ServerRequestHandler, TextDocumentIdentifier, TextDocumentSyncKind, WorkspaceFolder
} from 'vscode-languageserver';
import { URI } from 'vscode-uri';
import { LangiumServices, LangiumSharedServices } from '../services';
Expand All @@ -20,8 +20,11 @@ export function startLanguageServer(services: LangiumSharedServices): void {
if (!connection) {
throw new Error('Starting a language server requires the languageServer.Connection service to be set.');
}

let wsFolders: WorkspaceFolder[] | null;
let wsConfiguration = false;
connection.onInitialize(async params => {
wsFolders = params.workspaceFolders;
wsConfiguration = params.capabilities.workspace?.configuration??false;
const hasFormattingService = languages.some(e => e.lsp.Formatter !== undefined);
const formattingOnTypeOptions = languages.map(e => e.lsp.Formatter?.formatOnTypeOptions).find(e => !!e);
const hasCodeActionProvider = languages.some(e => e.lsp.CodeActionProvider !== undefined);
Expand Down Expand Up @@ -56,30 +59,28 @@ export function startLanguageServer(services: LangiumSharedServices): void {
};

await Promise.all(languages.map(languageService => initializeClientCapabilities(languageService, params.capabilities)));
return result;
});

connection.onInitialized(async () => {
connection.client.register(DidChangeConfigurationNotification.type, {
// Listen to configuration changes for all languages
section: languages.map(lang => lang.LanguageMetaData.languageId)
});
if (params.capabilities.workspace?.configuration) {
languages.map(lang => lang.LanguageMetaData.languageId).forEach(async section => {
// Update builder configuration before building
if (wsConfiguration) {
await Promise.all(languages.map(lang => lang.LanguageMetaData.languageId).map(async section => {
// TODO consider sending one getConfiguration(sections[]) request
const configs = await connection.workspace.getConfiguration(section);
services.workspace.ConfigurationProvider.updateConfiguration(section, configs);
});
}));
}
if (params.workspaceFolders) {
const folders = params.workspaceFolders;
if (wsFolders) {
const mutex = services.workspace.MutexLock;
mutex.lock(token => services.workspace.WorkspaceManager.initializeWorkspace(folders, token));
}
return result;
});
connection.onDidChangeConfiguration(change => {
if (change.settings) {
Object.keys(change.settings).forEach(section => {
services.workspace.ConfigurationProvider.updateConfiguration(section, change.settings[section]);
});
await mutex.lock(token => services.workspace.WorkspaceManager.initializeWorkspace(wsFolders!, token));
}
});

addDocumentsHandler(connection, services);
addDiagnosticsHandler(connection, services);
addCompletionHandler(connection, services);
Expand All @@ -93,6 +94,7 @@ export function startLanguageServer(services: LangiumSharedServices): void {
addRenameHandler(connection, services);
addHoverHandler(connection, services);
addSemanticTokenHandler(connection, services);
addConfigurationChangeHandler(connection, services);

// Make the text document manager listen on the connection for open, change and close text document events.
const documents = services.workspace.TextDocuments;
Expand Down Expand Up @@ -272,6 +274,15 @@ export function addSemanticTokenHandler(connection: Connection, services: Langiu
services
));
}
export function addConfigurationChangeHandler(connection: Connection, services: LangiumSharedServices): void {
connection.onDidChangeConfiguration(change => {
if (change.settings) {
Object.keys(change.settings).forEach(section => {
services.workspace.ConfigurationProvider.updateConfiguration(section, change.settings[section]);
});
}
});
}

export function createServerRequestHandler<P extends { textDocument: TextDocumentIdentifier }, R, PR, E = void>(
serviceCall: (services: LangiumServices, document: LangiumDocument, params: P, cancelToken: CancellationToken) => HandlerResult<R, E>,
Expand Down
2 changes: 1 addition & 1 deletion packages/langium/src/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export type LangiumLspServices = {
Formatter?: Formatter
}

export interface InitializableService<T extends Record<string, unknown>> {
export interface InitializableService<T extends object> {
/**
* Initializes the service with client (IDE) related capabilities.
*
Expand Down

0 comments on commit 86b0ea6

Please sign in to comment.