Skip to content

Commit

Permalink
Intercept workspace create requests and create a new model for the ed…
Browse files Browse the repository at this point in the history
…itor
  • Loading branch information
kaisalmen committed Nov 7, 2023
1 parent dcad609 commit 8dd0de6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
23 changes: 21 additions & 2 deletions packages/examples/src/common/client-commons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<string, any> = {}, secure: boolean = location.protocol === 'https:'): string => {
Expand Down
20 changes: 19 additions & 1 deletion packages/examples/src/json/server/json-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export class JsonServer {
}
};
});

this.connection.onCodeAction(params =>
this.codeAction(params)
);
Expand Down Expand Up @@ -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[] {
Expand Down

0 comments on commit 8dd0de6

Please sign in to comment.