From be0f48f7cf801a6330d3e6a65b3352d544d32f63 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 27 Oct 2019 16:48:20 +0300 Subject: move public stuff to top --- crates/ra_assists/src/assists/add_import.rs | 52 ++++++++++++++--------------- 1 file 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::{ AssistId, }; +// This function produces sequence of text edits into edit +// to import the target path in the most appropriate scope given +// the cursor position +pub fn auto_import_text_edit( + // Ideally the position of the cursor, used to + position: &SyntaxNode, + // The statement to use as anchor (last resort) + anchor: &SyntaxNode, + // The path to import as a sequence of strings + target: &[SmolStr], + edit: &mut TextEditBuilder, +) { + let container = position.ancestors().find_map(|n| { + if let Some(module) = ast::Module::cast(n.clone()) { + return module.item_list().map(|it| it.syntax().clone()); + } + ast::SourceFile::cast(n).map(|it| it.syntax().clone()) + }); + + if let Some(container) = container { + let action = best_action_for_target(container, anchor.clone(), target); + make_assist(&action, target, edit); + } +} + pub(crate) fn add_import(mut ctx: AssistCtx) -> Option { let path: ast::Path = ctx.find_node_at_offset()?; // We don't want to mess with use statements @@ -552,7 +577,7 @@ fn apply_auto_import( } } -pub fn collect_hir_path_segments(path: &hir::Path) -> Option> { +fn collect_hir_path_segments(path: &hir::Path) -> Option> { let mut ps = Vec::::with_capacity(10); match path.kind { hir::PathKind::Abs => ps.push("".into()), @@ -568,31 +593,6 @@ pub fn collect_hir_path_segments(path: &hir::Path) -> Option> { Some(ps) } -// This function produces sequence of text edits into edit -// to import the target path in the most appropriate scope given -// the cursor position -pub fn auto_import_text_edit( - // Ideally the position of the cursor, used to - position: &SyntaxNode, - // The statement to use as anchor (last resort) - anchor: &SyntaxNode, - // The path to import as a sequence of strings - target: &[SmolStr], - edit: &mut TextEditBuilder, -) { - let container = position.ancestors().find_map(|n| { - if let Some(module) = ast::Module::cast(n.clone()) { - return module.item_list().map(|it| it.syntax().clone()); - } - ast::SourceFile::cast(n).map(|it| it.syntax().clone()) - }); - - if let Some(container) = container { - let action = best_action_for_target(container, anchor.clone(), target); - make_assist(&action, target, edit); - } -} - #[cfg(test)] mod tests { use super::*; -- cgit v1.2.3