From 067cb928f09482c63ec2bfbdafb04fbd53b754e1 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 28 Feb 2020 22:43:13 +0100 Subject: Simplify --- crates/ra_assists/src/handlers/auto_import.rs | 1 - .../src/handlers/replace_qualified_name_with_use.rs | 16 +--------------- crates/ra_assists/src/utils/insert_use.rs | 9 ++------- 3 files changed, 3 insertions(+), 23 deletions(-) (limited to 'crates/ra_assists/src') diff --git a/crates/ra_assists/src/handlers/auto_import.rs b/crates/ra_assists/src/handlers/auto_import.rs index 46fbdb525..c8bf181f9 100644 --- a/crates/ra_assists/src/handlers/auto_import.rs +++ b/crates/ra_assists/src/handlers/auto_import.rs @@ -52,7 +52,6 @@ pub(crate) fn auto_import(ctx: AssistCtx) -> Option { group.add_assist(AssistId("auto_import"), format!("Import `{}`", &import), |edit| { edit.target(auto_import_assets.syntax_under_caret.text_range()); insert_use_statement( - &auto_import_assets.syntax_under_caret, &auto_import_assets.syntax_under_caret, &import, edit.text_edit_builder(), diff --git a/crates/ra_assists/src/handlers/replace_qualified_name_with_use.rs b/crates/ra_assists/src/handlers/replace_qualified_name_with_use.rs index 44f3f5e7f..94f5d6c50 100644 --- a/crates/ra_assists/src/handlers/replace_qualified_name_with_use.rs +++ b/crates/ra_assists/src/handlers/replace_qualified_name_with_use.rs @@ -33,26 +33,12 @@ pub(crate) fn replace_qualified_name_with_use(ctx: AssistCtx) -> Option return None; } - let module = path.syntax().ancestors().find_map(ast::Module::cast); - let position = match module.and_then(|it| it.item_list()) { - Some(item_list) => item_list.syntax().clone(), - None => { - let current_file = path.syntax().ancestors().find_map(ast::SourceFile::cast)?; - current_file.syntax().clone() - } - }; - ctx.add_assist( AssistId("replace_qualified_name_with_use"), "Replace qualified path with use", |edit| { let path_to_import = hir_path.mod_path().clone(); - insert_use_statement( - &position, - &path.syntax(), - &path_to_import, - edit.text_edit_builder(), - ); + insert_use_statement(path.syntax(), &path_to_import, edit.text_edit_builder()); if let Some(last) = path.segment() { // Here we are assuming the assist will provide a correct use statement diff --git a/crates/ra_assists/src/utils/insert_use.rs b/crates/ra_assists/src/utils/insert_use.rs index 7ae3440ca..36fd2fc0b 100644 --- a/crates/ra_assists/src/utils/insert_use.rs +++ b/crates/ra_assists/src/utils/insert_use.rs @@ -15,8 +15,6 @@ use ra_text_edit::TextEditBuilder; pub fn insert_use_statement( // Ideally the position of the cursor, used to position: &SyntaxNode, - // The statement to use as anchor (last resort) - anchor: &SyntaxNode, path_to_import: &ModPath, edit: &mut TextEditBuilder, ) { @@ -29,7 +27,7 @@ pub fn insert_use_statement( }); if let Some(container) = container { - let action = best_action_for_target(container, anchor.clone(), &target); + let action = best_action_for_target(container, position.clone(), &target); make_assist(&action, &target, edit); } } @@ -379,10 +377,7 @@ fn best_action_for_target( // another item and we use it as anchor. // If there are no items above, we choose the target path itself as anchor. // todo: we should include even whitespace blocks as anchor candidates - let anchor = container - .children() - .find(|n| n.text_range().start() < anchor.text_range().start()) - .or_else(|| Some(anchor)); + let anchor = container.children().next().or_else(|| Some(anchor)); let add_after_anchor = anchor .clone() -- cgit v1.2.3