From 75f641799effae2c0138215eca968b33429d23fe Mon Sep 17 00:00:00 2001 From: DropDemBits Date: Sun, 9 Oct 2022 17:58:55 -0400 Subject: [PATCH] Add `GenericParamList::to_generic_args` --- .../extract_struct_from_enum_variant.rs | 34 ++----------------- crates/syntax/src/ast/edit_in_place.rs | 17 ++++++++++ 2 files changed, 19 insertions(+), 32 deletions(-) diff --git a/crates/ide-assists/src/handlers/extract_struct_from_enum_variant.rs b/crates/ide-assists/src/handlers/extract_struct_from_enum_variant.rs index 8d5cab283d..970e948dfd 100644 --- a/crates/ide-assists/src/handlers/extract_struct_from_enum_variant.rs +++ b/crates/ide-assists/src/handlers/extract_struct_from_enum_variant.rs @@ -9,7 +9,7 @@ use ide_db::{ search::FileReference, FxHashSet, RootDatabase, }; -use itertools::{Itertools, Position}; +use itertools::Itertools; use syntax::{ ast::{ self, edit::IndentLevel, edit_in_place::Indent, make, AstNode, HasAttrs, HasGenericParams, @@ -298,37 +298,7 @@ fn update_variant(variant: &ast::Variant, generics: Option 0) - .map(|generics| { - let mut generic_str = String::with_capacity(8); - - for (p, more) in generics.generic_params().with_position().map(|p| match p { - Position::First(p) | Position::Middle(p) => (p, true), - Position::Last(p) | Position::Only(p) => (p, false), - }) { - match p { - ast::GenericParam::ConstParam(konst) => { - if let Some(name) = konst.name() { - generic_str.push_str(name.text().as_str()); - } - } - ast::GenericParam::LifetimeParam(lt) => { - if let Some(lt) = lt.lifetime() { - generic_str.push_str(lt.text().as_str()); - } - } - ast::GenericParam::TypeParam(ty) => { - if let Some(name) = ty.name() { - generic_str.push_str(name.text().as_str()); - } - } - } - if more { - generic_str.push_str(", "); - } - } - - make::ty(&format!("{}<{}>", &name.text(), &generic_str)) - }) + .map(|generics| make::ty(&format!("{}{}", &name.text(), generics.to_generic_args()))) .unwrap_or_else(|| make::ty(&name.text())); // change from a record to a tuple field list diff --git a/crates/syntax/src/ast/edit_in_place.rs b/crates/syntax/src/ast/edit_in_place.rs index eadebbe8a2..7d63235282 100644 --- a/crates/syntax/src/ast/edit_in_place.rs +++ b/crates/syntax/src/ast/edit_in_place.rs @@ -235,6 +235,23 @@ impl ast::GenericParamList { } } } + + /// Extracts the const, type, and lifetime names into a new [`ast::GenericParamList`] + pub fn to_generic_args(&self) -> ast::GenericParamList { + let params = self.generic_params().filter_map(|param| match param { + ast::GenericParam::ConstParam(it) => { + Some(ast::GenericParam::TypeParam(make::type_param(it.name()?, None))) + } + ast::GenericParam::LifetimeParam(it) => { + Some(ast::GenericParam::LifetimeParam(make::lifetime_param(it.lifetime()?))) + } + ast::GenericParam::TypeParam(it) => { + Some(ast::GenericParam::TypeParam(make::type_param(it.name()?, None))) + } + }); + + make::generic_param_list(params) + } } impl ast::WhereClause {