aboutsummaryrefslogtreecommitdiff
path: root/crates/syntax/src/ast/edit.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/syntax/src/ast/edit.rs')
-rw-r--r--crates/syntax/src/ast/edit.rs56
1 files changed, 2 insertions, 54 deletions
diff --git a/crates/syntax/src/ast/edit.rs b/crates/syntax/src/ast/edit.rs
index 61952377f..19107ee38 100644
--- a/crates/syntax/src/ast/edit.rs
+++ b/crates/syntax/src/ast/edit.rs
@@ -6,14 +6,12 @@ use std::{
6 ops::{self, RangeInclusive}, 6 ops::{self, RangeInclusive},
7}; 7};
8 8
9use arrayvec::ArrayVec;
10
11use crate::{ 9use crate::{
12 algo, 10 algo,
13 ast::{self, make, AstNode}, 11 ast::{self, make, AstNode},
14 ted, AstToken, InsertPosition, NodeOrToken, SyntaxElement, SyntaxKind, 12 ted, AstToken, NodeOrToken, SyntaxElement, SyntaxKind,
15 SyntaxKind::{ATTR, COMMENT, WHITESPACE}, 13 SyntaxKind::{ATTR, COMMENT, WHITESPACE},
16 SyntaxNode, SyntaxToken, T, 14 SyntaxNode, SyntaxToken,
17}; 15};
18 16
19impl ast::BinExpr { 17impl ast::BinExpr {
@@ -25,46 +23,6 @@ impl ast::BinExpr {
25 } 23 }
26} 24}
27 25
28impl ast::Path {
29 #[must_use]
30 pub fn with_segment(&self, segment: ast::PathSegment) -> ast::Path {
31 if let Some(old) = self.segment() {
32 return self.replace_children(
33 single_node(old.syntax().clone()),
34 iter::once(segment.syntax().clone().into()),
35 );
36 }
37 self.clone()
38 }
39}
40
41impl ast::PathSegment {
42 #[must_use]
43 pub fn with_generic_args(&self, type_args: ast::GenericArgList) -> ast::PathSegment {
44 self._with_generic_args(type_args, false)
45 }
46
47 #[must_use]
48 pub fn with_turbo_fish(&self, type_args: ast::GenericArgList) -> ast::PathSegment {
49 self._with_generic_args(type_args, true)
50 }
51
52 fn _with_generic_args(&self, type_args: ast::GenericArgList, turbo: bool) -> ast::PathSegment {
53 if let Some(old) = self.generic_arg_list() {
54 return self.replace_children(
55 single_node(old.syntax().clone()),
56 iter::once(type_args.syntax().clone().into()),
57 );
58 }
59 let mut to_insert: ArrayVec<SyntaxElement, 2> = ArrayVec::new();
60 if turbo {
61 to_insert.push(make::token(T![::]).into());
62 }
63 to_insert.push(type_args.syntax().clone().into());
64 self.insert_children(InsertPosition::Last, to_insert)
65 }
66}
67
68impl ast::UseTree { 26impl ast::UseTree {
69 /// Splits off the given prefix, making it the path component of the use tree, appending the rest of the path to all UseTreeList items. 27 /// Splits off the given prefix, making it the path component of the use tree, appending the rest of the path to all UseTreeList items.
70 #[must_use] 28 #[must_use]
@@ -234,16 +192,6 @@ fn prev_tokens(token: SyntaxToken) -> impl Iterator<Item = SyntaxToken> {
234 192
235pub trait AstNodeEdit: AstNode + Clone + Sized { 193pub trait AstNodeEdit: AstNode + Clone + Sized {
236 #[must_use] 194 #[must_use]
237 fn insert_children(
238 &self,
239 position: InsertPosition<SyntaxElement>,
240 to_insert: impl IntoIterator<Item = SyntaxElement>,
241 ) -> Self {
242 let new_syntax = algo::insert_children(self.syntax(), position, to_insert);
243 Self::cast(new_syntax).unwrap()
244 }
245
246 #[must_use]
247 fn replace_children( 195 fn replace_children(
248 &self, 196 &self,
249 to_replace: RangeInclusive<SyntaxElement>, 197 to_replace: RangeInclusive<SyntaxElement>,