2019-09-08 09:10:53 +00:00
|
|
|
//! `ra_assists` crate provides a bunch of code assists, also known as code
|
2019-02-03 18:26:35 +00: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 14:58:16 +00:00
|
|
|
#[allow(unused)]
|
|
|
|
macro_rules! eprintln {
|
|
|
|
($($tt:tt)*) => { stdx::eprintln!($($tt)*) };
|
|
|
|
}
|
|
|
|
|
2020-05-17 10:09:53 +00:00
|
|
|
mod assist_config;
|
2020-05-06 16:45:35 +00:00
|
|
|
mod assist_context;
|
2019-10-25 11:16:46 +00:00
|
|
|
#[cfg(test)]
|
2020-05-06 08:16:55 +00:00
|
|
|
mod tests;
|
2020-02-09 18:24:34 +00:00
|
|
|
pub mod utils;
|
2020-01-10 17:26:18 +00:00
|
|
|
pub mod ast_transform;
|
2019-02-11 17:07:21 +00:00
|
|
|
|
2020-05-05 18:44:13 +00:00
|
|
|
use hir::Semantics;
|
2020-05-06 14:39:11 +00:00
|
|
|
use ra_db::FileRange;
|
2020-05-06 13:26:40 +00:00
|
|
|
use ra_ide_db::{source_change::SourceChange, RootDatabase};
|
|
|
|
use ra_syntax::TextRange;
|
2019-02-03 18:26:35 +00:00
|
|
|
|
2020-05-06 16:45:35 +00:00
|
|
|
pub(crate) use crate::assist_context::{AssistContext, Assists};
|
2019-02-03 18:26:35 +00:00
|
|
|
|
2020-05-17 10:09:53 +00:00
|
|
|
pub use assist_config::AssistConfig;
|
|
|
|
|
2019-02-24 10:53:35 +00:00
|
|
|
/// Unique identifier of the assist, should not be shown to the user
|
|
|
|
/// directly.
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
|
|
pub struct AssistId(pub &'static str);
|
|
|
|
|
2020-05-07 15:29:23 +00:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct GroupLabel(pub String);
|
|
|
|
|
2019-02-11 17:07:21 +00:00
|
|
|
#[derive(Debug, Clone)]
|
2020-05-07 15:09:59 +00:00
|
|
|
pub struct Assist {
|
2020-05-05 18:30:33 +00:00
|
|
|
pub id: AssistId,
|
2019-02-03 18:26:35 +00:00
|
|
|
/// Short description of the assist, as shown in the UI.
|
|
|
|
pub label: String,
|
2020-05-05 18:42:52 +00:00
|
|
|
pub group: Option<GroupLabel>,
|
2020-05-06 10:51:28 +00:00
|
|
|
/// 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,
|
2019-02-03 18:26:35 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 15:29:23 +00:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct ResolvedAssist {
|
|
|
|
pub assist: Assist,
|
|
|
|
pub source_change: SourceChange,
|
|
|
|
}
|
2020-02-09 14:32:53 +00:00
|
|
|
|
2020-05-07 15:09:59 +00:00
|
|
|
impl Assist {
|
2020-05-07 15:29:23 +00:00
|
|
|
/// Return all the assists applicable at the given position.
|
|
|
|
///
|
|
|
|
/// Assists are returned in the "unresolved" state, that is only labels are
|
|
|
|
/// returned, without actual edits.
|
2020-05-17 10:09:53 +00:00
|
|
|
pub fn unresolved(db: &RootDatabase, config: &AssistConfig, range: FileRange) -> Vec<Assist> {
|
2020-05-07 15:29:23 +00:00
|
|
|
let sema = Semantics::new(db);
|
2020-05-17 10:09:53 +00:00
|
|
|
let ctx = AssistContext::new(sema, config, range);
|
2020-05-07 15:29:23 +00:00
|
|
|
let mut acc = Assists::new_unresolved(&ctx);
|
|
|
|
handlers::all().iter().for_each(|handler| {
|
|
|
|
handler(&mut acc, &ctx);
|
|
|
|
});
|
|
|
|
acc.finish_unresolved()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return all the assists applicable at the given position.
|
|
|
|
///
|
|
|
|
/// Assists are returned in the "resolved" state, that is with edit fully
|
|
|
|
/// computed.
|
2020-05-17 10:09:53 +00:00
|
|
|
pub fn resolved(
|
|
|
|
db: &RootDatabase,
|
|
|
|
config: &AssistConfig,
|
|
|
|
range: FileRange,
|
|
|
|
) -> Vec<ResolvedAssist> {
|
2020-05-07 15:29:23 +00:00
|
|
|
let sema = Semantics::new(db);
|
2020-05-17 10:09:53 +00:00
|
|
|
let ctx = AssistContext::new(sema, config, range);
|
2020-05-07 15:29:23 +00:00
|
|
|
let mut acc = Assists::new_resolved(&ctx);
|
|
|
|
handlers::all().iter().for_each(|handler| {
|
|
|
|
handler(&mut acc, &ctx);
|
|
|
|
});
|
|
|
|
acc.finish_resolved()
|
|
|
|
}
|
|
|
|
|
2020-05-06 10:51:28 +00:00
|
|
|
pub(crate) fn new(
|
|
|
|
id: AssistId,
|
|
|
|
label: String,
|
|
|
|
group: Option<GroupLabel>,
|
|
|
|
target: TextRange,
|
2020-05-07 15:09:59 +00:00
|
|
|
) -> Assist {
|
2020-02-07 14:04:50 +00:00
|
|
|
// FIXME: make fields private, so that this invariant can't be broken
|
2020-02-19 04:29:34 +00:00
|
|
|
assert!(label.starts_with(|c: char| c.is_uppercase()));
|
2020-05-07 15:09:59 +00:00
|
|
|
Assist { id, label, group, target }
|
2020-02-07 14:04:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-07 14:53:31 +00:00
|
|
|
mod handlers {
|
2020-05-06 16:45:35 +00:00
|
|
|
use crate::{AssistContext, Assists};
|
2020-05-05 20:14:01 +00:00
|
|
|
|
2020-05-06 16:45:35 +00:00
|
|
|
pub(crate) type Handler = fn(&mut Assists, &AssistContext) -> Option<()>;
|
2019-09-25 11:29:41 +00:00
|
|
|
|
2020-03-18 15:48:45 +00:00
|
|
|
mod add_custom_impl;
|
2019-09-25 11:29:41 +00:00
|
|
|
mod add_derive;
|
|
|
|
mod add_explicit_type;
|
2020-05-06 16:45:35 +00:00
|
|
|
mod add_from_impl_for_enum;
|
2020-03-26 19:59:35 +00:00
|
|
|
mod add_function;
|
2019-09-25 11:29:41 +00:00
|
|
|
mod add_impl;
|
2020-03-18 15:48:45 +00:00
|
|
|
mod add_missing_impl_members;
|
2019-11-09 15:56:36 +00:00
|
|
|
mod add_new;
|
2020-05-19 22:07:00 +00:00
|
|
|
mod add_turbo_fish;
|
2019-10-03 20:19:46 +00:00
|
|
|
mod apply_demorgan;
|
2019-12-24 00:19:09 +00:00
|
|
|
mod auto_import;
|
2020-05-06 16:45:35 +00:00
|
|
|
mod change_return_type_to_result;
|
2019-09-25 11:29:41 +00:00
|
|
|
mod change_visibility;
|
2020-03-18 15:48:45 +00:00
|
|
|
mod early_return;
|
2019-09-25 11:29:41 +00:00
|
|
|
mod fill_match_arms;
|
2020-03-18 15:48:45 +00:00
|
|
|
mod flip_binexpr;
|
|
|
|
mod flip_comma;
|
|
|
|
mod flip_trait_bound;
|
2019-09-25 11:29:41 +00:00
|
|
|
mod inline_local_variable;
|
2020-03-18 15:48:45 +00:00
|
|
|
mod introduce_variable;
|
|
|
|
mod invert_if;
|
2020-03-18 15:41:24 +00:00
|
|
|
mod merge_imports;
|
2020-03-18 15:48:45 +00:00
|
|
|
mod merge_match_arms;
|
|
|
|
mod move_bounds;
|
|
|
|
mod move_guard;
|
2019-09-25 11:29:41 +00:00
|
|
|
mod raw_string;
|
2020-03-18 15:48:45 +00:00
|
|
|
mod remove_dbg;
|
2020-02-19 11:44:20 +00:00
|
|
|
mod remove_mut;
|
2020-05-06 16:45:35 +00:00
|
|
|
mod reorder_fields;
|
2019-09-25 11:29:41 +00:00
|
|
|
mod replace_if_let_with_match;
|
2020-03-27 11:12:17 +00:00
|
|
|
mod replace_let_with_if_let;
|
2020-03-18 15:48:45 +00:00
|
|
|
mod replace_qualified_name_with_use;
|
2020-03-26 09:16:10 +00:00
|
|
|
mod replace_unwrap_with_match;
|
2019-09-25 11:29:41 +00:00
|
|
|
mod split_import;
|
2020-04-29 11:52:55 +00:00
|
|
|
mod unwrap_block;
|
2019-09-25 11:29:41 +00:00
|
|
|
|
2020-05-05 20:14:01 +00:00
|
|
|
pub(crate) fn all() -> &'static [Handler] {
|
2019-09-25 11:29:41 +00:00
|
|
|
&[
|
2020-04-09 08:00:27 +00:00
|
|
|
// These are alphabetic for the foolish consistency
|
2020-03-18 15:48:45 +00:00
|
|
|
add_custom_impl::add_custom_impl,
|
2019-09-25 11:29:41 +00:00
|
|
|
add_derive::add_derive,
|
|
|
|
add_explicit_type::add_explicit_type,
|
2020-05-05 20:14:01 +00:00
|
|
|
add_from_impl_for_enum::add_from_impl_for_enum,
|
2020-03-26 19:59:35 +00:00
|
|
|
add_function::add_function,
|
2019-09-25 11:29:41 +00:00
|
|
|
add_impl::add_impl,
|
2019-11-09 15:56:36 +00:00
|
|
|
add_new::add_new,
|
2020-05-19 22:07:00 +00:00
|
|
|
add_turbo_fish::add_turbo_fish,
|
2019-10-03 20:19:46 +00:00
|
|
|
apply_demorgan::apply_demorgan,
|
2020-03-18 15:48:45 +00:00
|
|
|
auto_import::auto_import,
|
2020-05-05 17:02:45 +00:00
|
|
|
change_return_type_to_result::change_return_type_to_result,
|
2019-09-25 11:29:41 +00:00
|
|
|
change_visibility::change_visibility,
|
2020-03-18 15:48:45 +00:00
|
|
|
early_return::convert_to_guarded_return,
|
2019-09-25 11:29:41 +00:00
|
|
|
fill_match_arms::fill_match_arms,
|
|
|
|
flip_binexpr::flip_binexpr,
|
2020-03-18 15:48:45 +00:00
|
|
|
flip_comma::flip_comma,
|
2019-10-26 20:24:48 +00:00
|
|
|
flip_trait_bound::flip_trait_bound,
|
2020-01-19 16:39:53 +00:00
|
|
|
inline_local_variable::inline_local_variable,
|
2020-03-18 15:48:45 +00:00
|
|
|
introduce_variable::introduce_variable,
|
|
|
|
invert_if::invert_if,
|
2020-03-18 15:41:24 +00:00
|
|
|
merge_imports::merge_imports,
|
2020-03-18 15:48:45 +00:00
|
|
|
merge_match_arms::merge_match_arms,
|
2019-09-25 11:29:41 +00:00
|
|
|
move_bounds::move_bounds_to_where_clause,
|
2020-03-18 15:48:45 +00:00
|
|
|
move_guard::move_arm_cond_to_match_guard,
|
|
|
|
move_guard::move_guard_to_arm_body,
|
2019-09-25 11:29:41 +00:00
|
|
|
raw_string::add_hash,
|
|
|
|
raw_string::make_raw_string,
|
|
|
|
raw_string::make_usual_string,
|
|
|
|
raw_string::remove_hash,
|
2020-03-18 15:48:45 +00:00
|
|
|
remove_dbg::remove_dbg,
|
2020-02-19 11:44:20 +00:00
|
|
|
remove_mut::remove_mut,
|
2020-05-05 20:14:01 +00:00
|
|
|
reorder_fields::reorder_fields,
|
2020-03-18 15:48:45 +00:00
|
|
|
replace_if_let_with_match::replace_if_let_with_match,
|
2020-03-27 11:12:17 +00:00
|
|
|
replace_let_with_if_let::replace_let_with_if_let,
|
2020-03-18 15:48:45 +00:00
|
|
|
replace_qualified_name_with_use::replace_qualified_name_with_use,
|
2020-03-26 09:16:10 +00:00
|
|
|
replace_unwrap_with_match::replace_unwrap_with_match,
|
2020-03-18 15:48:45 +00:00
|
|
|
split_import::split_import,
|
2020-04-29 11:52:55 +00:00
|
|
|
unwrap_block::unwrap_block,
|
2020-04-09 08:00:27 +00:00
|
|
|
// These are manually sorted for better priorities
|
|
|
|
add_missing_impl_members::add_missing_impl_members,
|
|
|
|
add_missing_impl_members::add_missing_default_members,
|
2020-05-05 20:14:01 +00:00
|
|
|
// Are you sure you want to add new assist here, and not to the
|
|
|
|
// sorted list above?
|
2019-09-25 11:29:41 +00:00
|
|
|
]
|
|
|
|
}
|
2019-02-03 18:26:35 +00:00
|
|
|
}
|