auto_import: use ra_fmt

This commit is contained in:
Andrea Pretto 2019-02-09 11:33:30 +01:00
parent ee9b0c89e6
commit 1a4faaffd7

View File

@ -5,42 +5,6 @@ use ra_syntax::{
}; };
use crate::assist_ctx::{AssistCtx, Assist, AssistBuilder}; use crate::assist_ctx::{AssistCtx, Assist, AssistBuilder};
// TODO: refactor this before merge
mod formatting {
use ra_syntax::{
AstNode, SyntaxNode,
ast::{self, AstToken},
algo::generate,
};
/// If the node is on the beginning of the line, calculate indent.
pub fn leading_indent(node: &SyntaxNode) -> Option<&str> {
for leaf in prev_leaves(node) {
if let Some(ws) = ast::Whitespace::cast(leaf) {
let ws_text = ws.text();
if let Some(pos) = ws_text.rfind('\n') {
return Some(&ws_text[pos + 1..]);
}
}
if leaf.leaf_text().unwrap().contains('\n') {
break;
}
}
None
}
fn prev_leaves(node: &SyntaxNode) -> impl Iterator<Item = &SyntaxNode> {
generate(prev_leaf(node), |&node| prev_leaf(node))
}
fn prev_leaf(node: &SyntaxNode) -> Option<&SyntaxNode> {
generate(node.ancestors().find_map(SyntaxNode::prev_sibling), |it| {
it.last_child()
})
.last()
}
}
fn collect_path_segments(path: &ast::Path) -> Option<Vec<&ast::PathSegment>> { fn collect_path_segments(path: &ast::Path) -> Option<Vec<&ast::PathSegment>> {
let mut v = Vec::new(); let mut v = Vec::new();
collect_path_segments_raw(&mut v, path)?; collect_path_segments_raw(&mut v, path)?;
@ -102,11 +66,7 @@ fn fmt_segments_raw(segments: &[&ast::PathSegment], buf: &mut String) {
// Returns the numeber of common segments. // Returns the numeber of common segments.
fn compare_path_segments(left: &[&ast::PathSegment], right: &[&ast::PathSegment]) -> usize { fn compare_path_segments(left: &[&ast::PathSegment], right: &[&ast::PathSegment]) -> usize {
return left return left.iter().zip(right).filter(|(l, r)| compare_path_segment(l, r)).count();
.iter()
.zip(right)
.filter(|(l, r)| compare_path_segment(l, r))
.count();
} }
fn compare_path_segment(a: &ast::PathSegment, b: &ast::PathSegment) -> bool { fn compare_path_segment(a: &ast::PathSegment, b: &ast::PathSegment) -> bool {
@ -166,10 +126,7 @@ enum ImportAction<'a> {
impl<'a> ImportAction<'a> { impl<'a> ImportAction<'a> {
fn add_new_use(anchor: Option<&'a SyntaxNode>, add_after_anchor: bool) -> Self { fn add_new_use(anchor: Option<&'a SyntaxNode>, add_after_anchor: bool) -> Self {
ImportAction::AddNewUse { ImportAction::AddNewUse { anchor, add_after_anchor }
anchor,
add_after_anchor,
}
} }
fn add_nested_import( fn add_nested_import(
@ -191,11 +148,7 @@ impl<'a> ImportAction<'a> {
tree_list: &'a ast::UseTreeList, tree_list: &'a ast::UseTreeList,
add_self: bool, add_self: bool,
) -> Self { ) -> Self {
ImportAction::AddInTreeList { ImportAction::AddInTreeList { common_segments, tree_list, add_self }
common_segments,
tree_list,
add_self,
}
} }
fn better<'b>(left: &'b ImportAction<'a>, right: &'b ImportAction<'a>) -> &'b ImportAction<'a> { fn better<'b>(left: &'b ImportAction<'a>, right: &'b ImportAction<'a>) -> &'b ImportAction<'a> {
@ -211,20 +164,12 @@ impl<'a> ImportAction<'a> {
(ImportAction::Nothing, _) => true, (ImportAction::Nothing, _) => true,
(ImportAction::AddInTreeList { .. }, ImportAction::Nothing) => false, (ImportAction::AddInTreeList { .. }, ImportAction::Nothing) => false,
( (
ImportAction::AddNestedImport { ImportAction::AddNestedImport { common_segments: n, .. },
common_segments: n, .. ImportAction::AddInTreeList { common_segments: m, .. },
},
ImportAction::AddInTreeList {
common_segments: m, ..
},
) => n > m, ) => n > m,
( (
ImportAction::AddInTreeList { ImportAction::AddInTreeList { common_segments: n, .. },
common_segments: n, .. ImportAction::AddNestedImport { common_segments: m, .. },
},
ImportAction::AddNestedImport {
common_segments: m, ..
},
) => n > m, ) => n > m,
(ImportAction::AddInTreeList { .. }, _) => true, (ImportAction::AddInTreeList { .. }, _) => true,
(ImportAction::AddNestedImport { .. }, ImportAction::Nothing) => false, (ImportAction::AddNestedImport { .. }, ImportAction::Nothing) => false,
@ -283,11 +228,7 @@ fn walk_use_tree_for_best_action<'a>(
// e.g: target is std::fmt and we can have // e.g: target is std::fmt and we can have
// use foo::bar // use foo::bar
// We add a brand new use statement // We add a brand new use statement
current_use_tree current_use_tree.syntax().ancestors().find_map(ast::UseItem::cast).map(AstNode::syntax),
.syntax()
.ancestors()
.find_map(ast::UseItem::cast)
.map(AstNode::syntax),
true, true,
), ),
common if common == left.len() && left.len() == right.len() => { common if common == left.len() && left.len() == right.len() => {
@ -398,8 +339,7 @@ fn best_action_for_target<'b, 'a: 'b>(
.filter_map(ast::UseItem::use_tree) .filter_map(ast::UseItem::use_tree)
.map(|u| walk_use_tree_for_best_action(&mut storage, None, u, target)) .map(|u| walk_use_tree_for_best_action(&mut storage, None, u, target))
.fold(None, |best, a| { .fold(None, |best, a| {
best.and_then(|best| Some(*ImportAction::better(&best, &a))) best.and_then(|best| Some(*ImportAction::better(&best, &a))).or(Some(a))
.or(Some(a))
}); });
match best_action { match best_action {
@ -421,15 +361,10 @@ fn best_action_for_target<'b, 'a: 'b>(
fn make_assist(action: &ImportAction, target: &[&ast::PathSegment], edit: &mut AssistBuilder) { fn make_assist(action: &ImportAction, target: &[&ast::PathSegment], edit: &mut AssistBuilder) {
match action { match action {
ImportAction::AddNewUse { ImportAction::AddNewUse { anchor, add_after_anchor } => {
anchor, make_assist_add_new_use(anchor, *add_after_anchor, target, edit)
add_after_anchor, }
} => make_assist_add_new_use(anchor, *add_after_anchor, target, edit), ImportAction::AddInTreeList { common_segments, tree_list, add_self } => {
ImportAction::AddInTreeList {
common_segments,
tree_list,
add_self,
} => {
// We know that the fist n segments already exists in the use statement we want // We know that the fist n segments already exists in the use statement we want
// to modify, so we want to add only the last target.len() - n segments. // to modify, so we want to add only the last target.len() - n segments.
let segments_to_add = target.split_at(*common_segments).1; let segments_to_add = target.split_at(*common_segments).1;
@ -461,7 +396,7 @@ fn make_assist_add_new_use(
edit: &mut AssistBuilder, edit: &mut AssistBuilder,
) { ) {
if let Some(anchor) = anchor { if let Some(anchor) = anchor {
let indent = formatting::leading_indent(anchor); let indent = ra_fmt::leading_indent(anchor);
let mut buf = String::new(); let mut buf = String::new();
if after { if after {
buf.push_str("\n"); buf.push_str("\n");
@ -478,11 +413,7 @@ fn make_assist_add_new_use(
buf.push_str(spaces); buf.push_str(spaces);
} }
} }
let position = if after { let position = if after { anchor.range().end() } else { anchor.range().start() };
anchor.range().end()
} else {
anchor.range().start()
};
edit.insert(position, buf); edit.insert(position, buf);
} }
} }
@ -496,10 +427,7 @@ fn make_assist_add_in_tree_list(
let last = tree_list.use_trees().last(); let last = tree_list.use_trees().last();
if let Some(last) = last { if let Some(last) = last {
let mut buf = String::new(); let mut buf = String::new();
let comma = last let comma = last.syntax().siblings(Direction::Next).find(|n| n.kind() == COMMA);
.syntax()
.siblings(Direction::Next)
.find(|n| n.kind() == COMMA);
let offset = if let Some(comma) = comma { let offset = if let Some(comma) = comma {
comma.range().end() comma.range().end()
} else { } else {
@ -558,12 +486,7 @@ pub(crate) fn auto_import(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
let path = node.ancestors().find_map(ast::Path::cast)?; let path = node.ancestors().find_map(ast::Path::cast)?;
// We don't want to mess with use statements // We don't want to mess with use statements
if path if path.syntax().ancestors().find_map(ast::UseItem::cast).is_some() {
.syntax()
.ancestors()
.find_map(ast::UseItem::cast)
.is_some()
{
return None; return None;
} }
@ -572,9 +495,7 @@ pub(crate) fn auto_import(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
return None; return None;
} }
ctx.build( ctx.build(format!("import {} in the current file", fmt_segments(&segments)), |edit| {
format!("import {} in the current file", fmt_segments(&segments)),
|edit| {
let action = best_action_for_target(current_file.syntax(), path, &segments); let action = best_action_for_target(current_file.syntax(), path, &segments);
make_assist(&action, segments.as_slice(), edit); make_assist(&action, segments.as_slice(), edit);
if let Some(last_segment) = path.segment() { if let Some(last_segment) = path.segment() {
@ -585,8 +506,7 @@ pub(crate) fn auto_import(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
last_segment.syntax().range().start(), last_segment.syntax().range().start(),
)); ));
} }
}, })
)
} }
#[cfg(test)] #[cfg(test)]