Skip to content

Commit

Permalink
fix(lsp): register formatter dynamically if possible (biomejs#1590)
Browse files Browse the repository at this point in the history
Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>
  • Loading branch information
nhedger and ematipico committed Jan 24, 2024
1 parent bf0004b commit 472e702
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ Read our [guidelines for writing a good changelog entry](https://github.com/biom

### CLI

### LSP

#### Bug fixes

- Fix [#1584](https://github.com/biomejs/biome/issues/1584). Ensure the LSP only registers the formatter once. Contributed by @nhedger

### Configuration

### Editors
Expand Down
51 changes: 45 additions & 6 deletions crates/biome_lsp/src/capabilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,48 @@ use tower_lsp::lsp_types::{
///
/// [`InitializeResult`]: lspower::lsp::InitializeResult
pub(crate) fn server_capabilities(capabilities: &ClientCapabilities) -> ServerCapabilities {
let supports_formatter_dynamic_registration = capabilities
.text_document
.as_ref()
.and_then(|text_document| text_document.formatting.as_ref())
.and_then(|formatting| formatting.dynamic_registration)
.and_then(|supported| {
if supported {
None
} else {
Some(OneOf::Left(true))
}
});

let supports_range_formatter_dynamic_registration = capabilities
.text_document
.as_ref()
.and_then(|text_document| text_document.range_formatting.as_ref())
.and_then(|range_formatting| range_formatting.dynamic_registration)
.and_then(|supported| {
if supported {
None
} else {
Some(OneOf::Left(true))
}
});

let supports_on_type_formatter_dynamic_registration = capabilities
.text_document
.as_ref()
.and_then(|text_document| text_document.on_type_formatting.as_ref())
.and_then(|on_type_formatting| on_type_formatting.dynamic_registration)
.and_then(|supported| {
if supported {
None
} else {
Some(DocumentOnTypeFormattingOptions {
first_trigger_character: String::from("}"),
more_trigger_character: Some(vec![String::from("]"), String::from(")")]),
})
}
});

ServerCapabilities {
position_encoding: Some(match negotiated_encoding(capabilities) {
PositionEncoding::Utf8 => PositionEncodingKind::UTF8,
Expand All @@ -19,12 +61,9 @@ pub(crate) fn server_capabilities(capabilities: &ClientCapabilities) -> ServerCa
text_document_sync: Some(TextDocumentSyncCapability::Kind(
TextDocumentSyncKind::INCREMENTAL,
)),
document_formatting_provider: Some(OneOf::Left(true)),
document_range_formatting_provider: Some(OneOf::Left(true)),
document_on_type_formatting_provider: Some(DocumentOnTypeFormattingOptions {
first_trigger_character: String::from("}"),
more_trigger_character: Some(vec![String::from("]"), String::from(")")]),
}),
document_formatting_provider: supports_formatter_dynamic_registration,
document_range_formatting_provider: supports_range_formatter_dynamic_registration,
document_on_type_formatting_provider: supports_on_type_formatter_dynamic_registration,
code_action_provider: Some(CodeActionProviderCapability::Simple(true)),
rename_provider: None,
..Default::default()
Expand Down
6 changes: 6 additions & 0 deletions website/src/content/docs/internals/changelog.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ Read our [guidelines for writing a good changelog entry](https://github.com/biom

### CLI

### LSP

#### Bug fixes

- Fix [#1584](https://github.com/biomejs/biome/issues/1584). Ensure the LSP only registers the formatter once. Contributed by @nhedger

### Configuration

### Editors
Expand Down

0 comments on commit 472e702

Please sign in to comment.