aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src/assists/add_import.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_assists/src/assists/add_import.rs')
-rw-r--r--crates/ra_assists/src/assists/add_import.rs66
1 files changed, 29 insertions, 37 deletions
diff --git a/crates/ra_assists/src/assists/add_import.rs b/crates/ra_assists/src/assists/add_import.rs
index 149d1403f..e87fae1af 100644
--- a/crates/ra_assists/src/assists/add_import.rs
+++ b/crates/ra_assists/src/assists/add_import.rs
@@ -1,5 +1,3 @@
1//! FIXME: write short doc here
2
3use hir::{self, db::HirDatabase}; 1use hir::{self, db::HirDatabase};
4use ra_syntax::{ 2use ra_syntax::{
5 ast::{self, NameOwner}, 3 ast::{self, NameOwner},
@@ -14,9 +12,9 @@ use crate::{
14 AssistId, 12 AssistId,
15}; 13};
16 14
17// This function produces sequence of text edits into edit 15/// This function produces sequence of text edits into edit
18// to import the target path in the most appropriate scope given 16/// to import the target path in the most appropriate scope given
19// the cursor position 17/// the cursor position
20pub fn auto_import_text_edit( 18pub fn auto_import_text_edit(
21 // Ideally the position of the cursor, used to 19 // Ideally the position of the cursor, used to
22 position: &SyntaxNode, 20 position: &SyntaxNode,
@@ -39,7 +37,20 @@ pub fn auto_import_text_edit(
39 } 37 }
40} 38}
41 39
42pub(crate) fn add_import(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { 40// Assist: add_import
41//
42// Adds a use statement for a given fully-qualified path.
43//
44// ```
45// fn process(map: std::collections::<|>HashMap<String, String>) {}
46// ```
47// ->
48// ```
49// use std::collections::HashMap;
50//
51// fn process(map: HashMap<String, String>) {}
52// ```
53pub(crate) fn add_import(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
43 let path: ast::Path = ctx.find_node_at_offset()?; 54 let path: ast::Path = ctx.find_node_at_offset()?;
44 // We don't want to mess with use statements 55 // We don't want to mess with use statements
45 if path.syntax().ancestors().find_map(ast::UseItem::cast).is_some() { 56 if path.syntax().ancestors().find_map(ast::UseItem::cast).is_some() {
@@ -52,38 +63,18 @@ pub(crate) fn add_import(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist>
52 return None; 63 return None;
53 } 64 }
54 65
55 if let Some(module) = path.syntax().ancestors().find_map(ast::Module::cast) { 66 let module = path.syntax().ancestors().find_map(ast::Module::cast);
56 if let (Some(item_list), Some(name)) = (module.item_list(), module.name()) { 67 let position = match module.and_then(|it| it.item_list()) {
57 ctx.add_action( 68 Some(item_list) => item_list.syntax().clone(),
58 AssistId("add_import"), 69 None => {
59 format!("import {} in mod {}", fmt_segments(&segments), name.text()), 70 let current_file = path.syntax().ancestors().find_map(ast::SourceFile::cast)?;
60 |edit| { 71 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 } 72 }
70 } else { 73 };
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 74
86 ctx.build() 75 ctx.add_assist(AssistId("add_import"), format!("import {}", fmt_segments(&segments)), |edit| {
76 apply_auto_import(&position, &path, &segments, edit.text_edit_builder());
77 })
87} 78}
88 79
89fn collect_path_segments_raw( 80fn collect_path_segments_raw(
@@ -595,9 +586,10 @@ fn collect_hir_path_segments(path: &hir::Path) -> Option<Vec<SmolStr>> {
595 586
596#[cfg(test)] 587#[cfg(test)]
597mod tests { 588mod tests {
598 use super::*;
599 use crate::helpers::{check_assist, check_assist_not_applicable}; 589 use crate::helpers::{check_assist, check_assist_not_applicable};
600 590
591 use super::*;
592
601 #[test] 593 #[test]
602 fn test_auto_import_add_use_no_anchor() { 594 fn test_auto_import_add_use_no_anchor() {
603 check_assist( 595 check_assist(