From 0fe43a124bb2b135cfd1268fda2941c3ac170c96 Mon Sep 17 00:00:00 2001 From: vsrs Date: Wed, 3 Jun 2020 17:35:26 +0300 Subject: Add capabilities tests. --- crates/rust-analyzer/src/lsp_ext.rs | 4 +- crates/rust-analyzer/src/main_loop/handlers.rs | 7 +- crates/rust-analyzer/tests/heavy_tests/main.rs | 180 +++++++++++++++++++++++++ 3 files changed, 184 insertions(+), 7 deletions(-) (limited to 'crates') diff --git a/crates/rust-analyzer/src/lsp_ext.rs b/crates/rust-analyzer/src/lsp_ext.rs index 75ea48892..1371f6cb4 100644 --- a/crates/rust-analyzer/src/lsp_ext.rs +++ b/crates/rust-analyzer/src/lsp_ext.rs @@ -273,8 +273,8 @@ impl Request for HoverRequest { pub struct Hover { #[serde(flatten)] pub hover: lsp_types::Hover, - #[serde(skip_serializing_if = "Option::is_none")] - pub actions: Option>, + #[serde(skip_serializing_if = "Vec::is_empty")] + pub actions: Vec, } #[derive(Debug, PartialEq, Clone, Default, Deserialize, Serialize)] diff --git a/crates/rust-analyzer/src/main_loop/handlers.rs b/crates/rust-analyzer/src/main_loop/handlers.rs index 894df5837..3ff779702 100644 --- a/crates/rust-analyzer/src/main_loop/handlers.rs +++ b/crates/rust-analyzer/src/main_loop/handlers.rs @@ -555,7 +555,7 @@ pub fn handle_hover( }), range: Some(range), }, - actions: Some(prepare_hover_actions(&snap, info.info.actions())), + actions: prepare_hover_actions(&snap, info.info.actions()), }; Ok(Some(hover)) @@ -1170,10 +1170,7 @@ fn show_references_command( } fn to_command_link(command: Command, tooltip: String) -> lsp_ext::CommandLink { - lsp_ext::CommandLink { - tooltip: Some(tooltip), - command, - } + lsp_ext::CommandLink { tooltip: Some(tooltip), command } } fn show_impl_command_link( diff --git a/crates/rust-analyzer/tests/heavy_tests/main.rs b/crates/rust-analyzer/tests/heavy_tests/main.rs index ad3476310..78c6195d7 100644 --- a/crates/rust-analyzer/tests/heavy_tests/main.rs +++ b/crates/rust-analyzer/tests/heavy_tests/main.rs @@ -715,3 +715,183 @@ pub fn foo(_input: TokenStream) -> TokenStream { let value = res.get("contents").unwrap().get("value").unwrap().to_string(); assert_eq!(value, r#""```rust\nfoo::Bar\n```\n\n```rust\nfn bar()\n```""#) } + +#[test] +fn test_client_support_hover_actions() { + if skip_slow_tests() { + return; + } + + let server = Project::with_fixture( + r#" +//- Cargo.toml +[package] +name = "foo" +version = "0.0.0" + +//- src/lib.rs +struct Foo(u32); + +struct NoImpl(u32); + +impl Foo { + fn new() -> Self { + Self(1) + } +} +"#, + ) + .with_config(|config| { + config.client_caps.hover_actions = true; + }) + .server(); + + server.wait_until_workspace_is_loaded(); + + // has 1 implementation + server.request::( + HoverParams { + text_document_position_params: TextDocumentPositionParams::new( + server.doc_id("src/lib.rs"), + Position::new(0, 9), + ), + work_done_progress_params: Default::default(), + }, + json!({ + "actions": [{ + "commands": [{ + "arguments": [ + "file:///[..]src/lib.rs", + { + "character": 7, + "line": 0 + }, + [{ + "range": { "end": { "character": 1, "line": 8 }, "start": { "character": 0, "line": 4 } }, + "uri": "file:///[..]src/lib.rs" + }] + ], + "command": "rust-analyzer.showReferences", + "title": "1 implementation", + "tooltip": "Go to implementations" + }] + }], + "contents": { "kind": "markdown", "value": "```rust\nfoo\n```\n\n```rust\nstruct Foo\n```" }, + "range": { "end": { "character": 10, "line": 0 }, "start": { "character": 7, "line": 0 } } + }) + ); + + // no hover + server.request::( + HoverParams { + text_document_position_params: TextDocumentPositionParams::new( + server.doc_id("src/lib.rs"), + Position::new(1, 0), + ), + work_done_progress_params: Default::default(), + }, + json!(null), + ); + + // no implementations + server.request::( + HoverParams { + text_document_position_params: TextDocumentPositionParams::new( + server.doc_id("src/lib.rs"), + Position::new(2, 12), + ), + work_done_progress_params: Default::default(), + }, + json!({ + "actions": [{ + "commands": [{ + "arguments": [ + "file:///[..]src/lib.rs", + { "character": 7, "line": 2 }, + [] + ], + "command": "rust-analyzer.showReferences", + "title": "0 implementations", + "tooltip": "Go to implementations" + }] + }], + "contents": { "kind": "markdown", "value": "```rust\nfoo\n```\n\n```rust\nstruct NoImpl\n```" }, + "range": { "end": { "character": 13, "line": 2 }, "start": { "character": 7, "line": 2 } } + }) + ); +} + +#[test] +fn test_client_does_not_support_hover_actions() { + if skip_slow_tests() { + return; + } + + let server = Project::with_fixture( + r#" +//- Cargo.toml +[package] +name = "foo" +version = "0.0.0" + +//- src/lib.rs +struct Foo(u32); + +struct NoImpl(u32); + +impl Foo { + fn new() -> Self { + Self(1) + } +} +"#, + ) + .with_config(|config| { + config.client_caps.hover_actions = false; + }) + .server(); + + server.wait_until_workspace_is_loaded(); + + // has 1 implementation + server.request::( + HoverParams { + text_document_position_params: TextDocumentPositionParams::new( + server.doc_id("src/lib.rs"), + Position::new(0, 9), + ), + work_done_progress_params: Default::default(), + }, + json!({ + "contents": { "kind": "markdown", "value": "```rust\nfoo\n```\n\n```rust\nstruct Foo\n```" }, + "range": { "end": { "character": 10, "line": 0 }, "start": { "character": 7, "line": 0 } } + }) + ); + + // no hover + server.request::( + HoverParams { + text_document_position_params: TextDocumentPositionParams::new( + server.doc_id("src/lib.rs"), + Position::new(1, 0), + ), + work_done_progress_params: Default::default(), + }, + json!(null), + ); + + // no implementations + server.request::( + HoverParams { + text_document_position_params: TextDocumentPositionParams::new( + server.doc_id("src/lib.rs"), + Position::new(2, 12), + ), + work_done_progress_params: Default::default(), + }, + json!({ + "contents": { "kind": "markdown", "value": "```rust\nfoo\n```\n\n```rust\nstruct NoImpl\n```" }, + "range": { "end": { "character": 13, "line": 2 }, "start": { "character": 7, "line": 2 } } + }) + ); +} -- cgit v1.2.3