Skip to content

Commit

Permalink
move formatting options inside lsp::Client
Browse files Browse the repository at this point in the history
  • Loading branch information
farwyler committed Jun 2, 2022
1 parent 563fccf commit 6c29b0e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 17 deletions.
2 changes: 1 addition & 1 deletion book/src/languages.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions helix-core/src/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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_json::Value>,

#[serde(default)]
pub diagnostic_severity: Severity,
Expand Down
22 changes: 21 additions & 1 deletion helix-lsp/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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 },
};

Expand Down
13 changes: 0 additions & 13 deletions helix-view/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 6c29b0e

Please sign in to comment.