Skip to content

Commit

Permalink
feat(lsp): define and read configuration values
Browse files Browse the repository at this point in the history
Create a sample configuration in the editor plugin and read it from the
LSP side (as well as reloading it on changes).
  • Loading branch information
dnaka91 committed Dec 4, 2023
1 parent 670d9d1 commit 192d943
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 4 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/stef-lsp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
7 changes: 7 additions & 0 deletions crates/stef-lsp/src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use serde::Deserialize;

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Global {
pub max_number_of_problems: u32,
}
61 changes: 58 additions & 3 deletions crates/stef-lsp/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand All @@ -23,6 +24,7 @@ use self::cli::Cli;

mod cli;
mod compile;
mod config;
mod utf16;

#[derive(Debug)]
Expand Down Expand Up @@ -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::<config::Global>(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");
}

Expand Down Expand Up @@ -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::<config::Global>(settings).unwrap();

self.client
.log_message(
MessageType::INFO,
format!("updated settings: {settings:#?}"),
)
.await;
}
}

#[tokio::main]
Expand Down
13 changes: 12 additions & 1 deletion vscode-extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 192d943

Please sign in to comment.