From 9e638c9f3ee68784cd93ec0458b0c92c18776f06 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 27 Oct 2019 17:26:51 +0300 Subject: simplify --- crates/ra_assists/src/assists/add_import.rs | 43 +++++++++-------------------- 1 file changed, 13 insertions(+), 30 deletions(-) (limited to 'crates/ra_assists/src/assists/add_import.rs') diff --git a/crates/ra_assists/src/assists/add_import.rs b/crates/ra_assists/src/assists/add_import.rs index 149d1403f..7aa66b1fe 100644 --- a/crates/ra_assists/src/assists/add_import.rs +++ b/crates/ra_assists/src/assists/add_import.rs @@ -52,36 +52,18 @@ pub(crate) fn add_import(mut ctx: AssistCtx) -> Option return None; } - if let Some(module) = path.syntax().ancestors().find_map(ast::Module::cast) { - if let (Some(item_list), Some(name)) = (module.item_list(), module.name()) { - ctx.add_action( - AssistId("add_import"), - format!("import {} in mod {}", fmt_segments(&segments), name.text()), - |edit| { - apply_auto_import( - item_list.syntax(), - &path, - &segments, - edit.text_edit_builder(), - ); - }, - ); + let module = path.syntax().ancestors().find_map(ast::Module::cast); + let anchor = 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() } - } else { - let current_file = path.syntax().ancestors().find_map(ast::SourceFile::cast)?; - ctx.add_action( - AssistId("add_import"), - format!("import {} in the current file", fmt_segments(&segments)), - |edit| { - apply_auto_import( - current_file.syntax(), - &path, - &segments, - edit.text_edit_builder(), - ); - }, - ); - } + }; + + ctx.add_action(AssistId("add_import"), format!("import {}", fmt_segments(&segments)), |edit| { + apply_auto_import(&anchor, &path, &segments, edit.text_edit_builder()); + }); ctx.build() } @@ -595,9 +577,10 @@ fn collect_hir_path_segments(path: &hir::Path) -> Option> { #[cfg(test)] mod tests { - use super::*; use crate::helpers::{check_assist, check_assist_not_applicable}; + use super::*; + #[test] fn test_auto_import_add_use_no_anchor() { check_assist( -- cgit v1.2.3 From cda6355de23825c201d02e6062cb2dd414e98bf9 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 27 Oct 2019 17:35:37 +0300 Subject: simplify AssistCtx API We never actually use ability to create multiple actions out of a single context --- crates/ra_assists/src/assists/add_import.rs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) (limited to 'crates/ra_assists/src/assists/add_import.rs') diff --git a/crates/ra_assists/src/assists/add_import.rs b/crates/ra_assists/src/assists/add_import.rs index 7aa66b1fe..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( } } -pub(crate) fn add_import(mut ctx: AssistCtx) -> Option { +pub(crate) fn add_import(ctx: AssistCtx) -> Option { let path: ast::Path = ctx.find_node_at_offset()?; // We don't want to mess with use statements if path.syntax().ancestors().find_map(ast::UseItem::cast).is_some() { @@ -53,7 +53,7 @@ pub(crate) fn add_import(mut ctx: AssistCtx) -> Option } let module = path.syntax().ancestors().find_map(ast::Module::cast); - let anchor = match module.and_then(|it| it.item_list()) { + 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)?; @@ -61,11 +61,9 @@ pub(crate) fn add_import(mut ctx: AssistCtx) -> Option } }; - ctx.add_action(AssistId("add_import"), format!("import {}", fmt_segments(&segments)), |edit| { - apply_auto_import(&anchor, &path, &segments, edit.text_edit_builder()); - }); - - ctx.build() + ctx.add_assist(AssistId("add_import"), format!("import {}", fmt_segments(&segments)), |edit| { + apply_auto_import(&position, &path, &segments, edit.text_edit_builder()); + }) } fn collect_path_segments_raw( -- cgit v1.2.3