Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing extra formatting options to LSPs #2635

Merged
merged 4 commits into from
Jun 5, 2022

Conversation

farwyler
Copy link
Contributor

@farwyler farwyler commented Jun 1, 2022

  • adds optional field format to [[language]] sections in languages.toml

  • passes specified options to the LSPs via FormattingOptions

  • updated documentation accordingly

typescript example:

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

- adds optional field 'format' to [[language]] sections in 'languages.toml'

- passes specified options the LSPs via FormattingOptions
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 }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a mistake we did with language server configuration: I'd prefer format was nested under the language-server key. Though maybe we can keep it toplevel

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would make sense inside the language server configuration, especially if we ever plan to support multiple LS per language. I have seen neovim setups where multiple LS get attached to a buffer, mostly for chaining formatting functionality. Can't say that i liked it. Have you guys thought in that direction? I haven't noticed the topic here, but i am just getting into the project.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i guess we need this. 6c29b0e nests it inside the lsp config now.

Comment on lines 420 to 429
let prop = if let Some(s) = value.as_str() {
lsp::FormattingProperty::String(s.to_owned())
} else if let Some(b) = value.as_bool() {
lsp::FormattingProperty::Bool(b)
} else if let Some(n) = value.as_i64() {
lsp::FormattingProperty::Number(n as i32)
} else {
log::warn!("invalid formatting property type {:?} for {}", value, key);
continue;
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should work:

let prop: lsp::FormattingProperty = serde_json::from_value(value)

Copy link
Contributor Author

@farwyler farwyler Jun 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great. i changed it to 563fccf

for (key, value) in fmt.iter() {
  if let Ok(prop) = serde_json::from_value(value.clone()) {
    properties.insert(key.to_owned(), prop);
  }
}

@@ -78,6 +78,9 @@ pub struct LanguageConfiguration {

#[serde(default)]
pub auto_format: bool,
#[serde(default, skip_serializing, deserialize_with = "deserialize_lsp_config")]
pub format: Option<serde_json::Value>,
Copy link
Member

@archseer archseer Jun 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, why not just use HashMap<String, FormattingProperty> here?

Copy link
Contributor Author

@farwyler farwyler Jun 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would make helix-core dependent on the lsp crate, just to get the FormattingProperty type into LanguageConfiguration.
no longer a concern with 6c29b0e

helix-lsp/src/client.rs Outdated Show resolved Hide resolved
Comment on lines 705 to 713
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);
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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 format_config: HashMap<String, lsp::FormattingProperty> = serde_json::from_value(format.clone());
// options take precedence over format_config
// extend replaces values with existing keys with new values returned from the iterator.
options = format_config.extend(options);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks. cleaned this up and eliminated clone

@farwyler farwyler requested a review from archseer June 4, 2022 15:40
Copy link
Member

@archseer archseer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@archseer archseer merged commit f92a25a into helix-editor:master Jun 5, 2022
@farwyler farwyler deleted the lsp-format-options branch June 6, 2022 05:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants