aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrea Pretto <[email protected]>2019-02-12 20:14:51 +0000
committerAndrea Pretto <[email protected]>2019-02-12 20:18:55 +0000
commit6518fb2bf8d970995f517ca222b11894edd61139 (patch)
tree2236bd993243c5c1b008fa4873b656ba93b15615
parent468e1d4c5e908022b858e0704abf6e01a6893829 (diff)
auto_import: import in enclosing module instead of file
-rw-r--r--crates/ra_assists/src/auto_import.rs42
1 files changed, 38 insertions, 4 deletions
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 @@
1use hir::db::HirDatabase; 1use hir::db::HirDatabase;
2use ra_syntax::{ 2use ra_syntax::{
3 ast, AstNode, SyntaxNode, Direction, TextRange, 3 ast::{ self, NameOwner }, AstNode, SyntaxNode, Direction, TextRange,
4 SyntaxKind::{ PATH, PATH_SEGMENT, COLONCOLON, COMMA } 4 SyntaxKind::{ PATH, PATH_SEGMENT, COLONCOLON, COMMA }
5}; 5};
6use crate::assist_ctx::{AssistCtx, Assist, AssistBuilder}; 6use crate::assist_ctx::{AssistCtx, Assist, AssistBuilder};
@@ -513,9 +513,20 @@ pub(crate) fn auto_import(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist
513 return None; 513 return None;
514 } 514 }
515 515
516 ctx.add_action(format!("import {} in the current file", fmt_segments(&segments)), |edit| { 516 if let Some(module) = path.syntax().ancestors().find_map(ast::Module::cast) {
517 apply_auto_import(current_file.syntax(), path, &segments, edit); 517 if let (Some(item_list), Some(name)) = (module.item_list(), module.name()) {
518 }); 518 ctx.add_action(
519 format!("import {} in mod {}", fmt_segments(&segments), name.text()),
520 |edit| {
521 apply_auto_import(item_list.syntax(), path, &segments, edit);
522 },
523 );
524 }
525 } else {
526 ctx.add_action(format!("import {} in the current file", fmt_segments(&segments)), |edit| {
527 apply_auto_import(current_file.syntax(), path, &segments, edit);
528 });
529 }
519 530
520 ctx.build() 531 ctx.build()
521} 532}
@@ -762,4 +773,27 @@ use std::fmt<|>;
762", 773",
763 ); 774 );
764 } 775 }
776
777 #[test]
778 fn test_auto_import_file_add_use_no_anchor_in_mod_mod() {
779 check_assist(
780 auto_import,
781 "
782mod foo {
783 mod bar {
784 std::fmt::Debug<|>
785 }
786}
787 ",
788 "
789mod foo {
790 mod bar {
791 use std::fmt::Debug;
792
793 Debug<|>
794 }
795}
796 ",
797 );
798 }
765} 799}