From a40f78f92ad2050d6178dfd70374701c6bc826ae Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 22 Jan 2021 18:59:22 +0300 Subject: [PATCH] More useful fn detail in completion Detail should be rendered as shtort one line, just dumping fn header there is not useful, despite the fact that TS does this. The fact that this is a function should be indicated by the icon, the same goes for pub/const/async etc qualitfiers name is already present in the lable (and arg list should be a part of that, as in idea) But the return type is the small genuinerlly useful bit of info we can show here --- crates/completion/src/completions/dot.rs | 22 ++++---- .../completion/src/completions/flyimport.rs | 16 +++--- .../src/completions/qualified_path.rs | 54 +++++++++---------- .../completion/src/completions/trait_impl.rs | 5 -- .../src/completions/unqualified_path.rs | 50 ++++++++--------- crates/completion/src/item.rs | 3 +- crates/completion/src/lib.rs | 9 ++-- crates/completion/src/render.rs | 12 ++--- crates/completion/src/render/function.rs | 7 +-- 9 files changed, 85 insertions(+), 93 deletions(-) diff --git a/crates/completion/src/completions/dot.rs b/crates/completion/src/completions/dot.rs index d04eef65a79..0880a38304e 100644 --- a/crates/completion/src/completions/dot.rs +++ b/crates/completion/src/completions/dot.rs @@ -83,7 +83,7 @@ fn foo(s: S) { s.$0 } "#, expect![[r#" fd foo u32 - me bar() fn bar(&self) + me bar() -> () "#]], ); } @@ -99,7 +99,7 @@ fn foo(self) { self.$0 } "#, expect![[r#" fd the_field (u32,) - me foo() fn foo(self) + me foo() -> () "#]], ) } @@ -115,7 +115,7 @@ fn foo(&self) { self.$0 } "#, expect![[r#" fd the_field (u32, i32) - me foo() fn foo(&self) + me foo() -> () "#]], ) } @@ -165,7 +165,7 @@ pub(crate) fn the_method(&self) {} fn foo(a: A) { a.$0 } "#, expect![[r#" - me the_method() pub(crate) fn the_method(&self) + me the_method() -> () "#]], ); } @@ -198,7 +198,7 @@ fn the_other_method(&self) {} fn foo(a: A) { a.$0 } "#, expect![[r#" - me the_method() fn the_method(&self) + me the_method() -> () "#]], ) } @@ -213,7 +213,7 @@ impl Trait for A {} fn foo(a: A) { a.$0 } "#, expect![[r#" - me the_method() fn the_method(&self) + me the_method() -> () "#]], ); } @@ -228,7 +228,7 @@ impl Trait for T {} fn foo(a: &A) { a.$0 } ", expect![[r#" - me the_method() fn the_method(&self) + me the_method() -> () "#]], ); } @@ -246,7 +246,7 @@ impl Trait for A {} fn foo(a: A) { a.$0 } ", expect![[r#" - me the_method() fn the_method(&self) + me the_method() -> () "#]], ); } @@ -300,7 +300,7 @@ fn foo(&self) { } "#, expect![[r#" - me blah() pub fn blah(&self) + me blah() -> () "#]], ); } @@ -409,7 +409,7 @@ fn foo() { } "#, expect![[r#" - me the_method() pub fn the_method(&self) + me the_method() -> () "#]], ); } @@ -424,7 +424,7 @@ impl S { fn foo(&self) {} } fn main() { make_s!().f$0; } "#, expect![[r#" - me foo() fn foo(&self) + me foo() -> () "#]], ) } diff --git a/crates/completion/src/completions/flyimport.rs b/crates/completion/src/completions/flyimport.rs index dc0b38a165d..6591127b1de 100644 --- a/crates/completion/src/completions/flyimport.rs +++ b/crates/completion/src/completions/flyimport.rs @@ -366,8 +366,8 @@ fn main() { check( fixture, expect![[r#" - fn weird_function() (dep::test_mod::TestTrait) fn weird_function() - "#]], + fn weird_function() (dep::test_mod::TestTrait) -> () + "#]], ); check_edit( @@ -459,8 +459,8 @@ fn main() { check( fixture, expect![[r#" - me random_method() (dep::test_mod::TestTrait) fn random_method(&self) - "#]], + me random_method() (dep::test_mod::TestTrait) -> () + "#]], ); check_edit( @@ -629,8 +629,8 @@ fn main() { } "#, expect![[r#" - me random_method() (dep::test_mod::TestTrait) fn random_method(&self) DEPRECATED - "#]], + me random_method() (dep::test_mod::TestTrait) -> () DEPRECATED + "#]], ); check( @@ -660,8 +660,8 @@ fn main() { "#, expect![[r#" ct SPECIAL_CONST (dep::test_mod::TestTrait) DEPRECATED - fn weird_function() (dep::test_mod::TestTrait) fn weird_function() DEPRECATED - "#]], + fn weird_function() (dep::test_mod::TestTrait) -> () DEPRECATED + "#]], ); } } diff --git a/crates/completion/src/completions/qualified_path.rs b/crates/completion/src/completions/qualified_path.rs index 33df2676156..bbeaab49657 100644 --- a/crates/completion/src/completions/qualified_path.rs +++ b/crates/completion/src/completions/qualified_path.rs @@ -359,8 +359,8 @@ fn b(&self) {} fn foo() { let _ = S::$0 } "#, expect![[r#" - fn a() fn a() - me b(…) fn b(&self) + fn a() -> () + me b(…) -> () ct C const C: i32 = 42; ta T type T = i32; "#]], @@ -387,7 +387,7 @@ fn private_method() { } fn foo() { let _ = S::$0 } "#, expect![[r#" - fn public_method() pub(crate) fn public_method() + fn public_method() -> () ct PUBLIC_CONST pub(crate) const PUBLIC_CONST: u32 = 1; ta PublicType pub(crate) type PublicType = u32; "#]], @@ -404,7 +404,7 @@ impl E { fn m() { } } fn foo() { let _ = E::$0 } "#, expect![[r#" - fn m() fn m() + fn m() -> () "#]], ); } @@ -419,7 +419,7 @@ impl U { fn m() { } } fn foo() { let _ = U::$0 } "#, expect![[r#" - fn m() fn m() + fn m() -> () "#]], ); } @@ -449,7 +449,7 @@ fn completes_trait_associated_method_1() { fn foo() { let _ = Trait::$0 } "#, expect![[r#" - fn m() fn m() + fn m() -> () "#]], ); } @@ -466,7 +466,7 @@ impl Trait for S {} fn foo() { let _ = S::$0 } "#, expect![[r#" - fn m() fn m() + fn m() -> () "#]], ); } @@ -483,7 +483,7 @@ impl Trait for S {} fn foo() { let _ = ::$0 } "#, expect![[r#" - fn m() fn m() + fn m() -> () "#]], ); } @@ -512,11 +512,11 @@ fn foo() { T::$0 } ta SubTy type SubTy; ta Ty type Ty; ct C2 const C2: (); - fn subfunc() fn subfunc() - me submethod(…) fn submethod(&self) + fn subfunc() -> () + me submethod(…) -> () ct CONST const CONST: u8; - fn func() fn func() - me method(…) fn method(&self) + fn func() -> () + me method(…) -> () "#]], ); } @@ -552,11 +552,11 @@ fn subfunc() { ta SubTy type SubTy; ta Ty type Ty; ct CONST const CONST: u8 = 0; - fn func() fn func() - me method(…) fn method(&self) + fn func() -> () + me method(…) -> () ct C2 const C2: () = (); - fn subfunc() fn subfunc() - me submethod(…) fn submethod(&self) + fn subfunc() -> () + me submethod(…) -> () "#]], ); } @@ -573,8 +573,8 @@ impl T { fn bar() {} } fn main() { T::$0; } "#, expect![[r#" - fn foo() fn foo() - fn bar() fn bar() + fn foo() -> () + fn bar() -> () "#]], ); } @@ -589,7 +589,7 @@ macro_rules! foo { () => {} } fn main() { let _ = crate::$0 } "#, expect![[r##" - fn main() fn main() + fn main() -> () ma foo!(…) #[macro_export] macro_rules! foo "##]], ); @@ -633,7 +633,7 @@ fn wrong_fn() {} "#, expect![[r#" ct RIGHT_CONST - fn right_fn() fn wrong_fn() + fn right_fn() -> () st RightType "#]], ); @@ -680,8 +680,8 @@ macro_rules! m { ($e:expr) => { $e } } fn foo() {} "#, expect![[r#" - fn main() fn main() - fn foo() fn foo() + fn main() -> () + fn foo() -> () "#]], ); } @@ -699,7 +699,7 @@ pub fn z() {} "#, expect![[r#" md z - fn z() pub fn z() + fn z() -> () "#]], ); } @@ -719,7 +719,7 @@ fn foo() { } "#, expect![[r#" - fn new() pub fn new() -> HashMap + fn new() -> HashMap "#]], ); } @@ -752,8 +752,8 @@ fn main() { } "#, expect![[r#" - fn main() fn main() - fn foo(…) fn foo(a: i32, b: i32) + fn main() -> () + fn foo(…) -> () "#]], ); } @@ -776,7 +776,7 @@ fn foo(self) { expect![[r#" ev Bar () ev Baz () - me foo(…) fn foo(self) + me foo(…) -> () "#]], ); } diff --git a/crates/completion/src/completions/trait_impl.rs b/crates/completion/src/completions/trait_impl.rs index f258ad9c3f0..b999540b84d 100644 --- a/crates/completion/src/completions/trait_impl.rs +++ b/crates/completion/src/completions/trait_impl.rs @@ -679,11 +679,6 @@ impl Test for () { #[test] fn complete_without_name() { let test = |completion: &str, hint: &str, completed: &str, next_sibling: &str| { - println!( - "completion='{}', hint='{}', next_sibling='{}'", - completion, hint, next_sibling - ); - check_edit( completion, &format!( diff --git a/crates/completion/src/completions/unqualified_path.rs b/crates/completion/src/completions/unqualified_path.rs index a289efc34eb..5d62fab9743 100644 --- a/crates/completion/src/completions/unqualified_path.rs +++ b/crates/completion/src/completions/unqualified_path.rs @@ -167,7 +167,7 @@ fn quux(x: i32) { expect![[r#" lc y i32 lc x i32 - fn quux(…) fn quux(x: i32) + fn quux(…) -> () "#]], ); } @@ -189,7 +189,7 @@ fn quux() { expect![[r#" lc b i32 lc a - fn quux() fn quux() + fn quux() -> () "#]], ); } @@ -204,7 +204,7 @@ fn quux() { "#, expect![[r#" lc x - fn quux() fn quux() + fn quux() -> () "#]], ); } @@ -235,14 +235,14 @@ fn completes_generic_params() { r#"fn quux() { $0 }"#, expect![[r#" tp T - fn quux() fn quux() + fn quux() -> () "#]], ); check( r#"fn quux() { $0 }"#, expect![[r#" cp C - fn quux() fn quux() + fn quux() -> () "#]], ); } @@ -253,7 +253,7 @@ fn does_not_complete_lifetimes() { check( r#"fn quux<'a>() { $0 }"#, expect![[r#" - fn quux() fn quux<'a>() + fn quux() -> () "#]], ); } @@ -291,7 +291,7 @@ fn quux() { $0 } "#, expect![[r#" st S - fn quux() fn quux() + fn quux() -> () en E "#]], ); @@ -344,7 +344,7 @@ fn quux() { $0 } } "#, expect![[r#" - fn quux() fn quux() + fn quux() -> () st Bar "#]], ); @@ -359,7 +359,7 @@ fn x() -> $0 "#, expect![[r#" st Foo - fn x() fn x() + fn x() -> () "#]], ); } @@ -380,7 +380,7 @@ fn foo() { expect![[r#" lc bar i32 lc bar i32 - fn foo() fn foo() + fn foo() -> () "#]], ); } @@ -410,7 +410,7 @@ fn foo() { let x: $0 } mod prelude { struct Option; } "#, expect![[r#" - fn foo() fn foo() + fn foo() -> () md std st Option "#]], @@ -440,7 +440,7 @@ macro_rules! concat { } } "#, expect![[r##" - fn f() fn f() + fn f() -> () ma concat!(…) #[macro_export] macro_rules! concat md std "##]], @@ -467,7 +467,7 @@ fn foo() { let x: $0 } mod prelude { struct String; } "#, expect![[r#" - fn foo() fn foo() + fn foo() -> () md std md core st String @@ -498,7 +498,7 @@ fn main() { let v = $0 } expect![[r##" md m1 ma baz!(…) #[macro_export] macro_rules! baz - fn main() fn main() + fn main() -> () md m2 ma bar!(…) macro_rules! bar ma foo!(…) macro_rules! foo @@ -514,7 +514,7 @@ macro_rules! foo { () => {} } fn foo() { $0 } "#, expect![[r#" - fn foo() fn foo() + fn foo() -> () ma foo!(…) macro_rules! foo "#]], ); @@ -528,7 +528,7 @@ macro_rules! foo { () => {} } fn main() { let x: $0 } "#, expect![[r#" - fn main() fn main() + fn main() -> () ma foo!(…) macro_rules! foo "#]], ); @@ -542,7 +542,7 @@ macro_rules! foo { () => {} } fn main() { $0 } "#, expect![[r#" - fn main() fn main() + fn main() -> () ma foo!(…) macro_rules! foo "#]], ); @@ -558,8 +558,8 @@ fn frobnicate() {} } "#, expect![[r#" - fn frobnicate() fn frobnicate() - fn main() fn main() + fn frobnicate() -> () + fn main() -> () "#]], ); } @@ -577,7 +577,7 @@ fn quux(x: i32) { expect![[r#" lc y i32 lc x i32 - fn quux(…) fn quux(x: i32) + fn quux(…) -> () ma m!(…) macro_rules! m "#]], ); @@ -596,7 +596,7 @@ fn quux(x: i32) { expect![[r#" lc y i32 lc x i32 - fn quux(…) fn quux(x: i32) + fn quux(…) -> () ma m!(…) macro_rules! m "#]], ); @@ -615,7 +615,7 @@ fn quux(x: i32) { expect![[r#" lc y i32 lc x i32 - fn quux(…) fn quux(x: i32) + fn quux(…) -> () ma m!(…) macro_rules! m "#]], ); @@ -630,7 +630,7 @@ fn completes_unresolved_uses() { fn main() { $0 } "#, expect![[r#" - fn main() fn main() + fn main() -> () ?? Quux "#]], ); @@ -708,7 +708,7 @@ fn main() { let foo: Foo = Q$0 } ev Foo::Baz () ev Foo::Quux () en Foo - fn main() fn main() + fn main() -> () "#]], ) } @@ -723,7 +723,7 @@ fn f() -> m::E { V$0 } expect![[r#" ev m::E::V () md m - fn f() fn f() -> m::E + fn f() -> E "#]], ) } diff --git a/crates/completion/src/item.rs b/crates/completion/src/item.rs index d2e6a6aeb4f..eeb952ec3c5 100644 --- a/crates/completion/src/item.rs +++ b/crates/completion/src/item.rs @@ -398,14 +398,13 @@ pub(crate) fn snippet_edit(mut self, _cap: SnippetCap, edit: TextEdit) -> Builde self.insert_text_format = InsertTextFormat::Snippet; self.text_edit(edit) } - #[allow(unused)] pub(crate) fn detail(self, detail: impl Into) -> Builder { self.set_detail(Some(detail)) } pub(crate) fn set_detail(mut self, detail: Option>) -> Builder { self.detail = detail.map(Into::into); if let Some(detail) = &self.detail { - if assert_never!(detail.contains('\n'), "multiline detail: {}", detail) { + if assert_never!(detail.contains('\n'), "multiline detail:\n{}", detail) { self.detail = Some(detail.splitn(2, '\n').next().unwrap().to_string()); } } diff --git a/crates/completion/src/lib.rs b/crates/completion/src/lib.rs index 2c4e5452497..50329b4990f 100644 --- a/crates/completion/src/lib.rs +++ b/crates/completion/src/lib.rs @@ -227,7 +227,7 @@ fn foo() { bar.fo$0; } "#, - DetailAndDocumentation { detail: "fn foo(&self)", documentation: "Do the foo" }, + DetailAndDocumentation { detail: "-> ()", documentation: "Do the foo" }, ); } @@ -253,7 +253,7 @@ fn foo() { bar.fo$0; } "#, - DetailAndDocumentation { detail: "fn foo(&self)", documentation: " Do the foo" }, + DetailAndDocumentation { detail: "-> ()", documentation: " Do the foo" }, ); } @@ -277,10 +277,7 @@ fn bar() { for c in fo$0 } "#, - DetailAndDocumentation { - detail: "fn foo() -> &'static str", - documentation: "Do the foo", - }, + DetailAndDocumentation { detail: "-> &str", documentation: "Do the foo" }, ); } } diff --git a/crates/completion/src/render.rs b/crates/completion/src/render.rs index fa594b5e527..6eb20df2bf4 100644 --- a/crates/completion/src/render.rs +++ b/crates/completion/src/render.rs @@ -523,7 +523,7 @@ fn main() { let _: m::Spam = S$0 } Function, ), lookup: "main", - detail: "fn main()", + detail: "-> ()", }, ] "#]], @@ -552,7 +552,7 @@ fn main() { som$0 } Function, ), lookup: "main", - detail: "fn main()", + detail: "-> ()", }, CompletionItem { label: "something_deprecated()", @@ -563,7 +563,7 @@ fn main() { som$0 } Function, ), lookup: "something_deprecated", - detail: "fn something_deprecated()", + detail: "-> ()", deprecated: true, }, CompletionItem { @@ -575,7 +575,7 @@ fn main() { som$0 } Function, ), lookup: "something_else_deprecated", - detail: "fn something_else_deprecated()", + detail: "-> ()", deprecated: true, }, ] @@ -626,7 +626,7 @@ fn bar(self) { self.$0 } insert: "bar()$0", kind: Method, lookup: "bar", - detail: "fn bar(self)", + detail: "-> ()", documentation: Documentation( "Method docs", ), @@ -726,7 +726,7 @@ fn foo(s: S) { s.$0 } insert: "the_method()$0", kind: Method, lookup: "the_method", - detail: "fn the_method(&self)", + detail: "-> ()", }, ] "#]], diff --git a/crates/completion/src/render/function.rs b/crates/completion/src/render/function.rs index 2d616b1fb54..e46e21d243f 100644 --- a/crates/completion/src/render/function.rs +++ b/crates/completion/src/render/function.rs @@ -1,8 +1,8 @@ //! Renderer for function calls. -use hir::{HasSource, Type}; +use hir::{HasSource, HirDisplay, Type}; use ide_db::SymbolKind; -use syntax::{ast::Fn, display::function_declaration}; +use syntax::ast::Fn; use test_utils::mark; use crate::{ @@ -55,7 +55,8 @@ fn render(self, import_to_add: Option) -> CompletionItem { } fn detail(&self) -> String { - function_declaration(&self.ast_node) + let ty = self.func.ret_type(self.ctx.db()); + format!("-> {}", ty.display(self.ctx.db())) } fn add_arg(&self, arg: &str, ty: &Type) -> String { -- 2.44.0