]> git.lizzy.rs Git - rust.git/commitdiff
show function params in completion detail
authorJosh Mcguigan <joshmcg88@gmail.com>
Sun, 7 Mar 2021 00:56:07 +0000 (16:56 -0800)
committerJosh Mcguigan <joshmcg88@gmail.com>
Fri, 12 Mar 2021 21:36:13 +0000 (13:36 -0800)
crates/hir/src/lib.rs
crates/ide_completion/src/completions/dot.rs
crates/ide_completion/src/completions/flyimport.rs
crates/ide_completion/src/completions/qualified_path.rs
crates/ide_completion/src/completions/unqualified_path.rs
crates/ide_completion/src/lib.rs
crates/ide_completion/src/render.rs
crates/ide_completion/src/render/function.rs

index 58adc8fd386cb25215189f22d6aad32e4a630b29..8d082994a6d7a0ea804df45a0f044298cd071d43 100644 (file)
@@ -921,6 +921,14 @@ pub fn access(self, db: &dyn HirDatabase) -> Access {
             })
             .unwrap_or(Access::Owned)
     }
+
+    pub fn display(self, db: &dyn HirDatabase) -> &'static str {
+        match self.access(db) {
+            Access::Shared => "&self",
+            Access::Exclusive => "&mut self",
+            Access::Owned => "self",
+        }
+    }
 }
 
 impl HasVisibility for Function {
index 5ee9a9f0770441d72baaef20dcdd3c0befc450be..cec2d0c3a37e794fe441c20ef3a3b30ba51100b2 100644 (file)
@@ -81,7 +81,7 @@ fn foo(s: S) { s.$0 }
 "#,
             expect![[r#"
                 fd foo   u32
-                me bar() -> ()
+                me bar() fn(&self)
             "#]],
         );
     }
@@ -97,7 +97,7 @@ fn foo(self) { self.$0 }
 "#,
             expect![[r#"
                 fd the_field (u32,)
-                me foo()     -> ()
+                me foo()     fn(self)
             "#]],
         )
     }
@@ -113,7 +113,7 @@ fn foo(&self) { self.$0 }
 "#,
             expect![[r#"
                 fd the_field (u32, i32)
-                me foo()     -> ()
+                me foo()     fn(&self)
             "#]],
         )
     }
@@ -163,7 +163,7 @@ pub(crate) fn the_method(&self) {}
 fn foo(a: A) { a.$0 }
 "#,
             expect![[r#"
-                me the_method() -> ()
+                me the_method() fn(&self)
             "#]],
         );
     }
@@ -196,7 +196,7 @@ fn the_other_method(&self) {}
 fn foo(a: A<u32>) { a.$0 }
 "#,
             expect![[r#"
-                me the_method() -> ()
+                me the_method() fn(&self)
             "#]],
         )
     }
@@ -211,7 +211,7 @@ impl Trait for A {}
 fn foo(a: A) { a.$0 }
 "#,
             expect![[r#"
-                me the_method() -> ()
+                me the_method() fn(&self)
             "#]],
         );
     }
@@ -226,7 +226,7 @@ impl<T> Trait for T {}
 fn foo(a: &A) { a.$0 }
 ",
             expect![[r#"
-                me the_method() -> ()
+                me the_method() fn(&self)
             "#]],
         );
     }
@@ -244,7 +244,7 @@ impl Trait for A {}
 fn foo(a: A) { a.$0 }
 ",
             expect![[r#"
-                me the_method() -> ()
+                me the_method() fn(&self)
             "#]],
         );
     }
@@ -298,7 +298,7 @@ fn foo(&self) {
 }
 "#,
             expect![[r#"
-                me blah() -> ()
+                me blah() fn(&self)
             "#]],
         );
     }
@@ -407,7 +407,7 @@ fn foo() {
 }
 "#,
             expect![[r#"
-                me the_method() -> ()
+                me the_method() fn(&self)
             "#]],
         );
     }
@@ -422,7 +422,7 @@ impl S { fn foo(&self) {} }
 fn main() { make_s!().f$0; }
 "#,
             expect![[r#"
-                me foo() -> ()
+                me foo() fn(&self)
             "#]],
         )
     }
@@ -450,7 +450,7 @@ fn baz() {
 }
         "#,
             expect![[r#"
-                me private() -> ()
+                me private() fn(&self)
             "#]],
         );
     }
index 391a11c91c9e9ea9e157945d0a97769582195403..08df2df3f680b0907cf89a3cbec39a5da5dc94e6 100644 (file)
@@ -402,7 +402,7 @@ fn main() {
         check(
             fixture,
             expect![[r#"
-                fn weird_function() (dep::test_mod::TestTrait) -> ()
+                fn weird_function() (dep::test_mod::TestTrait) fn()
             "#]],
         );
 
@@ -495,7 +495,7 @@ fn main() {
         check(
             fixture,
             expect![[r#"
-                me random_method() (dep::test_mod::TestTrait) -> ()
+                me random_method() (dep::test_mod::TestTrait) fn(&self)
             "#]],
         );
 
@@ -665,7 +665,7 @@ fn main() {
 }
         "#,
             expect![[r#"
-                me random_method() (dep::test_mod::TestTrait) -> () DEPRECATED
+                me random_method() (dep::test_mod::TestTrait) fn(&self) DEPRECATED
             "#]],
         );
 
@@ -696,7 +696,7 @@ fn main() {
 "#,
             expect![[r#"
                 ct SPECIAL_CONST (dep::test_mod::TestTrait) DEPRECATED
-                fn weird_function() (dep::test_mod::TestTrait) -> () DEPRECATED
+                fn weird_function() (dep::test_mod::TestTrait) fn() DEPRECATED
             "#]],
         );
     }
index df74b739e9dc184815945e364e04a79585b1984c..105ff60131dd9dd58b12757f4777ee4a66b8677c 100644 (file)
@@ -359,8 +359,8 @@ fn b(&self) {}
 fn foo() { let _ = S::$0 }
 "#,
             expect![[r#"
-                fn a()  -> ()
-                me b(…) -> ()
+                fn a()  fn()
+                me b(…) fn(&self)
                 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() -> ()
+                fn public_method() fn()
                 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()
             "#]],
         );
     }
@@ -419,7 +419,7 @@ impl U { fn m() { } }
 fn foo() { let _ = U::$0 }
 "#,
             expect![[r#"
-                fn m() -> ()
+                fn m() fn()
             "#]],
         );
     }
@@ -449,7 +449,7 @@ fn completes_trait_associated_method_1() {
 fn foo() { let _ = Trait::$0 }
 "#,
             expect![[r#"
-                fn m() -> ()
+                fn m() fn()
             "#]],
         );
     }
@@ -466,7 +466,7 @@ impl Trait for S {}
 fn foo() { let _ = S::$0 }
 "#,
             expect![[r#"
-                fn m() -> ()
+                fn m() fn()
             "#]],
         );
     }
@@ -483,7 +483,7 @@ impl Trait for S {}
 fn foo() { let _ = <S as Trait>::$0 }
 "#,
             expect![[r#"
-                fn m() -> ()
+                fn m() fn()
             "#]],
         );
     }
@@ -512,11 +512,11 @@ fn foo<T: Sub>() { T::$0 }
                 ta SubTy        type SubTy;
                 ta Ty           type Ty;
                 ct C2           const C2: ();
-                fn subfunc()    -> ()
-                me submethod(…) -> ()
+                fn subfunc()    fn()
+                me submethod(…) fn(&self)
                 ct CONST        const CONST: u8;
-                fn func()       -> ()
-                me method(…)    -> ()
+                fn func()       fn()
+                me method(…)    fn(&self)
             "#]],
         );
     }
@@ -552,11 +552,11 @@ fn subfunc() {
                 ta SubTy        type SubTy;
                 ta Ty           type Ty;
                 ct CONST        const CONST: u8 = 0;
-                fn func()       -> ()
-                me method(…)    -> ()
+                fn func()       fn()
+                me method(…)    fn(&self)
                 ct C2           const C2: () = ();
-                fn subfunc()    -> ()
-                me submethod(…) -> ()
+                fn subfunc()    fn()
+                me submethod(…) fn(&self)
             "#]],
         );
     }
@@ -573,8 +573,8 @@ impl T { fn bar() {} }
 fn main() { T::$0; }
 "#,
             expect![[r#"
-                fn foo() -> ()
-                fn bar() -> ()
+                fn foo() fn()
+                fn bar() fn()
             "#]],
         );
     }
@@ -589,7 +589,7 @@ macro_rules! foo { () => {} }
 fn main() { let _ = crate::$0 }
         "#,
             expect![[r##"
-                fn main()  -> ()
+                fn main()  fn()
                 ma foo!(…) #[macro_export] macro_rules! foo
             "##]],
         );
@@ -633,7 +633,7 @@ fn wrong_fn() {}
 "#,
             expect![[r#"
                 ct RIGHT_CONST
-                fn right_fn()  -> ()
+                fn right_fn()  fn()
                 st RightType
             "#]],
         );
@@ -680,8 +680,8 @@ macro_rules! m { ($e:expr) => { $e } }
 fn foo() {}
 "#,
             expect![[r#"
-                fn main() -> ()
-                fn foo()  -> ()
+                fn main() fn()
+                fn foo()  fn()
             "#]],
         );
     }
@@ -699,7 +699,7 @@ pub fn z() {}
 "#,
             expect![[r#"
                 md z
-                fn z() -> ()
+                fn z() fn()
             "#]],
         );
     }
@@ -719,7 +719,7 @@ fn foo() {
 }
 "#,
             expect![[r#"
-                fn new() -> HashMap<K, V, RandomState>
+                fn new() fn() -> HashMap<K, V, RandomState>
             "#]],
         );
     }
@@ -752,8 +752,8 @@ fn main() {
 }
 "#,
             expect![[r#"
-                fn main() -> ()
-                fn foo(…) -> ()
+                fn main() fn()
+                fn foo(…) fn(i32, i32)
             "#]],
         );
     }
@@ -776,7 +776,7 @@ fn foo(self) {
             expect![[r#"
                 ev Bar    ()
                 ev Baz    ()
-                me foo(…) -> ()
+                me foo(…) fn(self)
             "#]],
         );
     }
@@ -800,7 +800,7 @@ pub fn func(self) {}
 "#,
             expect![[r#"
                 ct MAX     pub const MAX: Self = 255;
-                me func(…) -> ()
+                me func(…) fn(self)
             "#]],
         );
     }
index 044dfd16027b09ee9772efd8983772c053bc9ccb..e4bf4a043e166667c688b1f6ee263f801143bb16 100644 (file)
@@ -139,7 +139,7 @@ fn quux(x: i32) {
             expect![[r#"
                 lc y       i32
                 lc x       i32
-                fn quux(…) -> ()
+                fn quux(…) fn(i32)
             "#]],
         );
     }
@@ -161,7 +161,7 @@ fn quux() {
             expect![[r#"
                 lc b      i32
                 lc a
-                fn quux() -> ()
+                fn quux() fn()
             "#]],
         );
     }
@@ -176,7 +176,7 @@ fn quux() {
 "#,
             expect![[r#"
                 lc x
-                fn quux() -> ()
+                fn quux() fn()
             "#]],
         );
     }
@@ -207,14 +207,14 @@ fn completes_generic_params() {
             r#"fn quux<T>() { $0 }"#,
             expect![[r#"
                 tp T
-                fn quux() -> ()
+                fn quux() fn()
             "#]],
         );
         check(
             r#"fn quux<const C: usize>() { $0 }"#,
             expect![[r#"
                 cp C
-                fn quux() -> ()
+                fn quux() fn()
             "#]],
         );
     }
@@ -225,7 +225,7 @@ fn does_not_complete_lifetimes() {
         check(
             r#"fn quux<'a>() { $0 }"#,
             expect![[r#"
-                fn quux() -> ()
+                fn quux() fn()
             "#]],
         );
     }
@@ -263,7 +263,7 @@ fn quux() { $0 }
 "#,
             expect![[r#"
                 st S
-                fn quux() -> ()
+                fn quux() fn()
                 en E
             "#]],
         );
@@ -316,7 +316,7 @@ fn quux() { $0 }
 }
 "#,
             expect![[r#"
-                fn quux() -> ()
+                fn quux() fn()
                 st Bar
             "#]],
         );
@@ -331,7 +331,7 @@ fn x() -> $0
 "#,
             expect![[r#"
                 st Foo
-                fn x() -> ()
+                fn x() fn()
             "#]],
         );
     }
@@ -352,7 +352,7 @@ fn foo() {
             expect![[r#"
                 lc bar   i32
                 lc bar   i32
-                fn foo() -> ()
+                fn foo() fn()
             "#]],
         );
     }
@@ -382,7 +382,7 @@ fn foo() { let x: $0 }
 mod prelude { struct Option; }
 "#,
             expect![[r#"
-                fn foo()  -> ()
+                fn foo()  fn()
                 md std
                 st Option
             "#]],
@@ -412,7 +412,7 @@ macro_rules! concat { }
 }
 "#,
             expect![[r##"
-                fn f()        -> ()
+                fn f()        fn()
                 ma concat!(…) #[macro_export] macro_rules! concat
                 md std
             "##]],
@@ -439,7 +439,7 @@ fn foo() { let x: $0 }
 mod prelude { struct String; }
 "#,
             expect![[r#"
-                fn foo()  -> ()
+                fn foo()  fn()
                 md std
                 md core
                 st String
@@ -470,7 +470,7 @@ fn main() { let v = $0 }
             expect![[r##"
                 md m1
                 ma baz!(…) #[macro_export] macro_rules! baz
-                fn main()  -> ()
+                fn main()  fn()
                 md m2
                 ma bar!(…) macro_rules! bar
                 ma foo!(…) macro_rules! foo
@@ -486,7 +486,7 @@ macro_rules! foo { () => {} }
 fn foo() { $0 }
 "#,
             expect![[r#"
-                fn foo()   -> ()
+                fn foo()   fn()
                 ma foo!(…) macro_rules! foo
             "#]],
         );
@@ -500,7 +500,7 @@ macro_rules! foo { () => {} }
 fn main() { let x: $0 }
 "#,
             expect![[r#"
-                fn main()  -> ()
+                fn main()  fn()
                 ma foo!(…) macro_rules! foo
             "#]],
         );
@@ -514,7 +514,7 @@ macro_rules! foo { () => {} }
 fn main() { $0 }
 "#,
             expect![[r#"
-                fn main()  -> ()
+                fn main()  fn()
                 ma foo!(…) macro_rules! foo
             "#]],
         );
@@ -530,8 +530,8 @@ fn frobnicate() {}
 }
 "#,
             expect![[r#"
-                fn frobnicate() -> ()
-                fn main()       -> ()
+                fn frobnicate() fn()
+                fn main()       fn()
             "#]],
         );
     }
@@ -549,7 +549,7 @@ fn quux(x: i32) {
             expect![[r#"
                 lc y       i32
                 lc x       i32
-                fn quux(…) -> ()
+                fn quux(…) fn(i32)
                 ma m!(…)   macro_rules! m
             "#]],
         );
@@ -568,7 +568,7 @@ fn quux(x: i32) {
             expect![[r#"
                 lc y       i32
                 lc x       i32
-                fn quux(…) -> ()
+                fn quux(…) fn(i32)
                 ma m!(…)   macro_rules! m
             "#]],
         );
@@ -587,7 +587,7 @@ fn quux(x: i32) {
             expect![[r#"
                 lc y       i32
                 lc x       i32
-                fn quux(…) -> ()
+                fn quux(…) fn(i32)
                 ma m!(…)   macro_rules! m
             "#]],
         );
@@ -602,7 +602,7 @@ fn completes_unresolved_uses() {
 fn main() { $0 }
 "#,
             expect![[r#"
-                fn main() -> ()
+                fn main() fn()
                 ?? Quux
             "#]],
         );
@@ -680,7 +680,7 @@ fn main() { let foo: Foo = Q$0 }
                 ev Foo::Baz  ()
                 ev Foo::Quux ()
                 en Foo
-                fn main()    -> ()
+                fn main()    fn()
             "#]],
         )
     }
@@ -695,7 +695,7 @@ fn f() -> m::E { V$0 }
             expect![[r#"
                 ev m::E::V ()
                 md m
-                fn f()     -> E
+                fn f()     fn() -> E
             "#]],
         )
     }
index 263554ecf72805a95d19176d33e6656e35a4b200..5b7ad38d5f98ea1cab60926b9c7973f1dc5ee4dd 100644 (file)
@@ -230,7 +230,7 @@ fn foo() {
     bar.fo$0;
 }
 "#,
-            DetailAndDocumentation { detail: "-> ()", documentation: "Do the foo" },
+            DetailAndDocumentation { detail: "fn(&self)", documentation: "Do the foo" },
         );
     }
 
@@ -255,7 +255,7 @@ fn foo() {
     bar.fo$0;
 }
 "#,
-            DetailAndDocumentation { detail: "-> ()", documentation: " Do the foo" },
+            DetailAndDocumentation { detail: "fn(&self)", documentation: " Do the foo" },
         );
     }
 
@@ -273,7 +273,7 @@ fn bar() {
     for c in fo$0
 }
 "#,
-            DetailAndDocumentation { detail: "-> &str", documentation: "Do the foo" },
+            DetailAndDocumentation { detail: "fn() -> &str", documentation: "Do the foo" },
         );
     }
 }
index db31896e53228fe3d6b8b61870de36580f20e54c..d9bf5258204221eda3002044cc3de5c7d922243c 100644 (file)
@@ -424,6 +424,44 @@ fn main() { Foo::Fo$0 }
         );
     }
 
+    #[test]
+    fn fn_detail_includes_args_and_return_type() {
+        check(
+            r#"
+fn foo<T>(a: u32, b: u32, t: T) -> (u32, T) { (a, t) }
+
+fn main() { fo$0 }
+"#,
+            expect![[r#"
+                [
+                    CompletionItem {
+                        label: "foo(…)",
+                        source_range: 68..70,
+                        delete: 68..70,
+                        insert: "foo(${1:a}, ${2:b}, ${3:t})$0",
+                        kind: SymbolKind(
+                            Function,
+                        ),
+                        lookup: "foo",
+                        detail: "fn(u32, u32, T) -> (u32, T)",
+                        trigger_call_info: true,
+                    },
+                    CompletionItem {
+                        label: "main()",
+                        source_range: 68..70,
+                        delete: 68..70,
+                        insert: "main()$0",
+                        kind: SymbolKind(
+                            Function,
+                        ),
+                        lookup: "main",
+                        detail: "fn()",
+                    },
+                ]
+            "#]],
+        );
+    }
+
     #[test]
     fn enum_detail_just_parentheses_for_unit() {
         check(
@@ -501,7 +539,7 @@ fn main() { let _: m::Spam = S$0 }
                             Function,
                         ),
                         lookup: "main",
-                        detail: "-> ()",
+                        detail: "fn()",
                     },
                 ]
             "#]],
@@ -530,7 +568,7 @@ fn main() { som$0 }
                             Function,
                         ),
                         lookup: "main",
-                        detail: "-> ()",
+                        detail: "fn()",
                     },
                     CompletionItem {
                         label: "something_deprecated()",
@@ -541,7 +579,7 @@ fn main() { som$0 }
                             Function,
                         ),
                         lookup: "something_deprecated",
-                        detail: "-> ()",
+                        detail: "fn()",
                         deprecated: true,
                     },
                     CompletionItem {
@@ -553,7 +591,7 @@ fn main() { som$0 }
                             Function,
                         ),
                         lookup: "something_else_deprecated",
-                        detail: "-> ()",
+                        detail: "fn()",
                         deprecated: true,
                     },
                 ]
@@ -604,7 +642,7 @@ fn bar(self) { self.$0 }
                         insert: "bar()$0",
                         kind: Method,
                         lookup: "bar",
-                        detail: "-> ()",
+                        detail: "fn(self)",
                         documentation: Documentation(
                             "Method docs",
                         ),
@@ -704,7 +742,7 @@ fn foo(s: S) { s.$0 }
                         insert: "the_method()$0",
                         kind: Method,
                         lookup: "the_method",
-                        detail: "-> ()",
+                        detail: "fn(&self)",
                     },
                 ]
             "#]],
@@ -954,7 +992,7 @@ fn main() {
                             Function,
                         ),
                         lookup: "foo",
-                        detail: "-> ()",
+                        detail: "fn(&mut S)",
                         trigger_call_info: true,
                     },
                     CompletionItem {
@@ -966,7 +1004,7 @@ fn main() {
                             Function,
                         ),
                         lookup: "main",
-                        detail: "-> ()",
+                        detail: "fn()",
                     },
                     CompletionItem {
                         label: "s",
index f4dabe3d10ac17e0173e2ba5c2e11e2ba5eb76f0..e154d63029fd5da579c5c15acf5f57c4ca27f3dc 100644 (file)
@@ -2,6 +2,7 @@
 
 use hir::{HasSource, HirDisplay, Type};
 use ide_db::SymbolKind;
+use itertools::Itertools;
 use syntax::ast::Fn;
 
 use crate::{
@@ -59,8 +60,34 @@ fn render(self, import_to_add: Option<ImportEdit>) -> CompletionItem {
     }
 
     fn detail(&self) -> String {
-        let ty = self.func.ret_type(self.ctx.db());
-        format!("-> {}", ty.display(self.ctx.db()))
+        let params = if let Some(self_param) = self.func.self_param(self.ctx.db()) {
+            let params = self
+                .func
+                .assoc_fn_params(self.ctx.db())
+                .into_iter()
+                .skip(1) // skip the self param because we are manually handling that
+                .map(|p| p.ty().display(self.ctx.db()).to_string());
+
+            std::iter::once(self_param.display(self.ctx.db()).to_owned()).chain(params).join(", ")
+        } else {
+            let params = self
+                .func
+                .assoc_fn_params(self.ctx.db())
+                .into_iter()
+                .map(|p| p.ty().display(self.ctx.db()).to_string())
+                .join(", ");
+            params
+        };
+
+        let ret_ty = self.func.ret_type(self.ctx.db());
+        let ret = if ret_ty.is_unit() {
+            // Omit the `-> ()` for unit return types
+            String::new()
+        } else {
+            format!(" -> {}", ret_ty.display(self.ctx.db()))
+        };
+
+        format!("fn({}){}", params, ret)
     }
 
     fn add_arg(&self, arg: &str, ty: &Type) -> String {