mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-25 11:17:13 +00:00
Add basic hover and completion doc tests for macro generated items
This commit is contained in:
parent
4c655c01f3
commit
5837acce53
@ -109,6 +109,6 @@ fn expand_doc_attrs(owner: &dyn ast::AttrsOwner) -> Option<String> {
|
||||
if docs.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(docs)
|
||||
Some(docs.trim_end_matches("\n\n").to_owned())
|
||||
}
|
||||
}
|
||||
|
@ -125,3 +125,81 @@ pub(crate) fn completions(
|
||||
|
||||
Some(acc)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::completion::completion_config::CompletionConfig;
|
||||
use crate::mock_analysis::analysis_and_position;
|
||||
|
||||
struct DetailAndDocumentation<'a> {
|
||||
detail: &'a str,
|
||||
documentation: &'a str,
|
||||
}
|
||||
|
||||
fn check_detail_and_documentation(fixture: &str, expected: DetailAndDocumentation) {
|
||||
let (analysis, position) = analysis_and_position(fixture);
|
||||
let config = CompletionConfig::default();
|
||||
let completions = analysis.completions(&config, position).unwrap().unwrap();
|
||||
for item in completions {
|
||||
if item.detail() == Some(expected.detail) {
|
||||
let opt = item.documentation();
|
||||
let doc = opt.as_ref().map(|it| it.as_str());
|
||||
assert_eq!(doc, Some(expected.documentation));
|
||||
return;
|
||||
}
|
||||
}
|
||||
panic!("completion detail not found: {}", expected.detail)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_completion_detail_from_macro_generated_struct_fn_doc_attr() {
|
||||
check_detail_and_documentation(
|
||||
r#"
|
||||
//- /lib.rs
|
||||
macro_rules! bar {
|
||||
() => {
|
||||
struct Bar;
|
||||
impl Bar {
|
||||
#[doc = "Do the foo"]
|
||||
fn foo(&self) {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bar!();
|
||||
|
||||
fn foo() {
|
||||
let bar = Bar;
|
||||
bar.fo<|>;
|
||||
}
|
||||
"#,
|
||||
DetailAndDocumentation { detail: "fn foo(&self)", documentation: "Do the foo" },
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_completion_detail_from_macro_generated_struct_fn_doc_comment() {
|
||||
check_detail_and_documentation(
|
||||
r#"
|
||||
//- /lib.rs
|
||||
macro_rules! bar {
|
||||
() => {
|
||||
struct Bar;
|
||||
impl Bar {
|
||||
/// Do the foo
|
||||
fn foo(&self) {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bar!();
|
||||
|
||||
fn foo() {
|
||||
let bar = Bar;
|
||||
bar.fo<|>;
|
||||
}
|
||||
"#,
|
||||
DetailAndDocumentation { detail: "fn foo(&self)", documentation: " Do the foo" },
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -256,7 +256,7 @@ fn expand_doc_attrs(owner: &dyn ast::AttrsOwner) -> Option<String> {
|
||||
if docs.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(docs)
|
||||
Some(docs.trim_end_matches("\n\n").to_owned())
|
||||
}
|
||||
}
|
||||
|
||||
@ -996,4 +996,106 @@ fn func(foo: i32) { if true { <|>foo; }; }
|
||||
&["mod my"],
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_hover_struct_doc_comment() {
|
||||
check_hover_result(
|
||||
r#"
|
||||
//- /lib.rs
|
||||
/// bar docs
|
||||
struct Bar;
|
||||
|
||||
fn foo() {
|
||||
let bar = Ba<|>r;
|
||||
}
|
||||
"#,
|
||||
&["struct Bar\n```\n___\n\nbar docs"],
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_hover_struct_doc_attr() {
|
||||
check_hover_result(
|
||||
r#"
|
||||
//- /lib.rs
|
||||
#[doc = "bar docs"]
|
||||
struct Bar;
|
||||
|
||||
fn foo() {
|
||||
let bar = Ba<|>r;
|
||||
}
|
||||
"#,
|
||||
&["struct Bar\n```\n___\n\nbar docs"],
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_hover_struct_doc_attr_multiple_and_mixed() {
|
||||
check_hover_result(
|
||||
r#"
|
||||
//- /lib.rs
|
||||
/// bar docs 0
|
||||
#[doc = "bar docs 1"]
|
||||
#[doc = "bar docs 2"]
|
||||
struct Bar;
|
||||
|
||||
fn foo() {
|
||||
let bar = Ba<|>r;
|
||||
}
|
||||
"#,
|
||||
&["struct Bar\n```\n___\n\nbar docs 0\n\nbar docs 1\n\nbar docs 2"],
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_hover_macro_generated_struct_fn_doc_comment() {
|
||||
check_hover_result(
|
||||
r#"
|
||||
//- /lib.rs
|
||||
macro_rules! bar {
|
||||
() => {
|
||||
struct Bar;
|
||||
impl Bar {
|
||||
/// Do the foo
|
||||
fn foo(&self) {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bar!();
|
||||
|
||||
fn foo() {
|
||||
let bar = Bar;
|
||||
bar.fo<|>o();
|
||||
}
|
||||
"#,
|
||||
&["Bar\n```\n\n```rust\nfn foo(&self)\n```\n___\n\n Do the foo"],
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_hover_macro_generated_struct_fn_doc_attr() {
|
||||
check_hover_result(
|
||||
r#"
|
||||
//- /lib.rs
|
||||
macro_rules! bar {
|
||||
() => {
|
||||
struct Bar;
|
||||
impl Bar {
|
||||
#[doc = "Do the foo"]
|
||||
fn foo(&self) {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bar!();
|
||||
|
||||
fn foo() {
|
||||
let bar = Bar;
|
||||
bar.fo<|>o();
|
||||
}
|
||||
"#,
|
||||
&["Bar\n```\n\n```rust\nfn foo(&self)\n```\n___\n\nDo the foo"],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user