From 192d9437e8e29a0a54b59d0b18030302b1f167d4 Mon Sep 17 00:00:00 2001 From: Dominik Nakamura Date: Mon, 4 Dec 2023 15:39:05 +0900 Subject: [PATCH] feat(lsp): define and read configuration values Create a sample configuration in the editor plugin and read it from the LSP side (as well as reloading it on changes). --- Cargo.lock | 2 ++ crates/stef-lsp/Cargo.toml | 2 ++ crates/stef-lsp/src/config.rs | 7 ++++ crates/stef-lsp/src/main.rs | 61 +++++++++++++++++++++++++++++++++-- vscode-extension/package.json | 13 +++++++- 5 files changed, 81 insertions(+), 4 deletions(-) create mode 100644 crates/stef-lsp/src/config.rs diff --git a/Cargo.lock b/Cargo.lock index 087cd73..d1aa91d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1175,6 +1175,8 @@ dependencies = [ "clap", "directories", "ouroboros", + "serde", + "serde_json", "stef-compiler", "stef-parser", "tokio", diff --git a/crates/stef-lsp/Cargo.toml b/crates/stef-lsp/Cargo.toml index a679ce3..1de9057 100644 --- a/crates/stef-lsp/Cargo.toml +++ b/crates/stef-lsp/Cargo.toml @@ -13,6 +13,8 @@ license.workspace = true clap.workspace = true directories = "5.0.1" ouroboros = "0.18.0" +serde = { version = "1.0.193", features = ["derive"] } +serde_json = "1.0.108" stef-compiler = { path = "../stef-compiler" } stef-parser = { path = "../stef-parser" } tokio = { version = "1.34.0", features = ["fs", "io-std", "io-util", "macros", "rt-multi-thread"] } diff --git a/crates/stef-lsp/src/config.rs b/crates/stef-lsp/src/config.rs new file mode 100644 index 0000000..d5c473c --- /dev/null +++ b/crates/stef-lsp/src/config.rs @@ -0,0 +1,7 @@ +use serde::Deserialize; + +#[derive(Debug, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct Global { + pub max_number_of_problems: u32, +} diff --git a/crates/stef-lsp/src/main.rs b/crates/stef-lsp/src/main.rs index f813be8..b179008 100644 --- a/crates/stef-lsp/src/main.rs +++ b/crates/stef-lsp/src/main.rs @@ -10,9 +10,10 @@ use tower_lsp::{ async_trait, jsonrpc::Result, lsp_types::{ - Diagnostic, DidChangeTextDocumentParams, DidOpenTextDocumentParams, InitializeParams, - InitializeResult, InitializedParams, MessageType, ServerCapabilities, ServerInfo, - TextDocumentSyncCapability, TextDocumentSyncKind, Url, + ConfigurationItem, Diagnostic, DidChangeConfigurationParams, DidChangeTextDocumentParams, + DidOpenTextDocumentParams, InitializeParams, InitializeResult, InitializedParams, + MessageType, Registration, ServerCapabilities, ServerInfo, TextDocumentSyncCapability, + TextDocumentSyncKind, Url, }, Client, LanguageServer, LspService, Server, }; @@ -23,6 +24,7 @@ use self::cli::Cli; mod cli; mod compile; +mod config; mod utf16; #[derive(Debug)] @@ -59,6 +61,38 @@ impl LanguageServer for Backend { } async fn initialized(&self, _params: InitializedParams) { + let settings = self + .client + .configuration(vec![ConfigurationItem { + scope_uri: None, + section: Some("stef-lsp".to_owned()), + }]) + .await + .unwrap() + .remove(0); + + let settings = serde_json::from_value::(settings).unwrap(); + + self.client + .log_message( + MessageType::INFO, + format!("current settings: {settings:#?}"), + ) + .await; + + self.client + .register_capability(vec![Registration { + id: "1".to_owned(), + method: "workspace/didChangeConfiguration".to_owned(), + register_options: None, + }]) + .await + .unwrap(); + + self.client + .log_message(MessageType::INFO, "server initialized!") + .await; + debug!("initialized"); } @@ -119,6 +153,27 @@ impl LanguageServer for Backend { .await .insert(params.text_document.uri, file); } + + async fn did_change_configuration(&self, _: DidChangeConfigurationParams) { + let settings = self + .client + .configuration(vec![ConfigurationItem { + scope_uri: None, + section: Some("stef-lsp".to_owned()), + }]) + .await + .unwrap() + .remove(0); + + let settings = serde_json::from_value::(settings).unwrap(); + + self.client + .log_message( + MessageType::INFO, + format!("updated settings: {settings:#?}"), + ) + .await; + } } #[tokio::main] diff --git a/vscode-extension/package.json b/vscode-extension/package.json index 0cdf363..fa8d953 100644 --- a/vscode-extension/package.json +++ b/vscode-extension/package.json @@ -36,7 +36,18 @@ "scopeName": "source.stef", "path": "./syntaxes/stef.tmLanguage.json" } - ] + ], + "configuration": { + "title": "STEF", + "properties": { + "stef-lsp.maxNumberOfProblems": { + "scope": "resource", + "type": "number", + "default": 100, + "description": "Controls the maximum number of problems produced by the server." + } + } + } }, "vsce": { "dependencies": false