From 3f6dc20d3cf3fa101552a9067b98a1314260a679 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 18 Mar 2020 16:41:24 +0100 Subject: Merge imports assist Work towards #2220 --- crates/ra_syntax/src/ast/edit.rs | 20 ++++++++++++++++++++ crates/ra_syntax/src/ast/extensions.rs | 14 ++++++++++++++ 2 files changed, 34 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 1e34db5ae..68dae008f 100644 --- a/crates/ra_syntax/src/ast/edit.rs +++ b/crates/ra_syntax/src/ast/edit.rs @@ -273,6 +273,26 @@ impl ast::UseTree { } self.clone() } + + #[must_use] + pub fn split_prefix(&self, prefix: &ast::Path) -> ast::UseTree { + let suffix = match split_path_prefix(&prefix) { + Some(it) => it, + None => return self.clone(), + }; + let use_tree = make::use_tree(suffix.clone(), self.use_tree_list(), self.alias()); + let nested = make::use_tree_list(iter::once(use_tree)); + return make::use_tree(prefix.clone(), Some(nested), None); + + fn split_path_prefix(prefix: &ast::Path) -> Option { + let parent = prefix.parent_path()?; + let mut res = make::path_unqualified(parent.segment()?); + for p in iter::successors(parent.parent_path(), |it| it.parent_path()) { + res = make::path_qualified(res, p.segment()?); + } + Some(res) + } + } } #[must_use] diff --git a/crates/ra_syntax/src/ast/extensions.rs b/crates/ra_syntax/src/ast/extensions.rs index d5986e8b4..c3ae8f90e 100644 --- a/crates/ra_syntax/src/ast/extensions.rs +++ b/crates/ra_syntax/src/ast/extensions.rs @@ -167,6 +167,20 @@ impl ast::UseTreeList { .and_then(ast::UseTree::cast) .expect("UseTreeLists are always nested in UseTrees") } + pub fn l_curly(&self) -> Option { + self.token(T!['{']) + } + + pub fn r_curly(&self) -> Option { + self.token(T!['}']) + } + + fn token(&self, kind: SyntaxKind) -> Option { + self.syntax() + .children_with_tokens() + .filter_map(|it| it.into_token()) + .find(|it| it.kind() == kind) + } } impl ast::ImplDef { -- cgit v1.2.3