aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-10-27 14:26:51 +0000
committerAleksey Kladov <[email protected]>2019-10-27 14:27:05 +0000
commit9e638c9f3ee68784cd93ec0458b0c92c18776f06 (patch)
tree2fee74650c4390b0abacb3e422f7fc9c438872e7
parentad950830d0902aaacfb5a76355a203626eb93b5f (diff)
simplify
-rw-r--r--crates/ra_assists/src/assists/add_import.rs43
1 files changed, 13 insertions, 30 deletions
diff --git a/crates/ra_assists/src/assists/add_import.rs b/crates/ra_assists/src/assists/add_import.rs
index 149d1403f..7aa66b1fe 100644
--- a/crates/ra_assists/src/assists/add_import.rs
+++ b/crates/ra_assists/src/assists/add_import.rs
@@ -52,36 +52,18 @@ pub(crate) fn add_import(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist>
52 return None; 52 return None;
53 } 53 }
54 54
55 if let Some(module) = path.syntax().ancestors().find_map(ast::Module::cast) { 55 let module = path.syntax().ancestors().find_map(ast::Module::cast);
56 if let (Some(item_list), Some(name)) = (module.item_list(), module.name()) { 56 let anchor = match module.and_then(|it| it.item_list()) {
57 ctx.add_action( 57 Some(item_list) => item_list.syntax().clone(),
58 AssistId("add_import"), 58 None => {
59 format!("import {} in mod {}", fmt_segments(&segments), name.text()), 59 let current_file = path.syntax().ancestors().find_map(ast::SourceFile::cast)?;
60 |edit| { 60 current_file.syntax().clone()
61 apply_auto_import(
62 item_list.syntax(),
63 &path,
64 &segments,
65 edit.text_edit_builder(),
66 );
67 },
68 );
69 } 61 }
70 } else { 62 };
71 let current_file = path.syntax().ancestors().find_map(ast::SourceFile::cast)?; 63
72 ctx.add_action( 64 ctx.add_action(AssistId("add_import"), format!("import {}", fmt_segments(&segments)), |edit| {
73 AssistId("add_import"), 65 apply_auto_import(&anchor, &path, &segments, edit.text_edit_builder());
74 format!("import {} in the current file", fmt_segments(&segments)), 66 });
75 |edit| {
76 apply_auto_import(
77 current_file.syntax(),
78 &path,
79 &segments,
80 edit.text_edit_builder(),
81 );
82 },
83 );
84 }
85 67
86 ctx.build() 68 ctx.build()
87} 69}
@@ -595,9 +577,10 @@ fn collect_hir_path_segments(path: &hir::Path) -> Option<Vec<SmolStr>> {
595 577
596#[cfg(test)] 578#[cfg(test)]
597mod tests { 579mod tests {
598 use super::*;
599 use crate::helpers::{check_assist, check_assist_not_applicable}; 580 use crate::helpers::{check_assist, check_assist_not_applicable};
600 581
582 use super::*;
583
601 #[test] 584 #[test]
602 fn test_auto_import_add_use_no_anchor() { 585 fn test_auto_import_add_use_no_anchor() {
603 check_assist( 586 check_assist(