Skip to content

Commit

Permalink
Passing extra formatting options to LSPs (#2635)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
farwyler authored Jun 5, 2022
1 parent b2bd87d commit f92a25a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
12 changes: 12 additions & 0 deletions book/src/languages.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ name = "rust"
auto-format = false
```

## LSP formatting options

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).

```toml
[[language]]
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.
config = { format = { "semicolons" = "insert", "insertSpaceBeforeFunctionParenthesis" = true } }
```

## Tree-sitter grammars

Tree-sitter grammars can also be configured in `languages.toml`:
Expand Down
1 change: 1 addition & 0 deletions helix-core/src/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ pub struct LanguageConfiguration {

#[serde(default)]
pub auto_format: bool,

#[serde(default)]
pub diagnostic_severity: Severity,

Expand Down
20 changes: 20 additions & 0 deletions helix-lsp/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ use anyhow::anyhow;
use helix_core::{find_root, ChangeSet, Rope};
use jsonrpc_core as jsonrpc;
use lsp_types as lsp;
use serde::Deserialize;
use serde_json::Value;
use std::collections::HashMap;
use std::future::Future;
use std::process::Stdio;
use std::sync::{
Expand Down Expand Up @@ -693,6 +695,24 @@ impl Client {
};
// TODO: return err::unavailable so we can fall back to tree sitter formatting

// merge FormattingOptions with 'config.format'
let config_format = self
.config
.as_ref()
.and_then(|cfg| cfg.get("format"))
.and_then(|fmt| HashMap::<String, lsp::FormattingProperty>::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 params = lsp::DocumentFormattingParams {
text_document,
options,
Expand Down
1 change: 1 addition & 0 deletions helix-view/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ impl Document {
let language_server = self.language_server()?;
let text = self.text.clone();
let offset_encoding = language_server.offset_encoding();

let request = language_server.text_document_formatting(
self.identifier(),
lsp::FormattingOptions {
Expand Down

0 comments on commit f92a25a

Please sign in to comment.