From 7675c3170625aff50933783c440efdd6d33b4afa Mon Sep 17 00:00:00 2001 From: rszyma Date: Mon, 10 Jun 2024 02:40:13 +0200 Subject: [PATCH] refactor: move server init params to rust (#35) --- kls/src/lib.rs | 100 ++++++++++++++++++++++++++++++++++++++++++-- server/src/index.ts | 46 +++++++------------- 2 files changed, 110 insertions(+), 36 deletions(-) diff --git a/kls/src/lib.rs b/kls/src/lib.rs index 5535177..7d40c29 100644 --- a/kls/src/lib.rs +++ b/kls/src/lib.rs @@ -18,12 +18,15 @@ use lsp_types::{ DidChangeTextDocument, DidChangeWatchedFiles, DidCloseTextDocument, DidDeleteFiles, DidOpenTextDocument, DidSaveTextDocument, Initialized, Notification, }, - request::{Formatting, GotoDefinition, Request}, + request::{Formatting, GotoDefinition, Initialize, Request, SemanticTokensFullRequest}, DeleteFilesParams, Diagnostic, DiagnosticSeverity, DiagnosticTag, DidChangeTextDocumentParams, DidChangeWatchedFilesParams, DidOpenTextDocumentParams, DidSaveTextDocumentParams, - DocumentFormattingParams, FileChangeType, FileDelete, FileEvent, GotoDefinitionParams, - GotoDefinitionResponse, InitializeParams, LocationLink, Position, PublishDiagnosticsParams, - TextDocumentItem, TextEdit, Url, VersionedTextDocumentIdentifier, + DocumentFormattingParams, FileChangeType, FileDelete, FileEvent, FileOperationFilter, + FileOperationPattern, GotoDefinitionParams, GotoDefinitionResponse, InitializeParams, + InitializeResult, LocationLink, Position, PositionEncodingKind, PublishDiagnosticsParams, + SemanticToken, SemanticTokenModifier, SemanticTokenType, SemanticTokens, + SemanticTokensFullOptions, SemanticTokensLegend, SemanticTokensParams, SemanticTokensResult, + TextDocumentItem, TextDocumentSyncKind, TextEdit, Url, VersionedTextDocumentIdentifier, }; use serde::Deserialize; use serde_wasm_bindgen::{from_value, to_value}; @@ -324,6 +327,95 @@ impl KanataLanguageServer { // self_ } + #[allow(unused_variables)] + #[wasm_bindgen(js_class = KanataLanguageServer, js_name = initialize)] + pub fn initialize(&mut self, params: JsValue) -> JsValue { + type Params = ::Params; + type Result = ::Result; + let params = from_value::(params).expect("deserializes"); + to_value::(&self.initialize_impl(¶ms)).expect("no conversion error") + } + + fn initialize_impl(&mut self, _params: &InitializeParams) -> InitializeResult { + let sem_tokens_legend = SemanticTokensLegend { + token_types: vec![ + SemanticTokenType::NAMESPACE, + SemanticTokenType::TYPE, + SemanticTokenType::CLASS, + SemanticTokenType::ENUM, + SemanticTokenType::INTERFACE, + SemanticTokenType::STRUCT, + SemanticTokenType::TYPE_PARAMETER, + SemanticTokenType::PARAMETER, + SemanticTokenType::VARIABLE, + SemanticTokenType::PROPERTY, + SemanticTokenType::ENUM_MEMBER, + SemanticTokenType::EVENT, + SemanticTokenType::FUNCTION, + SemanticTokenType::METHOD, + SemanticTokenType::MACRO, + SemanticTokenType::KEYWORD, + SemanticTokenType::MODIFIER, + SemanticTokenType::COMMENT, + SemanticTokenType::STRING, + SemanticTokenType::NUMBER, + SemanticTokenType::REGEXP, + SemanticTokenType::OPERATOR, + ], + token_modifiers: vec![ + SemanticTokenModifier::DECLARATION, + SemanticTokenModifier::DEFINITION, + SemanticTokenModifier::READONLY, + SemanticTokenModifier::STATIC, + SemanticTokenModifier::DEPRECATED, + SemanticTokenModifier::ABSTRACT, + SemanticTokenModifier::ASYNC, + SemanticTokenModifier::MODIFICATION, + SemanticTokenModifier::DOCUMENTATION, + SemanticTokenModifier::DEFAULT_LIBRARY, + ], + }; + + InitializeResult { + capabilities: lsp_types::ServerCapabilities { + // UTF-8 is not supported in vscode-languageserver/node. See: + // https://github.com/microsoft/vscode-languageserver-node/issues/1224 + position_encoding: Some(PositionEncodingKind::UTF16), + // textDocumentSync: { + // openClose: true, + // save: { includeText: false }, + // change: TextDocumentSyncKind.Full, + // }, + text_document_sync: Some(lsp_types::TextDocumentSyncCapability::Kind( + TextDocumentSyncKind::FULL, + )), + definition_provider: Some(lsp_types::OneOf::Left(true)), + document_formatting_provider: Some(lsp_types::OneOf::Left(true)), + workspace: Some(lsp_types::WorkspaceServerCapabilities { + workspace_folders: Some(lsp_types::WorkspaceFoldersServerCapabilities { + supported: Some(false), + change_notifications: None, + }), + file_operations: Some(lsp_types::WorkspaceFileOperationsServerCapabilities { + did_delete: Some(lsp_types::FileOperationRegistrationOptions { + filters: vec![FileOperationFilter { + scheme: None, + pattern: FileOperationPattern { + glob: "**".to_string(), + matches: None, + options: None, + }, + }], + }), + ..Default::default() + }), + }), + ..Default::default() + }, + server_info: None, + } + } + /// Catch-all handler for notifications sent by the LSP client. /// /// This function receives a notification's `method` and `params` and dispatches to the diff --git a/server/src/index.ts b/server/src/index.ts index c71ad2a..f800829 100644 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -2,49 +2,31 @@ import { createConnection, ProposedFeatures, PublishDiagnosticsParams, - TextDocumentSyncKind, InitializeParams, - PositionEncodingKind, } from "vscode-languageserver/node"; import { KanataLanguageServer } from "../../out/kls"; // Create LSP connection const connection = createConnection(ProposedFeatures.all); -const sendDiagnosticsCallback = (params: PublishDiagnosticsParams) => - connection.sendDiagnostics(params); - connection.onInitialize((params: InitializeParams) => { - const kls = new KanataLanguageServer(params, sendDiagnosticsCallback); - + const kls = new KanataLanguageServer( + params, + (params: PublishDiagnosticsParams) => connection.sendDiagnostics(params), + ); connection.onNotification((...args) => kls.onNotification(...args)); connection.onDocumentFormatting((...args) => - kls.onDocumentFormatting(...args), + // eslint-disable-next-line @typescript-eslint/no-unsafe-return + kls.onDocumentFormatting(args[0]), ); - connection.onDefinition((...args) => kls.onDefinition(...args)); - - return { - capabilities: { - textDocumentSync: { - openClose: true, - save: { includeText: false }, - change: TextDocumentSyncKind.Full, - }, - // UTF-8 is not supported in vscode-languageserver/node. See: - // https://github.com/microsoft/vscode-languageserver-node/issues/1224 - positionEncoding: PositionEncodingKind.UTF16, - documentFormattingProvider: true, - workspace: { - workspaceFolders: { supported: false }, - fileOperations: { - didDelete: { - filters: [{ pattern: { /* matches: 'folder', */ glob: "**" } }], - }, - }, - }, - definitionProvider: true, - }, - }; + // eslint-disable-next-line @typescript-eslint/no-unsafe-return + connection.onDefinition((...args) => kls.onDefinition(args[0])); + connection.languages.semanticTokens.on((...args) => + // eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-call + kls.onSemanticTokens(args[0]), + ); + // eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-call + return kls.initialize(params); }); connection.listen();