Skip to content

Commit d5ab27a

Browse files
committed
Auto merge of rust-lang#12304 - jonas-schievink:more-doc-gen-improvements, r=jonas-schievink
minor: Include self type in generated getter/setter docs
2 parents b6b17c7 + 5279cdb commit d5ab27a

File tree

2 files changed

+39
-17
lines changed

2 files changed

+39
-17
lines changed

crates/ide-assists/src/handlers/generate_documentation_template.rs

+38-16
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use crate::assist_context::{AssistContext, Assists};
2626
// ```
2727
// pub struct S;
2828
// impl S {
29-
// /// Sets the length.
29+
// /// Sets the length of this [`S`].
3030
// ///
3131
// /// # Errors
3232
// ///
@@ -183,11 +183,13 @@ fn introduction_builder(ast_func: &ast::Fn, ctx: &AssistContext) -> Option<Strin
183183
let ret_ty = hir_func.ret_type(ctx.db());
184184
let self_ty = imp.self_ty(ctx.db());
185185
let name = ast_func.name()?.to_string();
186+
let linkable_self_ty = self_type_without_lifetimes(ast_func);
187+
let linkable_self_ty = linkable_self_ty.as_deref();
186188

187189
let intro_for_new = || {
188190
let is_new = name == "new";
189191
if is_new && ret_ty == self_ty {
190-
Some(format!("Creates a new [`{}`].", self_type_without_lifetimes(ast_func)?))
192+
Some(format!("Creates a new [`{}`].", linkable_self_ty?))
191193
} else {
192194
None
193195
}
@@ -204,15 +206,15 @@ fn introduction_builder(ast_func: &ast::Fn, ctx: &AssistContext) -> Option<Strin
204206
let mut what = name.trim_end_matches("_mut").replace('_', " ");
205207
if what == "len" {
206208
what = "length".into()
207-
};
209+
}
208210
let reference = if ret_ty.is_mutable_reference() {
209211
" a mutable reference to"
210212
} else if ret_ty.is_reference() {
211213
" a reference to"
212214
} else {
213215
""
214216
};
215-
Some(format!("Returns{reference} the {what}."))
217+
Some(format!("Returns{reference} the {what} of this [`{}`].", linkable_self_ty?))
216218
}
217219
_ => None,
218220
};
@@ -226,7 +228,7 @@ fn introduction_builder(ast_func: &ast::Fn, ctx: &AssistContext) -> Option<Strin
226228
if what == "len" {
227229
what = "length".into()
228230
};
229-
Some(format!("Sets the {what}."))
231+
Some(format!("Sets the {what} of this [`{}`].", linkable_self_ty?))
230232
};
231233

232234
if let Some(intro) = intro_for_new() {
@@ -325,11 +327,11 @@ fn self_type_without_lifetimes(ast_func: &ast::Fn) -> Option<String> {
325327
_ => return None,
326328
};
327329
let mut name = path_segment.name_ref()?.to_string();
328-
let generics = path_segment
329-
.generic_arg_list()?
330-
.generic_args()
331-
.filter(|generic| matches!(generic, ast::GenericArg::TypeArg(_)))
332-
.map(|generic| generic.to_string());
330+
let generics = path_segment.generic_arg_list().into_iter().flat_map(|list| {
331+
list.generic_args()
332+
.filter(|generic| matches!(generic, ast::GenericArg::TypeArg(_)))
333+
.map(|generic| generic.to_string())
334+
});
333335
let generics: String = generics.format(", ").to_string();
334336
if !generics.is_empty() {
335337
name.push('<');
@@ -970,6 +972,26 @@ pub trait MyTrait {
970972
check_assist(
971973
generate_documentation_template,
972974
r#"
975+
pub struct String(u8);
976+
impl String {
977+
pub fn new$0(x: u8) -> String {
978+
String(x)
979+
}
980+
}
981+
"#,
982+
r#"
983+
pub struct String(u8);
984+
impl String {
985+
/// Creates a new [`String`].
986+
pub fn new(x: u8) -> String {
987+
String(x)
988+
}
989+
}
990+
"#,
991+
);
992+
check_assist(
993+
generate_documentation_template,
994+
r#"
973995
#[derive(Debug, PartialEq)]
974996
pub struct MyGenericStruct<T> {
975997
pub x: T,
@@ -1193,7 +1215,7 @@ impl S {
11931215
r#"
11941216
pub struct S;
11951217
impl S {
1196-
/// Returns the speed.
1218+
/// Returns the speed of this [`S`].
11971219
pub fn speed(&self) -> f32 { 0.0 }
11981220
}
11991221
"#,
@@ -1209,7 +1231,7 @@ impl S {
12091231
r#"
12101232
pub struct S;
12111233
impl S {
1212-
/// Returns a reference to the data.
1234+
/// Returns a reference to the data of this [`S`].
12131235
pub fn data(&self) -> &[u8] { &[] }
12141236
}
12151237
"#,
@@ -1225,7 +1247,7 @@ impl S {
12251247
r#"
12261248
pub struct S;
12271249
impl S {
1228-
/// Returns a mutable reference to the data.
1250+
/// Returns a mutable reference to the data of this [`S`].
12291251
pub fn data(&mut self) -> &mut [u8] { &mut [] }
12301252
}
12311253
"#,
@@ -1241,7 +1263,7 @@ impl S {
12411263
r#"
12421264
pub struct S;
12431265
impl S {
1244-
/// Returns a mutable reference to the data.
1266+
/// Returns a mutable reference to the data of this [`S`].
12451267
pub fn data_mut(&mut self) -> &mut [u8] { &mut [] }
12461268
}
12471269
"#,
@@ -1281,7 +1303,7 @@ impl S {
12811303
r#"
12821304
pub struct S;
12831305
impl S {
1284-
/// Sets the data.
1306+
/// Sets the data of this [`S`].
12851307
pub fn set_data(&mut self, data: Vec<u8>) {}
12861308
}
12871309
"#,
@@ -1297,7 +1319,7 @@ impl S {
12971319
r#"
12981320
pub struct S;
12991321
impl S {
1300-
/// Sets the domain name.
1322+
/// Sets the domain name of this [`S`].
13011323
pub fn set_domain_name(&mut self, name: String) {}
13021324
}
13031325
"#,

crates/ide-assists/src/tests/generated.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,7 @@ impl S {
900900
r#####"
901901
pub struct S;
902902
impl S {
903-
/// Sets the length.
903+
/// Sets the length of this [`S`].
904904
///
905905
/// # Errors
906906
///

0 commit comments

Comments
 (0)