diff --git a/book/src/languages.md b/book/src/languages.md index b446e4de0889..8c27785e2197 100644 --- a/book/src/languages.md +++ b/book/src/languages.md @@ -23,7 +23,7 @@ Use `format` field to pass extra formatting options to [Document Formatting Requ name = "typescript" auto-format = true # pass format options according to https://github.com/typescript-language-server/typescript-language-server#workspacedidchangeconfiguration omitting the "[language].format." prefix. -format = { "semicolons" = "insert", "insertSpaceBeforeFunctionParenthesis" = true } +config = { format = { "semicolons" = "insert", "insertSpaceBeforeFunctionParenthesis" = true } } ``` ## Tree-sitter grammars diff --git a/helix-core/src/syntax.rs b/helix-core/src/syntax.rs index 4c0149ea11df..ca497b648229 100644 --- a/helix-core/src/syntax.rs +++ b/helix-core/src/syntax.rs @@ -78,8 +78,6 @@ pub struct LanguageConfiguration { #[serde(default)] pub auto_format: bool, - #[serde(default, skip_serializing, deserialize_with = "deserialize_lsp_config")] - pub format: Option, #[serde(default)] pub diagnostic_severity: Severity, diff --git a/helix-lsp/src/client.rs b/helix-lsp/src/client.rs index 08201b3f4268..55143de5991e 100644 --- a/helix-lsp/src/client.rs +++ b/helix-lsp/src/client.rs @@ -8,6 +8,7 @@ use helix_core::{find_root, ChangeSet, Rope}; use jsonrpc_core as jsonrpc; use lsp_types as lsp; use serde_json::Value; +use std::collections::HashMap; use std::future::Future; use std::process::Stdio; use std::sync::{ @@ -693,9 +694,28 @@ impl Client { }; // TODO: return err::unavailable so we can fall back to tree sitter formatting + // merge FormattingOptions with values from lsp config + let mut merged_options = options.clone(); + if let Some(format) = self + .config + .as_ref() + .and_then(|cfg| cfg.get("format")) + .and_then(|fmt| fmt.as_object()) + { + for (key, value) in format { + // upstream properties take precedence + if merged_options.properties.get(key).is_some() { + continue; + } + if let Ok(prop) = serde_json::from_value(value.clone()) { + merged_options.properties.insert(key.to_owned(), prop); + } + } + } + let params = lsp::DocumentFormattingParams { text_document, - options, + options: merged_options, work_done_progress_params: lsp::WorkDoneProgressParams { work_done_token }, }; diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs index 1c3952c56000..2c4b5de96b95 100644 --- a/helix-view/src/document.rs +++ b/helix-view/src/document.rs @@ -411,24 +411,11 @@ impl Document { let text = self.text.clone(); let offset_encoding = language_server.offset_encoding(); - let mut properties = HashMap::new(); - if let Some(fmt) = self - .language_config() - .and_then(|cfg| cfg.format.as_ref().and_then(|c| c.as_object())) - { - for (key, value) in fmt.iter() { - if let Ok(prop) = serde_json::from_value(value.clone()) { - properties.insert(key.to_owned(), prop); - } - } - } - let request = language_server.text_document_formatting( self.identifier(), lsp::FormattingOptions { tab_size: self.tab_width() as u32, insert_spaces: matches!(self.indent_style, IndentStyle::Spaces(_)), - properties, ..Default::default() }, None,