Skip to content

Commit b198815

Browse files
committed
Auto merge of #14775 - hecatia-elegua:doc-alias-methods, r=Veykril
feat: add #[doc(alias(..))]-based method completions ![Screenshot showing a completion when you type a doc alias instead of a real name](https://github.com/rust-lang/rust-analyzer/assets/108802164/e7c69bb9-3da6-4d8f-a09b-fece1bdd1c0e)
2 parents bc03418 + 2a182f3 commit b198815

File tree

3 files changed

+68
-2
lines changed

3 files changed

+68
-2
lines changed

crates/ide-completion/src/completions.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,9 +295,12 @@ impl Completions {
295295
Visible::Editable => true,
296296
Visible::No => return,
297297
};
298+
let doc_aliases = ctx.doc_aliases(&func);
298299
self.add(
299300
render_fn(
300-
RenderContext::new(ctx).private_editable(is_private_editable),
301+
RenderContext::new(ctx)
302+
.private_editable(is_private_editable)
303+
.doc_aliases(doc_aliases),
301304
path_ctx,
302305
local_name,
303306
func,
@@ -322,9 +325,12 @@ impl Completions {
322325
Visible::Editable => true,
323326
Visible::No => return,
324327
};
328+
let doc_aliases = ctx.doc_aliases(&func);
325329
self.add(
326330
render_method(
327-
RenderContext::new(ctx).private_editable(is_private_editable),
331+
RenderContext::new(ctx)
332+
.private_editable(is_private_editable)
333+
.doc_aliases(doc_aliases),
328334
dot_access,
329335
receiver,
330336
local_name,
@@ -349,10 +355,12 @@ impl Completions {
349355
Visible::Editable => true,
350356
Visible::No => return,
351357
};
358+
let doc_aliases = ctx.doc_aliases(&func);
352359
self.add(
353360
render_method(
354361
RenderContext::new(ctx)
355362
.private_editable(is_private_editable)
363+
.doc_aliases(doc_aliases)
356364
.import_to_add(Some(import)),
357365
dot_access,
358366
None,

crates/ide-completion/src/tests/special.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,6 +1106,62 @@ fn here_we_go() {
11061106
);
11071107
}
11081108

1109+
#[test]
1110+
fn completes_struct_fn_name_via_doc_alias_in_fn_body() {
1111+
check(
1112+
r#"
1113+
struct Foo;
1114+
impl Foo {
1115+
#[doc(alias = "qux")]
1116+
fn bar() -> u8 { 1 }
1117+
}
1118+
1119+
fn here_we_go() {
1120+
Foo::q$0
1121+
}
1122+
"#,
1123+
expect![[r#"
1124+
fn bar() (alias qux) fn() -> u8
1125+
"#]],
1126+
);
1127+
}
1128+
1129+
#[test]
1130+
fn completes_method_name_via_doc_alias_in_fn_body() {
1131+
check(
1132+
r#"
1133+
struct Foo {
1134+
bar: u8
1135+
}
1136+
impl Foo {
1137+
#[doc(alias = "qux")]
1138+
fn baz(&self) -> u8 {
1139+
self.bar
1140+
}
1141+
}
1142+
1143+
fn here_we_go() {
1144+
let foo = Foo { field: 42 };
1145+
foo.q$0
1146+
}
1147+
"#,
1148+
expect![[r#"
1149+
fd bar u8
1150+
me baz() (alias qux) fn(&self) -> u8
1151+
sn box Box::new(expr)
1152+
sn call function(expr)
1153+
sn dbg dbg!(expr)
1154+
sn dbgr dbg!(&expr)
1155+
sn let let
1156+
sn letm let mut
1157+
sn match match expr {}
1158+
sn ref &expr
1159+
sn refm &mut expr
1160+
sn unsafe unsafe {}
1161+
"#]],
1162+
);
1163+
}
1164+
11091165
#[test]
11101166
fn completes_fn_name_via_doc_alias_in_fn_body() {
11111167
check(

crates/rust-analyzer/tests/slow-tests/tidy.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,8 @@ fn check_dbg(path: &Path, text: &str) {
257257
"ide-db/src/generated/lints.rs",
258258
// test for doc test for remove_dbg
259259
"src/tests/generated.rs",
260+
// `expect!` string can contain `dbg!` (due to .dbg postfix)
261+
"ide-completion/src/tests/special.rs",
260262
];
261263
if need_dbg.iter().any(|p| path.ends_with(p)) {
262264
return;

0 commit comments

Comments
 (0)