};
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>> {
let mut v = Vec::new();
collect_path_segments_raw(&mut v, path)?;
// Returns the numeber of common segments.
fn compare_path_segments(left: &[&ast::PathSegment], right: &[&ast::PathSegment]) -> usize {
- return left
- .iter()
- .zip(right)
- .filter(|(l, r)| compare_path_segment(l, r))
- .count();
+ return left.iter().zip(right).filter(|(l, r)| compare_path_segment(l, r)).count();
}
fn compare_path_segment(a: &ast::PathSegment, b: &ast::PathSegment) -> bool {
impl<'a> ImportAction<'a> {
fn add_new_use(anchor: Option<&'a SyntaxNode>, add_after_anchor: bool) -> Self {
- ImportAction::AddNewUse {
- anchor,
- add_after_anchor,
- }
+ ImportAction::AddNewUse { anchor, add_after_anchor }
}
fn add_nested_import(
tree_list: &'a ast::UseTreeList,
add_self: bool,
) -> Self {
- ImportAction::AddInTreeList {
- common_segments,
- tree_list,
- add_self,
- }
+ ImportAction::AddInTreeList { common_segments, tree_list, add_self }
}
fn better<'b>(left: &'b ImportAction<'a>, right: &'b ImportAction<'a>) -> &'b ImportAction<'a> {
(ImportAction::Nothing, _) => true,
(ImportAction::AddInTreeList { .. }, ImportAction::Nothing) => false,
(
- ImportAction::AddNestedImport {
- common_segments: n, ..
- },
- ImportAction::AddInTreeList {
- common_segments: m, ..
- },
+ ImportAction::AddNestedImport { common_segments: n, .. },
+ ImportAction::AddInTreeList { common_segments: m, .. },
) => n > m,
(
- ImportAction::AddInTreeList {
- common_segments: n, ..
- },
- ImportAction::AddNestedImport {
- common_segments: m, ..
- },
+ ImportAction::AddInTreeList { common_segments: n, .. },
+ ImportAction::AddNestedImport { common_segments: m, .. },
) => n > m,
(ImportAction::AddInTreeList { .. }, _) => true,
(ImportAction::AddNestedImport { .. }, ImportAction::Nothing) => false,
// e.g: target is std::fmt and we can have
// use foo::bar
// We add a brand new use statement
- current_use_tree
- .syntax()
- .ancestors()
- .find_map(ast::UseItem::cast)
- .map(AstNode::syntax),
+ current_use_tree.syntax().ancestors().find_map(ast::UseItem::cast).map(AstNode::syntax),
true,
),
common if common == left.len() && left.len() == right.len() => {
.filter_map(ast::UseItem::use_tree)
.map(|u| walk_use_tree_for_best_action(&mut storage, None, u, target))
.fold(None, |best, a| {
- best.and_then(|best| Some(*ImportAction::better(&best, &a)))
- .or(Some(a))
+ best.and_then(|best| Some(*ImportAction::better(&best, &a))).or(Some(a))
});
match best_action {
fn make_assist(action: &ImportAction, target: &[&ast::PathSegment], edit: &mut AssistBuilder) {
match action {
- ImportAction::AddNewUse {
- anchor,
- add_after_anchor,
- } => make_assist_add_new_use(anchor, *add_after_anchor, target, edit),
- ImportAction::AddInTreeList {
- common_segments,
- tree_list,
- add_self,
- } => {
+ ImportAction::AddNewUse { anchor, add_after_anchor } => {
+ make_assist_add_new_use(anchor, *add_after_anchor, target, edit)
+ }
+ ImportAction::AddInTreeList { common_segments, tree_list, add_self } => {
// 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.
let segments_to_add = target.split_at(*common_segments).1;
edit: &mut AssistBuilder,
) {
if let Some(anchor) = anchor {
- let indent = formatting::leading_indent(anchor);
+ let indent = ra_fmt::leading_indent(anchor);
let mut buf = String::new();
if after {
buf.push_str("\n");
buf.push_str(spaces);
}
}
- let position = if after {
- anchor.range().end()
- } else {
- anchor.range().start()
- };
+ let position = if after { anchor.range().end() } else { anchor.range().start() };
edit.insert(position, buf);
}
}
let last = tree_list.use_trees().last();
if let Some(last) = last {
let mut buf = String::new();
- let comma = last
- .syntax()
- .siblings(Direction::Next)
- .find(|n| n.kind() == COMMA);
+ let comma = last.syntax().siblings(Direction::Next).find(|n| n.kind() == COMMA);
let offset = if let Some(comma) = comma {
comma.range().end()
} else {
let path = node.ancestors().find_map(ast::Path::cast)?;
// We don't want to mess with use statements
- if path
- .syntax()
- .ancestors()
- .find_map(ast::UseItem::cast)
- .is_some()
- {
+ if path.syntax().ancestors().find_map(ast::UseItem::cast).is_some() {
return None;
}
return None;
}
- ctx.build(
- format!("import {} in the current file", fmt_segments(&segments)),
- |edit| {
- let action = best_action_for_target(current_file.syntax(), path, &segments);
- make_assist(&action, segments.as_slice(), edit);
- if let Some(last_segment) = path.segment() {
- // Here we are assuming the assist will provide a correct use statement
- // so we can delete the path qualifier
- edit.delete(TextRange::from_to(
- path.syntax().range().start(),
- last_segment.syntax().range().start(),
- ));
- }
- },
- )
+ ctx.build(format!("import {} in the current file", fmt_segments(&segments)), |edit| {
+ let action = best_action_for_target(current_file.syntax(), path, &segments);
+ make_assist(&action, segments.as_slice(), edit);
+ if let Some(last_segment) = path.segment() {
+ // Here we are assuming the assist will provide a correct use statement
+ // so we can delete the path qualifier
+ edit.delete(TextRange::from_to(
+ path.syntax().range().start(),
+ last_segment.syntax().range().start(),
+ ));
+ }
+ })
}
#[cfg(test)]