Merge imports assist

Work towards #2220
This commit is contained in:
Aleksey Kladov 2020-03-18 16:41:24 +01:00
parent 4e50efcfc5
commit 3f6dc20d3c
8 changed files with 225 additions and 25 deletions

View file

@ -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<ast::Path> {
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]

View file

@ -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<SyntaxToken> {
self.token(T!['{'])
}
pub fn r_curly(&self) -> Option<SyntaxToken> {
self.token(T!['}'])
}
fn token(&self, kind: SyntaxKind) -> Option<SyntaxToken> {
self.syntax()
.children_with_tokens()
.filter_map(|it| it.into_token())
.find(|it| it.kind() == kind)
}
}
impl ast::ImplDef {