Skip to content

Commit 0fe43a1

Browse files
committed
Add capabilities tests.
1 parent e35418c commit 0fe43a1

File tree

3 files changed

+184
-7
lines changed

3 files changed

+184
-7
lines changed

crates/rust-analyzer/src/lsp_ext.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,8 @@ impl Request for HoverRequest {
273273
pub struct Hover {
274274
#[serde(flatten)]
275275
pub hover: lsp_types::Hover,
276-
#[serde(skip_serializing_if = "Option::is_none")]
277-
pub actions: Option<Vec<CommandLinkGroup>>,
276+
#[serde(skip_serializing_if = "Vec::is_empty")]
277+
pub actions: Vec<CommandLinkGroup>,
278278
}
279279

280280
#[derive(Debug, PartialEq, Clone, Default, Deserialize, Serialize)]

crates/rust-analyzer/src/main_loop/handlers.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ pub fn handle_hover(
555555
}),
556556
range: Some(range),
557557
},
558-
actions: Some(prepare_hover_actions(&snap, info.info.actions())),
558+
actions: prepare_hover_actions(&snap, info.info.actions()),
559559
};
560560

561561
Ok(Some(hover))
@@ -1170,10 +1170,7 @@ fn show_references_command(
11701170
}
11711171

11721172
fn to_command_link(command: Command, tooltip: String) -> lsp_ext::CommandLink {
1173-
lsp_ext::CommandLink {
1174-
tooltip: Some(tooltip),
1175-
command,
1176-
}
1173+
lsp_ext::CommandLink { tooltip: Some(tooltip), command }
11771174
}
11781175

11791176
fn show_impl_command_link(

crates/rust-analyzer/tests/heavy_tests/main.rs

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,3 +715,183 @@ pub fn foo(_input: TokenStream) -> TokenStream {
715715
let value = res.get("contents").unwrap().get("value").unwrap().to_string();
716716
assert_eq!(value, r#""```rust\nfoo::Bar\n```\n\n```rust\nfn bar()\n```""#)
717717
}
718+
719+
#[test]
720+
fn test_client_support_hover_actions() {
721+
if skip_slow_tests() {
722+
return;
723+
}
724+
725+
let server = Project::with_fixture(
726+
r#"
727+
//- Cargo.toml
728+
[package]
729+
name = "foo"
730+
version = "0.0.0"
731+
732+
//- src/lib.rs
733+
struct Foo(u32);
734+
735+
struct NoImpl(u32);
736+
737+
impl Foo {
738+
fn new() -> Self {
739+
Self(1)
740+
}
741+
}
742+
"#,
743+
)
744+
.with_config(|config| {
745+
config.client_caps.hover_actions = true;
746+
})
747+
.server();
748+
749+
server.wait_until_workspace_is_loaded();
750+
751+
// has 1 implementation
752+
server.request::<HoverRequest>(
753+
HoverParams {
754+
text_document_position_params: TextDocumentPositionParams::new(
755+
server.doc_id("src/lib.rs"),
756+
Position::new(0, 9),
757+
),
758+
work_done_progress_params: Default::default(),
759+
},
760+
json!({
761+
"actions": [{
762+
"commands": [{
763+
"arguments": [
764+
"file:///[..]src/lib.rs",
765+
{
766+
"character": 7,
767+
"line": 0
768+
},
769+
[{
770+
"range": { "end": { "character": 1, "line": 8 }, "start": { "character": 0, "line": 4 } },
771+
"uri": "file:///[..]src/lib.rs"
772+
}]
773+
],
774+
"command": "rust-analyzer.showReferences",
775+
"title": "1 implementation",
776+
"tooltip": "Go to implementations"
777+
}]
778+
}],
779+
"contents": { "kind": "markdown", "value": "```rust\nfoo\n```\n\n```rust\nstruct Foo\n```" },
780+
"range": { "end": { "character": 10, "line": 0 }, "start": { "character": 7, "line": 0 } }
781+
})
782+
);
783+
784+
// no hover
785+
server.request::<HoverRequest>(
786+
HoverParams {
787+
text_document_position_params: TextDocumentPositionParams::new(
788+
server.doc_id("src/lib.rs"),
789+
Position::new(1, 0),
790+
),
791+
work_done_progress_params: Default::default(),
792+
},
793+
json!(null),
794+
);
795+
796+
// no implementations
797+
server.request::<HoverRequest>(
798+
HoverParams {
799+
text_document_position_params: TextDocumentPositionParams::new(
800+
server.doc_id("src/lib.rs"),
801+
Position::new(2, 12),
802+
),
803+
work_done_progress_params: Default::default(),
804+
},
805+
json!({
806+
"actions": [{
807+
"commands": [{
808+
"arguments": [
809+
"file:///[..]src/lib.rs",
810+
{ "character": 7, "line": 2 },
811+
[]
812+
],
813+
"command": "rust-analyzer.showReferences",
814+
"title": "0 implementations",
815+
"tooltip": "Go to implementations"
816+
}]
817+
}],
818+
"contents": { "kind": "markdown", "value": "```rust\nfoo\n```\n\n```rust\nstruct NoImpl\n```" },
819+
"range": { "end": { "character": 13, "line": 2 }, "start": { "character": 7, "line": 2 } }
820+
})
821+
);
822+
}
823+
824+
#[test]
825+
fn test_client_does_not_support_hover_actions() {
826+
if skip_slow_tests() {
827+
return;
828+
}
829+
830+
let server = Project::with_fixture(
831+
r#"
832+
//- Cargo.toml
833+
[package]
834+
name = "foo"
835+
version = "0.0.0"
836+
837+
//- src/lib.rs
838+
struct Foo(u32);
839+
840+
struct NoImpl(u32);
841+
842+
impl Foo {
843+
fn new() -> Self {
844+
Self(1)
845+
}
846+
}
847+
"#,
848+
)
849+
.with_config(|config| {
850+
config.client_caps.hover_actions = false;
851+
})
852+
.server();
853+
854+
server.wait_until_workspace_is_loaded();
855+
856+
// has 1 implementation
857+
server.request::<HoverRequest>(
858+
HoverParams {
859+
text_document_position_params: TextDocumentPositionParams::new(
860+
server.doc_id("src/lib.rs"),
861+
Position::new(0, 9),
862+
),
863+
work_done_progress_params: Default::default(),
864+
},
865+
json!({
866+
"contents": { "kind": "markdown", "value": "```rust\nfoo\n```\n\n```rust\nstruct Foo\n```" },
867+
"range": { "end": { "character": 10, "line": 0 }, "start": { "character": 7, "line": 0 } }
868+
})
869+
);
870+
871+
// no hover
872+
server.request::<HoverRequest>(
873+
HoverParams {
874+
text_document_position_params: TextDocumentPositionParams::new(
875+
server.doc_id("src/lib.rs"),
876+
Position::new(1, 0),
877+
),
878+
work_done_progress_params: Default::default(),
879+
},
880+
json!(null),
881+
);
882+
883+
// no implementations
884+
server.request::<HoverRequest>(
885+
HoverParams {
886+
text_document_position_params: TextDocumentPositionParams::new(
887+
server.doc_id("src/lib.rs"),
888+
Position::new(2, 12),
889+
),
890+
work_done_progress_params: Default::default(),
891+
},
892+
json!({
893+
"contents": { "kind": "markdown", "value": "```rust\nfoo\n```\n\n```rust\nstruct NoImpl\n```" },
894+
"range": { "end": { "character": 13, "line": 2 }, "start": { "character": 7, "line": 2 } }
895+
})
896+
);
897+
}

0 commit comments

Comments
 (0)