aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_assists/src')
-rw-r--r--crates/ra_assists/src/assists/add_import.rs52
1 files changed, 26 insertions, 26 deletions
diff --git a/crates/ra_assists/src/assists/add_import.rs b/crates/ra_assists/src/assists/add_import.rs
index 048bc49c8..149d1403f 100644
--- a/crates/ra_assists/src/assists/add_import.rs
+++ b/crates/ra_assists/src/assists/add_import.rs
@@ -14,6 +14,31 @@ use crate::{
14 AssistId, 14 AssistId,
15}; 15};
16 16
17// This function produces sequence of text edits into edit
18// to import the target path in the most appropriate scope given
19// the cursor position
20pub fn auto_import_text_edit(
21 // Ideally the position of the cursor, used to
22 position: &SyntaxNode,
23 // The statement to use as anchor (last resort)
24 anchor: &SyntaxNode,
25 // The path to import as a sequence of strings
26 target: &[SmolStr],
27 edit: &mut TextEditBuilder,
28) {
29 let container = position.ancestors().find_map(|n| {
30 if let Some(module) = ast::Module::cast(n.clone()) {
31 return module.item_list().map(|it| it.syntax().clone());
32 }
33 ast::SourceFile::cast(n).map(|it| it.syntax().clone())
34 });
35
36 if let Some(container) = container {
37 let action = best_action_for_target(container, anchor.clone(), target);
38 make_assist(&action, target, edit);
39 }
40}
41
17pub(crate) fn add_import(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { 42pub(crate) fn add_import(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
18 let path: ast::Path = ctx.find_node_at_offset()?; 43 let path: ast::Path = ctx.find_node_at_offset()?;
19 // We don't want to mess with use statements 44 // We don't want to mess with use statements
@@ -552,7 +577,7 @@ fn apply_auto_import(
552 } 577 }
553} 578}
554 579
555pub fn collect_hir_path_segments(path: &hir::Path) -> Option<Vec<SmolStr>> { 580fn collect_hir_path_segments(path: &hir::Path) -> Option<Vec<SmolStr>> {
556 let mut ps = Vec::<SmolStr>::with_capacity(10); 581 let mut ps = Vec::<SmolStr>::with_capacity(10);
557 match path.kind { 582 match path.kind {
558 hir::PathKind::Abs => ps.push("".into()), 583 hir::PathKind::Abs => ps.push("".into()),
@@ -568,31 +593,6 @@ pub fn collect_hir_path_segments(path: &hir::Path) -> Option<Vec<SmolStr>> {
568 Some(ps) 593 Some(ps)
569} 594}
570 595
571// This function produces sequence of text edits into edit
572// to import the target path in the most appropriate scope given
573// the cursor position
574pub fn auto_import_text_edit(
575 // Ideally the position of the cursor, used to
576 position: &SyntaxNode,
577 // The statement to use as anchor (last resort)
578 anchor: &SyntaxNode,
579 // The path to import as a sequence of strings
580 target: &[SmolStr],
581 edit: &mut TextEditBuilder,
582) {
583 let container = position.ancestors().find_map(|n| {
584 if let Some(module) = ast::Module::cast(n.clone()) {
585 return module.item_list().map(|it| it.syntax().clone());
586 }
587 ast::SourceFile::cast(n).map(|it| it.syntax().clone())
588 });
589
590 if let Some(container) = container {
591 let action = best_action_for_target(container, anchor.clone(), target);
592 make_assist(&action, target, edit);
593 }
594}
595
596#[cfg(test)] 596#[cfg(test)]
597mod tests { 597mod tests {
598 use super::*; 598 use super::*;