|
1 | 1 | use either::Either; |
2 | 2 | use syntax::{ |
3 | | - ast::{self, AstNode, HasGenericParams, HasName, HasTypeBounds, syntax_factory::SyntaxFactory}, |
| 3 | + ast::{self, AstNode, HasName, HasTypeBounds, syntax_factory::SyntaxFactory}, |
4 | 4 | match_ast, |
5 | | - syntax_editor::{Position, Removable}, |
| 5 | + syntax_editor::{GetOrCreateWhereClause, Removable}, |
6 | 6 | }; |
7 | 7 |
|
8 | 8 | use crate::{AssistContext, AssistId, Assists}; |
@@ -53,61 +53,18 @@ pub(crate) fn move_bounds_to_where_clause( |
53 | 53 | .filter_map(|param| build_predicate(param, &make)) |
54 | 54 | .collect(); |
55 | 55 |
|
56 | | - let existing_where: Option<ast::WhereClause> = match_ast! { |
| 56 | + match_ast! { |
57 | 57 | match (&parent) { |
58 | | - ast::Fn(it) => it.where_clause(), |
59 | | - ast::Trait(it) => it.where_clause(), |
60 | | - ast::Impl(it) => it.where_clause(), |
61 | | - ast::Enum(it) => it.where_clause(), |
62 | | - ast::Struct(it) => it.where_clause(), |
63 | | - ast::TypeAlias(it) => it.where_clause(), |
64 | | - _ => None, |
| 58 | + ast::Fn(it) => it.get_or_create_where_clause(&mut edit, &make, new_preds.into_iter()), |
| 59 | + ast::Trait(it) => it.get_or_create_where_clause(&mut edit, &make, new_preds.into_iter()), |
| 60 | + ast::Impl(it) => it.get_or_create_where_clause(&mut edit, &make, new_preds.into_iter()), |
| 61 | + ast::Enum(it) => it.get_or_create_where_clause(&mut edit, &make, new_preds.into_iter()), |
| 62 | + ast::Struct(it) => it.get_or_create_where_clause(&mut edit, &make, new_preds.into_iter()), |
| 63 | + ast::TypeAlias(it) => it.get_or_create_where_clause(&mut edit, &make, new_preds.into_iter()), |
| 64 | + _ => return, |
65 | 65 | } |
66 | 66 | }; |
67 | 67 |
|
68 | | - let all_preds = existing_where.iter().flat_map(|wc| wc.predicates()).chain(new_preds); |
69 | | - let new_where = make.where_clause(all_preds); |
70 | | - |
71 | | - if let Some(existing) = &existing_where { |
72 | | - edit.replace(existing.syntax(), new_where.syntax()); |
73 | | - } else { |
74 | | - let pos: Option<Position> = match_ast! { |
75 | | - match (&parent) { |
76 | | - ast::Fn(it) => it.ret_type() |
77 | | - .map(|t| Position::after(t.syntax())) |
78 | | - .or_else(|| it.param_list().map(|t| Position::after(t.syntax()))), |
79 | | - ast::Trait(it) => it.generic_param_list() |
80 | | - .map(|t| Position::after(t.syntax())) |
81 | | - .or_else(|| it.name().map(|t| Position::after(t.syntax()))), |
82 | | - ast::Impl(it) => it.self_ty() |
83 | | - .map(|t| Position::after(t.syntax())), |
84 | | - ast::Enum(it) => it.generic_param_list() |
85 | | - .map(|t| Position::after(t.syntax())) |
86 | | - .or_else(|| it.name().map(|t| Position::after(t.syntax()))), |
87 | | - ast::Struct(it) => it.field_list() |
88 | | - .and_then(|fl| match fl { |
89 | | - ast::FieldList::TupleFieldList(it) => { |
90 | | - Some(Position::after(it.syntax())) |
91 | | - } |
92 | | - ast::FieldList::RecordFieldList(_) => None, |
93 | | - }) |
94 | | - .or_else(|| it.generic_param_list() |
95 | | - .map(|t| Position::after(t.syntax()))) |
96 | | - .or_else(|| it.name().map(|t| Position::after(t.syntax()))), |
97 | | - ast::TypeAlias(it) => it.generic_param_list() |
98 | | - .map(|t| Position::after(t.syntax())) |
99 | | - .or_else(|| it.name().map(|t| Position::after(t.syntax()))), |
100 | | - _ => None, |
101 | | - } |
102 | | - }; |
103 | | - if let Some(pos) = pos { |
104 | | - edit.insert_all( |
105 | | - pos, |
106 | | - vec![make.whitespace(" ").into(), new_where.syntax().clone().into()], |
107 | | - ); |
108 | | - } |
109 | | - } |
110 | | - |
111 | 68 | for generic_param in type_param_list.generic_params() { |
112 | 69 | let param: &dyn HasTypeBounds = match &generic_param { |
113 | 70 | ast::GenericParam::TypeParam(t) => t, |
|
0 commit comments