From 468e1d4c5e908022b858e0704abf6e01a6893829 Mon Sep 17 00:00:00 2001 From: Andrea Pretto Date: Mon, 11 Feb 2019 21:57:38 +0100 Subject: auto_import: more tests and some refactorings --- crates/ra_assists/src/auto_import.rs | 58 ++++++++++++++++++++++++++++-------- 1 file changed, 46 insertions(+), 12 deletions(-) (limited to 'crates/ra_assists/src/auto_import.rs') diff --git a/crates/ra_assists/src/auto_import.rs b/crates/ra_assists/src/auto_import.rs index b251c9369..3255a1008 100644 --- a/crates/ra_assists/src/auto_import.rs +++ b/crates/ra_assists/src/auto_import.rs @@ -345,9 +345,9 @@ fn best_action_for_target<'b, 'a: 'b>( match best_action { Some(action) => return action, None => { - // We have no action we no use item was found in container so we find + // 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 not items, we choose the target path itself as anchor. + // If there are no items, we choose the target path itself as anchor. let anchor = container .children() .find_map(ast::ModuleItem::cast) @@ -480,6 +480,24 @@ fn make_assist_add_nested_import( } } +fn apply_auto_import<'a>( + container: &SyntaxNode, + path: &ast::Path, + target: &[&'a ast::PathSegment], + edit: &mut AssistBuilder, +) { + let action = best_action_for_target(container, path, target); + make_assist(&action, target, edit); + if let (Some(first), Some(last)) = (target.first(), target.last()) { + // Here we are assuming the assist will provide a correct use statement + // so we can delete the path qualifier + edit.delete(TextRange::from_to( + first.syntax().range().start(), + last.syntax().range().start(), + )); + } +} + pub(crate) fn auto_import(mut ctx: AssistCtx) -> Option { let node = ctx.covering_node(); let current_file = node.ancestors().find_map(ast::SourceFile::cast)?; @@ -496,16 +514,7 @@ pub(crate) fn auto_import(mut ctx: AssistCtx) -> Option ); } + #[test] + fn test_auto_import_file_add_use_no_anchor_2seg() { + check_assist( + auto_import, + " +std::fmt<|>::Debug + ", + " +use std::fmt; + +fmt<|>::Debug + ", + ); + } + #[test] fn test_auto_import_file_add_use() { check_assist( @@ -725,6 +749,16 @@ impl Debug<|> for Foo { " impl foo<|> for Foo { } +", + ); + } + + #[test] + fn test_auto_import_not_applicable_in_use() { + check_assist_not_applicable( + auto_import, + " +use std::fmt<|>; ", ); } -- cgit v1.2.3 From 6518fb2bf8d970995f517ca222b11894edd61139 Mon Sep 17 00:00:00 2001 From: Andrea Pretto Date: Tue, 12 Feb 2019 21:14:51 +0100 Subject: auto_import: import in enclosing module instead of file --- crates/ra_assists/src/auto_import.rs | 42 ++++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 4 deletions(-) (limited to 'crates/ra_assists/src/auto_import.rs') diff --git a/crates/ra_assists/src/auto_import.rs b/crates/ra_assists/src/auto_import.rs index 3255a1008..52c2a0b2b 100644 --- a/crates/ra_assists/src/auto_import.rs +++ b/crates/ra_assists/src/auto_import.rs @@ -1,6 +1,6 @@ use hir::db::HirDatabase; use ra_syntax::{ - ast, AstNode, SyntaxNode, Direction, TextRange, + ast::{ self, NameOwner }, AstNode, SyntaxNode, Direction, TextRange, SyntaxKind::{ PATH, PATH_SEGMENT, COLONCOLON, COMMA } }; use crate::assist_ctx::{AssistCtx, Assist, AssistBuilder}; @@ -513,9 +513,20 @@ pub(crate) fn auto_import(mut ctx: AssistCtx) -> Option; ", ); } + + #[test] + fn test_auto_import_file_add_use_no_anchor_in_mod_mod() { + check_assist( + auto_import, + " +mod foo { + mod bar { + std::fmt::Debug<|> + } +} + ", + " +mod foo { + mod bar { + use std::fmt::Debug; + + Debug<|> + } +} + ", + ); + } } -- cgit v1.2.3