Skip to content

Commit

Permalink
Support all text document operations (#1474)
Browse files Browse the repository at this point in the history
  • Loading branch information
msujew committed May 15, 2024
1 parent a00974d commit 75aa73e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
18 changes: 15 additions & 3 deletions packages/langium/src/lsp/document-update-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
* terms of the MIT License, which is available in the project root.
******************************************************************************/

import { DidChangeWatchedFilesNotification, FileChangeType, type DidChangeWatchedFilesParams, type DidChangeWatchedFilesRegistrationOptions, type TextDocumentChangeEvent } from 'vscode-languageserver';
import type { TextDocumentWillSaveEvent, DidChangeWatchedFilesParams, DidChangeWatchedFilesRegistrationOptions, TextDocumentChangeEvent, TextEdit } from 'vscode-languageserver';
import { DidChangeWatchedFilesNotification, FileChangeType } from 'vscode-languageserver';
import { stream } from '../utils/stream.js';
import { URI } from '../utils/uri-utils.js';
import type { DocumentBuilder } from '../workspace/document-builder.js';
Expand All @@ -13,21 +14,32 @@ import type { WorkspaceLock } from '../workspace/workspace-lock.js';
import type { LangiumSharedServices } from './lsp-services.js';
import type { WorkspaceManager } from '../workspace/workspace-manager.js';
import type { ServiceRegistry } from '../service-registry.js';
import type { MaybePromise } from '../utils/promise-utils.js';

/**
* Shared service for handling text document changes and watching relevant files.
*/
export interface DocumentUpdateHandler {

didOpenDocument?(event: TextDocumentChangeEvent<TextDocument>): void;

/**
* A content change event was triggered by the `TextDocuments` service.
*/
didChangeContent(change: TextDocumentChangeEvent<TextDocument>): void;
didChangeContent?(event: TextDocumentChangeEvent<TextDocument>): void;

willSaveDocument?(event: TextDocumentWillSaveEvent<TextDocument>): void;

willSaveDocumentWaitUntil?(event: TextDocumentWillSaveEvent<TextDocument>): MaybePromise<TextEdit[]>;

didSaveDocument?(event: TextDocumentChangeEvent<TextDocument>): void;

didCloseDocument?(event: TextDocumentChangeEvent<TextDocument>): void;

/**
* The client detected changes to files and folders watched by the language client.
*/
didChangeWatchedFiles(params: DidChangeWatchedFilesParams): void;
didChangeWatchedFiles?(params: DidChangeWatchedFilesParams): void;

}

Expand Down
32 changes: 29 additions & 3 deletions packages/langium/src/lsp/language-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export class DefaultLanguageServer implements LanguageServer {
}

protected buildInitializeResult(_params: InitializeParams): InitializeResult {
const documentUpdateHandler = this.services.lsp.DocumentUpdateHandler;
const fileOperationOptions = this.services.lsp.FileOperationHandler?.fileOperationOptions;
const allServices: readonly LangiumCoreAndPartialLSPServices[] = this.services.ServiceRegistry.all;
const hasFormattingService = this.hasService(e => e.lsp?.Formatter);
Expand Down Expand Up @@ -136,7 +137,13 @@ export class DefaultLanguageServer implements LanguageServer {
executeCommandProvider: commandNames && {
commands: commandNames
},
textDocumentSync: TextDocumentSyncKind.Incremental,
textDocumentSync: {
change: TextDocumentSyncKind.Incremental,
openClose: true,
save: Boolean(documentUpdateHandler.didSaveDocument),
willSave: Boolean(documentUpdateHandler.willSaveDocument),
willSaveWaitUntil: Boolean(documentUpdateHandler.willSaveDocumentWaitUntil)
},
completionProvider: hasCompletionProvider ? completionOptions : undefined,
referencesProvider: hasReferencesProvider,
documentSymbolProvider: hasDocumentSymbolProvider,
Expand Down Expand Up @@ -267,8 +274,27 @@ export function startLanguageServer(services: LangiumSharedServices): void {
export function addDocumentUpdateHandler(connection: Connection, services: LangiumSharedServices): void {
const handler = services.lsp.DocumentUpdateHandler;
const documents = services.workspace.TextDocuments;
documents.onDidChangeContent(change => handler.didChangeContent(change));
connection.onDidChangeWatchedFiles(params => handler.didChangeWatchedFiles(params));
if (handler.didOpenDocument) {
documents.onDidOpen(change => handler.didOpenDocument!(change));
}
if (handler.didChangeContent) {
documents.onDidChangeContent(change => handler.didChangeContent!(change));
}
if (handler.didCloseDocument) {
documents.onDidClose(change => handler.didCloseDocument!(change));
}
if (handler.didSaveDocument) {
documents.onDidSave(change => handler.didSaveDocument!(change));
}
if (handler.willSaveDocument) {
documents.onWillSave(event => handler.willSaveDocument!(event));
}
if (handler.willSaveDocumentWaitUntil) {
documents.onWillSaveWaitUntil(event => handler.willSaveDocumentWaitUntil!(event));
}
if (handler.didChangeWatchedFiles) {
connection.onDidChangeWatchedFiles(params => handler.didChangeWatchedFiles!(params));
}
}

export function addFileOperationHandler(connection: Connection, services: LangiumSharedServices): void {
Expand Down

0 comments on commit 75aa73e

Please sign in to comment.