From 5279cdbefbdef2f71febb1ab430060e7f0a4021e Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Wed, 18 May 2022 18:38:41 +0200 Subject: [PATCH] Include self type in generated getter/setter docs --- .../generate_documentation_template.rs | 54 +++++++++++++------ crates/ide-assists/src/tests/generated.rs | 2 +- 2 files changed, 39 insertions(+), 17 deletions(-) diff --git a/crates/ide-assists/src/handlers/generate_documentation_template.rs b/crates/ide-assists/src/handlers/generate_documentation_template.rs index 74c71f13ee3..d22d8c831fb 100644 --- a/crates/ide-assists/src/handlers/generate_documentation_template.rs +++ b/crates/ide-assists/src/handlers/generate_documentation_template.rs @@ -26,7 +26,7 @@ // ``` // pub struct S; // impl S { -// /// Sets the length. +// /// Sets the length of this [`S`]. // /// // /// # Errors // /// @@ -183,11 +183,13 @@ fn introduction_builder(ast_func: &ast::Fn, ctx: &AssistContext) -> Option Option Option None, }; @@ -226,7 +228,7 @@ fn introduction_builder(ast_func: &ast::Fn, ctx: &AssistContext) -> Option Option { _ => 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('<'); @@ -970,6 +972,26 @@ fn detects_new() { 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 { pub x: T, @@ -1193,7 +1215,7 @@ pub fn speed$0(&self) -> f32 { 0.0 } r#" pub struct S; impl S { - /// Returns the speed. + /// Returns the speed of this [`S`]. pub fn speed(&self) -> f32 { 0.0 } } "#, @@ -1209,7 +1231,7 @@ pub fn data$0(&self) -> &[u8] { &[] } 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] { &[] } } "#, @@ -1225,7 +1247,7 @@ pub fn data$0(&mut self) -> &mut [u8] { &mut [] } 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 [] } } "#, @@ -1241,7 +1263,7 @@ pub fn data_mut$0(&mut self) -> &mut [u8] { &mut [] } 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 [] } } "#, @@ -1281,7 +1303,7 @@ pub fn set_data$0(&mut self, data: Vec) {} r#" pub struct S; impl S { - /// Sets the data. + /// Sets the data of this [`S`]. pub fn set_data(&mut self, data: Vec) {} } "#, @@ -1297,7 +1319,7 @@ pub fn set_domain_name$0(&mut self, name: String) {} 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) {} } "#, diff --git a/crates/ide-assists/src/tests/generated.rs b/crates/ide-assists/src/tests/generated.rs index 0da4197afe8..69d754d4f70 100644 --- a/crates/ide-assists/src/tests/generated.rs +++ b/crates/ide-assists/src/tests/generated.rs @@ -900,7 +900,7 @@ pub unsafe fn set_len$0(&mut self, len: usize) -> Result<(), std::io::Error> { r#####" pub struct S; impl S { - /// Sets the length. + /// Sets the length of this [`S`]. /// /// # Errors ///