From 4362297a0b25ba4b3993324755c2f57cd846bc8e Mon Sep 17 00:00:00 2001 From: Matt Hooper Date: Mon, 21 Sep 2020 00:38:18 +0100 Subject: Add edit utility for adding an associated item list to a impl def --- crates/syntax/src/ast/edit.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'crates/syntax/src') diff --git a/crates/syntax/src/ast/edit.rs b/crates/syntax/src/ast/edit.rs index 45cf31f13..ceeb0fb4b 100644 --- a/crates/syntax/src/ast/edit.rs +++ b/crates/syntax/src/ast/edit.rs @@ -93,6 +93,22 @@ where } } +impl ast::Impl { + #[must_use] + pub fn with_items(&self, items: ast::AssocItemList) -> ast::Impl { + let mut to_insert: ArrayVec<[SyntaxElement; 2]> = ArrayVec::new(); + if let Some(old_items) = self.assoc_item_list() { + let to_replace: SyntaxElement = old_items.syntax().clone().into(); + to_insert.push(items.syntax().clone().into()); + self.replace_children(single_node(to_replace), to_insert) + } else { + to_insert.push(make::tokens::single_space().into()); + to_insert.push(items.syntax().clone().into()); + self.insert_children(InsertPosition::Last, to_insert) + } + } +} + impl ast::AssocItemList { #[must_use] pub fn append_items( -- cgit v1.2.3 From ae7c9824215dbaa53786cba2fcc1d9537cb06276 Mon Sep 17 00:00:00 2001 From: Matt Hooper Date: Mon, 21 Sep 2020 00:39:17 +0100 Subject: Add make utility for empty associated item lists --- crates/syntax/src/ast/make.rs | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'crates/syntax/src') diff --git a/crates/syntax/src/ast/make.rs b/crates/syntax/src/ast/make.rs index 6868feed9..4a0ffcbb0 100644 --- a/crates/syntax/src/ast/make.rs +++ b/crates/syntax/src/ast/make.rs @@ -21,6 +21,10 @@ pub fn ty(text: &str) -> ast::Type { ast_from_text(&format!("impl {} for D {{}};", text)) } +pub fn assoc_item_list() -> ast::AssocItemList { + ast_from_text("impl C for D {};") +} + pub fn path_segment(name_ref: ast::NameRef) -> ast::PathSegment { ast_from_text(&format!("use {};", name_ref)) } -- cgit v1.2.3 From 7d90bb1f47e1ab6f0ac1d7a042d7161295cf9320 Mon Sep 17 00:00:00 2001 From: Matt Hooper Date: Mon, 21 Sep 2020 10:01:50 +0100 Subject: Rename impl edit method to be more explicit --- crates/syntax/src/ast/edit.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/syntax/src') diff --git a/crates/syntax/src/ast/edit.rs b/crates/syntax/src/ast/edit.rs index ceeb0fb4b..dda0a0319 100644 --- a/crates/syntax/src/ast/edit.rs +++ b/crates/syntax/src/ast/edit.rs @@ -95,7 +95,7 @@ where impl ast::Impl { #[must_use] - pub fn with_items(&self, items: ast::AssocItemList) -> ast::Impl { + pub fn with_assoc_item_list(&self, items: ast::AssocItemList) -> ast::Impl { let mut to_insert: ArrayVec<[SyntaxElement; 2]> = ArrayVec::new(); if let Some(old_items) = self.assoc_item_list() { let to_replace: SyntaxElement = old_items.syntax().clone().into(); -- cgit v1.2.3