about summary refs log tree commit diff
path: root/src/tools
diff options
context:
space:
mode:
authorlh123 <1585086582@qq.com>2025-01-16 14:47:42 +0800
committerlh123 <1585086582@qq.com>2025-01-16 14:47:42 +0800
commitfdc672e40267dac8e2d902e721d8fc52be4586c2 (patch)
treeb17b47c179c879dfef0bdf425096381259f755c6 /src/tools
parentb6ba392bb1dce09c789aa99297d0f6a5fdc04117 (diff)
downloadrust-fdc672e40267dac8e2d902e721d8fc52be4586c2.tar.gz
rust-fdc672e40267dac8e2d902e721d8fc52be4586c2.zip
add goto-to-def actions for trait bound
Diffstat (limited to 'src/tools')
-rw-r--r--src/tools/rust-analyzer/crates/ide/src/hover.rs14
-rw-r--r--src/tools/rust-analyzer/crates/ide/src/hover/tests.rs91
2 files changed, 104 insertions, 1 deletions
diff --git a/src/tools/rust-analyzer/crates/ide/src/hover.rs b/src/tools/rust-analyzer/crates/ide/src/hover.rs
index 6b461a62687..87f666fa408 100644
--- a/src/tools/rust-analyzer/crates/ide/src/hover.rs
+++ b/src/tools/rust-analyzer/crates/ide/src/hover.rs
@@ -559,8 +559,20 @@ fn goto_type_action_for_def(
             .for_each(|it| push_new_def(it.into()));
     } else if let Definition::Function(function) = def {
         walk_and_push_ty(db, &function.ret_type(db), &mut push_new_def);
+
+        let krate = function.module(db).krate();
+        let sized_trait =
+            db.lang_item(krate.into(), LangItem::Sized).and_then(|lang_item| lang_item.as_trait());
         for param in function.params_without_self(db) {
-            walk_and_push_ty(db, param.ty(), &mut push_new_def);
+            if let Some(type_param) = param.ty().as_type_param(db) {
+                type_param
+                    .trait_bounds(db)
+                    .into_iter()
+                    .filter(|&it| Some(it.into()) != sized_trait)
+                    .for_each(|it| push_new_def(it.into()));
+            } else {
+                walk_and_push_ty(db, param.ty(), &mut push_new_def);
+            }
         }
     } else {
         let ty = match def {
diff --git a/src/tools/rust-analyzer/crates/ide/src/hover/tests.rs b/src/tools/rust-analyzer/crates/ide/src/hover/tests.rs
index 4edfa6d5c36..014b751f95b 100644
--- a/src/tools/rust-analyzer/crates/ide/src/hover/tests.rs
+++ b/src/tools/rust-analyzer/crates/ide/src/hover/tests.rs
@@ -2369,6 +2369,97 @@ fn test() {
 }
 
 #[test]
+fn test_hover_show_type_def_for_func_param() {
+    check_actions(
+        r#"
+struct Bar;
+fn f(b: Bar) {
+
+}
+
+fn test() {
+    let b = Bar;
+    f$0(b);
+}
+"#,
+        expect![[r#"
+            [
+                Reference(
+                    FilePositionWrapper {
+                        file_id: FileId(
+                            0,
+                        ),
+                        offset: 15,
+                    },
+                ),
+                GoToType(
+                    [
+                        HoverGotoTypeData {
+                            mod_path: "ra_test_fixture::Bar",
+                            nav: NavigationTarget {
+                                file_id: FileId(
+                                    0,
+                                ),
+                                full_range: 0..11,
+                                focus_range: 7..10,
+                                name: "Bar",
+                                kind: Struct,
+                                description: "struct Bar",
+                            },
+                        },
+                    ],
+                ),
+            ]
+        "#]],
+    );
+}
+
+#[test]
+fn test_hover_show_type_def_for_trait_bound() {
+    check_actions(
+        r#"
+trait Bar {}
+fn f<T: Bar>(b: T) {
+
+}
+
+fn test() {
+    f$0();
+}
+"#,
+        expect![[r#"
+            [
+                Reference(
+                    FilePositionWrapper {
+                        file_id: FileId(
+                            0,
+                        ),
+                        offset: 16,
+                    },
+                ),
+                GoToType(
+                    [
+                        HoverGotoTypeData {
+                            mod_path: "ra_test_fixture::Bar",
+                            nav: NavigationTarget {
+                                file_id: FileId(
+                                    0,
+                                ),
+                                full_range: 0..12,
+                                focus_range: 6..9,
+                                name: "Bar",
+                                kind: Trait,
+                                description: "trait Bar",
+                            },
+                        },
+                    ],
+                ),
+            ]
+        "#]],
+    );
+}
+
+#[test]
 fn test_hover_non_ascii_space_doc() {
     check(
         "