From cf0eff2e332f46eda4fcecb043854c9c0d710e4e Mon Sep 17 00:00:00 2001 From: Andrea Pretto Date: Fri, 19 Apr 2019 22:30:05 +0200 Subject: auto_import: better no anchor management --- crates/ra_assists/src/auto_import.rs | 55 +++++++++++++++++++++++++++++++----- 1 file changed, 48 insertions(+), 7 deletions(-) diff --git a/crates/ra_assists/src/auto_import.rs b/crates/ra_assists/src/auto_import.rs index 7527d805d..b002d0e4d 100644 --- a/crates/ra_assists/src/auto_import.rs +++ b/crates/ra_assists/src/auto_import.rs @@ -168,7 +168,7 @@ fn walk_use_tree_for_best_action<'a>( current_path_segments: &mut Vec<&'a ast::PathSegment>, // buffer containing path segments current_parent_use_tree_list: Option<&'a ast::UseTreeList>, // will be Some value if we are in a nested import current_use_tree: &'a ast::UseTree, // the use tree we are currently examinating - target: &[SmolStr], // the path we want to import + target: &[SmolStr], // the path we want to import ) -> ImportAction<'a> { // We save the number of segments in the buffer so we can restore the correct segments // before returning. Recursive call will add segments so we need to delete them. @@ -341,11 +341,11 @@ fn best_action_for_target<'b, 'a: 'b>( None => { // We have no action and no UseItem was found in container so we find // another item and we use it as anchor. - // If there are no items, we choose the target path itself as anchor. + // If there are no items above, we choose the target path itself as anchor. + // todo: we should include even whitespace blocks as anchor candidates let anchor = container .children() - .find_map(ast::ModuleItem::cast) - .map(AstNode::syntax) + .find(|n| n.range().start() < anchor.range().start()) .or(Some(anchor)); return ImportAction::add_new_use(anchor, false); @@ -498,9 +498,9 @@ pub fn collect_hir_path_segments(path: &hir::Path) -> Vec { match path.kind { hir::PathKind::Abs => ps.push("".into()), hir::PathKind::Crate => ps.push("crate".into()), - hir::PathKind::Plain => {}, + hir::PathKind::Plain => {} hir::PathKind::Self_ => ps.push("self".into()), - hir::PathKind::Super => ps.push("super".into()) + hir::PathKind::Super => ps.push("super".into()), } for s in path.segments.iter() { ps.push(s.name.to_smolstr()); @@ -513,7 +513,7 @@ pub fn collect_hir_path_segments(path: &hir::Path) -> Vec { // the cursor position #[allow(unused)] pub fn auto_import_text_edit( - // Ideally the position of the cursor, used to + // Ideally the position of the cursor, used to position: &SyntaxNode, // The statement to use as anchor (last resort) anchor: &SyntaxNode, @@ -590,6 +590,47 @@ std::fmt::Debug<|> " use std::fmt::Debug; +Debug<|> + ", + ); + } + #[test] + fn test_auto_import_add_use_no_anchor_with_item_below() { + check_assist( + auto_import, + " +std::fmt::Debug<|> + +fn main() { +} + ", + " +use std::fmt::Debug; + +Debug<|> + +fn main() { +} + ", + ); + } + + #[test] + fn test_auto_import_add_use_no_anchor_with_item_above() { + check_assist( + auto_import, + " +fn main() { +} + +std::fmt::Debug<|> + ", + " +use std::fmt::Debug; + +fn main() { +} + Debug<|> ", ); -- cgit v1.2.3