From 561b4b11ff1d87ea1ff2477dcba6ae1f396573a3 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 7 Feb 2020 15:53:31 +0100 Subject: [PATCH] Name assist handlers --- crates/ra_assists/src/assist_ctx.rs | 2 + .../{assists => handlers}/add_custom_impl.rs | 0 .../src/{assists => handlers}/add_derive.rs | 0 .../add_explicit_type.rs | 0 .../src/{assists => handlers}/add_impl.rs | 0 .../src/{assists => handlers}/add_import.rs | 0 .../add_missing_impl_members.rs | 0 .../src/{assists => handlers}/add_new.rs | 0 .../{assists => handlers}/apply_demorgan.rs | 0 .../src/{assists => handlers}/auto_import.rs | 0 .../change_visibility.rs | 0 .../src/{assists => handlers}/early_return.rs | 2 +- .../{assists => handlers}/fill_match_arms.rs | 0 .../src/{assists => handlers}/flip_binexpr.rs | 0 .../src/{assists => handlers}/flip_comma.rs | 0 .../{assists => handlers}/flip_trait_bound.rs | 0 .../inline_local_variable.rs | 0 .../introduce_variable.rs | 0 .../src/{assists => handlers}/invert_if.rs | 0 .../{assists => handlers}/merge_match_arms.rs | 0 .../src/{assists => handlers}/move_bounds.rs | 0 .../src/{assists => handlers}/move_guard.rs | 0 .../src/{assists => handlers}/raw_string.rs | 0 .../src/{assists => handlers}/remove_dbg.rs | 0 .../replace_if_let_with_match.rs | 0 .../src/{assists => handlers}/split_import.rs | 0 crates/ra_assists/src/lib.rs | 46 ++++++------------- xtask/src/codegen.rs | 2 +- xtask/tests/tidy-tests/docs.rs | 2 +- 29 files changed, 19 insertions(+), 35 deletions(-) rename crates/ra_assists/src/{assists => handlers}/add_custom_impl.rs (100%) rename crates/ra_assists/src/{assists => handlers}/add_derive.rs (100%) rename crates/ra_assists/src/{assists => handlers}/add_explicit_type.rs (100%) rename crates/ra_assists/src/{assists => handlers}/add_impl.rs (100%) rename crates/ra_assists/src/{assists => handlers}/add_import.rs (100%) rename crates/ra_assists/src/{assists => handlers}/add_missing_impl_members.rs (100%) rename crates/ra_assists/src/{assists => handlers}/add_new.rs (100%) rename crates/ra_assists/src/{assists => handlers}/apply_demorgan.rs (100%) rename crates/ra_assists/src/{assists => handlers}/auto_import.rs (100%) rename crates/ra_assists/src/{assists => handlers}/change_visibility.rs (100%) rename crates/ra_assists/src/{assists => handlers}/early_return.rs (99%) rename crates/ra_assists/src/{assists => handlers}/fill_match_arms.rs (100%) rename crates/ra_assists/src/{assists => handlers}/flip_binexpr.rs (100%) rename crates/ra_assists/src/{assists => handlers}/flip_comma.rs (100%) rename crates/ra_assists/src/{assists => handlers}/flip_trait_bound.rs (100%) rename crates/ra_assists/src/{assists => handlers}/inline_local_variable.rs (100%) rename crates/ra_assists/src/{assists => handlers}/introduce_variable.rs (100%) rename crates/ra_assists/src/{assists => handlers}/invert_if.rs (100%) rename crates/ra_assists/src/{assists => handlers}/merge_match_arms.rs (100%) rename crates/ra_assists/src/{assists => handlers}/move_bounds.rs (100%) rename crates/ra_assists/src/{assists => handlers}/move_guard.rs (100%) rename crates/ra_assists/src/{assists => handlers}/raw_string.rs (100%) rename crates/ra_assists/src/{assists => handlers}/remove_dbg.rs (100%) rename crates/ra_assists/src/{assists => handlers}/replace_if_let_with_match.rs (100%) rename crates/ra_assists/src/{assists => handlers}/split_import.rs (100%) diff --git a/crates/ra_assists/src/assist_ctx.rs b/crates/ra_assists/src/assist_ctx.rs index 44d6f68081..81f999090a 100644 --- a/crates/ra_assists/src/assist_ctx.rs +++ b/crates/ra_assists/src/assist_ctx.rs @@ -19,6 +19,8 @@ pub(crate) enum Assist { Resolved { assist: ResolvedAssist }, } +pub(crate) type AssistHandler = fn(AssistCtx) -> Option; + /// `AssistCtx` allows to apply an assist or check if it could be applied. /// /// Assists use a somewhat over-engineered approach, given the current needs. The diff --git a/crates/ra_assists/src/assists/add_custom_impl.rs b/crates/ra_assists/src/handlers/add_custom_impl.rs similarity index 100% rename from crates/ra_assists/src/assists/add_custom_impl.rs rename to crates/ra_assists/src/handlers/add_custom_impl.rs diff --git a/crates/ra_assists/src/assists/add_derive.rs b/crates/ra_assists/src/handlers/add_derive.rs similarity index 100% rename from crates/ra_assists/src/assists/add_derive.rs rename to crates/ra_assists/src/handlers/add_derive.rs diff --git a/crates/ra_assists/src/assists/add_explicit_type.rs b/crates/ra_assists/src/handlers/add_explicit_type.rs similarity index 100% rename from crates/ra_assists/src/assists/add_explicit_type.rs rename to crates/ra_assists/src/handlers/add_explicit_type.rs diff --git a/crates/ra_assists/src/assists/add_impl.rs b/crates/ra_assists/src/handlers/add_impl.rs similarity index 100% rename from crates/ra_assists/src/assists/add_impl.rs rename to crates/ra_assists/src/handlers/add_impl.rs diff --git a/crates/ra_assists/src/assists/add_import.rs b/crates/ra_assists/src/handlers/add_import.rs similarity index 100% rename from crates/ra_assists/src/assists/add_import.rs rename to crates/ra_assists/src/handlers/add_import.rs diff --git a/crates/ra_assists/src/assists/add_missing_impl_members.rs b/crates/ra_assists/src/handlers/add_missing_impl_members.rs similarity index 100% rename from crates/ra_assists/src/assists/add_missing_impl_members.rs rename to crates/ra_assists/src/handlers/add_missing_impl_members.rs diff --git a/crates/ra_assists/src/assists/add_new.rs b/crates/ra_assists/src/handlers/add_new.rs similarity index 100% rename from crates/ra_assists/src/assists/add_new.rs rename to crates/ra_assists/src/handlers/add_new.rs diff --git a/crates/ra_assists/src/assists/apply_demorgan.rs b/crates/ra_assists/src/handlers/apply_demorgan.rs similarity index 100% rename from crates/ra_assists/src/assists/apply_demorgan.rs rename to crates/ra_assists/src/handlers/apply_demorgan.rs diff --git a/crates/ra_assists/src/assists/auto_import.rs b/crates/ra_assists/src/handlers/auto_import.rs similarity index 100% rename from crates/ra_assists/src/assists/auto_import.rs rename to crates/ra_assists/src/handlers/auto_import.rs diff --git a/crates/ra_assists/src/assists/change_visibility.rs b/crates/ra_assists/src/handlers/change_visibility.rs similarity index 100% rename from crates/ra_assists/src/assists/change_visibility.rs rename to crates/ra_assists/src/handlers/change_visibility.rs diff --git a/crates/ra_assists/src/assists/early_return.rs b/crates/ra_assists/src/handlers/early_return.rs similarity index 99% rename from crates/ra_assists/src/assists/early_return.rs rename to crates/ra_assists/src/handlers/early_return.rs index 8f30dc5860..8eb3bd4738 100644 --- a/crates/ra_assists/src/assists/early_return.rs +++ b/crates/ra_assists/src/handlers/early_return.rs @@ -10,7 +10,7 @@ use ra_syntax::{ use crate::{ assist_ctx::{Assist, AssistCtx}, - assists::invert_if::invert_boolean_expression, + handlers::invert_if::invert_boolean_expression, AssistId, }; diff --git a/crates/ra_assists/src/assists/fill_match_arms.rs b/crates/ra_assists/src/handlers/fill_match_arms.rs similarity index 100% rename from crates/ra_assists/src/assists/fill_match_arms.rs rename to crates/ra_assists/src/handlers/fill_match_arms.rs diff --git a/crates/ra_assists/src/assists/flip_binexpr.rs b/crates/ra_assists/src/handlers/flip_binexpr.rs similarity index 100% rename from crates/ra_assists/src/assists/flip_binexpr.rs rename to crates/ra_assists/src/handlers/flip_binexpr.rs diff --git a/crates/ra_assists/src/assists/flip_comma.rs b/crates/ra_assists/src/handlers/flip_comma.rs similarity index 100% rename from crates/ra_assists/src/assists/flip_comma.rs rename to crates/ra_assists/src/handlers/flip_comma.rs diff --git a/crates/ra_assists/src/assists/flip_trait_bound.rs b/crates/ra_assists/src/handlers/flip_trait_bound.rs similarity index 100% rename from crates/ra_assists/src/assists/flip_trait_bound.rs rename to crates/ra_assists/src/handlers/flip_trait_bound.rs diff --git a/crates/ra_assists/src/assists/inline_local_variable.rs b/crates/ra_assists/src/handlers/inline_local_variable.rs similarity index 100% rename from crates/ra_assists/src/assists/inline_local_variable.rs rename to crates/ra_assists/src/handlers/inline_local_variable.rs diff --git a/crates/ra_assists/src/assists/introduce_variable.rs b/crates/ra_assists/src/handlers/introduce_variable.rs similarity index 100% rename from crates/ra_assists/src/assists/introduce_variable.rs rename to crates/ra_assists/src/handlers/introduce_variable.rs diff --git a/crates/ra_assists/src/assists/invert_if.rs b/crates/ra_assists/src/handlers/invert_if.rs similarity index 100% rename from crates/ra_assists/src/assists/invert_if.rs rename to crates/ra_assists/src/handlers/invert_if.rs diff --git a/crates/ra_assists/src/assists/merge_match_arms.rs b/crates/ra_assists/src/handlers/merge_match_arms.rs similarity index 100% rename from crates/ra_assists/src/assists/merge_match_arms.rs rename to crates/ra_assists/src/handlers/merge_match_arms.rs diff --git a/crates/ra_assists/src/assists/move_bounds.rs b/crates/ra_assists/src/handlers/move_bounds.rs similarity index 100% rename from crates/ra_assists/src/assists/move_bounds.rs rename to crates/ra_assists/src/handlers/move_bounds.rs diff --git a/crates/ra_assists/src/assists/move_guard.rs b/crates/ra_assists/src/handlers/move_guard.rs similarity index 100% rename from crates/ra_assists/src/assists/move_guard.rs rename to crates/ra_assists/src/handlers/move_guard.rs diff --git a/crates/ra_assists/src/assists/raw_string.rs b/crates/ra_assists/src/handlers/raw_string.rs similarity index 100% rename from crates/ra_assists/src/assists/raw_string.rs rename to crates/ra_assists/src/handlers/raw_string.rs diff --git a/crates/ra_assists/src/assists/remove_dbg.rs b/crates/ra_assists/src/handlers/remove_dbg.rs similarity index 100% rename from crates/ra_assists/src/assists/remove_dbg.rs rename to crates/ra_assists/src/handlers/remove_dbg.rs diff --git a/crates/ra_assists/src/assists/replace_if_let_with_match.rs b/crates/ra_assists/src/handlers/replace_if_let_with_match.rs similarity index 100% rename from crates/ra_assists/src/assists/replace_if_let_with_match.rs rename to crates/ra_assists/src/handlers/replace_if_let_with_match.rs diff --git a/crates/ra_assists/src/assists/split_import.rs b/crates/ra_assists/src/handlers/split_import.rs similarity index 100% rename from crates/ra_assists/src/assists/split_import.rs rename to crates/ra_assists/src/handlers/split_import.rs diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs index d476088a23..7b08e8fd7f 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs @@ -19,8 +19,8 @@ use ra_ide_db::RootDatabase; use ra_syntax::{TextRange, TextUnit}; use ra_text_edit::TextEdit; -pub(crate) use crate::assist_ctx::{Assist, AssistCtx}; -pub use crate::assists::add_import::auto_import_text_edit; +pub(crate) use crate::assist_ctx::{Assist, AssistCtx, AssistHandler}; +pub use crate::handlers::add_import::auto_import_text_edit; /// Unique identifier of the assist, should not be shown to the user /// directly. @@ -72,7 +72,7 @@ impl ResolvedAssist { /// returned, without actual edits. pub fn unresolved_assists(db: &RootDatabase, range: FileRange) -> Vec { let ctx = AssistCtx::new(db, range, false); - assists::all() + handlers::all() .iter() .filter_map(|f| f(ctx.clone())) .map(|a| match a { @@ -88,7 +88,7 @@ pub fn unresolved_assists(db: &RootDatabase, range: FileRange) -> Vec Vec { let ctx = AssistCtx::new(db, range, true); - let mut a = assists::all() + let mut a = handlers::all() .iter() .filter_map(|f| f(ctx.clone())) .map(|a| match a { @@ -109,8 +109,8 @@ fn sort_assists(assists: &mut [ResolvedAssist]) { }); } -mod assists { - use crate::{Assist, AssistCtx}; +mod handlers { + use crate::AssistHandler; mod add_derive; mod add_explicit_type; @@ -138,7 +138,7 @@ mod assists { mod move_bounds; mod early_return; - pub(crate) fn all() -> &'static [fn(AssistCtx) -> Option] { + pub(crate) fn all() -> &'static [AssistHandler] { &[ add_derive::add_derive, add_explicit_type::add_explicit_type, @@ -183,7 +183,7 @@ mod helpers { use ra_syntax::TextRange; use test_utils::{add_cursor, assert_eq_text, extract_offset, extract_range}; - use crate::{Assist, AssistCtx}; + use crate::{Assist, AssistCtx, AssistHandler}; pub(crate) fn with_single_file(text: &str) -> (RootDatabase, FileId) { let (mut db, file_id) = RootDatabase::with_single_file(text); @@ -194,7 +194,7 @@ mod helpers { (db, file_id) } - pub(crate) fn check_assist(assist: fn(AssistCtx) -> Option, before: &str, after: &str) { + pub(crate) fn check_assist(assist: AssistHandler, before: &str, after: &str) { let (before_cursor_pos, before) = extract_offset(before); let (db, file_id) = with_single_file(&before); let frange = @@ -218,11 +218,7 @@ mod helpers { assert_eq_text!(after, &actual); } - pub(crate) fn check_assist_range( - assist: fn(AssistCtx) -> Option, - before: &str, - after: &str, - ) { + pub(crate) fn check_assist_range(assist: AssistHandler, before: &str, after: &str) { let (range, before) = extract_range(before); let (db, file_id) = with_single_file(&before); let frange = FileRange { file_id, range }; @@ -240,11 +236,7 @@ mod helpers { assert_eq_text!(after, &actual); } - pub(crate) fn check_assist_target( - assist: fn(AssistCtx) -> Option, - before: &str, - target: &str, - ) { + pub(crate) fn check_assist_target(assist: AssistHandler, before: &str, target: &str) { let (before_cursor_pos, before) = extract_offset(before); let (db, file_id) = with_single_file(&before); let frange = @@ -260,11 +252,7 @@ mod helpers { assert_eq_text!(&before[range.start().to_usize()..range.end().to_usize()], target); } - pub(crate) fn check_assist_range_target( - assist: fn(AssistCtx) -> Option, - before: &str, - target: &str, - ) { + pub(crate) fn check_assist_range_target(assist: AssistHandler, before: &str, target: &str) { let (range, before) = extract_range(before); let (db, file_id) = with_single_file(&before); let frange = FileRange { file_id, range }; @@ -279,10 +267,7 @@ mod helpers { assert_eq_text!(&before[range.start().to_usize()..range.end().to_usize()], target); } - pub(crate) fn check_assist_not_applicable( - assist: fn(AssistCtx) -> Option, - before: &str, - ) { + pub(crate) fn check_assist_not_applicable(assist: AssistHandler, before: &str) { let (before_cursor_pos, before) = extract_offset(before); let (db, file_id) = with_single_file(&before); let frange = @@ -291,10 +276,7 @@ mod helpers { assert!(assist.is_none()); } - pub(crate) fn check_assist_range_not_applicable( - assist: fn(AssistCtx) -> Option, - before: &str, - ) { + pub(crate) fn check_assist_range_not_applicable(assist: AssistHandler, before: &str) { let (range, before) = extract_range(before); let (db, file_id) = with_single_file(&before); let frange = FileRange { file_id, range }; diff --git a/xtask/src/codegen.rs b/xtask/src/codegen.rs index efa638e060..a53d573359 100644 --- a/xtask/src/codegen.rs +++ b/xtask/src/codegen.rs @@ -25,7 +25,7 @@ const ERR_INLINE_TESTS_DIR: &str = "crates/ra_syntax/test_data/parser/inline/err pub const SYNTAX_KINDS: &str = "crates/ra_parser/src/syntax_kind/generated.rs"; pub const AST: &str = "crates/ra_syntax/src/ast/generated.rs"; -const ASSISTS_DIR: &str = "crates/ra_assists/src/assists"; +const ASSISTS_DIR: &str = "crates/ra_assists/src/handlers"; const ASSISTS_TESTS: &str = "crates/ra_assists/src/doc_tests/generated.rs"; const ASSISTS_DOCS: &str = "docs/user/assists.md"; diff --git a/xtask/tests/tidy-tests/docs.rs b/xtask/tests/tidy-tests/docs.rs index 8a005d6c4d..6a69e7d6a0 100644 --- a/xtask/tests/tidy-tests/docs.rs +++ b/xtask/tests/tidy-tests/docs.rs @@ -6,7 +6,7 @@ use xtask::project_root; fn is_exclude_dir(p: &Path) -> bool { // Test hopefully don't really need comments, and for assists we already // have special comments which are source of doc tests and user docs. - let exclude_dirs = ["tests", "test_data", "assists"]; + let exclude_dirs = ["tests", "test_data", "handlers"]; let mut cur_path = p; while let Some(path) = cur_path.parent() { if exclude_dirs.iter().any(|dir| path.ends_with(dir)) {