Improve readability of signature help tests

This commit is contained in:
Jonas Schievink 2022-03-22 15:03:19 +01:00
parent 5d2cd18765
commit c9cefb9916

View File

@ -208,8 +208,11 @@ fn signature_help_for_generics(
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::iter;
use expect_test::{expect, Expect}; use expect_test::{expect, Expect};
use ide_db::base_db::{fixture::ChangeFixture, FilePosition}; use ide_db::base_db::{fixture::ChangeFixture, FilePosition};
use stdx::format_to;
use crate::RootDatabase; use crate::RootDatabase;
@ -233,26 +236,32 @@ mod tests {
"# "#
); );
let (db, position) = position(&fixture); let (db, position) = position(&fixture);
let call_info = crate::signature_help::signature_help(&db, position); let sig_help = crate::signature_help::signature_help(&db, position);
let actual = match call_info { let actual = match sig_help {
Some(call_info) => { Some(sig_help) => {
let docs = match &call_info.doc { let mut rendered = String::new();
None => "".to_string(), if let Some(docs) = &sig_help.doc {
Some(docs) => format!("{}\n------\n", docs.as_str()), format_to!(rendered, "{}\n------\n", docs.as_str());
}; }
let params = call_info format_to!(rendered, "{}\n", sig_help.signature);
.parameter_labels() let mut offset = 0;
.enumerate() for (i, range) in sig_help.parameter_ranges().iter().enumerate() {
.map(|(i, param)| { let is_active = sig_help.active_parameter == Some(i);
if Some(i) == call_info.active_parameter {
format!("<{}>", param) let start = u32::from(range.start());
} else { let gap = start.checked_sub(offset).unwrap_or_else(|| {
param.to_string() panic!("parameter ranges out of order: {:?}", sig_help.parameter_ranges())
} });
}) rendered.extend(iter::repeat(' ').take(gap as usize));
.collect::<Vec<_>>() let width = u32::from(range.end() - range.start());
.join(", "); let marker = if is_active { '^' } else { '-' };
format!("{}{}\n({})\n", docs, call_info.signature, params) rendered.extend(iter::repeat(marker).take(width as usize));
offset += gap + width;
}
if !sig_help.parameter_ranges().is_empty() {
format_to!(rendered, "\n");
}
rendered
} }
None => String::new(), None => String::new(),
}; };
@ -268,7 +277,7 @@ fn bar() { foo($03, ); }
"#, "#,
expect![[r#" expect![[r#"
fn foo(x: u32, y: u32) -> u32 fn foo(x: u32, y: u32) -> u32
(<x: u32>, y: u32) ^^^^^^ ------
"#]], "#]],
); );
check( check(
@ -278,7 +287,7 @@ fn bar() { foo(3$0, ); }
"#, "#,
expect![[r#" expect![[r#"
fn foo(x: u32, y: u32) -> u32 fn foo(x: u32, y: u32) -> u32
(<x: u32>, y: u32) ^^^^^^ ------
"#]], "#]],
); );
check( check(
@ -288,7 +297,7 @@ fn bar() { foo(3,$0 ); }
"#, "#,
expect![[r#" expect![[r#"
fn foo(x: u32, y: u32) -> u32 fn foo(x: u32, y: u32) -> u32
(x: u32, <y: u32>) ------ ^^^^^^
"#]], "#]],
); );
check( check(
@ -298,7 +307,7 @@ fn bar() { foo(3, $0); }
"#, "#,
expect![[r#" expect![[r#"
fn foo(x: u32, y: u32) -> u32 fn foo(x: u32, y: u32) -> u32
(x: u32, <y: u32>) ------ ^^^^^^
"#]], "#]],
); );
} }
@ -312,7 +321,7 @@ fn bar() { foo($0); }
"#, "#,
expect![[r#" expect![[r#"
fn foo(x: u32, y: u32) -> u32 fn foo(x: u32, y: u32) -> u32
(<x: u32>, y: u32) ^^^^^^ ------
"#]], "#]],
); );
} }
@ -329,7 +338,7 @@ fn bar() { foo($03, ); }
"#, "#,
expect![[r#" expect![[r#"
fn foo(x: i32, y: {unknown}) -> u32 fn foo(x: i32, y: {unknown}) -> u32
(<x: i32>, y: {unknown}) ^^^^^^ ------------
"#]], "#]],
); );
} }
@ -343,7 +352,6 @@ fn bar() { foo($0); }
"#, "#,
expect![[r#" expect![[r#"
fn foo() -> {unknown} fn foo() -> {unknown}
()
"#]], "#]],
); );
} }
@ -360,7 +368,6 @@ fn bar() {
"#, "#,
expect![[r#" expect![[r#"
fn new() fn new()
()
"#]], "#]],
); );
} }
@ -379,7 +386,6 @@ fn bar() {
"#, "#,
expect![[r#" expect![[r#"
fn do_it(&self) fn do_it(&self)
()
"#]], "#]],
); );
} }
@ -397,7 +403,7 @@ fn main() { S.foo($0); }
"#, "#,
expect![[r#" expect![[r#"
fn foo(&self, x: i32) fn foo(&self, x: i32)
(<x: i32>) ^^^^^^
"#]], "#]],
); );
} }
@ -415,7 +421,7 @@ fn main() { S(1u32).foo($0); }
"#, "#,
expect![[r#" expect![[r#"
fn foo(&self, x: u32) fn foo(&self, x: u32)
(<x: u32>) ^^^^^^
"#]], "#]],
); );
} }
@ -433,7 +439,7 @@ fn main() { S::foo($0); }
"#, "#,
expect![[r#" expect![[r#"
fn foo(self: &S, x: i32) fn foo(self: &S, x: i32)
(<self: &S>, x: i32) ^^^^^^^^ ------
"#]], "#]],
); );
} }
@ -453,11 +459,11 @@ fn bar() {
} }
"#, "#,
expect![[r#" expect![[r#"
test test
------ ------
fn foo(j: u32) -> u32 fn foo(j: u32) -> u32
(<j: u32>) ^^^^^^
"#]], "#]],
); );
} }
@ -482,19 +488,19 @@ pub fn do() {
add_one($0 add_one($0
}"#, }"#,
expect![[r##" expect![[r##"
Adds one to the number given. Adds one to the number given.
# Examples # Examples
``` ```
let five = 5; let five = 5;
assert_eq!(6, my_crate::add_one(5)); assert_eq!(6, my_crate::add_one(5));
``` ```
------ ------
fn add_one(x: i32) -> i32 fn add_one(x: i32) -> i32
(<x: i32>) ^^^^^^
"##]], "##]],
); );
} }
@ -524,19 +530,19 @@ pub fn do_it() {
} }
"#, "#,
expect![[r##" expect![[r##"
Adds one to the number given. Adds one to the number given.
# Examples # Examples
``` ```
let five = 5; let five = 5;
assert_eq!(6, my_crate::add_one(5)); assert_eq!(6, my_crate::add_one(5));
``` ```
------ ------
fn add_one(x: i32) -> i32 fn add_one(x: i32) -> i32
(<x: i32>) ^^^^^^
"##]], "##]],
); );
} }
@ -568,13 +574,13 @@ pub fn foo(mut r: WriteHandler<()>) {
} }
"#, "#,
expect![[r#" expect![[r#"
Method is called when writer finishes. Method is called when writer finishes.
By default this method stops actor's `Context`. By default this method stops actor's `Context`.
------ ------
fn finished(&mut self, ctx: &mut {unknown}) fn finished(&mut self, ctx: &mut {unknown})
(<ctx: &mut {unknown}>) ^^^^^^^^^^^^^^^^^^^
"#]], "#]],
); );
} }
@ -605,7 +611,7 @@ fn main() {
"#, "#,
expect![[r#" expect![[r#"
fn bar(&self, _: u32) fn bar(&self, _: u32)
(<_: u32>) ^^^^^^
"#]], "#]],
); );
} }
@ -621,11 +627,11 @@ fn main() {
} }
"#, "#,
expect![[r#" expect![[r#"
A cool tuple struct A cool tuple struct
------ ------
struct S(u32, i32) struct S(u32, i32)
(u32, <i32>) --- ^^^
"#]], "#]],
); );
} }
@ -640,7 +646,7 @@ fn main() {
"#, "#,
expect![[r#" expect![[r#"
struct S({unknown}) struct S({unknown})
(<{unknown}>) ^^^^^^^^^
"#]], "#]],
); );
} }
@ -663,11 +669,11 @@ fn main() {
} }
"#, "#,
expect![[r#" expect![[r#"
A Variant A Variant
------ ------
enum E::A(i32) enum E::A(i32)
(<i32>) ^^^
"#]], "#]],
); );
} }
@ -717,7 +723,6 @@ id! {
"#, "#,
expect![[r#" expect![[r#"
fn foo() fn foo()
()
"#]], "#]],
); );
} }
@ -734,7 +739,7 @@ fn main() {
"#, "#,
expect![[r#" expect![[r#"
(S) -> i32 (S) -> i32
(<S>) ^
"#]], "#]],
) )
} }
@ -749,7 +754,7 @@ fn main(f: fn(i32, f64) -> char) {
"#, "#,
expect![[r#" expect![[r#"
(i32, f64) -> char (i32, f64) -> char
(i32, <f64>) --- ^^^
"#]], "#]],
) )
} }
@ -763,9 +768,9 @@ fn main() {
foo($0 foo($0
}"#, }"#,
expect![[r#" expect![[r#"
fn foo(foo: u32, bar: u32) fn foo(foo: u32, bar: u32)
(<foo: u32>, bar: u32) ^^^^^^^^ --------
"#]], "#]],
); );
// check with surrounding space // check with surrounding space
check( check(
@ -775,9 +780,9 @@ fn main() {
foo( $0 foo( $0
}"#, }"#,
expect![[r#" expect![[r#"
fn foo(foo: u32, bar: u32) fn foo(foo: u32, bar: u32)
(<foo: u32>, bar: u32) ^^^^^^^^ --------
"#]], "#]],
) )
} }
@ -799,7 +804,7 @@ fn f() {
Option docs. Option docs.
------ ------
enum Option<T> enum Option<T>
(<T>) ^
"#]], "#]],
); );
} }
@ -826,7 +831,7 @@ fn f() {
None docs. None docs.
------ ------
enum Option<T> enum Option<T>
(<T>) ^
"#]], "#]],
); );
} }
@ -849,7 +854,7 @@ fn f() {
"#, "#,
expect![[r#" expect![[r#"
fn f<G: Tr<()>, H> fn f<G: Tr<()>, H>
(G: Tr<()>, <H>) --------- ^
"#]], "#]],
); );
} }
@ -872,7 +877,7 @@ fn f() {
"#, "#,
expect![[r#" expect![[r#"
fn f<T: Tr, U> fn f<T: Tr, U>
(<T: Tr>, U) ^^^^^ -
"#]], "#]],
); );
} }
@ -892,9 +897,9 @@ fn f() {
} }
"#, "#,
expect![[r#" expect![[r#"
fn f<T> fn f<T>
(<T>) ^
"#]], "#]],
); );
} }
@ -909,9 +914,9 @@ fn f() {
} }
"#, "#,
expect![[r#" expect![[r#"
fn callee<'a, const A: (), T, const C: u8> fn callee<'a, const A: (), T, const C: u8>
('a, <const A: ()>, T, const C: u8) -- ^^^^^^^^^^^ - -----------
"#]], "#]],
); );
} }
} }