auto_import: import in enclosing module instead of file
This commit is contained in:
parent
468e1d4c5e
commit
6518fb2bf8
@ -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<impl HirDatabase>) -> Option<Assist
|
||||
return None;
|
||||
}
|
||||
|
||||
ctx.add_action(format!("import {} in the current file", fmt_segments(&segments)), |edit| {
|
||||
apply_auto_import(current_file.syntax(), path, &segments, edit);
|
||||
});
|
||||
if let Some(module) = path.syntax().ancestors().find_map(ast::Module::cast) {
|
||||
if let (Some(item_list), Some(name)) = (module.item_list(), module.name()) {
|
||||
ctx.add_action(
|
||||
format!("import {} in mod {}", fmt_segments(&segments), name.text()),
|
||||
|edit| {
|
||||
apply_auto_import(item_list.syntax(), path, &segments, edit);
|
||||
},
|
||||
);
|
||||
}
|
||||
} else {
|
||||
ctx.add_action(format!("import {} in the current file", fmt_segments(&segments)), |edit| {
|
||||
apply_auto_import(current_file.syntax(), path, &segments, edit);
|
||||
});
|
||||
}
|
||||
|
||||
ctx.build()
|
||||
}
|
||||
@ -762,4 +773,27 @@ use std::fmt<|>;
|
||||
",
|
||||
);
|
||||
}
|
||||
|
||||
#[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<|>
|
||||
}
|
||||
}
|
||||
",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user