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

fix(lsp): register formatter dynamically if possible #1590

Merged
merged 7 commits into from
Jan 18, 2024
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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