From 306a441e3b7b8cc1892d0ccce579e2236d045db4 Mon Sep 17 00:00:00 2001 From: suxin2017 Date: Wed, 28 Aug 2024 17:16:21 +0800 Subject: [PATCH 1/4] done --- crates/biome_lsp/src/capabilities.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/crates/biome_lsp/src/capabilities.rs b/crates/biome_lsp/src/capabilities.rs index fb59a6c723c9..155b6885ef4d 100644 --- a/crates/biome_lsp/src/capabilities.rs +++ b/crates/biome_lsp/src/capabilities.rs @@ -1,7 +1,8 @@ use crate::converters::{negotiated_encoding, PositionEncoding, WideEncoding}; use tower_lsp::lsp_types::{ - ClientCapabilities, CodeActionProviderCapability, DocumentOnTypeFormattingOptions, OneOf, - PositionEncodingKind, ServerCapabilities, TextDocumentSyncCapability, TextDocumentSyncKind, + ClientCapabilities, CodeActionKind, CodeActionOptions, CodeActionProviderCapability, + DocumentOnTypeFormattingOptions, OneOf, PositionEncodingKind, ServerCapabilities, + TextDocumentSyncCapability, TextDocumentSyncKind, }; /// The capabilities to send from server as part of [`InitializeResult`] @@ -64,7 +65,13 @@ pub(crate) fn server_capabilities(capabilities: &ClientCapabilities) -> ServerCa 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)), + code_action_provider: Some(CodeActionProviderCapability::Options(CodeActionOptions { + code_action_kinds: Some(vec![ + CodeActionKind::SOURCE_FIX_ALL, + CodeActionKind::new("source.fixAll.biome"), + ]), + ..Default::default() + })), rename_provider: None, ..Default::default() } From 7312b072aaf787b8b75b804960a971f465948c12 Mon Sep 17 00:00:00 2001 From: suxin2017 Date: Wed, 28 Aug 2024 19:39:51 +0800 Subject: [PATCH 2/4] add test and same as eslint lsp --- crates/biome_lsp/src/capabilities.rs | 24 ++++-- crates/biome_lsp/tests/server.rs | 109 ++++++++++++++++++++++++++- 2 files changed, 125 insertions(+), 8 deletions(-) diff --git a/crates/biome_lsp/src/capabilities.rs b/crates/biome_lsp/src/capabilities.rs index 155b6885ef4d..5a68da7ce78b 100644 --- a/crates/biome_lsp/src/capabilities.rs +++ b/crates/biome_lsp/src/capabilities.rs @@ -51,6 +51,22 @@ pub(crate) fn server_capabilities(capabilities: &ClientCapabilities) -> ServerCa } }); + let code_action_provider = capabilities + .text_document + .as_ref() + .and_then(|text_document| text_document.code_action.as_ref()) + .and_then(|code_action| code_action.code_action_literal_support.as_ref()) + .map(|_| { + CodeActionProviderCapability::Options(CodeActionOptions { + code_action_kinds: Some(vec![ + CodeActionKind::SOURCE_FIX_ALL, + CodeActionKind::new("source.fixAll.biome"), + ]), + ..Default::default() + }) + }) + .or(Some(CodeActionProviderCapability::Simple(true))); + ServerCapabilities { position_encoding: Some(match negotiated_encoding(capabilities) { PositionEncoding::Utf8 => PositionEncodingKind::UTF8, @@ -65,13 +81,7 @@ pub(crate) fn server_capabilities(capabilities: &ClientCapabilities) -> ServerCa 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::Options(CodeActionOptions { - code_action_kinds: Some(vec![ - CodeActionKind::SOURCE_FIX_ALL, - CodeActionKind::new("source.fixAll.biome"), - ]), - ..Default::default() - })), + code_action_provider, rename_provider: None, ..Default::default() } diff --git a/crates/biome_lsp/tests/server.rs b/crates/biome_lsp/tests/server.rs index 7506d1efd575..b5a6c8cdc9a5 100644 --- a/crates/biome_lsp/tests/server.rs +++ b/crates/biome_lsp/tests/server.rs @@ -28,6 +28,12 @@ use tower::{Service, ServiceExt}; use tower_lsp::jsonrpc; use tower_lsp::jsonrpc::Response; use tower_lsp::lsp_types as lsp; +use tower_lsp::lsp_types::CodeActionClientCapabilities; +use tower_lsp::lsp_types::CodeActionKind; +use tower_lsp::lsp_types::CodeActionKindLiteralSupport; +use tower_lsp::lsp_types::CodeActionLiteralSupport; +use tower_lsp::lsp_types::CodeActionOptions; +use tower_lsp::lsp_types::CodeActionProviderCapability; use tower_lsp::lsp_types::DidOpenTextDocumentParams; use tower_lsp::lsp_types::DocumentFormattingParams; use tower_lsp::lsp_types::FormattingOptions; @@ -36,6 +42,7 @@ use tower_lsp::lsp_types::InitializedParams; use tower_lsp::lsp_types::Position; use tower_lsp::lsp_types::PublishDiagnosticsParams; use tower_lsp::lsp_types::Range; +use tower_lsp::lsp_types::TextDocumentClientCapabilities; use tower_lsp::lsp_types::TextDocumentContentChangeEvent; use tower_lsp::lsp_types::TextDocumentIdentifier; use tower_lsp::lsp_types::TextDocumentItem; @@ -183,7 +190,6 @@ impl Server { ) .await? .context("initialize returned None")?; - Ok(()) } @@ -405,6 +411,107 @@ where Ok(()) } +#[allow(deprecated)] +#[tokio::test] +async fn server_capabilities_fix_all_code_action_kinds() -> Result<()> { + let factory = ServerFactory::default(); + let (service, client) = factory.create(None).into_inner(); + let (stream, sink) = client.split(); + let mut server = Server::new(service); + + let (sender, _) = channel(CHANNEL_BUFFER_SIZE); + let reader = tokio::spawn(client_handler(stream, sink, sender)); + let expect_code_action_provider = + Some(CodeActionProviderCapability::Options(CodeActionOptions { + code_action_kinds: Some(vec![ + CodeActionKind::SOURCE_FIX_ALL, + CodeActionKind::new("source.fixAll.biome"), + ]), + ..Default::default() + })); + let res: InitializeResult = server + .request( + "initialize", + "_init", + InitializeParams { + process_id: None, + root_path: None, + root_uri: Some(url!("")), + initialization_options: None, + capabilities: ClientCapabilities { + text_document: Some(TextDocumentClientCapabilities { + code_action: Some(CodeActionClientCapabilities { + code_action_literal_support: Some(CodeActionLiteralSupport { + code_action_kind: CodeActionKindLiteralSupport { + value_set: Vec::new(), + }, + }), + ..CodeActionClientCapabilities::default() + }), + + ..TextDocumentClientCapabilities::default() + }), + + ..ClientCapabilities::default() + }, + trace: None, + workspace_folders: None, + client_info: None, + locale: None, + }, + ) + .await? + .context("initialize returned None")?; + + assert_eq!( + res.capabilities.code_action_provider, + expect_code_action_provider + ); + server.shutdown().await?; + reader.abort(); + + Ok(()) +} + +#[allow(deprecated)] +#[tokio::test] +async fn server_capabilities_default_code_action_kinds() -> Result<()> { + let factory = ServerFactory::default(); + let (service, client) = factory.create(None).into_inner(); + let (stream, sink) = client.split(); + let mut server = Server::new(service); + + let (sender, _) = channel(CHANNEL_BUFFER_SIZE); + let reader = tokio::spawn(client_handler(stream, sink, sender)); + let expect_code_action_provider = Some(CodeActionProviderCapability::Simple(true)); + let res: InitializeResult = server + .request( + "initialize", + "_init", + InitializeParams { + process_id: None, + root_path: None, + root_uri: Some(url!("")), + initialization_options: None, + capabilities: ClientCapabilities::default(), + trace: None, + workspace_folders: None, + client_info: None, + locale: None, + }, + ) + .await? + .context("initialize returned None")?; + assert_eq!( + res.capabilities.code_action_provider, + expect_code_action_provider + ); + server.shutdown().await?; + reader.abort(); + + Ok(()) +} + #[tokio::test] async fn basic_lifecycle() -> Result<()> { let factory = ServerFactory::default(); From 5cc06865b9b82724a52aace66bd2ea84d25065a9 Mon Sep 17 00:00:00 2001 From: suxin2017 Date: Wed, 28 Aug 2024 19:42:21 +0800 Subject: [PATCH 3/4] only support biome fixall --- crates/biome_lsp/src/capabilities.rs | 1 - crates/biome_lsp/tests/server.rs | 1 - 2 files changed, 2 deletions(-) diff --git a/crates/biome_lsp/src/capabilities.rs b/crates/biome_lsp/src/capabilities.rs index 5a68da7ce78b..6752c742ad03 100644 --- a/crates/biome_lsp/src/capabilities.rs +++ b/crates/biome_lsp/src/capabilities.rs @@ -59,7 +59,6 @@ pub(crate) fn server_capabilities(capabilities: &ClientCapabilities) -> ServerCa .map(|_| { CodeActionProviderCapability::Options(CodeActionOptions { code_action_kinds: Some(vec![ - CodeActionKind::SOURCE_FIX_ALL, CodeActionKind::new("source.fixAll.biome"), ]), ..Default::default() diff --git a/crates/biome_lsp/tests/server.rs b/crates/biome_lsp/tests/server.rs index b5a6c8cdc9a5..33495c61c30b 100644 --- a/crates/biome_lsp/tests/server.rs +++ b/crates/biome_lsp/tests/server.rs @@ -424,7 +424,6 @@ async fn server_capabilities_fix_all_code_action_kinds() -> Result<()> { let expect_code_action_provider = Some(CodeActionProviderCapability::Options(CodeActionOptions { code_action_kinds: Some(vec![ - CodeActionKind::SOURCE_FIX_ALL, CodeActionKind::new("source.fixAll.biome"), ]), ..Default::default() From eb2aded2d4e1f093d1fba278b61ffb318d76dd43 Mon Sep 17 00:00:00 2001 From: suxin2017 Date: Wed, 28 Aug 2024 19:45:33 +0800 Subject: [PATCH 4/4] format --- crates/biome_lsp/src/capabilities.rs | 4 +--- crates/biome_lsp/tests/server.rs | 4 +--- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/crates/biome_lsp/src/capabilities.rs b/crates/biome_lsp/src/capabilities.rs index 6752c742ad03..ecf8771e0319 100644 --- a/crates/biome_lsp/src/capabilities.rs +++ b/crates/biome_lsp/src/capabilities.rs @@ -58,9 +58,7 @@ pub(crate) fn server_capabilities(capabilities: &ClientCapabilities) -> ServerCa .and_then(|code_action| code_action.code_action_literal_support.as_ref()) .map(|_| { CodeActionProviderCapability::Options(CodeActionOptions { - code_action_kinds: Some(vec![ - CodeActionKind::new("source.fixAll.biome"), - ]), + code_action_kinds: Some(vec![CodeActionKind::new("source.fixAll.biome")]), ..Default::default() }) }) diff --git a/crates/biome_lsp/tests/server.rs b/crates/biome_lsp/tests/server.rs index 33495c61c30b..63ca91a2f2d0 100644 --- a/crates/biome_lsp/tests/server.rs +++ b/crates/biome_lsp/tests/server.rs @@ -423,9 +423,7 @@ async fn server_capabilities_fix_all_code_action_kinds() -> Result<()> { let reader = tokio::spawn(client_handler(stream, sink, sender)); let expect_code_action_provider = Some(CodeActionProviderCapability::Options(CodeActionOptions { - code_action_kinds: Some(vec![ - CodeActionKind::new("source.fixAll.biome"), - ]), + code_action_kinds: Some(vec![CodeActionKind::new("source.fixAll.biome")]), ..Default::default() })); let res: InitializeResult = server