Skip to content

Commit 5837acc

Browse files
committed
Add basic hover and completion doc tests for macro generated items
1 parent 4c655c0 commit 5837acc

File tree

3 files changed

+182
-2
lines changed

3 files changed

+182
-2
lines changed

crates/ra_hir_def/src/docs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,6 @@ fn expand_doc_attrs(owner: &dyn ast::AttrsOwner) -> Option<String> {
109109
if docs.is_empty() {
110110
None
111111
} else {
112-
Some(docs)
112+
Some(docs.trim_end_matches("\n\n").to_owned())
113113
}
114114
}

crates/ra_ide/src/completion.rs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,81 @@ pub(crate) fn completions(
125125

126126
Some(acc)
127127
}
128+
129+
#[cfg(test)]
130+
mod tests {
131+
use crate::completion::completion_config::CompletionConfig;
132+
use crate::mock_analysis::analysis_and_position;
133+
134+
struct DetailAndDocumentation<'a> {
135+
detail: &'a str,
136+
documentation: &'a str,
137+
}
138+
139+
fn check_detail_and_documentation(fixture: &str, expected: DetailAndDocumentation) {
140+
let (analysis, position) = analysis_and_position(fixture);
141+
let config = CompletionConfig::default();
142+
let completions = analysis.completions(&config, position).unwrap().unwrap();
143+
for item in completions {
144+
if item.detail() == Some(expected.detail) {
145+
let opt = item.documentation();
146+
let doc = opt.as_ref().map(|it| it.as_str());
147+
assert_eq!(doc, Some(expected.documentation));
148+
return;
149+
}
150+
}
151+
panic!("completion detail not found: {}", expected.detail)
152+
}
153+
154+
#[test]
155+
fn test_completion_detail_from_macro_generated_struct_fn_doc_attr() {
156+
check_detail_and_documentation(
157+
r#"
158+
//- /lib.rs
159+
macro_rules! bar {
160+
() => {
161+
struct Bar;
162+
impl Bar {
163+
#[doc = "Do the foo"]
164+
fn foo(&self) {}
165+
}
166+
}
167+
}
168+
169+
bar!();
170+
171+
fn foo() {
172+
let bar = Bar;
173+
bar.fo<|>;
174+
}
175+
"#,
176+
DetailAndDocumentation { detail: "fn foo(&self)", documentation: "Do the foo" },
177+
);
178+
}
179+
180+
#[test]
181+
fn test_completion_detail_from_macro_generated_struct_fn_doc_comment() {
182+
check_detail_and_documentation(
183+
r#"
184+
//- /lib.rs
185+
macro_rules! bar {
186+
() => {
187+
struct Bar;
188+
impl Bar {
189+
/// Do the foo
190+
fn foo(&self) {}
191+
}
192+
}
193+
}
194+
195+
bar!();
196+
197+
fn foo() {
198+
let bar = Bar;
199+
bar.fo<|>;
200+
}
201+
"#,
202+
DetailAndDocumentation { detail: "fn foo(&self)", documentation: " Do the foo" },
203+
);
204+
}
205+
}

crates/ra_ide/src/hover.rs

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ fn expand_doc_attrs(owner: &dyn ast::AttrsOwner) -> Option<String> {
256256
if docs.is_empty() {
257257
None
258258
} else {
259-
Some(docs)
259+
Some(docs.trim_end_matches("\n\n").to_owned())
260260
}
261261
}
262262

@@ -996,4 +996,106 @@ fn func(foo: i32) { if true { <|>foo; }; }
996996
&["mod my"],
997997
);
998998
}
999+
1000+
#[test]
1001+
fn test_hover_struct_doc_comment() {
1002+
check_hover_result(
1003+
r#"
1004+
//- /lib.rs
1005+
/// bar docs
1006+
struct Bar;
1007+
1008+
fn foo() {
1009+
let bar = Ba<|>r;
1010+
}
1011+
"#,
1012+
&["struct Bar\n```\n___\n\nbar docs"],
1013+
);
1014+
}
1015+
1016+
#[test]
1017+
fn test_hover_struct_doc_attr() {
1018+
check_hover_result(
1019+
r#"
1020+
//- /lib.rs
1021+
#[doc = "bar docs"]
1022+
struct Bar;
1023+
1024+
fn foo() {
1025+
let bar = Ba<|>r;
1026+
}
1027+
"#,
1028+
&["struct Bar\n```\n___\n\nbar docs"],
1029+
);
1030+
}
1031+
1032+
#[test]
1033+
fn test_hover_struct_doc_attr_multiple_and_mixed() {
1034+
check_hover_result(
1035+
r#"
1036+
//- /lib.rs
1037+
/// bar docs 0
1038+
#[doc = "bar docs 1"]
1039+
#[doc = "bar docs 2"]
1040+
struct Bar;
1041+
1042+
fn foo() {
1043+
let bar = Ba<|>r;
1044+
}
1045+
"#,
1046+
&["struct Bar\n```\n___\n\nbar docs 0\n\nbar docs 1\n\nbar docs 2"],
1047+
);
1048+
}
1049+
1050+
#[test]
1051+
fn test_hover_macro_generated_struct_fn_doc_comment() {
1052+
check_hover_result(
1053+
r#"
1054+
//- /lib.rs
1055+
macro_rules! bar {
1056+
() => {
1057+
struct Bar;
1058+
impl Bar {
1059+
/// Do the foo
1060+
fn foo(&self) {}
1061+
}
1062+
}
1063+
}
1064+
1065+
bar!();
1066+
1067+
fn foo() {
1068+
let bar = Bar;
1069+
bar.fo<|>o();
1070+
}
1071+
"#,
1072+
&["Bar\n```\n\n```rust\nfn foo(&self)\n```\n___\n\n Do the foo"],
1073+
);
1074+
}
1075+
1076+
#[test]
1077+
fn test_hover_macro_generated_struct_fn_doc_attr() {
1078+
check_hover_result(
1079+
r#"
1080+
//- /lib.rs
1081+
macro_rules! bar {
1082+
() => {
1083+
struct Bar;
1084+
impl Bar {
1085+
#[doc = "Do the foo"]
1086+
fn foo(&self) {}
1087+
}
1088+
}
1089+
}
1090+
1091+
bar!();
1092+
1093+
fn foo() {
1094+
let bar = Bar;
1095+
bar.fo<|>o();
1096+
}
1097+
"#,
1098+
&["Bar\n```\n\n```rust\nfn foo(&self)\n```\n___\n\nDo the foo"],
1099+
);
1100+
}
9991101
}

0 commit comments

Comments
 (0)