From 8dd0de6b3086c04b0a68e728a1b8eb1cfadca3e3 Mon Sep 17 00:00:00 2001 From: Kai Salmen Date: Tue, 7 Nov 2023 16:55:42 +0100 Subject: [PATCH] Intercept workspace create requests and create a new model for the editor --- .../examples/src/common/client-commons.ts | 23 +++++++++++++++++-- .../examples/src/json/server/json-server.ts | 20 +++++++++++++++- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/packages/examples/src/common/client-commons.ts b/packages/examples/src/common/client-commons.ts index 240040784..d90c57b28 100644 --- a/packages/examples/src/common/client-commons.ts +++ b/packages/examples/src/common/client-commons.ts @@ -12,12 +12,13 @@ import getKeybindingsServiceOverride from '@codingame/monaco-vscode-keybindings- import getThemeServiceOverride from '@codingame/monaco-vscode-theme-service-override'; import getTextmateServiceOverride from '@codingame/monaco-vscode-textmate-service-override'; import { initServices, MonacoLanguageClient } from 'monaco-languageclient'; -import { CloseAction, ErrorAction, MessageTransports } from 'vscode-languageclient'; +import { ApplyWorkspaceEditParams, CloseAction, ErrorAction, MessageTransports } from 'vscode-languageclient'; import { WebSocketMessageReader, WebSocketMessageWriter, toSocket } from 'vscode-ws-jsonrpc'; +import { CreateFile } from 'vscode-languageserver-types'; import { Uri } from 'vscode'; export const createLanguageClient = (transports: MessageTransports): MonacoLanguageClient => { - return new MonacoLanguageClient({ + const client = new MonacoLanguageClient({ name: 'Sample Language Client', clientOptions: { // use a language id as a document selector @@ -35,6 +36,24 @@ export const createLanguageClient = (transports: MessageTransports): MonacoLangu } } }); + + // intercept workspace create requests and create a new model for the editor + client.onRequest('workspace/applyEdit', async (params: ApplyWorkspaceEditParams) => { + const workspaceEdit = params.edit; + if (workspaceEdit.documentChanges) { + for (const dc of workspaceEdit.documentChanges) { + const cf = dc as CreateFile; + if (cf.kind === 'create') { + console.log(cf); + const uri = Uri.parse(cf.uri); + const modelRef = await createModelReference(uri, ''); + modelRef.object.setLanguageId('json'); + } + } + } + }); + + return client; }; export const createUrl = (hostname: string, port: number, path: string, searchParams: Record = {}, secure: boolean = location.protocol === 'https:'): string => { diff --git a/packages/examples/src/json/server/json-server.ts b/packages/examples/src/json/server/json-server.ts index 931ed72ce..a334c0f2a 100644 --- a/packages/examples/src/json/server/json-server.ts +++ b/packages/examples/src/json/server/json-server.ts @@ -62,6 +62,7 @@ export class JsonServer { } }; }); + this.connection.onCodeAction(params => this.codeAction(params) ); @@ -94,8 +95,25 @@ export class JsonServer { ); } - start() { + async start() { this.connection.listen(); + + // Create a file after language server is started + setTimeout(async () => { + await this.connection.workspace.applyEdit({ + edit: { + documentChanges: [ + { + kind: 'create', + uri: '/workspace/tester.json', + options: { + overwrite: true + } + } + ] + } + }); + }, 2000); } protected getFoldingRanges(params: FoldingRangeParams): FoldingRange[] {