From ea0c124219da33462b9d0be93f7abe0478cc7af2 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 5 Mar 2020 19:03:14 +0100 Subject: Rerail split_import API onto AST The code is more verbose and less efficient now, but should be reusable in add_import context as well --- crates/ra_syntax/src/ast/edit.rs | 18 ++++++++++++++++++ crates/ra_syntax/src/ast/make.rs | 21 +++++++++++++++++++++ 2 files changed, 39 insertions(+) (limited to 'crates/ra_syntax') diff --git a/crates/ra_syntax/src/ast/edit.rs b/crates/ra_syntax/src/ast/edit.rs index 1858e2b6c..e4cdccdb4 100644 --- a/crates/ra_syntax/src/ast/edit.rs +++ b/crates/ra_syntax/src/ast/edit.rs @@ -259,6 +259,24 @@ impl ast::UseItem { } } +impl ast::UseTree { + #[must_use] + pub fn with_path(&self, path: ast::Path) -> ast::UseTree { + if let Some(old) = self.path() { + return replace_descendants(self, iter::once((old, path))); + } + self.clone() + } + + #[must_use] + pub fn with_use_tree_list(&self, use_tree_list: ast::UseTreeList) -> ast::UseTree { + if let Some(old) = self.use_tree_list() { + return replace_descendants(self, iter::once((old, use_tree_list))); + } + self.clone() + } +} + #[must_use] pub fn strip_attrs_and_docs(node: &N) -> N { N::cast(strip_attrs_and_docs_inner(node.syntax().clone())).unwrap() diff --git a/crates/ra_syntax/src/ast/make.rs b/crates/ra_syntax/src/ast/make.rs index 0da24560e..22c54f363 100644 --- a/crates/ra_syntax/src/ast/make.rs +++ b/crates/ra_syntax/src/ast/make.rs @@ -25,6 +25,27 @@ fn path_from_text(text: &str) -> ast::Path { ast_from_text(text) } +pub fn use_tree( + path: ast::Path, + use_tree_list: Option, + alias: Option, +) -> ast::UseTree { + let mut buf = "use ".to_string(); + buf += &path.syntax().to_string(); + if let Some(use_tree_list) = use_tree_list { + buf += &format!("::{}", use_tree_list.syntax()); + } + if let Some(alias) = alias { + buf += &format!(" {}", alias.syntax()); + } + ast_from_text(&buf) +} + +pub fn use_tree_list(use_trees: impl IntoIterator) -> ast::UseTreeList { + let use_trees = use_trees.into_iter().map(|it| it.syntax().clone()).join(", "); + ast_from_text(&format!("use {{{}}};", use_trees)) +} + pub fn record_field(name: ast::NameRef, expr: Option) -> ast::RecordField { return match expr { Some(expr) => from_text(&format!("{}: {}", name.syntax(), expr.syntax())), -- cgit v1.2.3