Skip to content

Commit 26e82c7

Browse files
willruggianoWill Ruggiano
and
Will Ruggiano
authored
feat: allow configuration through workspace/didChangeConfiguration (#316)
## What kind of change does this PR introduce? It enables passing workspace settings via the workspace/didChangeConfiguration notification. This in turn enables the client to specify settings dynamically, rather than being limited to configuration files. This is _a_ solution to #302. See example usage (with lspconfig) here: willruggiano/neovim.drv@9aa06ad. ## What is the current behavior? There is none. The payload of this handler is currently ignored. ## What is the new behavior? The configuration received by the handler is merged with the fs configuration. --------- Co-authored-by: Will Ruggiano <[email protected]>
1 parent be94461 commit 26e82c7

File tree

4 files changed

+28
-14
lines changed

4 files changed

+28
-14
lines changed

biome.jsonc

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"$schema": "https://biomejs.dev/schemas/1.9.4/schema.json",
2+
"$schema": "./node_modules/@biomejs/biome/configuration_schema.json",
33
"vcs": {
44
"enabled": false,
55
"clientKind": "git",
@@ -8,7 +8,7 @@
88
"files": {
99
"ignoreUnknown": false,
1010
"ignore": [],
11-
"include": ["packages/**/*"]
11+
"include": ["/packages/**/*"]
1212
},
1313
"formatter": {
1414
"enabled": true,

crates/pgt_lsp/src/server.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ impl LanguageServer for LSPServer {
132132
ConfigName::pgt_jsonc()
133133
);
134134

135-
futures::join!(self.session.load_workspace_settings());
135+
futures::join!(self.session.load_workspace_settings(None));
136136

137137
let msg = format!("Server initialized with PID: {}", std::process::id());
138138
self.session
@@ -152,8 +152,10 @@ impl LanguageServer for LSPServer {
152152
}
153153

154154
#[tracing::instrument(level = "info", skip_all)]
155-
async fn did_change_configuration(&self, _params: DidChangeConfigurationParams) {
156-
self.session.load_workspace_settings().await;
155+
async fn did_change_configuration(&self, params: DidChangeConfigurationParams) {
156+
self.session
157+
.load_workspace_settings(serde_json::from_value(params.settings).ok())
158+
.await;
157159
self.setup_capabilities().await;
158160
self.session.update_all_diagnostics().await;
159161
}
@@ -174,7 +176,7 @@ impl LanguageServer for LSPServer {
174176
if ConfigName::file_names()
175177
.contains(&&*watched_file.display().to_string())
176178
{
177-
self.session.load_workspace_settings().await;
179+
self.session.load_workspace_settings(None).await;
178180
self.setup_capabilities().await;
179181
// self.session.update_all_diagnostics().await;
180182
// for now we are only interested to the configuration file,

crates/pgt_lsp/src/session.rs

+19-8
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ use crate::diagnostics::LspError;
33
use crate::documents::Document;
44
use crate::utils;
55
use anyhow::Result;
6+
use biome_deserialize::Merge;
67
use futures::StreamExt;
78
use futures::stream::FuturesUnordered;
89
use pgt_analyse::RuleCategoriesBuilder;
9-
use pgt_configuration::ConfigurationPathHint;
10+
use pgt_configuration::{ConfigurationPathHint, PartialConfiguration};
1011
use pgt_diagnostics::{DiagnosticExt, Error};
1112
use pgt_fs::{FileSystem, PgTPath};
1213
use pgt_workspace::Workspace;
@@ -386,11 +387,13 @@ impl Session {
386387
/// This function attempts to read the `postgrestools.jsonc` configuration file from
387388
/// the root URI and update the workspace settings accordingly
388389
#[tracing::instrument(level = "trace", skip(self))]
389-
pub(crate) async fn load_workspace_settings(&self) {
390+
pub(crate) async fn load_workspace_settings(&self, extra_config: Option<PartialConfiguration>) {
390391
// Providing a custom configuration path will not allow to support workspaces
391392
if let Some(config_path) = &self.config_path {
392393
let base_path = ConfigurationPathHint::FromUser(config_path.clone());
393-
let status = self.load_pgt_configuration_file(base_path).await;
394+
let status = self
395+
.load_pgt_configuration_file(base_path, extra_config)
396+
.await;
394397
self.set_configuration_status(status);
395398
} else if let Some(folders) = self.get_workspace_folders() {
396399
info!("Detected workspace folder.");
@@ -401,9 +404,10 @@ impl Session {
401404
match base_path {
402405
Ok(base_path) => {
403406
let status = self
404-
.load_pgt_configuration_file(ConfigurationPathHint::FromWorkspace(
405-
base_path,
406-
))
407+
.load_pgt_configuration_file(
408+
ConfigurationPathHint::FromWorkspace(base_path),
409+
extra_config.clone(),
410+
)
407411
.await;
408412
self.set_configuration_status(status);
409413
}
@@ -420,25 +424,32 @@ impl Session {
420424
None => ConfigurationPathHint::default(),
421425
Some(path) => ConfigurationPathHint::FromLsp(path),
422426
};
423-
let status = self.load_pgt_configuration_file(base_path).await;
427+
let status = self
428+
.load_pgt_configuration_file(base_path, extra_config)
429+
.await;
424430
self.set_configuration_status(status);
425431
}
426432
}
427433

428434
async fn load_pgt_configuration_file(
429435
&self,
430436
base_path: ConfigurationPathHint,
437+
extra_config: Option<PartialConfiguration>,
431438
) -> ConfigurationStatus {
432439
match load_configuration(&self.fs, base_path.clone()) {
433440
Ok(loaded_configuration) => {
434441
let LoadedConfiguration {
435-
configuration: fs_configuration,
442+
configuration: mut fs_configuration,
436443
directory_path: configuration_path,
437444
..
438445
} = loaded_configuration;
439446
info!("Configuration loaded successfully from disk.");
440447
info!("Update workspace settings.");
441448

449+
if let Some(ws_configuration) = extra_config {
450+
fs_configuration.merge_with(ws_configuration);
451+
}
452+
442453
let result = fs_configuration
443454
.retrieve_gitignore_matches(&self.fs, configuration_path.as_deref());
444455

crates/pgt_workspace/src/workspace/server.rs

+1
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ impl Workspace for WorkspaceServer {
167167
)?;
168168

169169
tracing::info!("Updated settings in workspace");
170+
tracing::debug!("Updated settings are {:#?}", self.settings());
170171

171172
self.connection
172173
.write()

0 commit comments

Comments
 (0)