mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 11:31:15 +00:00
fix: remove brackets if no generic types
This commit is contained in:
parent
4a5341e044
commit
c2d3f90886
@ -89,7 +89,7 @@ fn introduction_builder(ast_func: &ast::Fn, ctx: &AssistContext) -> String {
|
|||||||
let is_new = ast_func.name()?.to_string() == "new";
|
let is_new = ast_func.name()?.to_string() == "new";
|
||||||
match is_new && ret_ty == self_ty {
|
match is_new && ret_ty == self_ty {
|
||||||
true => {
|
true => {
|
||||||
Some(format!("Creates a new [`{}`].", lifetimes_removed(&self_type(ast_func)?)))
|
Some(format!("Creates a new [`{}`].", self_type_without_lifetimes(ast_func)?))
|
||||||
}
|
}
|
||||||
false => None,
|
false => None,
|
||||||
}
|
}
|
||||||
@ -223,61 +223,32 @@ fn self_name(ast_func: &ast::Fn) -> Option<String> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Heper function to get the name of the type of `self`
|
/// Heper function to get the name of the type of `self`
|
||||||
fn self_type(ast_func: &ast::Fn) -> Option<String> {
|
fn self_type(ast_func: &ast::Fn) -> Option<ast::Type> {
|
||||||
ast_func
|
ast_func.syntax().ancestors().find_map(ast::Impl::cast).and_then(|i| i.self_ty())
|
||||||
.syntax()
|
|
||||||
.ancestors()
|
|
||||||
.find_map(ast::Impl::cast)
|
|
||||||
.and_then(|i| i.self_ty())
|
|
||||||
.map(|t| (t.to_string()))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Output the same string as the input, removing lifetimes.
|
/// Output the real name of `Self` like `MyType<T>`, without the lifetimes.
|
||||||
///
|
fn self_type_without_lifetimes(ast_func: &ast::Fn) -> Option<String> {
|
||||||
/// Lifetimes are detected as starting with a `'` and ending with `,\s*` or before a `>`.
|
let path_segment =
|
||||||
fn lifetimes_removed(with_lifetimes: &str) -> String {
|
ast::PathType::cast(self_type(ast_func)?.syntax().clone())?.path()?.segment()?;
|
||||||
#[derive(Debug)]
|
let mut name = path_segment.name_ref()?.to_string();
|
||||||
enum State {
|
let generics = path_segment
|
||||||
OutOfLifetime,
|
.generic_arg_list()?
|
||||||
AfterLifetime,
|
.generic_args()
|
||||||
InLifetime,
|
.filter(|generic| matches!(generic, ast::GenericArg::TypeArg(_)))
|
||||||
|
.map(|generic| generic.to_string());
|
||||||
|
let generics: String = Itertools::intersperse(generics, ", ".to_string()).collect();
|
||||||
|
if !generics.is_empty() {
|
||||||
|
name.push('<');
|
||||||
|
name.push_str(&generics);
|
||||||
|
name.push('>');
|
||||||
}
|
}
|
||||||
|
Some(name)
|
||||||
let mut state = State::OutOfLifetime;
|
|
||||||
let mut without_lifetimes = String::new();
|
|
||||||
for c in with_lifetimes.chars() {
|
|
||||||
match state {
|
|
||||||
State::OutOfLifetime => {
|
|
||||||
if c == '\'' {
|
|
||||||
state = State::InLifetime;
|
|
||||||
} else {
|
|
||||||
without_lifetimes.push(c);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
State::InLifetime => {
|
|
||||||
if c == ',' {
|
|
||||||
state = State::AfterLifetime;
|
|
||||||
} else if c == '>' {
|
|
||||||
without_lifetimes.push(c);
|
|
||||||
state = State::OutOfLifetime;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
State::AfterLifetime => {
|
|
||||||
if c == '\'' {
|
|
||||||
state = State::InLifetime;
|
|
||||||
} else if !c.is_whitespace() {
|
|
||||||
without_lifetimes.push(c);
|
|
||||||
state = State::OutOfLifetime;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
without_lifetimes
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Heper function to get the name of the type of `self` without generic arguments
|
/// Heper function to get the name of the type of `self` without generic arguments
|
||||||
fn self_partial_type(ast_func: &ast::Fn) -> Option<String> {
|
fn self_partial_type(ast_func: &ast::Fn) -> Option<String> {
|
||||||
let mut self_type = self_type(ast_func)?;
|
let mut self_type = self_type(ast_func)?.to_string();
|
||||||
if let Some(idx) = self_type.find(|c| ['<', ' '].contains(&c)) {
|
if let Some(idx) = self_type.find(|c| ['<', ' '].contains(&c)) {
|
||||||
self_type.truncate(idx);
|
self_type.truncate(idx);
|
||||||
}
|
}
|
||||||
@ -1114,6 +1085,46 @@ impl<'a, 'b, T> MyGenericStruct<'a, 'b, T> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn removes_all_lifetimes_and_brackets_from_description() {
|
||||||
|
check_assist(
|
||||||
|
generate_documentation_template,
|
||||||
|
r#"
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
|
pub struct MyGenericStruct<'a, 'b> {
|
||||||
|
pub x: &'a usize,
|
||||||
|
pub y: &'b usize,
|
||||||
|
}
|
||||||
|
impl<'a, 'b> MyGenericStruct<'a, 'b> {
|
||||||
|
pub fn new$0(x: &'a usize, y: &'b usize) -> Self {
|
||||||
|
MyGenericStruct { x, y }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
r#"
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
|
pub struct MyGenericStruct<'a, 'b> {
|
||||||
|
pub x: &'a usize,
|
||||||
|
pub y: &'b usize,
|
||||||
|
}
|
||||||
|
impl<'a, 'b> MyGenericStruct<'a, 'b> {
|
||||||
|
/// Creates a new [`MyGenericStruct`].
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// use test::MyGenericStruct;
|
||||||
|
///
|
||||||
|
/// assert_eq!(MyGenericStruct::new(x, y), );
|
||||||
|
/// ```
|
||||||
|
pub fn new(x: &'a usize, y: &'b usize) -> Self {
|
||||||
|
MyGenericStruct { x, y }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn detects_new_with_self() {
|
fn detects_new_with_self() {
|
||||||
check_assist(
|
check_assist(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user