diff options
Diffstat (limited to 'crates/ra_syntax')
-rw-r--r-- | crates/ra_syntax/src/algo.rs | 31 | ||||
-rw-r--r-- | crates/ra_syntax/src/ast/edit.rs | 11 | ||||
-rw-r--r-- | crates/ra_syntax/src/ast/make.rs | 2 |
3 files changed, 34 insertions, 10 deletions
diff --git a/crates/ra_syntax/src/algo.rs b/crates/ra_syntax/src/algo.rs index ebf59288a..b87c7084e 100644 --- a/crates/ra_syntax/src/algo.rs +++ b/crates/ra_syntax/src/algo.rs | |||
@@ -144,6 +144,15 @@ pub fn diff(from: &SyntaxNode, to: &SyntaxNode) -> TreeDiff { | |||
144 | pub fn insert_children( | 144 | pub fn insert_children( |
145 | parent: &SyntaxNode, | 145 | parent: &SyntaxNode, |
146 | position: InsertPosition<SyntaxElement>, | 146 | position: InsertPosition<SyntaxElement>, |
147 | to_insert: impl IntoIterator<Item = SyntaxElement>, | ||
148 | ) -> SyntaxNode { | ||
149 | let mut to_insert = to_insert.into_iter(); | ||
150 | _insert_children(parent, position, &mut to_insert) | ||
151 | } | ||
152 | |||
153 | fn _insert_children( | ||
154 | parent: &SyntaxNode, | ||
155 | position: InsertPosition<SyntaxElement>, | ||
147 | to_insert: &mut dyn Iterator<Item = SyntaxElement>, | 156 | to_insert: &mut dyn Iterator<Item = SyntaxElement>, |
148 | ) -> SyntaxNode { | 157 | ) -> SyntaxNode { |
149 | let mut delta = TextUnit::default(); | 158 | let mut delta = TextUnit::default(); |
@@ -178,6 +187,15 @@ pub fn insert_children( | |||
178 | pub fn replace_children( | 187 | pub fn replace_children( |
179 | parent: &SyntaxNode, | 188 | parent: &SyntaxNode, |
180 | to_delete: RangeInclusive<SyntaxElement>, | 189 | to_delete: RangeInclusive<SyntaxElement>, |
190 | to_insert: impl IntoIterator<Item = SyntaxElement>, | ||
191 | ) -> SyntaxNode { | ||
192 | let mut to_insert = to_insert.into_iter(); | ||
193 | _replace_children(parent, to_delete, &mut to_insert) | ||
194 | } | ||
195 | |||
196 | fn _replace_children( | ||
197 | parent: &SyntaxNode, | ||
198 | to_delete: RangeInclusive<SyntaxElement>, | ||
181 | to_insert: &mut dyn Iterator<Item = SyntaxElement>, | 199 | to_insert: &mut dyn Iterator<Item = SyntaxElement>, |
182 | ) -> SyntaxNode { | 200 | ) -> SyntaxNode { |
183 | let start = position_of_child(parent, to_delete.start().clone()); | 201 | let start = position_of_child(parent, to_delete.start().clone()); |
@@ -202,14 +220,21 @@ pub fn replace_children( | |||
202 | /// to create a type-safe abstraction on top of it instead. | 220 | /// to create a type-safe abstraction on top of it instead. |
203 | pub fn replace_descendants( | 221 | pub fn replace_descendants( |
204 | parent: &SyntaxNode, | 222 | parent: &SyntaxNode, |
205 | map: &impl Fn(&SyntaxElement) -> Option<SyntaxElement>, | 223 | map: impl Fn(&SyntaxElement) -> Option<SyntaxElement>, |
224 | ) -> SyntaxNode { | ||
225 | _replace_descendants(parent, &map) | ||
226 | } | ||
227 | |||
228 | fn _replace_descendants( | ||
229 | parent: &SyntaxNode, | ||
230 | map: &dyn Fn(&SyntaxElement) -> Option<SyntaxElement>, | ||
206 | ) -> SyntaxNode { | 231 | ) -> SyntaxNode { |
207 | // FIXME: this could be made much faster. | 232 | // FIXME: this could be made much faster. |
208 | let new_children = parent.children_with_tokens().map(|it| go(map, it)).collect::<Vec<_>>(); | 233 | let new_children = parent.children_with_tokens().map(|it| go(map, it)).collect::<Vec<_>>(); |
209 | return with_children(parent, new_children); | 234 | return with_children(parent, new_children); |
210 | 235 | ||
211 | fn go( | 236 | fn go( |
212 | map: &impl Fn(&SyntaxElement) -> Option<SyntaxElement>, | 237 | map: &dyn Fn(&SyntaxElement) -> Option<SyntaxElement>, |
213 | element: SyntaxElement, | 238 | element: SyntaxElement, |
214 | ) -> NodeOrToken<rowan::GreenNode, rowan::GreenToken> { | 239 | ) -> NodeOrToken<rowan::GreenNode, rowan::GreenToken> { |
215 | if let Some(replacement) = map(&element) { | 240 | if let Some(replacement) = map(&element) { |
@@ -221,7 +246,7 @@ pub fn replace_descendants( | |||
221 | match element { | 246 | match element { |
222 | NodeOrToken::Token(it) => NodeOrToken::Token(it.green().clone()), | 247 | NodeOrToken::Token(it) => NodeOrToken::Token(it.green().clone()), |
223 | NodeOrToken::Node(it) => { | 248 | NodeOrToken::Node(it) => { |
224 | NodeOrToken::Node(replace_descendants(&it, map).green().clone()) | 249 | NodeOrToken::Node(_replace_descendants(&it, map).green().clone()) |
225 | } | 250 | } |
226 | } | 251 | } |
227 | } | 252 | } |
diff --git a/crates/ra_syntax/src/ast/edit.rs b/crates/ra_syntax/src/ast/edit.rs index 0e78d8b63..d2630e9e9 100644 --- a/crates/ra_syntax/src/ast/edit.rs +++ b/crates/ra_syntax/src/ast/edit.rs | |||
@@ -276,7 +276,7 @@ pub fn replace_descendants<N: AstNode, D: AstNode>( | |||
276 | .into_iter() | 276 | .into_iter() |
277 | .map(|(from, to)| (from.syntax().clone().into(), to.syntax().clone().into())) | 277 | .map(|(from, to)| (from.syntax().clone().into(), to.syntax().clone().into())) |
278 | .collect::<FxHashMap<SyntaxElement, _>>(); | 278 | .collect::<FxHashMap<SyntaxElement, _>>(); |
279 | let new_syntax = algo::replace_descendants(parent.syntax(), &|n| map.get(n).cloned()); | 279 | let new_syntax = algo::replace_descendants(parent.syntax(), |n| map.get(n).cloned()); |
280 | N::cast(new_syntax).unwrap() | 280 | N::cast(new_syntax).unwrap() |
281 | } | 281 | } |
282 | 282 | ||
@@ -331,7 +331,7 @@ impl IndentLevel { | |||
331 | ) | 331 | ) |
332 | }) | 332 | }) |
333 | .collect(); | 333 | .collect(); |
334 | algo::replace_descendants(&node, &|n| replacements.get(n).cloned()) | 334 | algo::replace_descendants(&node, |n| replacements.get(n).cloned()) |
335 | } | 335 | } |
336 | 336 | ||
337 | pub fn decrease_indent<N: AstNode>(self, node: N) -> N { | 337 | pub fn decrease_indent<N: AstNode>(self, node: N) -> N { |
@@ -359,7 +359,7 @@ impl IndentLevel { | |||
359 | ) | 359 | ) |
360 | }) | 360 | }) |
361 | .collect(); | 361 | .collect(); |
362 | algo::replace_descendants(&node, &|n| replacements.get(n).cloned()) | 362 | algo::replace_descendants(&node, |n| replacements.get(n).cloned()) |
363 | } | 363 | } |
364 | } | 364 | } |
365 | 365 | ||
@@ -389,7 +389,7 @@ fn insert_children<N: AstNode>( | |||
389 | position: InsertPosition<SyntaxElement>, | 389 | position: InsertPosition<SyntaxElement>, |
390 | to_insert: impl IntoIterator<Item = SyntaxElement>, | 390 | to_insert: impl IntoIterator<Item = SyntaxElement>, |
391 | ) -> N { | 391 | ) -> N { |
392 | let new_syntax = algo::insert_children(parent.syntax(), position, &mut to_insert.into_iter()); | 392 | let new_syntax = algo::insert_children(parent.syntax(), position, to_insert); |
393 | N::cast(new_syntax).unwrap() | 393 | N::cast(new_syntax).unwrap() |
394 | } | 394 | } |
395 | 395 | ||
@@ -404,8 +404,7 @@ fn replace_children<N: AstNode>( | |||
404 | to_replace: RangeInclusive<SyntaxElement>, | 404 | to_replace: RangeInclusive<SyntaxElement>, |
405 | to_insert: impl IntoIterator<Item = SyntaxElement>, | 405 | to_insert: impl IntoIterator<Item = SyntaxElement>, |
406 | ) -> N { | 406 | ) -> N { |
407 | let new_syntax = | 407 | let new_syntax = algo::replace_children(parent.syntax(), to_replace, to_insert); |
408 | algo::replace_children(parent.syntax(), to_replace, &mut to_insert.into_iter()); | ||
409 | N::cast(new_syntax).unwrap() | 408 | N::cast(new_syntax).unwrap() |
410 | } | 409 | } |
411 | 410 | ||
diff --git a/crates/ra_syntax/src/ast/make.rs b/crates/ra_syntax/src/ast/make.rs index 60cd9d260..3f11b747f 100644 --- a/crates/ra_syntax/src/ast/make.rs +++ b/crates/ra_syntax/src/ast/make.rs | |||
@@ -15,7 +15,7 @@ pub fn name_ref(text: &str) -> ast::NameRef { | |||
15 | pub fn path_segment(name_ref: ast::NameRef) -> ast::PathSegment { | 15 | pub fn path_segment(name_ref: ast::NameRef) -> ast::PathSegment { |
16 | ast_from_text(&format!("use {};", name_ref.syntax())) | 16 | ast_from_text(&format!("use {};", name_ref.syntax())) |
17 | } | 17 | } |
18 | pub fn path_unqalified(segment: ast::PathSegment) -> ast::Path { | 18 | pub fn path_unqualified(segment: ast::PathSegment) -> ast::Path { |
19 | path_from_text(&format!("use {}", segment.syntax())) | 19 | path_from_text(&format!("use {}", segment.syntax())) |
20 | } | 20 | } |
21 | pub fn path_qualified(qual: ast::Path, segment: ast::PathSegment) -> ast::Path { | 21 | pub fn path_qualified(qual: ast::Path, segment: ast::PathSegment) -> ast::Path { |