2020-08-13 17:33:38 +02:00
|
|
|
//! `assists` crate provides a bunch of code assists, also known as code
|
2019-02-03 21:26:35 +03:00
|
|
|
//! actions (in LSP) or intentions (in IntelliJ).
|
|
|
|
//!
|
|
|
|
//! An assist is a micro-refactoring, which is automatically activated in
|
|
|
|
//! certain context. For example, if the cursor is over `,`, a "swap `,`" assist
|
|
|
|
//! becomes available.
|
|
|
|
|
2020-04-06 16:58:16 +02:00
|
|
|
#[allow(unused)]
|
|
|
|
macro_rules! eprintln {
|
|
|
|
($($tt:tt)*) => { stdx::eprintln!($($tt)*) };
|
|
|
|
}
|
|
|
|
|
2020-05-17 12:09:53 +02:00
|
|
|
mod assist_config;
|
2020-05-06 18:45:35 +02:00
|
|
|
mod assist_context;
|
2019-10-25 14:16:46 +03:00
|
|
|
#[cfg(test)]
|
2020-05-06 10:16:55 +02:00
|
|
|
mod tests;
|
2020-02-09 12:24:34 -06:00
|
|
|
pub mod utils;
|
2019-02-11 18:07:21 +01:00
|
|
|
|
2020-05-05 20:44:13 +02:00
|
|
|
use hir::Semantics;
|
2021-06-14 13:27:11 +03:00
|
|
|
use ide_db::{base_db::FileRange, RootDatabase};
|
2020-08-12 18:26:51 +02:00
|
|
|
use syntax::TextRange;
|
2019-02-03 21:26:35 +03:00
|
|
|
|
2020-05-06 18:45:35 +02:00
|
|
|
pub(crate) use crate::assist_context::{AssistContext, Assists};
|
2019-02-03 21:26:35 +03:00
|
|
|
|
2021-01-16 19:33:36 +02:00
|
|
|
pub use assist_config::AssistConfig;
|
2021-06-14 13:27:11 +03:00
|
|
|
pub use ide_db::assists::{
|
|
|
|
Assist, AssistId, AssistKind, AssistResolveStrategy, GroupLabel, SingleResolve,
|
|
|
|
};
|
2020-02-09 15:32:53 +01:00
|
|
|
|
2021-06-14 13:18:03 +03:00
|
|
|
/// Return all the assists applicable at the given position.
|
|
|
|
pub fn assists(
|
|
|
|
db: &RootDatabase,
|
|
|
|
config: &AssistConfig,
|
|
|
|
resolve: AssistResolveStrategy,
|
|
|
|
range: FileRange,
|
|
|
|
) -> Vec<Assist> {
|
|
|
|
let sema = Semantics::new(db);
|
|
|
|
let ctx = AssistContext::new(sema, config, range);
|
|
|
|
let mut acc = Assists::new(&ctx, resolve);
|
|
|
|
handlers::all().iter().for_each(|handler| {
|
|
|
|
handler(&mut acc, &ctx);
|
|
|
|
});
|
|
|
|
acc.finish()
|
2020-02-07 15:04:50 +01:00
|
|
|
}
|
|
|
|
|
2020-02-07 15:53:31 +01:00
|
|
|
mod handlers {
|
2020-05-06 18:45:35 +02:00
|
|
|
use crate::{AssistContext, Assists};
|
2020-05-05 22:14:01 +02:00
|
|
|
|
2020-05-06 18:45:35 +02:00
|
|
|
pub(crate) type Handler = fn(&mut Assists, &AssistContext) -> Option<()>;
|
2019-09-25 14:29:41 +03:00
|
|
|
|
|
|
|
mod add_explicit_type;
|
2021-01-17 15:15:23 +01:00
|
|
|
mod add_lifetime_to_type;
|
2020-03-18 16:48:45 +01:00
|
|
|
mod add_missing_impl_members;
|
2020-05-20 00:07:00 +02:00
|
|
|
mod add_turbo_fish;
|
2019-10-03 22:19:46 +02:00
|
|
|
mod apply_demorgan;
|
2019-12-24 02:19:09 +02:00
|
|
|
mod auto_import;
|
2019-09-25 14:29:41 +03:00
|
|
|
mod change_visibility;
|
2021-08-08 17:12:08 +02:00
|
|
|
mod convert_bool_then;
|
2021-02-24 11:25:10 -08:00
|
|
|
mod convert_comment_block;
|
2021-08-08 17:12:08 +02:00
|
|
|
mod convert_integer_literal;
|
2021-04-02 14:00:56 +01:00
|
|
|
mod convert_into_to_from;
|
2021-08-08 17:12:08 +02:00
|
|
|
mod convert_iter_for_each_to_for;
|
2021-04-04 00:04:31 +03:00
|
|
|
mod convert_tuple_struct_to_named_struct;
|
2021-08-14 18:28:38 +03:00
|
|
|
mod convert_to_guarded_return;
|
2021-08-17 19:22:57 +02:00
|
|
|
mod destructure_tuple_binding;
|
2020-08-02 22:56:54 +03:00
|
|
|
mod expand_glob_import;
|
2021-02-03 10:57:11 +03:00
|
|
|
mod extract_function;
|
2020-05-22 22:28:30 +02:00
|
|
|
mod extract_struct_from_enum_variant;
|
2021-03-26 19:39:20 +01:00
|
|
|
mod extract_type_alias;
|
2020-06-27 01:21:43 +02:00
|
|
|
mod extract_variable;
|
2019-09-25 14:29:41 +03:00
|
|
|
mod fill_match_arms;
|
2020-05-20 13:33:13 +02:00
|
|
|
mod fix_visibility;
|
2020-03-18 16:48:45 +01:00
|
|
|
mod flip_binexpr;
|
|
|
|
mod flip_comma;
|
|
|
|
mod flip_trait_bound;
|
2020-12-13 22:00:44 +01:00
|
|
|
mod generate_default_from_enum_variant;
|
2021-02-28 01:48:51 +05:30
|
|
|
mod generate_default_from_new;
|
2021-04-11 00:31:20 -07:00
|
|
|
mod generate_deref;
|
2021-01-04 16:38:34 +03:00
|
|
|
mod generate_derive;
|
2021-02-15 23:28:57 +02:00
|
|
|
mod generate_enum_is_method;
|
2021-02-15 23:25:33 +02:00
|
|
|
mod generate_enum_projection_method;
|
2020-07-03 18:15:03 +02:00
|
|
|
mod generate_from_impl_for_enum;
|
|
|
|
mod generate_function;
|
2021-02-28 21:11:41 +03:00
|
|
|
mod generate_getter;
|
2020-07-03 18:15:03 +02:00
|
|
|
mod generate_impl;
|
2021-08-08 17:12:08 +02:00
|
|
|
mod generate_is_empty_from_len;
|
2020-07-03 18:15:03 +02:00
|
|
|
mod generate_new;
|
2021-02-09 12:30:13 +01:00
|
|
|
mod generate_setter;
|
2020-11-06 01:47:41 +01:00
|
|
|
mod infer_function_return_type;
|
2021-07-03 17:13:56 +02:00
|
|
|
mod inline_call;
|
2019-09-25 14:29:41 +03:00
|
|
|
mod inline_local_variable;
|
2020-06-01 15:36:51 +02:00
|
|
|
mod introduce_named_lifetime;
|
2020-03-18 16:48:45 +01:00
|
|
|
mod invert_if;
|
2020-03-18 16:41:24 +01:00
|
|
|
mod merge_imports;
|
2020-03-18 16:48:45 +01:00
|
|
|
mod merge_match_arms;
|
|
|
|
mod move_bounds;
|
|
|
|
mod move_guard;
|
2021-01-06 16:24:47 +03:00
|
|
|
mod move_module_to_file;
|
2021-01-04 16:38:34 +03:00
|
|
|
mod pull_assignment_up;
|
2020-10-14 19:56:20 +02:00
|
|
|
mod qualify_path;
|
2019-09-25 14:29:41 +03:00
|
|
|
mod raw_string;
|
2020-03-18 16:48:45 +01:00
|
|
|
mod remove_dbg;
|
2020-02-19 12:44:20 +01:00
|
|
|
mod remove_mut;
|
2020-08-19 18:44:33 +02:00
|
|
|
mod remove_unused_param;
|
2020-05-06 18:45:35 +02:00
|
|
|
mod reorder_fields;
|
2020-12-10 12:42:04 -07:00
|
|
|
mod reorder_impl;
|
2020-11-09 13:07:18 +01:00
|
|
|
mod replace_derive_with_manual_impl;
|
2021-02-28 21:11:41 +03:00
|
|
|
mod replace_for_loop_with_for_each;
|
2019-09-25 14:29:41 +03:00
|
|
|
mod replace_if_let_with_match;
|
2020-09-03 01:32:18 +03:00
|
|
|
mod replace_impl_trait_with_generic;
|
2020-03-27 12:12:17 +01:00
|
|
|
mod replace_let_with_if_let;
|
2020-03-18 16:48:45 +01:00
|
|
|
mod replace_qualified_name_with_use;
|
2020-10-16 20:21:16 +02:00
|
|
|
mod replace_string_with_char;
|
2019-09-25 14:29:41 +03:00
|
|
|
mod split_import;
|
2021-07-31 05:15:28 +03:00
|
|
|
mod sort_items;
|
2020-11-30 13:45:32 +03:00
|
|
|
mod toggle_ignore;
|
2021-01-15 22:14:51 +03:00
|
|
|
mod unmerge_use;
|
2020-04-29 13:52:55 +02:00
|
|
|
mod unwrap_block;
|
2020-11-09 13:18:40 +01:00
|
|
|
mod wrap_return_type_in_result;
|
2019-09-25 14:29:41 +03:00
|
|
|
|
2020-05-05 22:14:01 +02:00
|
|
|
pub(crate) fn all() -> &'static [Handler] {
|
2019-09-25 14:29:41 +03:00
|
|
|
&[
|
2020-04-09 10:00:27 +02:00
|
|
|
// These are alphabetic for the foolish consistency
|
2019-09-25 14:29:41 +03:00
|
|
|
add_explicit_type::add_explicit_type,
|
2021-01-17 15:15:23 +01:00
|
|
|
add_lifetime_to_type::add_lifetime_to_type,
|
2020-05-20 00:07:00 +02:00
|
|
|
add_turbo_fish::add_turbo_fish,
|
2019-10-03 22:19:46 +02:00
|
|
|
apply_demorgan::apply_demorgan,
|
2020-03-18 16:48:45 +01:00
|
|
|
auto_import::auto_import,
|
2019-09-25 14:29:41 +03:00
|
|
|
change_visibility::change_visibility,
|
2021-08-10 13:03:12 +02:00
|
|
|
convert_bool_then::convert_bool_then_to_if,
|
2021-08-14 18:28:38 +03:00
|
|
|
convert_bool_then::convert_if_to_bool_then,
|
2021-02-24 11:25:10 -08:00
|
|
|
convert_comment_block::convert_comment_block,
|
2021-08-08 17:12:08 +02:00
|
|
|
convert_integer_literal::convert_integer_literal,
|
2021-04-02 14:00:56 +01:00
|
|
|
convert_into_to_from::convert_into_to_from,
|
2021-08-08 17:12:08 +02:00
|
|
|
convert_iter_for_each_to_for::convert_iter_for_each_to_for,
|
2021-08-14 18:28:38 +03:00
|
|
|
convert_to_guarded_return::convert_to_guarded_return,
|
2021-04-04 00:04:31 +03:00
|
|
|
convert_tuple_struct_to_named_struct::convert_tuple_struct_to_named_struct,
|
2021-08-17 19:22:57 +02:00
|
|
|
destructure_tuple_binding::destructure_tuple_binding,
|
2020-08-02 22:56:54 +03:00
|
|
|
expand_glob_import::expand_glob_import,
|
2020-06-03 20:43:57 +02:00
|
|
|
extract_struct_from_enum_variant::extract_struct_from_enum_variant,
|
2021-03-26 19:39:20 +01:00
|
|
|
extract_type_alias::extract_type_alias,
|
2019-09-25 14:29:41 +03:00
|
|
|
fill_match_arms::fill_match_arms,
|
2020-05-20 13:33:13 +02:00
|
|
|
fix_visibility::fix_visibility,
|
2019-09-25 14:29:41 +03:00
|
|
|
flip_binexpr::flip_binexpr,
|
2020-03-18 16:48:45 +01:00
|
|
|
flip_comma::flip_comma,
|
2019-10-26 16:24:48 -04:00
|
|
|
flip_trait_bound::flip_trait_bound,
|
2020-12-13 22:00:44 +01:00
|
|
|
generate_default_from_enum_variant::generate_default_from_enum_variant,
|
2021-02-28 01:48:51 +05:30
|
|
|
generate_default_from_new::generate_default_from_new,
|
2021-04-11 00:31:20 -07:00
|
|
|
generate_deref::generate_deref,
|
2021-01-04 16:38:34 +03:00
|
|
|
generate_derive::generate_derive,
|
2021-02-15 23:28:57 +02:00
|
|
|
generate_enum_is_method::generate_enum_is_method,
|
2021-02-15 23:25:33 +02:00
|
|
|
generate_enum_projection_method::generate_enum_as_method,
|
2021-02-28 21:11:41 +03:00
|
|
|
generate_enum_projection_method::generate_enum_try_into_method,
|
2020-07-03 18:15:03 +02:00
|
|
|
generate_from_impl_for_enum::generate_from_impl_for_enum,
|
|
|
|
generate_function::generate_function,
|
|
|
|
generate_impl::generate_impl,
|
2021-08-14 18:28:38 +03:00
|
|
|
generate_is_empty_from_len::generate_is_empty_from_len,
|
2020-07-03 18:15:03 +02:00
|
|
|
generate_new::generate_new,
|
2020-11-06 01:47:41 +01:00
|
|
|
infer_function_return_type::infer_function_return_type,
|
2021-07-03 17:13:56 +02:00
|
|
|
inline_call::inline_call,
|
2020-01-19 17:39:53 +01:00
|
|
|
inline_local_variable::inline_local_variable,
|
2020-06-01 15:36:51 +02:00
|
|
|
introduce_named_lifetime::introduce_named_lifetime,
|
2020-03-18 16:48:45 +01:00
|
|
|
invert_if::invert_if,
|
2020-03-18 16:41:24 +01:00
|
|
|
merge_imports::merge_imports,
|
2020-03-18 16:48:45 +01:00
|
|
|
merge_match_arms::merge_match_arms,
|
2019-09-25 14:29:41 +03:00
|
|
|
move_bounds::move_bounds_to_where_clause,
|
2020-03-18 16:48:45 +01:00
|
|
|
move_guard::move_arm_cond_to_match_guard,
|
|
|
|
move_guard::move_guard_to_arm_body,
|
2021-02-28 21:11:41 +03:00
|
|
|
move_module_to_file::move_module_to_file,
|
2021-01-04 16:38:34 +03:00
|
|
|
pull_assignment_up::pull_assignment_up,
|
2020-10-14 19:56:20 +02:00
|
|
|
qualify_path::qualify_path,
|
2019-09-25 14:29:41 +03:00
|
|
|
raw_string::add_hash,
|
|
|
|
raw_string::make_usual_string,
|
|
|
|
raw_string::remove_hash,
|
2020-03-18 16:48:45 +01:00
|
|
|
remove_dbg::remove_dbg,
|
2020-02-19 12:44:20 +01:00
|
|
|
remove_mut::remove_mut,
|
2020-08-19 18:44:33 +02:00
|
|
|
remove_unused_param::remove_unused_param,
|
2020-05-05 22:14:01 +02:00
|
|
|
reorder_fields::reorder_fields,
|
2020-12-10 12:42:04 -07:00
|
|
|
reorder_impl::reorder_impl,
|
2020-11-09 13:07:18 +01:00
|
|
|
replace_derive_with_manual_impl::replace_derive_with_manual_impl,
|
2021-02-28 21:11:41 +03:00
|
|
|
replace_for_loop_with_for_each::replace_for_loop_with_for_each,
|
2020-03-18 16:48:45 +01:00
|
|
|
replace_if_let_with_match::replace_if_let_with_match,
|
2020-12-05 15:41:36 +01:00
|
|
|
replace_if_let_with_match::replace_match_with_if_let,
|
2020-09-03 01:32:18 +03:00
|
|
|
replace_impl_trait_with_generic::replace_impl_trait_with_generic,
|
2020-03-27 12:12:17 +01:00
|
|
|
replace_let_with_if_let::replace_let_with_if_let,
|
2020-03-18 16:48:45 +01:00
|
|
|
replace_qualified_name_with_use::replace_qualified_name_with_use,
|
2021-07-31 05:15:28 +03:00
|
|
|
sort_items::sort_items,
|
2020-03-18 16:48:45 +01:00
|
|
|
split_import::split_import,
|
2020-11-30 13:45:32 +03:00
|
|
|
toggle_ignore::toggle_ignore,
|
2021-01-15 22:14:51 +03:00
|
|
|
unmerge_use::unmerge_use,
|
2020-04-29 13:52:55 +02:00
|
|
|
unwrap_block::unwrap_block,
|
2020-11-09 13:18:40 +01:00
|
|
|
wrap_return_type_in_result::wrap_return_type_in_result,
|
2021-02-22 15:18:11 +03:00
|
|
|
// These are manually sorted for better priorities. By default,
|
|
|
|
// priority is determined by the size of the target range (smaller
|
|
|
|
// target wins). If the ranges are equal, position in this list is
|
|
|
|
// used as a tie-breaker.
|
2020-04-09 10:00:27 +02:00
|
|
|
add_missing_impl_members::add_missing_impl_members,
|
|
|
|
add_missing_impl_members::add_missing_default_members,
|
2020-10-26 21:59:28 +01:00
|
|
|
//
|
|
|
|
replace_string_with_char::replace_string_with_char,
|
2021-07-30 16:46:06 +02:00
|
|
|
replace_string_with_char::replace_char_with_string,
|
2020-10-26 21:59:28 +01:00
|
|
|
raw_string::make_raw_string,
|
2021-02-22 15:18:11 +03:00
|
|
|
//
|
|
|
|
extract_variable::extract_variable,
|
|
|
|
extract_function::extract_function,
|
2021-08-14 18:28:38 +03:00
|
|
|
//
|
|
|
|
generate_getter::generate_getter,
|
|
|
|
generate_getter::generate_getter_mut,
|
|
|
|
generate_setter::generate_setter,
|
2020-05-05 22:14:01 +02:00
|
|
|
// Are you sure you want to add new assist here, and not to the
|
|
|
|
// sorted list above?
|
2019-09-25 14:29:41 +03:00
|
|
|
]
|
|
|
|
}
|
2019-02-03 21:26:35 +03:00
|
|
|
}
|