aboutsummaryrefslogtreecommitdiff
path: root/crates/syntax/src/ast
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2021-03-19 17:46:18 +0000
committerAleksey Kladov <[email protected]>2021-03-19 17:53:23 +0000
commita61691026a169a17f62de9de6ebab197faa7c703 (patch)
treef6eb960ed1787c4f5db128f339ebb201aabf34c0 /crates/syntax/src/ast
parentdc8e2fea98aecbba0c587e5999b9e09533e0091c (diff)
Make ast editing more ergonomic
changelog internal
Diffstat (limited to 'crates/syntax/src/ast')
-rw-r--r--crates/syntax/src/ast/edit_in_place.rs41
1 files changed, 20 insertions, 21 deletions
diff --git a/crates/syntax/src/ast/edit_in_place.rs b/crates/syntax/src/ast/edit_in_place.rs
index 449b058fb..b1eed0a2c 100644
--- a/crates/syntax/src/ast/edit_in_place.rs
+++ b/crates/syntax/src/ast/edit_in_place.rs
@@ -8,7 +8,7 @@ use parser::T;
8use crate::{ 8use crate::{
9 ast, 9 ast,
10 ted::{self, Position}, 10 ted::{self, Position},
11 AstNode, Direction, SyntaxElement, 11 AstNode, Direction,
12}; 12};
13 13
14use super::NameOwner; 14use super::NameOwner;
@@ -21,11 +21,11 @@ impl GenericParamsOwnerEdit for ast::Fn {
21 fn get_or_create_where_clause(&self) -> WhereClause { 21 fn get_or_create_where_clause(&self) -> WhereClause {
22 if self.where_clause().is_none() { 22 if self.where_clause().is_none() {
23 let position = if let Some(ty) = self.ret_type() { 23 let position = if let Some(ty) = self.ret_type() {
24 Position::after(ty.syntax().clone()) 24 Position::after(ty.syntax())
25 } else if let Some(param_list) = self.param_list() { 25 } else if let Some(param_list) = self.param_list() {
26 Position::after(param_list.syntax().clone()) 26 Position::after(param_list.syntax())
27 } else { 27 } else {
28 Position::last_child_of(self.syntax().clone()) 28 Position::last_child_of(self.syntax())
29 }; 29 };
30 create_where_clause(position) 30 create_where_clause(position)
31 } 31 }
@@ -37,9 +37,9 @@ impl GenericParamsOwnerEdit for ast::Impl {
37 fn get_or_create_where_clause(&self) -> WhereClause { 37 fn get_or_create_where_clause(&self) -> WhereClause {
38 if self.where_clause().is_none() { 38 if self.where_clause().is_none() {
39 let position = if let Some(items) = self.assoc_item_list() { 39 let position = if let Some(items) = self.assoc_item_list() {
40 Position::before(items.syntax().clone()) 40 Position::before(items.syntax())
41 } else { 41 } else {
42 Position::last_child_of(self.syntax().clone()) 42 Position::last_child_of(self.syntax())
43 }; 43 };
44 create_where_clause(position) 44 create_where_clause(position)
45 } 45 }
@@ -51,9 +51,9 @@ impl GenericParamsOwnerEdit for ast::Trait {
51 fn get_or_create_where_clause(&self) -> WhereClause { 51 fn get_or_create_where_clause(&self) -> WhereClause {
52 if self.where_clause().is_none() { 52 if self.where_clause().is_none() {
53 let position = if let Some(items) = self.assoc_item_list() { 53 let position = if let Some(items) = self.assoc_item_list() {
54 Position::before(items.syntax().clone()) 54 Position::before(items.syntax())
55 } else { 55 } else {
56 Position::last_child_of(self.syntax().clone()) 56 Position::last_child_of(self.syntax())
57 }; 57 };
58 create_where_clause(position) 58 create_where_clause(position)
59 } 59 }
@@ -69,13 +69,13 @@ impl GenericParamsOwnerEdit for ast::Struct {
69 ast::FieldList::TupleFieldList(it) => Some(it), 69 ast::FieldList::TupleFieldList(it) => Some(it),
70 }); 70 });
71 let position = if let Some(tfl) = tfl { 71 let position = if let Some(tfl) = tfl {
72 Position::after(tfl.syntax().clone()) 72 Position::after(tfl.syntax())
73 } else if let Some(gpl) = self.generic_param_list() { 73 } else if let Some(gpl) = self.generic_param_list() {
74 Position::after(gpl.syntax().clone()) 74 Position::after(gpl.syntax())
75 } else if let Some(name) = self.name() { 75 } else if let Some(name) = self.name() {
76 Position::after(name.syntax().clone()) 76 Position::after(name.syntax())
77 } else { 77 } else {
78 Position::last_child_of(self.syntax().clone()) 78 Position::last_child_of(self.syntax())
79 }; 79 };
80 create_where_clause(position) 80 create_where_clause(position)
81 } 81 }
@@ -87,11 +87,11 @@ impl GenericParamsOwnerEdit for ast::Enum {
87 fn get_or_create_where_clause(&self) -> WhereClause { 87 fn get_or_create_where_clause(&self) -> WhereClause {
88 if self.where_clause().is_none() { 88 if self.where_clause().is_none() {
89 let position = if let Some(gpl) = self.generic_param_list() { 89 let position = if let Some(gpl) = self.generic_param_list() {
90 Position::after(gpl.syntax().clone()) 90 Position::after(gpl.syntax())
91 } else if let Some(name) = self.name() { 91 } else if let Some(name) = self.name() {
92 Position::after(name.syntax().clone()) 92 Position::after(name.syntax())
93 } else { 93 } else {
94 Position::last_child_of(self.syntax().clone()) 94 Position::last_child_of(self.syntax())
95 }; 95 };
96 create_where_clause(position) 96 create_where_clause(position)
97 } 97 }
@@ -100,19 +100,18 @@ impl GenericParamsOwnerEdit for ast::Enum {
100} 100}
101 101
102fn create_where_clause(position: Position) { 102fn create_where_clause(position: Position) {
103 let where_clause: SyntaxElement = 103 let where_clause = make::where_clause(empty()).clone_for_update();
104 make::where_clause(empty()).clone_for_update().syntax().clone().into(); 104 ted::insert(position, where_clause.syntax());
105 ted::insert(position, where_clause);
106} 105}
107 106
108impl ast::WhereClause { 107impl ast::WhereClause {
109 pub fn add_predicate(&self, predicate: ast::WherePred) { 108 pub fn add_predicate(&self, predicate: ast::WherePred) {
110 if let Some(pred) = self.predicates().last() { 109 if let Some(pred) = self.predicates().last() {
111 if !pred.syntax().siblings_with_tokens(Direction::Next).any(|it| it.kind() == T![,]) { 110 if !pred.syntax().siblings_with_tokens(Direction::Next).any(|it| it.kind() == T![,]) {
112 ted::append_child_raw(self.syntax().clone(), make::token(T![,])); 111 ted::append_child_raw(self.syntax(), make::token(T![,]));
113 } 112 }
114 } 113 }
115 ted::append_child(self.syntax().clone(), predicate.syntax().clone()) 114 ted::append_child(self.syntax(), predicate.syntax())
116 } 115 }
117} 116}
118 117
@@ -123,7 +122,7 @@ impl ast::TypeBoundList {
123 { 122 {
124 ted::remove_all(colon..=self.syntax().clone().into()) 123 ted::remove_all(colon..=self.syntax().clone().into())
125 } else { 124 } else {
126 ted::remove(self.syntax().clone()) 125 ted::remove(self.syntax())
127 } 126 }
128 } 127 }
129} 128}