]> git.lizzy.rs Git - rust.git/blobdiff - crates/ra_assists/src/auto_import.rs
ra_assists: assist "providers" can produce multiple assists
[rust.git] / crates / ra_assists / src / auto_import.rs
index 2ac19ab27fb6b401df7c935b4ad980aa210dd5db..b251c936948ea5ed15767a8fc42cf6b3be83a741 100644 (file)
@@ -4,43 +4,6 @@
     SyntaxKind::{ PATH, PATH_SEGMENT, COLONCOLON, COMMA }
 };
 use crate::assist_ctx::{AssistCtx, Assist, AssistBuilder};
-use itertools::{ Itertools, EitherOrBoth };
-
-// 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();
@@ -101,44 +64,9 @@ fn fmt_segments_raw(segments: &[&ast::PathSegment], buf: &mut String) {
     }
 }
 
-#[derive(Copy, Clone)]
-enum PathSegmentsMatch {
-    // Patch matches exactly
-    Full,
-    // When some of the segments matched
-    Partial(usize),
-    // When all the segments of the right path are matched against the left path,
-    // but the left path is longer.
-    PartialLeft(usize),
-    // When all the segments of the left path are matched against the right path,
-    // but the right path is longer.
-    PartialRight(usize),
-    // In all the three cases above we keep track of how many segments matched
-}
-
-fn compare_path_segments(
-    left: &[&ast::PathSegment],
-    right: &[&ast::PathSegment],
-) -> PathSegmentsMatch {
-    let mut matching = 0;
-    for either_or_both in left.iter().zip_longest(right.iter()) {
-        match either_or_both {
-            EitherOrBoth::Both(left, right) => {
-                if compare_path_segment(left, right) {
-                    matching += 1
-                } else {
-                    return PathSegmentsMatch::Partial(matching);
-                }
-            }
-            EitherOrBoth::Left(_) => {
-                return PathSegmentsMatch::PartialLeft(matching);
-            }
-            EitherOrBoth::Right(_) => {
-                return PathSegmentsMatch::PartialRight(matching);
-            }
-        }
-    }
-    return PathSegmentsMatch::Full;
+// 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();
 }
 
 fn compare_path_segment(a: &ast::PathSegment, b: &ast::PathSegment) -> bool {
@@ -172,30 +100,57 @@ fn compare_path_segment_with_name(a: &ast::PathSegment, b: &ast::Name) -> bool {
 enum ImportAction<'a> {
     Nothing,
     // Add a brand new use statement.
-    AddNewUse(
-        Option<&'a SyntaxNode>, // anchor node
-        bool,                   // true if we want to add the new statement after the anchor
-    ),
-
-    // In the following actions we keep track of how may segments matched,
-    // so we can choose the best action to take.
+    AddNewUse {
+        anchor: Option<&'a SyntaxNode>, // anchor node
+        add_after_anchor: bool,
+    },
 
     // To split an existing use statement creating a nested import.
-    AddNestedImport(
-        usize,
-        &'a ast::Path,                // the complete path we want to split
-        Option<&'a ast::PathSegment>, // the first segment of path we want to add into the new nested list
-        bool,                         // true if we want to add 'self' in addition to the segment
-    ),
+    AddNestedImport {
+        // how may segments matched with the target path
+        common_segments: usize,
+        path_to_split: &'a ast::Path,
+        // the first segment of path_to_split we want to add into the new nested list
+        first_segment_to_split: Option<&'a ast::PathSegment>,
+        // Wether to add 'self' in addition to the target path
+        add_self: bool,
+    },
     // To add the target path to an existing nested import tree list.
-    AddInTreeList(
-        usize,
-        &'a ast::UseTreeList,
-        bool, // true if we want to add 'self'
-    ),
+    AddInTreeList {
+        common_segments: usize,
+        // The UseTreeList where to add the target path
+        tree_list: &'a ast::UseTreeList,
+        add_self: bool,
+    },
 }
 
 impl<'a> ImportAction<'a> {
+    fn add_new_use(anchor: Option<&'a SyntaxNode>, add_after_anchor: bool) -> Self {
+        ImportAction::AddNewUse { anchor, add_after_anchor }
+    }
+
+    fn add_nested_import(
+        common_segments: usize,
+        path_to_split: &'a ast::Path,
+        first_segment_to_split: Option<&'a ast::PathSegment>,
+        add_self: bool,
+    ) -> Self {
+        ImportAction::AddNestedImport {
+            common_segments,
+            path_to_split,
+            first_segment_to_split,
+            add_self,
+        }
+    }
+
+    fn add_in_tree_list(
+        common_segments: usize,
+        tree_list: &'a ast::UseTreeList,
+        add_self: bool,
+    ) -> Self {
+        ImportAction::AddInTreeList { common_segments, tree_list, add_self }
+    }
+
     fn better<'b>(left: &'b ImportAction<'a>, right: &'b ImportAction<'a>) -> &'b ImportAction<'a> {
         if left.is_better(right) {
             left
@@ -207,13 +162,19 @@ fn better<'b>(left: &'b ImportAction<'a>, right: &'b ImportAction<'a>) -> &'b Im
     fn is_better(&self, other: &ImportAction) -> bool {
         match (self, other) {
             (ImportAction::Nothing, _) => true,
-            (ImportAction::AddInTreeList(..), ImportAction::Nothing) => false,
-            (ImportAction::AddNestedImport(n, ..), ImportAction::AddInTreeList(m, ..)) => n > m,
-            (ImportAction::AddInTreeList(n, ..), ImportAction::AddNestedImport(m, ..)) => n > m,
-            (ImportAction::AddInTreeList(..), _) => true,
-            (ImportAction::AddNestedImport(..), ImportAction::Nothing) => false,
-            (ImportAction::AddNestedImport(..), _) => true,
-            (ImportAction::AddNewUse(..), _) => false,
+            (ImportAction::AddInTreeList { .. }, ImportAction::Nothing) => false,
+            (
+                ImportAction::AddNestedImport { common_segments: n, .. },
+                ImportAction::AddInTreeList { common_segments: m, .. },
+            ) => n > m,
+            (
+                ImportAction::AddInTreeList { common_segments: n, .. },
+                ImportAction::AddNestedImport { common_segments: m, .. },
+            ) => n > m,
+            (ImportAction::AddInTreeList { .. }, _) => true,
+            (ImportAction::AddNestedImport { .. }, ImportAction::Nothing) => false,
+            (ImportAction::AddNestedImport { .. }, _) => true,
+            (ImportAction::AddNewUse { .. }, _) => false,
         }
     }
 }
@@ -237,7 +198,7 @@ fn walk_use_tree_for_best_action<'a>(
         Some(path) => path,
         None => {
             // If the use item don't have a path, it means it's broken (syntax error)
-            return ImportAction::AddNewUse(
+            return ImportAction::add_new_use(
                 current_use_tree
                     .syntax()
                     .ancestors()
@@ -259,13 +220,18 @@ fn walk_use_tree_for_best_action<'a>(
 
     // We compare only the new segments added in the line just above.
     // The first prev_len segments were already compared in 'parent' recursive calls.
-    let c = compare_path_segments(
-        target.split_at(prev_len).1,
-        current_path_segments.split_at(prev_len).1,
-    );
-
-    let mut action = match c {
-        PathSegmentsMatch::Full => {
+    let left = target.split_at(prev_len).1;
+    let right = current_path_segments.split_at(prev_len).1;
+    let common = compare_path_segments(left, right);
+    let mut action = match common {
+        0 => ImportAction::add_new_use(
+            // 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),
+            true,
+        ),
+        common if common == left.len() && left.len() == right.len() => {
             // e.g: target is std::fmt and we can have
             // 1- use std::fmt;
             // 2- use std::fmt:{ ... }
@@ -282,38 +248,32 @@ fn walk_use_tree_for_best_action<'a>(
                 if has_self {
                     ImportAction::Nothing
                 } else {
-                    ImportAction::AddInTreeList(current_path_segments.len(), list, true)
+                    ImportAction::add_in_tree_list(current_path_segments.len(), list, true)
                 }
             } else {
                 // Case 1
                 ImportAction::Nothing
             }
         }
-        PathSegmentsMatch::Partial(0) => ImportAction::AddNewUse(
-            // 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),
-            true,
-        ),
-        PathSegmentsMatch::Partial(n) => {
+        common if common != left.len() && left.len() == right.len() => {
             // e.g: target is std::fmt and we have
             // use std::io;
             // We need to split.
-            let segments_to_split = current_path_segments.split_at(prev_len + n).1;
-            ImportAction::AddNestedImport(prev_len + n, path, Some(segments_to_split[0]), false)
+            let segments_to_split = current_path_segments.split_at(prev_len + common).1;
+            ImportAction::add_nested_import(
+                prev_len + common,
+                path,
+                Some(segments_to_split[0]),
+                false,
+            )
         }
-        PathSegmentsMatch::PartialLeft(n) => {
+        common if left.len() > right.len() => {
             // e.g: target is std::fmt and we can have
             // 1- use std;
             // 2- use std::{ ... };
 
             // fallback action
-            let mut better_action = ImportAction::AddNewUse(
+            let mut better_action = ImportAction::add_new_use(
                 current_use_tree
                     .syntax()
                     .ancestors()
@@ -335,23 +295,29 @@ fn walk_use_tree_for_best_action<'a>(
                 }
             } else {
                 // Case 1, split
-                better_action = ImportAction::AddNestedImport(prev_len + n, path, None, true)
+                better_action = ImportAction::add_nested_import(prev_len + common, path, None, true)
             }
             better_action
         }
-        PathSegmentsMatch::PartialRight(n) => {
+        common if left.len() < right.len() => {
             // e.g: target is std::fmt and we can have
             // use std::fmt::Debug;
-            let segments_to_split = current_path_segments.split_at(prev_len + n).1;
-            ImportAction::AddNestedImport(prev_len + n, path, Some(segments_to_split[0]), true)
+            let segments_to_split = current_path_segments.split_at(prev_len + common).1;
+            ImportAction::add_nested_import(
+                prev_len + common,
+                path,
+                Some(segments_to_split[0]),
+                true,
+            )
         }
+        _ => unreachable!(),
     };
 
     // If we are inside a UseTreeList adding a use statement become adding to the existing
     // tree list.
     action = match (current_parent_use_tree_list, action) {
-        (Some(use_tree_list), ImportAction::AddNewUse(..)) => {
-            ImportAction::AddInTreeList(prev_len, use_tree_list, false)
+        (Some(use_tree_list), ImportAction::AddNewUse { .. }) => {
+            ImportAction::add_in_tree_list(prev_len, use_tree_list, false)
         }
         (_, _) => action,
     };
@@ -373,8 +339,7 @@ fn best_action_for_target<'b, 'a: 'b>(
         .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 {
@@ -389,26 +354,31 @@ fn best_action_for_target<'b, 'a: 'b>(
                 .map(AstNode::syntax)
                 .or(Some(path.syntax()));
 
-            return ImportAction::AddNewUse(anchor, false);
+            return ImportAction::add_new_use(anchor, false);
         }
     }
 }
 
 fn make_assist(action: &ImportAction, target: &[&ast::PathSegment], edit: &mut AssistBuilder) {
     match action {
-        ImportAction::AddNewUse(anchor, after) => {
-            make_assist_add_new_use(anchor, *after, target, edit)
+        ImportAction::AddNewUse { anchor, add_after_anchor } => {
+            make_assist_add_new_use(anchor, *add_after_anchor, target, edit)
         }
-        ImportAction::AddInTreeList(n, tree_list_node, 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
             // to modify, so we want to add only the last target.len() - n segments.
-            let segments_to_add = target.split_at(*n).1;
-            make_assist_add_in_tree_list(tree_list_node, segments_to_add, *add_self, edit)
+            let segments_to_add = target.split_at(*common_segments).1;
+            make_assist_add_in_tree_list(tree_list, segments_to_add, *add_self, edit)
         }
-        ImportAction::AddNestedImport(n, path, first_segment_to_split, add_self) => {
-            let segments_to_add = target.split_at(*n).1;
+        ImportAction::AddNestedImport {
+            common_segments,
+            path_to_split,
+            first_segment_to_split,
+            add_self,
+        } => {
+            let segments_to_add = target.split_at(*common_segments).1;
             make_assist_add_nested_import(
-                path,
+                path_to_split,
                 first_segment_to_split,
                 segments_to_add,
                 *add_self,
@@ -426,7 +396,7 @@ fn make_assist_add_new_use(
     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");
@@ -443,11 +413,7 @@ fn make_assist_add_new_use(
                 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);
     }
 }
@@ -461,10 +427,7 @@ fn make_assist_add_in_tree_list(
     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 {
@@ -517,18 +480,13 @@ fn make_assist_add_nested_import(
     }
 }
 
-pub(crate) fn auto_import(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
+pub(crate) fn auto_import(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
     let node = ctx.covering_node();
     let current_file = node.ancestors().find_map(ast::SourceFile::cast)?;
 
     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;
     }
 
@@ -537,21 +495,20 @@ pub(crate) fn auto_import(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
         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.add_action(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()
 }
 
 #[cfg(test)]