diff --git a/CHANGELOG.md b/CHANGELOG.md index 81c8aa0a0a4e..7cd38f9fe86c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/crates/biome_lsp/src/capabilities.rs b/crates/biome_lsp/src/capabilities.rs index 2b1e2ea0927c..fb59a6c723c9 100644 --- a/crates/biome_lsp/src/capabilities.rs +++ b/crates/biome_lsp/src/capabilities.rs @@ -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, @@ -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() diff --git a/website/src/content/docs/internals/changelog.mdx b/website/src/content/docs/internals/changelog.mdx index f894a5ad3fbb..e42c71152cf3 100644 --- a/website/src/content/docs/internals/changelog.mdx +++ b/website/src/content/docs/internals/changelog.mdx @@ -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