rust/crates/ide-assists/src/lib.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

320 lines
14 KiB
Rust
Raw Normal View History

//! `assists` crate provides a bunch of code assists, also known as code actions
//! (in LSP) or intentions (in IntelliJ).
2019-02-03 12:26:35 -06:00
//!
//! 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.
//!
//! ## Assists Guidelines
//!
//! Assists are the main mechanism to deliver advanced IDE features to the user,
//! so we should pay extra attention to the UX.
//!
//! The power of assists comes from their context-awareness. The main problem
//! with IDE features is that there are a lot of them, and it's hard to teach
//! the user what's available. Assists solve this problem nicely: 💡 signifies
//! that *something* is possible, and clicking on it reveals a *short* list of
//! actions. Contrast it with Emacs `M-x`, which just spits an infinite list of
//! all the features.
//!
//! Here are some considerations when creating a new assist:
//!
//! * It's good to preserve semantics, and it's good to keep the code compiling,
//! but it isn't necessary. Example: "flip binary operation" might change
//! semantics.
//! * Assist shouldn't necessary make the code "better". A lot of assist come in
//! pairs: "if let <-> match".
//! * Assists should have as narrow scope as possible. Each new assists greatly
//! improves UX for cases where the user actually invokes it, but it makes UX
//! worse for every case where the user clicks 💡 to invoke some *other*
//! assist. So, a rarely useful assist which is always applicable can be a net
//! negative.
//! * Rarely useful actions are tricky. Sometimes there are features which are
//! clearly useful to some users, but are just noise most of the time. We
//! don't have a good solution here, our current approach is to make this
//! functionality available only if assist is applicable to the whole
//! selection. Example: `sort_items` sorts items alphabetically. Naively, it
//! should be available more or less everywhere, which isn't useful. So
//! instead we only show it if the user *selects* the items they want to sort.
//! * Consider grouping related assists together (see [`Assists::add_group`]).
//! * Make assists robust. If the assist depends on results of type-inference too
//! much, it might only fire in fully-correct code. This makes assist less
//! useful and (worse) less predictable. The user should have a clear
//! intuition when each particular assist is available.
//! * Make small assists, which compose. Example: rather than auto-importing
//! enums in `add_missing_match_arms`, we use fully-qualified names. There's a
//! separate assist to shorten a fully-qualified name.
//! * Distinguish between assists and fixits for diagnostics. Internally, fixits
//! and assists are equivalent. They have the same "show a list + invoke a
//! single element" workflow, and both use [`Assist`] data structure. The main
//! difference is in the UX: while 💡 looks only at the cursor position,
//! diagnostics squigglies and fixits are calculated for the whole file and
//! are presented to the user eagerly. So, diagnostics should be fixable
//! errors, while assists can be just suggestions for an alternative way to do
//! something. If something *could* be a diagnostic, it should be a
//! diagnostic. Conversely, it might be valuable to turn a diagnostic with a
//! lot of false errors into an assist.
//!
//! See also this post:
//! <https://rust-analyzer.github.io/blog/2020/09/28/how-to-make-a-light-bulb.html>
#![warn(rust_2018_idioms, unused_lifetimes, semicolon_in_expressions_from_macros)]
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;
2020-05-05 13:44:13 -05:00
use hir::Semantics;
use ide_db::{base_db::FileRange, 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;
pub use ide_db::assists::{
Assist, AssistId, AssistKind, AssistResolveStrategy, GroupLabel, SingleResolve,
};
/// Return all the assists applicable at the given position.
///
// NOTE: We don't have a `Feature: ` section for assists, they are special-cased
// in the manual.
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 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
2022-07-20 08:02:08 -05:00
pub(crate) type Handler = fn(&mut Assists, &AssistContext<'_>) -> Option<()>;
2019-09-25 06:29:41 -05:00
mod add_explicit_type;
2022-06-06 05:49:29 -05:00
mod add_label_to_loop;
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;
2021-08-08 10:12:08 -05:00
mod convert_bool_then;
2021-02-24 13:25:10 -06:00
mod convert_comment_block;
2021-08-08 10:12:08 -05:00
mod convert_integer_literal;
mod convert_into_to_from;
2021-08-08 10:12:08 -05:00
mod convert_iter_for_each_to_for;
mod convert_let_else_to_match;
mod convert_tuple_struct_to_named_struct;
mod convert_to_guarded_return;
mod convert_two_arm_bool_match_to_matches_macro;
2021-09-16 08:36:44 -05:00
mod convert_while_to_loop;
2021-08-17 12:22:57 -05:00
mod destructure_tuple_binding;
2020-08-02 14:56:54 -05:00
mod expand_glob_import;
mod extract_function;
2021-08-16 03:15:10 -05:00
mod extract_module;
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;
2022-05-20 07:39:22 -05:00
mod add_missing_match_arms;
mod fix_visibility;
2020-03-18 10:48:45 -05:00
mod flip_binexpr;
mod flip_comma;
mod flip_trait_bound;
2022-09-10 08:36:50 -05:00
mod move_format_string_arg;
2021-10-04 10:53:32 -05:00
mod generate_constant;
mod generate_default_from_enum_variant;
mod generate_default_from_new;
2021-04-11 02:31:20 -05:00
mod generate_deref;
2022-05-20 07:39:22 -05:00
mod generate_derive;
mod generate_documentation_template;
mod generate_enum_is_method;
mod generate_enum_projection_method;
mod generate_enum_variant;
2020-07-03 11:15:03 -05:00
mod generate_from_impl_for_enum;
mod generate_function;
2021-02-28 12:11:41 -06:00
mod generate_getter;
2020-07-03 11:15:03 -05:00
mod generate_impl;
2021-08-08 10:12:08 -05:00
mod generate_is_empty_from_len;
2020-07-03 11:15:03 -05:00
mod generate_new;
mod generate_setter;
2021-10-14 06:52:31 -05:00
mod generate_delegate_methods;
2021-09-20 16:53:05 -05:00
mod add_return_type;
mod inline_call;
2019-09-25 06:29:41 -05:00
mod inline_local_variable;
mod inline_type_alias;
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;
2021-09-25 09:47:20 -05:00
mod move_to_mod_rs;
2021-09-27 21:20:29 -05:00
mod move_from_mod_rs;
2021-12-12 12:00:40 -06:00
mod number_representation;
mod promote_local_to_const;
mod pull_assignment_up;
2020-10-14 12:56:20 -05:00
mod qualify_path;
2021-10-21 01:28:19 -05:00
mod qualify_method_call;
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;
2022-05-03 10:41:33 -05:00
mod reorder_impl_items;
2021-10-06 13:11:00 -05:00
mod replace_try_expr_with_match;
2020-11-09 06:07:18 -06:00
mod replace_derive_with_manual_impl;
2019-09-25 06:29:41 -05:00
mod replace_if_let_with_match;
mod replace_or_with_or_else;
2021-09-20 16:53:05 -05:00
mod introduce_named_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;
mod replace_turbofish_with_explicit_type;
2019-09-25 06:29:41 -05:00
mod split_import;
mod unmerge_match_arm;
2021-07-30 21:15:28 -05:00
mod sort_items;
2020-11-30 04:45:32 -06:00
mod toggle_ignore;
2021-01-15 13:14:51 -06:00
mod unmerge_use;
mod unnecessary_async;
mod unwrap_block;
mod unwrap_result_return_type;
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,
2022-06-06 05:49:29 -05:00
add_label_to_loop::add_label_to_loop,
2021-09-20 16:53:05 -05:00
add_missing_match_arms::add_missing_match_arms,
add_lifetime_to_type::add_lifetime_to_type,
2021-09-20 16:53:05 -05:00
add_return_type::add_return_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,
2021-08-10 06:03:12 -05:00
convert_bool_then::convert_bool_then_to_if,
convert_bool_then::convert_if_to_bool_then,
2021-02-24 13:25:10 -06:00
convert_comment_block::convert_comment_block,
2021-08-08 10:12:08 -05:00
convert_integer_literal::convert_integer_literal,
convert_into_to_from::convert_into_to_from,
2021-08-08 10:12:08 -05:00
convert_iter_for_each_to_for::convert_iter_for_each_to_for,
convert_iter_for_each_to_for::convert_for_loop_with_for_each,
convert_let_else_to_match::convert_let_else_to_match,
convert_to_guarded_return::convert_to_guarded_return,
convert_tuple_struct_to_named_struct::convert_tuple_struct_to_named_struct,
convert_two_arm_bool_match_to_matches_macro::convert_two_arm_bool_match_to_matches_macro,
2021-09-16 08:36:44 -05:00
convert_while_to_loop::convert_while_to_loop,
2021-08-17 12:22:57 -05:00
destructure_tuple_binding::destructure_tuple_binding,
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,
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,
2021-10-04 10:53:32 -05:00
generate_constant::generate_constant,
generate_default_from_enum_variant::generate_default_from_enum_variant,
generate_default_from_new::generate_default_from_new,
2022-05-20 07:39:22 -05:00
generate_derive::generate_derive,
generate_documentation_template::generate_documentation_template,
2022-05-18 11:05:21 -05:00
generate_documentation_template::generate_doc_example,
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,
generate_enum_variant::generate_enum_variant,
2020-07-03 11:15:03 -05:00
generate_from_impl_for_enum::generate_from_impl_for_enum,
generate_function::generate_function,
generate_impl::generate_impl,
generate_is_empty_from_len::generate_is_empty_from_len,
2020-07-03 11:15:03 -05:00
generate_new::generate_new,
inline_call::inline_call,
2021-09-25 11:39:43 -05:00
inline_call::inline_into_callers,
2020-01-19 10:39:53 -06:00
inline_local_variable::inline_local_variable,
inline_type_alias::inline_type_alias,
inline_type_alias::inline_type_alias_uses,
2021-09-20 16:53:05 -05:00
introduce_named_generic::introduce_named_generic,
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,
move_format_string_arg::move_format_string_arg,
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,
2021-09-25 09:47:20 -05:00
move_to_mod_rs::move_to_mod_rs,
2021-09-27 21:20:29 -05:00
move_from_mod_rs::move_from_mod_rs,
2021-12-12 12:00:40 -06:00
number_representation::reformat_number_literal,
pull_assignment_up::pull_assignment_up,
promote_local_to_const::promote_local_to_const,
2020-10-14 12:56:20 -05:00
qualify_path::qualify_path,
2021-10-21 01:28:19 -05:00
qualify_method_call::qualify_method_call,
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,
2022-05-03 10:41:33 -05:00
reorder_impl_items::reorder_impl_items,
2021-10-06 13:11:00 -05:00
replace_try_expr_with_match::replace_try_expr_with_match,
2020-11-09 06:07:18 -06:00
replace_derive_with_manual_impl::replace_derive_with_manual_impl,
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,
2020-03-27 06:12:17 -05:00
replace_let_with_if_let::replace_let_with_if_let,
replace_or_with_or_else::replace_or_else_with_or,
replace_or_with_or_else::replace_or_with_or_else,
replace_turbofish_with_explicit_type::replace_turbofish_with_explicit_type,
2020-03-18 10:48:45 -05:00
replace_qualified_name_with_use::replace_qualified_name_with_use,
2021-07-30 21:15:28 -05:00
sort_items::sort_items,
2020-03-18 10:48:45 -05:00
split_import::split_import,
2020-11-30 04:45:32 -06:00
toggle_ignore::toggle_ignore,
unmerge_match_arm::unmerge_match_arm,
2021-01-15 13:14:51 -06:00
unmerge_use::unmerge_use,
unnecessary_async::unnecessary_async,
unwrap_block::unwrap_block,
unwrap_result_return_type::unwrap_result_return_type,
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,
2021-07-30 09:46:06 -05:00
replace_string_with_char::replace_char_with_string,
2020-10-26 15:59:28 -05:00
raw_string::make_raw_string,
2021-02-22 06:18:11 -06:00
//
extract_variable::extract_variable,
extract_function::extract_function,
2021-08-16 03:15:10 -05:00
extract_module::extract_module,
//
generate_getter::generate_getter,
generate_getter::generate_getter_mut,
generate_setter::generate_setter,
generate_delegate_methods::generate_delegate_methods,
generate_deref::generate_deref,
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
}