diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-10-27 14:38:28 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2019-10-27 14:38:28 +0000 |
commit | cf309b6a5f2d51e9509568ab82446cc3eae29f94 (patch) | |
tree | 54c4a26d7a6688a89515cdc97ec02c1a9f9103bf /crates/ra_assists/src/assists/add_import.rs | |
parent | ad950830d0902aaacfb5a76355a203626eb93b5f (diff) | |
parent | cda6355de23825c201d02e6062cb2dd414e98bf9 (diff) |
Merge #2094
2094: simplify AssistCtx API r=matklad a=matklad
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_assists/src/assists/add_import.rs')
-rw-r--r-- | crates/ra_assists/src/assists/add_import.rs | 45 |
1 files changed, 13 insertions, 32 deletions
diff --git a/crates/ra_assists/src/assists/add_import.rs b/crates/ra_assists/src/assists/add_import.rs index 149d1403f..c522d6a5a 100644 --- a/crates/ra_assists/src/assists/add_import.rs +++ b/crates/ra_assists/src/assists/add_import.rs | |||
@@ -39,7 +39,7 @@ pub fn auto_import_text_edit( | |||
39 | } | 39 | } |
40 | } | 40 | } |
41 | 41 | ||
42 | pub(crate) fn add_import(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 42 | pub(crate) fn add_import(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { |
43 | let path: ast::Path = ctx.find_node_at_offset()?; | 43 | let path: ast::Path = ctx.find_node_at_offset()?; |
44 | // We don't want to mess with use statements | 44 | // We don't want to mess with use statements |
45 | if path.syntax().ancestors().find_map(ast::UseItem::cast).is_some() { | 45 | if path.syntax().ancestors().find_map(ast::UseItem::cast).is_some() { |
@@ -52,38 +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 position = 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)?; | ||
72 | ctx.add_action( | ||
73 | AssistId("add_import"), | ||
74 | format!("import {} in the current file", fmt_segments(&segments)), | ||
75 | |edit| { | ||
76 | apply_auto_import( | ||
77 | current_file.syntax(), | ||
78 | &path, | ||
79 | &segments, | ||
80 | edit.text_edit_builder(), | ||
81 | ); | ||
82 | }, | ||
83 | ); | ||
84 | } | ||
85 | 63 | ||
86 | ctx.build() | 64 | ctx.add_assist(AssistId("add_import"), format!("import {}", fmt_segments(&segments)), |edit| { |
65 | apply_auto_import(&position, &path, &segments, edit.text_edit_builder()); | ||
66 | }) | ||
87 | } | 67 | } |
88 | 68 | ||
89 | fn collect_path_segments_raw( | 69 | fn collect_path_segments_raw( |
@@ -595,9 +575,10 @@ fn collect_hir_path_segments(path: &hir::Path) -> Option<Vec<SmolStr>> { | |||
595 | 575 | ||
596 | #[cfg(test)] | 576 | #[cfg(test)] |
597 | mod tests { | 577 | mod tests { |
598 | use super::*; | ||
599 | use crate::helpers::{check_assist, check_assist_not_applicable}; | 578 | use crate::helpers::{check_assist, check_assist_not_applicable}; |
600 | 579 | ||
580 | use super::*; | ||
581 | |||
601 | #[test] | 582 | #[test] |
602 | fn test_auto_import_add_use_no_anchor() { | 583 | fn test_auto_import_add_use_no_anchor() { |
603 | check_assist( | 584 | check_assist( |