rust/crates/ide_assists/src/lib.rs

268 lines
9.6 KiB
Rust
Raw Normal View History

2020-08-13 10:33:38 -05:00
//! `assists` crate provides a bunch of code assists, also known as code
2019-02-03 12:26:35 -06: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 09:58:16 -05:00
#[allow(unused)]
macro_rules! eprintln {
($($tt:tt)*) => { stdx::eprintln!($($tt)*) };
}
2020-05-17 05:09:53 -05:00
mod assist_config;
mod assist_context;
2019-10-25 06:16:46 -05:00
#[cfg(test)]
2020-05-06 03:16:55 -05:00
mod tests;
pub mod utils;
pub mod ast_transform;
2020-05-05 13:44:13 -05:00
use hir::Semantics;
2020-10-24 03:39:57 -05:00
use ide_db::base_db::FileRange;
2020-08-18 09:41:21 -05:00
use ide_db::{label::Label, source_change::SourceChange, RootDatabase};
2020-08-12 11:26:51 -05:00
use syntax::TextRange;
2019-02-03 12:26:35 -06:00
pub(crate) use crate::assist_context::{AssistContext, Assists};
2019-02-03 12:26:35 -06:00
pub use assist_config::AssistConfig;
2020-05-17 05:09:53 -05:00
2020-07-02 16:48:35 -05:00
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AssistKind {
// FIXME: does the None variant make sense? Probably not.
2020-07-02 16:48:35 -05:00
None,
2020-07-02 16:48:35 -05:00
QuickFix,
2020-07-03 12:14:42 -05:00
Generate,
2020-07-02 16:48:35 -05:00
Refactor,
RefactorExtract,
RefactorInline,
RefactorRewrite,
}
2020-07-13 16:41:47 -05:00
impl AssistKind {
pub fn contains(self, other: AssistKind) -> bool {
if self == other {
return true;
}
match self {
AssistKind::None | AssistKind::Generate => return true,
AssistKind::Refactor => match other {
AssistKind::RefactorExtract
| AssistKind::RefactorInline
| AssistKind::RefactorRewrite => return true,
_ => return false,
},
_ => return false,
}
}
}
2019-02-24 04:53:35 -06:00
/// Unique identifier of the assist, should not be shown to the user
/// directly.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2020-07-02 16:48:35 -05:00
pub struct AssistId(pub &'static str, pub AssistKind);
2019-02-24 04:53:35 -06:00
2020-05-07 10:29:23 -05:00
#[derive(Clone, Debug)]
pub struct GroupLabel(pub String);
#[derive(Debug, Clone)]
2020-05-07 10:09:59 -05:00
pub struct Assist {
pub id: AssistId,
2019-02-03 12:26:35 -06:00
/// Short description of the assist, as shown in the UI.
2020-08-18 09:41:21 -05:00
pub label: Label,
pub group: Option<GroupLabel>,
/// Target ranges are used to sort assists: the smaller the target range,
/// the more specific assist is, and so it should be sorted first.
pub target: TextRange,
/// Computing source change sometimes is much more costly then computing the
/// other fields. Additionally, the actual change is not required to show
/// the lightbulb UI, it only is needed when the user tries to apply an
/// assist. So, we compute it lazily: the API allow requesting assists with
/// or without source change. We could (and in fact, used to) distinguish
/// between resolved and unresolved assists at the type level, but this is
/// cumbersome, especially if you want to embed an assist into another data
/// structure, such as a diagnostic.
pub source_change: Option<SourceChange>,
2020-05-07 10:29:23 -05:00
}
2020-05-07 10:09:59 -05:00
impl Assist {
2020-05-07 10:29:23 -05:00
/// Return all the assists applicable at the given position.
pub fn get(
2020-05-17 05:09:53 -05:00
db: &RootDatabase,
config: &AssistConfig,
resolve: bool,
2020-05-17 05:09:53 -05:00
range: FileRange,
) -> Vec<Assist> {
2020-05-07 10:29:23 -05:00
let sema = Semantics::new(db);
2020-07-15 08:45:30 -05:00
let ctx = AssistContext::new(sema, config, range);
let mut acc = Assists::new(&ctx, resolve);
2020-05-07 10:29:23 -05:00
handlers::all().iter().for_each(|handler| {
handler(&mut acc, &ctx);
});
acc.finish()
2020-05-07 10:29:23 -05:00
}
2020-02-07 08:04:50 -06:00
}
2020-02-07 08:53:31 -06:00
mod handlers {
use crate::{AssistContext, Assists};
2020-05-05 15:14:01 -05:00
pub(crate) type Handler = fn(&mut Assists, &AssistContext) -> Option<()>;
2019-09-25 06:29:41 -05:00
mod add_explicit_type;
mod add_lifetime_to_type;
2020-03-18 10:48:45 -05:00
mod add_missing_impl_members;
2020-05-19 17:07:00 -05:00
mod add_turbo_fish;
mod apply_demorgan;
mod auto_import;
2019-09-25 06:29:41 -05:00
mod change_visibility;
2020-09-29 13:48:43 -05:00
mod convert_integer_literal;
2021-02-24 13:25:10 -06:00
mod convert_comment_block;
mod convert_iter_for_each_to_for;
mod convert_into_to_from;
2020-03-18 10:48:45 -05:00
mod early_return;
2020-08-02 14:56:54 -05:00
mod expand_glob_import;
mod extract_function;
mod extract_struct_from_enum_variant;
2021-03-26 13:39:20 -05:00
mod extract_type_alias;
2020-06-26 18:21:43 -05:00
mod extract_variable;
2019-09-25 06:29:41 -05:00
mod fill_match_arms;
mod fix_visibility;
2020-03-18 10:48:45 -05:00
mod flip_binexpr;
mod flip_comma;
mod flip_trait_bound;
mod generate_default_from_enum_variant;
mod generate_default_from_new;
mod generate_is_empty_from_len;
2021-04-11 02:31:20 -05:00
mod generate_deref;
mod generate_derive;
mod generate_enum_is_method;
mod generate_enum_projection_method;
2020-07-03 11:15:03 -05:00
mod generate_from_impl_for_enum;
mod generate_function;
mod generate_getter_mut;
2021-02-28 12:11:41 -06:00
mod generate_getter;
2020-07-03 11:15:03 -05:00
mod generate_impl;
mod generate_new;
mod generate_setter;
2020-11-05 18:47:41 -06:00
mod infer_function_return_type;
mod inline_function;
2019-09-25 06:29:41 -05:00
mod inline_local_variable;
2020-06-01 08:36:51 -05:00
mod introduce_named_lifetime;
2020-03-18 10:48:45 -05:00
mod invert_if;
mod merge_imports;
2020-03-18 10:48:45 -05:00
mod merge_match_arms;
mod move_bounds;
mod move_guard;
2021-01-06 07:24:47 -06:00
mod move_module_to_file;
mod pull_assignment_up;
2020-10-14 12:56:20 -05:00
mod qualify_path;
2019-09-25 06:29:41 -05:00
mod raw_string;
2020-03-18 10:48:45 -05:00
mod remove_dbg;
2020-02-19 05:44:20 -06:00
mod remove_mut;
mod remove_unused_param;
mod reorder_fields;
mod reorder_impl;
2020-11-09 06:07:18 -06:00
mod replace_derive_with_manual_impl;
2021-02-28 12:11:41 -06:00
mod replace_for_loop_with_for_each;
2019-09-25 06:29:41 -05:00
mod replace_if_let_with_match;
mod replace_impl_trait_with_generic;
2020-03-27 06:12:17 -05:00
mod replace_let_with_if_let;
2020-03-18 10:48:45 -05:00
mod replace_qualified_name_with_use;
mod replace_string_with_char;
2020-03-26 04:16:10 -05:00
mod replace_unwrap_with_match;
2019-09-25 06:29:41 -05:00
mod split_import;
2020-11-30 04:45:32 -06:00
mod toggle_ignore;
2021-01-15 13:14:51 -06:00
mod unmerge_use;
mod unwrap_block;
2020-11-09 06:18:40 -06:00
mod wrap_return_type_in_result;
2019-09-25 06:29:41 -05:00
2020-05-05 15:14:01 -05:00
pub(crate) fn all() -> &'static [Handler] {
2019-09-25 06:29:41 -05:00
&[
// These are alphabetic for the foolish consistency
2019-09-25 06:29:41 -05:00
add_explicit_type::add_explicit_type,
add_lifetime_to_type::add_lifetime_to_type,
2020-05-19 17:07:00 -05:00
add_turbo_fish::add_turbo_fish,
apply_demorgan::apply_demorgan,
2020-03-18 10:48:45 -05:00
auto_import::auto_import,
2019-09-25 06:29:41 -05:00
change_visibility::change_visibility,
2020-09-29 13:48:43 -05:00
convert_integer_literal::convert_integer_literal,
2021-02-24 13:25:10 -06:00
convert_comment_block::convert_comment_block,
convert_iter_for_each_to_for::convert_iter_for_each_to_for,
convert_into_to_from::convert_into_to_from,
2020-03-18 10:48:45 -05:00
early_return::convert_to_guarded_return,
2020-08-02 14:56:54 -05:00
expand_glob_import::expand_glob_import,
2020-06-03 13:43:57 -05:00
extract_struct_from_enum_variant::extract_struct_from_enum_variant,
2021-03-26 13:39:20 -05:00
extract_type_alias::extract_type_alias,
2019-09-25 06:29:41 -05:00
fill_match_arms::fill_match_arms,
fix_visibility::fix_visibility,
2019-09-25 06:29:41 -05:00
flip_binexpr::flip_binexpr,
2020-03-18 10:48:45 -05:00
flip_comma::flip_comma,
flip_trait_bound::flip_trait_bound,
generate_default_from_enum_variant::generate_default_from_enum_variant,
generate_default_from_new::generate_default_from_new,
generate_is_empty_from_len::generate_is_empty_from_len,
2021-04-11 02:31:20 -05:00
generate_deref::generate_deref,
generate_derive::generate_derive,
generate_enum_is_method::generate_enum_is_method,
generate_enum_projection_method::generate_enum_as_method,
2021-02-28 12:11:41 -06:00
generate_enum_projection_method::generate_enum_try_into_method,
2020-07-03 11:15:03 -05:00
generate_from_impl_for_enum::generate_from_impl_for_enum,
generate_function::generate_function,
generate_getter_mut::generate_getter_mut,
2021-02-28 12:11:41 -06:00
generate_getter::generate_getter,
2020-07-03 11:15:03 -05:00
generate_impl::generate_impl,
generate_new::generate_new,
generate_setter::generate_setter,
2020-11-05 18:47:41 -06:00
infer_function_return_type::infer_function_return_type,
inline_function::inline_function,
2020-01-19 10:39:53 -06:00
inline_local_variable::inline_local_variable,
2020-06-01 08:36:51 -05:00
introduce_named_lifetime::introduce_named_lifetime,
2020-03-18 10:48:45 -05:00
invert_if::invert_if,
merge_imports::merge_imports,
2020-03-18 10:48:45 -05:00
merge_match_arms::merge_match_arms,
2019-09-25 06:29:41 -05:00
move_bounds::move_bounds_to_where_clause,
2020-03-18 10:48:45 -05:00
move_guard::move_arm_cond_to_match_guard,
move_guard::move_guard_to_arm_body,
2021-02-28 12:11:41 -06:00
move_module_to_file::move_module_to_file,
pull_assignment_up::pull_assignment_up,
2020-10-14 12:56:20 -05:00
qualify_path::qualify_path,
2019-09-25 06:29:41 -05:00
raw_string::add_hash,
raw_string::make_usual_string,
raw_string::remove_hash,
2020-03-18 10:48:45 -05:00
remove_dbg::remove_dbg,
2020-02-19 05:44:20 -06:00
remove_mut::remove_mut,
remove_unused_param::remove_unused_param,
2020-05-05 15:14:01 -05:00
reorder_fields::reorder_fields,
reorder_impl::reorder_impl,
2020-11-09 06:07:18 -06:00
replace_derive_with_manual_impl::replace_derive_with_manual_impl,
2021-02-28 12:11:41 -06:00
replace_for_loop_with_for_each::replace_for_loop_with_for_each,
2020-03-18 10:48:45 -05:00
replace_if_let_with_match::replace_if_let_with_match,
2020-12-05 08:41:36 -06:00
replace_if_let_with_match::replace_match_with_if_let,
replace_impl_trait_with_generic::replace_impl_trait_with_generic,
2020-03-27 06:12:17 -05:00
replace_let_with_if_let::replace_let_with_if_let,
2020-03-18 10:48:45 -05:00
replace_qualified_name_with_use::replace_qualified_name_with_use,
2020-03-26 04:16:10 -05:00
replace_unwrap_with_match::replace_unwrap_with_match,
2020-03-18 10:48:45 -05:00
split_import::split_import,
2020-11-30 04:45:32 -06:00
toggle_ignore::toggle_ignore,
2021-01-15 13:14:51 -06:00
unmerge_use::unmerge_use,
unwrap_block::unwrap_block,
2020-11-09 06:18:40 -06:00
wrap_return_type_in_result::wrap_return_type_in_result,
2021-02-22 06:18:11 -06: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.
add_missing_impl_members::add_missing_impl_members,
add_missing_impl_members::add_missing_default_members,
2020-10-26 15:59:28 -05:00
//
replace_string_with_char::replace_string_with_char,
raw_string::make_raw_string,
2021-02-22 06:18:11 -06:00
//
extract_variable::extract_variable,
extract_function::extract_function,
2020-05-05 15:14:01 -05:00
// Are you sure you want to add new assist here, and not to the
// sorted list above?
2019-09-25 06:29:41 -05:00
]
}
2019-02-03 12:26:35 -06:00
}