on master help sign in
Passing extra formatting options to LSPs (#2635)

* allows passing extra formatting options to LSPs - adds optional field 'format' to [[language]] sections in 'languages.toml' - passes specified options the LSPs via FormattingOptions * cleaner conversion of formatting properties * move formatting options inside lsp::Client * cleans up formatting properties merge

commitf92a25a856d572c7bd4b8e597a3f86ce211e81d5 authorfarwyler <1705805+farwyler@users.noreply.github.com> dateSun 5 Jun 2022, 12:50 (4 years ago) parentb2bd87d
4 files changed+34
book/src/languages.md+12 helix-core/src/syntax.rs+1 helix-lsp/src/client.rs+20 helix-view/src/document.rs+1
book/src/languages.md+12
@@ -14,6 +14,18 @@ name = "rust"
1414auto-format = false
1515```
1616
17+## LSP formatting options
18+
19+Use `format` field to pass extra formatting options to [Document Formatting Requests](https://github.com/microsoft/language-server-protocol/blob/gh-pages/_specifications/specification-3-16.md#document-formatting-request--leftwards_arrow_with_hook).
20+
21+```toml
22+[[language]]
23+name = "typescript"
24+auto-format = true
25+# pass format options according to https://github.com/typescript-language-server/typescript-language-server#workspacedidchangeconfiguration omitting the "[language].format." prefix.
26+config = { format = { "semicolons" = "insert", "insertSpaceBeforeFunctionParenthesis" = true } }
27+```
28+
1729## Tree-sitter grammars
1830
1931Tree-sitter grammars can also be configured in `languages.toml`:
helix-core/src/syntax.rs+1
@@ -78,6 +78,7 @@ pub struct LanguageConfiguration {
7878
7979 #[serde(default)]
8080 pub auto_format: bool,
81+
8182 #[serde(default)]
8283 pub diagnostic_severity: Severity,
8384
helix-lsp/src/client.rs+20
@@ -7,7 +7,9 @@ use anyhow::anyhow;
77use helix_core::{find_root, ChangeSet, Rope};
88use jsonrpc_core as jsonrpc;
99use lsp_types as lsp;
10+use serde::Deserialize;
1011use serde_json::Value;
12+use std::collections::HashMap;
1113use std::future::Future;
1214use std::process::Stdio;
1315use std::sync::{
@@ -693,6 +695,24 @@ impl Client {
693695 };
694696 // TODO: return err::unavailable so we can fall back to tree sitter formatting
695697
698+ // merge FormattingOptions with 'config.format'
699+ let config_format = self
700+ .config
701+ .as_ref()
702+ .and_then(|cfg| cfg.get("format"))
703+ .and_then(|fmt| HashMap::<String, lsp::FormattingProperty>::deserialize(fmt).ok());
704+
705+ let options = if let Some(mut properties) = config_format {
706+ // passed in options take precedence over 'config.format'
707+ properties.extend(options.properties);
708+ lsp::FormattingOptions {
709+ properties,
710+ ..options
711+ }
712+ } else {
713+ options
714+ };
715+
696716 let params = lsp::DocumentFormattingParams {
697717 text_document,
698718 options,
helix-view/src/document.rs+1
@@ -410,6 +410,7 @@ impl Document {
410410 let language_server = self.language_server()?;
411411 let text = self.text.clone();
412412 let offset_encoding = language_server.offset_encoding();
413+
413414 let request = language_server.text_document_formatting(
414415 self.identifier(),
415416 lsp::FormattingOptions {