From 9dbfb9b4eb18d314d0108eb774c14dd9a22358a7 Mon Sep 17 00:00:00 2001 From: yuri <164520341+yohaneYuri@users.noreply.github.com> Date: Tue, 10 Jun 2025 22:55:46 +0800 Subject: [PATCH] Merge formatting options and `config.format` in range format request (#13734) --- helix-lsp/src/client.rs | 43 +++++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/helix-lsp/src/client.rs b/helix-lsp/src/client.rs index c901ad334..afb3b3a56 100644 --- a/helix-lsp/src/client.rs +++ b/helix-lsp/src/client.rs @@ -176,6 +176,29 @@ impl Client { self.did_change_workspace(vec![workspace_for_uri(root_uri)], Vec::new()) } + /// Merge FormattingOptions with 'config.format' and return it + fn get_merged_formatting_options( + &self, + options: lsp::FormattingOptions, + ) -> lsp::FormattingOptions { + let config_format = self + .config + .as_ref() + .and_then(|cfg| cfg.get("format")) + .and_then(|fmt| HashMap::::deserialize(fmt).ok()); + + if let Some(mut properties) = config_format { + // passed in options take precedence over 'config.format' + properties.extend(options.properties); + lsp::FormattingOptions { + properties, + ..options + } + } else { + options + } + } + #[allow(clippy::type_complexity, clippy::too_many_arguments)] pub fn start( cmd: &str, @@ -1168,23 +1191,7 @@ impl Client { _ => return None, }; - // merge FormattingOptions with 'config.format' - let config_format = self - .config - .as_ref() - .and_then(|cfg| cfg.get("format")) - .and_then(|fmt| HashMap::::deserialize(fmt).ok()); - - let options = if let Some(mut properties) = config_format { - // passed in options take precedence over 'config.format' - properties.extend(options.properties); - lsp::FormattingOptions { - properties, - ..options - } - } else { - options - }; + let options = self.get_merged_formatting_options(options); let params = lsp::DocumentFormattingParams { text_document, @@ -1210,6 +1217,8 @@ impl Client { _ => return None, }; + let options = self.get_merged_formatting_options(options); + let params = lsp::DocumentRangeFormattingParams { text_document, range,