Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support all text document operations #1474

Merged
merged 2 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
34 changes: 31 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,15 @@ export class DefaultLanguageServer implements LanguageServer {
executeCommandProvider: commandNames && {
commands: commandNames
},
textDocumentSync: TextDocumentSyncKind.Incremental,
textDocumentSync: {
change: documentUpdateHandler.didChangeContent
? TextDocumentSyncKind.Incremental
: TextDocumentSyncKind.None,
openClose: Boolean(documentUpdateHandler.didOpenDocument) || Boolean(documentUpdateHandler.didCloseDocument),
spoenemann marked this conversation as resolved.
Show resolved Hide resolved
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 +276,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