Skip to content

Commit

Permalink
slintpad: Fix showDocument sent from LSP
Browse files Browse the repository at this point in the history
The latest version of the monaco LS integration has changed how
to show documents in an editor again. We would need to include even more
services to make it automatically handle the `showDocument` the LS sends.

Instead just handle the request going around the "official" API.
  • Loading branch information
hunger committed Jul 12, 2024
1 parent edea933 commit 99098b3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
5 changes: 5 additions & 0 deletions tools/slintpad/src/editor_widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,11 @@ class EditorPaneWidget extends Widget {
monaco.editor.onDidCreateModel((model: monaco.editor.ITextModel) =>
this.add_model_listener(model),
);

lsp.show_document_callback = (uri, position) => {
this.goto_position(uri, position);
return true;
};
}

async map_url(url_: string): Promise<string | undefined> {
Expand Down
38 changes: 38 additions & 0 deletions tools/slintpad/src/lsp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
NotificationMessage,
RequestMessage,
ResponseMessage,
ShowDocumentParams,
} from "vscode-languageclient";
import { MonacoLanguageClient } from "monaco-languageclient";

Expand All @@ -19,12 +20,18 @@ import {
MessageReader,
MessageWriter,
} from "vscode-languageserver-protocol/browser";
import { LspPosition } from "./lsp_integration";

import slint_init, * as slint_preview from "@lsp/slint_lsp_wasm.js";

import type { ResourceUrlMapperFunction } from "@lsp/slint_lsp_wasm.js";
export { ResourceUrlMapperFunction };

export type ShowDocumentCallback = (
_uri: string,
_posiiton: LspPosition,
) => boolean;

function createLanguageClient(
transports: MessageTransports,
): MonacoLanguageClient {
Expand Down Expand Up @@ -107,6 +114,8 @@ export class Lsp {
#lsp_client: MonacoLanguageClient | null = null;
#file_reader: FileReader | null = null;

#show_document_callback: ShowDocumentCallback;

readonly #lsp_worker: Worker;
readonly #lsp_reader: MessageReader;
readonly #lsp_writer: MessageWriter;
Expand All @@ -115,6 +124,8 @@ export class Lsp {

constructor(worker: Worker) {
this.#lsp_worker = worker;
this.#show_document_callback = (_uri, _pos) => true;

const reader = new FilterProxyReader(
new BrowserMessageReader(this.#lsp_worker),
(data: Message) => {
Expand Down Expand Up @@ -156,6 +167,29 @@ export class Lsp {

return true;
}
if ((data as RequestMessage).method == "window/showDocument") {
const request = data as RequestMessage;
const params = request.params as ShowDocumentParams;

const selection = params.selection?.start || {
line: 1,
character: 1,
};

const success = this.#show_document_callback(
params.uri,
selection,
);

writer.write({
jsonrpc: request.jsonrpc,
id: request.id,
result: { success: success },
error: undefined,
} as ResponseMessage);

return true;
}
return false;
},
);
Expand All @@ -181,6 +215,10 @@ export class Lsp {
this.#file_reader = fr;
}

set show_document_callback(cb: ShowDocumentCallback) {
this.#show_document_callback = cb;
}

private read_url(url: string): Promise<string> {
try {
return this.#file_reader?.(url) ?? Promise.reject();
Expand Down

0 comments on commit 99098b3

Please sign in to comment.