vscode: Mirror the code in slintpad for the LSP worker more closely

The code is not identical, so it can not be shared (yet), but at least
minimize the diff.
This commit is contained in:
Tobias Hunger 2023-09-18 16:42:37 +02:00 committed by Tobias Hunger
parent 8311533cb1
commit a562c534ad
2 changed files with 22 additions and 26 deletions

View file

@ -1,46 +1,34 @@
// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-1.1 OR LicenseRef-Slint-commercial
import slint_init, * as slint_lsp from "../out/slint_lsp_wasm.js";
import slint_wasm_data from "../out/slint_lsp_wasm_bg.wasm";
import { InitializeParams, InitializeResult } from "vscode-languageserver";
import {
createConnection,
BrowserMessageReader,
BrowserMessageWriter,
} from "vscode-languageserver/browser";
import { InitializeParams, InitializeResult } from "vscode-languageserver";
import slint_init, * as slint_lsp from "../out/slint_lsp_wasm.js";
import slint_wasm_data from "../out/slint_lsp_wasm_bg.wasm";
slint_init(slint_wasm_data).then((_) => {
const messageReader = new BrowserMessageReader(self);
const messageWriter = new BrowserMessageWriter(self);
const connection = createConnection(messageReader, messageWriter);
const reader = new BrowserMessageReader(self);
const writer = new BrowserMessageWriter(self);
let the_lsp: slint_lsp.SlintServer;
const connection = createConnection(reader, writer);
function send_notification(method: string, params: any): boolean {
connection.sendNotification(method, params);
return true;
}
async function load_file(path: string): Promise<string> {
const contents: Uint8Array = await connection.sendRequest(
"slint/load_file",
path,
);
return new TextDecoder().decode(contents);
}
async function send_request(method: string, params: any): Promise<any> {
async function send_request(method: string, params: any): Promise<unknown> {
return await connection.sendRequest(method, params);
}
function highlight(path: string, offset: number) {
connection.sendRequest("slint/preview_message", {
command: "highlight",
data: { path: path, offset: offset },
});
async function load_file(path: string): Promise<string> {
return await connection.sendRequest("slint/load_file", path);
}
connection.onInitialize((params: InitializeParams): InitializeResult => {
@ -49,7 +37,6 @@ slint_init(slint_wasm_data).then((_) => {
send_notification,
send_request,
load_file,
highlight,
);
return the_lsp.server_initialize_result(params.capabilities);
});
@ -58,9 +45,13 @@ slint_init(slint_wasm_data).then((_) => {
return await the_lsp.handle_request(token, method, params);
});
connection.onDidChangeConfiguration(async (_) => {
the_lsp.reload_config();
});
connection.onNotification(
"slint/preview_to_lsp",
// eslint-disable-next-line @typescript-eslint/no-explicit-any
async (params: any) => {
await the_lsp.process_preview_to_lsp_message(params);
},
);
connection.onDidChangeTextDocument(async (param) => {
await the_lsp.reload_document(
@ -78,6 +69,10 @@ slint_init(slint_wasm_data).then((_) => {
);
});
connection.onDidChangeConfiguration(async (_param: unknown) => {
the_lsp.reload_config();
});
// Listen on the connection
connection.listen();

View file

@ -64,6 +64,7 @@ function startClient(
let contents = await vscode.workspace.fs.readFile(
Uri.parse(param, true),
);
return new TextDecoder().decode(contents);
});
});