diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-03-06 13:09:56 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2020-03-06 13:09:56 +0000 |
commit | 5947c1f8b52deb4fcfd97970ba6eb473f092cb94 (patch) | |
tree | 18d0c7a15da29214a6387f0880135fdf7f738df1 /crates/ra_syntax/src | |
parent | d75577fcee79aac06bdddb01fde431f26432c36c (diff) | |
parent | ea0c124219da33462b9d0be93f7abe0478cc7af2 (diff) |
Merge #3487
3487: Rerail split_import API onto AST r=matklad a=matklad
The code is more verbose and less efficient now, but should be
reusable in add_import context as well
bors r+
🤖
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_syntax/src')
-rw-r--r-- | crates/ra_syntax/src/ast/edit.rs | 18 | ||||
-rw-r--r-- | crates/ra_syntax/src/ast/make.rs | 21 |
2 files changed, 39 insertions, 0 deletions
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 { | |||
259 | } | 259 | } |
260 | } | 260 | } |
261 | 261 | ||
262 | impl ast::UseTree { | ||
263 | #[must_use] | ||
264 | pub fn with_path(&self, path: ast::Path) -> ast::UseTree { | ||
265 | if let Some(old) = self.path() { | ||
266 | return replace_descendants(self, iter::once((old, path))); | ||
267 | } | ||
268 | self.clone() | ||
269 | } | ||
270 | |||
271 | #[must_use] | ||
272 | pub fn with_use_tree_list(&self, use_tree_list: ast::UseTreeList) -> ast::UseTree { | ||
273 | if let Some(old) = self.use_tree_list() { | ||
274 | return replace_descendants(self, iter::once((old, use_tree_list))); | ||
275 | } | ||
276 | self.clone() | ||
277 | } | ||
278 | } | ||
279 | |||
262 | #[must_use] | 280 | #[must_use] |
263 | pub fn strip_attrs_and_docs<N: ast::AttrsOwner>(node: &N) -> N { | 281 | pub fn strip_attrs_and_docs<N: ast::AttrsOwner>(node: &N) -> N { |
264 | N::cast(strip_attrs_and_docs_inner(node.syntax().clone())).unwrap() | 282 | 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 { | |||
25 | ast_from_text(text) | 25 | ast_from_text(text) |
26 | } | 26 | } |
27 | 27 | ||
28 | pub fn use_tree( | ||
29 | path: ast::Path, | ||
30 | use_tree_list: Option<ast::UseTreeList>, | ||
31 | alias: Option<ast::Alias>, | ||
32 | ) -> ast::UseTree { | ||
33 | let mut buf = "use ".to_string(); | ||
34 | buf += &path.syntax().to_string(); | ||
35 | if let Some(use_tree_list) = use_tree_list { | ||
36 | buf += &format!("::{}", use_tree_list.syntax()); | ||
37 | } | ||
38 | if let Some(alias) = alias { | ||
39 | buf += &format!(" {}", alias.syntax()); | ||
40 | } | ||
41 | ast_from_text(&buf) | ||
42 | } | ||
43 | |||
44 | pub fn use_tree_list(use_trees: impl IntoIterator<Item = ast::UseTree>) -> ast::UseTreeList { | ||
45 | let use_trees = use_trees.into_iter().map(|it| it.syntax().clone()).join(", "); | ||
46 | ast_from_text(&format!("use {{{}}};", use_trees)) | ||
47 | } | ||
48 | |||
28 | pub fn record_field(name: ast::NameRef, expr: Option<ast::Expr>) -> ast::RecordField { | 49 | pub fn record_field(name: ast::NameRef, expr: Option<ast::Expr>) -> ast::RecordField { |
29 | return match expr { | 50 | return match expr { |
30 | Some(expr) => from_text(&format!("{}: {}", name.syntax(), expr.syntax())), | 51 | Some(expr) => from_text(&format!("{}: {}", name.syntax(), expr.syntax())), |