Skip to content

Commit 1a8e21c

Browse files
committed
feat: allow configuration through workspace/didChangeConfiguration
1 parent 6f697bf commit 1a8e21c

File tree

4 files changed

+26
-12
lines changed

4 files changed

+26
-12
lines changed

crates/pgt_configuration/src/lib.rs

+6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub use analyser::{
2222
RulePlainConfiguration, RuleSelector, RuleWithFixOptions, RuleWithOptions, Rules,
2323
partial_linter_configuration,
2424
};
25+
use biome_deserialize::Merge;
2526
use biome_deserialize_macros::{Merge, Partial};
2627
use bpaf::Bpaf;
2728
use database::{
@@ -115,6 +116,11 @@ impl PartialConfiguration {
115116
}),
116117
}
117118
}
119+
120+
pub fn merge(&mut self, other: Self) -> Self {
121+
self.merge_with(other);
122+
self.clone()
123+
}
118124
}
119125

120126
pub struct ConfigurationPayload {

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

+13-8
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use anyhow::Result;
66
use futures::StreamExt;
77
use futures::stream::FuturesUnordered;
88
use pgt_analyse::RuleCategoriesBuilder;
9-
use pgt_configuration::ConfigurationPathHint;
9+
use pgt_configuration::{ConfigurationPathHint, PartialConfiguration};
1010
use pgt_diagnostics::{DiagnosticExt, Error};
1111
use pgt_fs::{FileSystem, PgTPath};
1212
use pgt_workspace::Workspace;
@@ -386,11 +386,11 @@ impl Session {
386386
/// This function attempts to read the `postgrestools.jsonc` configuration file from
387387
/// the root URI and update the workspace settings accordingly
388388
#[tracing::instrument(level = "trace", skip(self))]
389-
pub(crate) async fn load_workspace_settings(&self) {
389+
pub(crate) async fn load_workspace_settings(&self, params: Option<PartialConfiguration>) {
390390
// Providing a custom configuration path will not allow to support workspaces
391391
if let Some(config_path) = &self.config_path {
392392
let base_path = ConfigurationPathHint::FromUser(config_path.clone());
393-
let status = self.load_pgt_configuration_file(base_path).await;
393+
let status = self.load_pgt_configuration_file(base_path, params).await;
394394
self.set_configuration_status(status);
395395
} else if let Some(folders) = self.get_workspace_folders() {
396396
info!("Detected workspace folder.");
@@ -401,9 +401,10 @@ impl Session {
401401
match base_path {
402402
Ok(base_path) => {
403403
let status = self
404-
.load_pgt_configuration_file(ConfigurationPathHint::FromWorkspace(
405-
base_path,
406-
))
404+
.load_pgt_configuration_file(
405+
ConfigurationPathHint::FromWorkspace(base_path),
406+
params.clone(),
407+
)
407408
.await;
408409
self.set_configuration_status(status);
409410
}
@@ -420,14 +421,15 @@ impl Session {
420421
None => ConfigurationPathHint::default(),
421422
Some(path) => ConfigurationPathHint::FromLsp(path),
422423
};
423-
let status = self.load_pgt_configuration_file(base_path).await;
424+
let status = self.load_pgt_configuration_file(base_path, params).await;
424425
self.set_configuration_status(status);
425426
}
426427
}
427428

428429
async fn load_pgt_configuration_file(
429430
&self,
430431
base_path: ConfigurationPathHint,
432+
extra_config: Option<PartialConfiguration>,
431433
) -> ConfigurationStatus {
432434
match load_configuration(&self.fs, base_path.clone()) {
433435
Ok(loaded_configuration) => {
@@ -446,7 +448,10 @@ impl Session {
446448
Ok((vcs_base_path, gitignore_matches)) => {
447449
let result = self.workspace.update_settings(UpdateSettingsParams {
448450
workspace_directory: self.fs.working_directory(),
449-
configuration: fs_configuration,
451+
configuration: match extra_config {
452+
Some(config) => fs_configuration.clone().merge(config),
453+
None => fs_configuration,
454+
},
450455
vcs_base_path,
451456
gitignore_matches,
452457
skip_db: false,

crates/pgt_workspace/src/workspace/server.rs

+1
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ impl Workspace for WorkspaceServer {
165165
)?;
166166

167167
tracing::info!("Updated settings in workspace");
168+
tracing::debug!("Updated settings are {:#?}", self.settings());
168169

169170
if !params.skip_db {
170171
self.connection

0 commit comments

Comments
 (0)