Skip to content

Commit

Permalink
Auto merge of rust-lang#12304 - jonas-schievink:more-doc-gen-improvem…
Browse files Browse the repository at this point in the history
…ents, r=jonas-schievink

minor: Include self type in generated getter/setter docs
  • Loading branch information
bors committed May 18, 2022
2 parents b6b17c7 + 5279cdb commit d5ab27a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 17 deletions.
54 changes: 38 additions & 16 deletions crates/ide-assists/src/handlers/generate_documentation_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use crate::assist_context::{AssistContext, Assists};
// ```
// pub struct S;
// impl S {
// /// Sets the length.
// /// Sets the length of this [`S`].
// ///
// /// # Errors
// ///
Expand Down Expand Up @@ -183,11 +183,13 @@ fn introduction_builder(ast_func: &ast::Fn, ctx: &AssistContext) -> Option<Strin
let ret_ty = hir_func.ret_type(ctx.db());
let self_ty = imp.self_ty(ctx.db());
let name = ast_func.name()?.to_string();
let linkable_self_ty = self_type_without_lifetimes(ast_func);
let linkable_self_ty = linkable_self_ty.as_deref();

let intro_for_new = || {
let is_new = name == "new";
if is_new && ret_ty == self_ty {
Some(format!("Creates a new [`{}`].", self_type_without_lifetimes(ast_func)?))
Some(format!("Creates a new [`{}`].", linkable_self_ty?))
} else {
None
}
Expand All @@ -204,15 +206,15 @@ fn introduction_builder(ast_func: &ast::Fn, ctx: &AssistContext) -> Option<Strin
let mut what = name.trim_end_matches("_mut").replace('_', " ");
if what == "len" {
what = "length".into()
};
}
let reference = if ret_ty.is_mutable_reference() {
" a mutable reference to"
} else if ret_ty.is_reference() {
" a reference to"
} else {
""
};
Some(format!("Returns{reference} the {what}."))
Some(format!("Returns{reference} the {what} of this [`{}`].", linkable_self_ty?))
}
_ => None,
};
Expand All @@ -226,7 +228,7 @@ fn introduction_builder(ast_func: &ast::Fn, ctx: &AssistContext) -> Option<Strin
if what == "len" {
what = "length".into()
};
Some(format!("Sets the {what}."))
Some(format!("Sets the {what} of this [`{}`].", linkable_self_ty?))
};

if let Some(intro) = intro_for_new() {
Expand Down Expand Up @@ -325,11 +327,11 @@ fn self_type_without_lifetimes(ast_func: &ast::Fn) -> Option<String> {
_ => return None,
};
let mut name = path_segment.name_ref()?.to_string();
let generics = path_segment
.generic_arg_list()?
.generic_args()
.filter(|generic| matches!(generic, ast::GenericArg::TypeArg(_)))
.map(|generic| generic.to_string());
let generics = path_segment.generic_arg_list().into_iter().flat_map(|list| {
list.generic_args()
.filter(|generic| matches!(generic, ast::GenericArg::TypeArg(_)))
.map(|generic| generic.to_string())
});
let generics: String = generics.format(", ").to_string();
if !generics.is_empty() {
name.push('<');
Expand Down Expand Up @@ -970,6 +972,26 @@ pub trait MyTrait {
check_assist(
generate_documentation_template,
r#"
pub struct String(u8);
impl String {
pub fn new$0(x: u8) -> String {
String(x)
}
}
"#,
r#"
pub struct String(u8);
impl String {
/// Creates a new [`String`].
pub fn new(x: u8) -> String {
String(x)
}
}
"#,
);
check_assist(
generate_documentation_template,
r#"
#[derive(Debug, PartialEq)]
pub struct MyGenericStruct<T> {
pub x: T,
Expand Down Expand Up @@ -1193,7 +1215,7 @@ impl S {
r#"
pub struct S;
impl S {
/// Returns the speed.
/// Returns the speed of this [`S`].
pub fn speed(&self) -> f32 { 0.0 }
}
"#,
Expand All @@ -1209,7 +1231,7 @@ impl S {
r#"
pub struct S;
impl S {
/// Returns a reference to the data.
/// Returns a reference to the data of this [`S`].
pub fn data(&self) -> &[u8] { &[] }
}
"#,
Expand All @@ -1225,7 +1247,7 @@ impl S {
r#"
pub struct S;
impl S {
/// Returns a mutable reference to the data.
/// Returns a mutable reference to the data of this [`S`].
pub fn data(&mut self) -> &mut [u8] { &mut [] }
}
"#,
Expand All @@ -1241,7 +1263,7 @@ impl S {
r#"
pub struct S;
impl S {
/// Returns a mutable reference to the data.
/// Returns a mutable reference to the data of this [`S`].
pub fn data_mut(&mut self) -> &mut [u8] { &mut [] }
}
"#,
Expand Down Expand Up @@ -1281,7 +1303,7 @@ impl S {
r#"
pub struct S;
impl S {
/// Sets the data.
/// Sets the data of this [`S`].
pub fn set_data(&mut self, data: Vec<u8>) {}
}
"#,
Expand All @@ -1297,7 +1319,7 @@ impl S {
r#"
pub struct S;
impl S {
/// Sets the domain name.
/// Sets the domain name of this [`S`].
pub fn set_domain_name(&mut self, name: String) {}
}
"#,
Expand Down
2 changes: 1 addition & 1 deletion crates/ide-assists/src/tests/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -900,7 +900,7 @@ impl S {
r#####"
pub struct S;
impl S {
/// Sets the length.
/// Sets the length of this [`S`].
///
/// # Errors
///
Expand Down

0 comments on commit d5ab27a

Please sign in to comment.