aboutsummaryrefslogtreecommitdiff
path: root/crates/syntax/src
diff options
context:
space:
mode:
authorAleksei Sidorov <[email protected]>2020-09-03 12:46:28 +0100
committerAleksei Sidorov <[email protected]>2020-09-03 15:46:04 +0100
commit7259cc82f362bd1cd00f47ff3ffc71be769162b6 (patch)
tree1f3d6a6170cca8eee1973d7c3fd8cd47cbb6de77 /crates/syntax/src
parentfe3170dc344f73126cd4ff2f197d49a8b7f2fe1f (diff)
Resolve most of corner cases
Diffstat (limited to 'crates/syntax/src')
-rw-r--r--crates/syntax/src/ast/edit.rs26
1 files changed, 22 insertions, 4 deletions
diff --git a/crates/syntax/src/ast/edit.rs b/crates/syntax/src/ast/edit.rs
index 1ccb4de6a..68987dbf6 100644
--- a/crates/syntax/src/ast/edit.rs
+++ b/crates/syntax/src/ast/edit.rs
@@ -13,7 +13,7 @@ use crate::{
13 ast::{ 13 ast::{
14 self, 14 self,
15 make::{self, tokens}, 15 make::{self, tokens},
16 AstNode, TypeBoundsOwner, 16 AstNode, GenericParamsOwner, NameOwner, TypeBoundsOwner,
17 }, 17 },
18 AstToken, Direction, InsertPosition, SmolStr, SyntaxElement, SyntaxKind, 18 AstToken, Direction, InsertPosition, SmolStr, SyntaxElement, SyntaxKind,
19 SyntaxKind::{ATTR, COMMENT, WHITESPACE}, 19 SyntaxKind::{ATTR, COMMENT, WHITESPACE},
@@ -46,6 +46,19 @@ impl ast::Fn {
46 to_insert.push(body.syntax().clone().into()); 46 to_insert.push(body.syntax().clone().into());
47 self.replace_children(single_node(old_body_or_semi), to_insert) 47 self.replace_children(single_node(old_body_or_semi), to_insert)
48 } 48 }
49
50 #[must_use]
51 pub fn with_generic_params(&self, generic_args: ast::GenericParamList) -> ast::Fn {
52 if let Some(old) = self.generic_param_list() {
53 return self.replace_descendant(old, generic_args);
54 }
55
56 let anchor = self.name().expect("The function must have a name").syntax().clone();
57
58 let mut to_insert: ArrayVec<[SyntaxElement; 1]> = ArrayVec::new();
59 to_insert.push(generic_args.syntax().clone().into());
60 self.insert_children(InsertPosition::After(anchor.into()), to_insert)
61 }
49} 62}
50 63
51fn make_multiline<N>(node: N) -> N 64fn make_multiline<N>(node: N) -> N
@@ -461,14 +474,17 @@ impl ast::MatchArmList {
461 474
462impl ast::GenericParamList { 475impl ast::GenericParamList {
463 #[must_use] 476 #[must_use]
464 pub fn append_params(&self, params: impl IntoIterator<Item = ast::GenericParam>) -> Self { 477 pub fn append_params(
478 &self,
479 params: impl IntoIterator<Item = ast::GenericParam>,
480 ) -> ast::GenericParamList {
465 let mut res = self.clone(); 481 let mut res = self.clone();
466 params.into_iter().for_each(|it| res = res.append_param(it)); 482 params.into_iter().for_each(|it| res = res.append_param(it));
467 res 483 res
468 } 484 }
469 485
470 #[must_use] 486 #[must_use]
471 pub fn append_param(&self, item: ast::GenericParam) -> Self { 487 pub fn append_param(&self, item: ast::GenericParam) -> ast::GenericParamList {
472 let is_multiline = self.syntax().text().contains_char('\n'); 488 let is_multiline = self.syntax().text().contains_char('\n');
473 let ws; 489 let ws;
474 let space = if is_multiline { 490 let space = if is_multiline {
@@ -482,7 +498,9 @@ impl ast::GenericParamList {
482 }; 498 };
483 499
484 let mut to_insert: ArrayVec<[SyntaxElement; 4]> = ArrayVec::new(); 500 let mut to_insert: ArrayVec<[SyntaxElement; 4]> = ArrayVec::new();
485 to_insert.push(space.into()); 501 if self.generic_params().next().is_some() {
502 to_insert.push(space.into());
503 }
486 to_insert.push(item.syntax().clone().into()); 504 to_insert.push(item.syntax().clone().into());
487 to_insert.push(make::token(T![,]).into()); 505 to_insert.push(make::token(T![,]).into());
488 506