1 //! `ra_assists` crate provides a bunch of code assists, also known as code
2 //! actions (in LSP) or intentions (in IntelliJ).
4 //! An assist is a micro-refactoring, which is automatically activated in
5 //! certain context. For example, if the cursor is over `,`, a "swap `,`" assist
9 macro_rules! eprintln {
10 ($($tt:tt)*) => { stdx::eprintln!($($tt)*) };
18 pub mod ast_transform;
22 use ra_ide_db::{source_change::SourceChange, RootDatabase};
23 use ra_syntax::TextRange;
25 pub(crate) use crate::assist_context::{AssistContext, Assists};
27 pub use assist_config::AssistConfig;
29 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
41 /// Unique identifier of the assist, should not be shown to the user
43 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
44 pub struct AssistId(pub &'static str, pub AssistKind);
46 #[derive(Clone, Debug)]
47 pub struct GroupLabel(pub String);
49 #[derive(Debug, Clone)]
52 /// Short description of the assist, as shown in the UI.
54 pub group: Option<GroupLabel>,
55 /// Target ranges are used to sort assists: the smaller the target range,
56 /// the more specific assist is, and so it should be sorted first.
57 pub target: TextRange,
60 #[derive(Debug, Clone)]
61 pub struct ResolvedAssist {
63 pub source_change: SourceChange,
67 /// Return all the assists applicable at the given position.
69 /// Assists are returned in the "unresolved" state, that is only labels are
70 /// returned, without actual edits.
71 pub fn unresolved(db: &RootDatabase, config: &AssistConfig, range: FileRange) -> Vec<Assist> {
72 let sema = Semantics::new(db);
73 let ctx = AssistContext::new(sema, config, range);
74 let mut acc = Assists::new_unresolved(&ctx);
75 handlers::all().iter().for_each(|handler| {
76 handler(&mut acc, &ctx);
78 acc.finish_unresolved()
81 /// Return all the assists applicable at the given position.
83 /// Assists are returned in the "resolved" state, that is with edit fully
87 config: &AssistConfig,
89 ) -> Vec<ResolvedAssist> {
90 let sema = Semantics::new(db);
91 let ctx = AssistContext::new(sema, config, range);
92 let mut acc = Assists::new_resolved(&ctx);
93 handlers::all().iter().for_each(|handler| {
94 handler(&mut acc, &ctx);
102 group: Option<GroupLabel>,
105 // FIXME: make fields private, so that this invariant can't be broken
106 assert!(label.starts_with(|c: char| c.is_uppercase()));
107 Assist { id, label, group, target }
112 use crate::{AssistContext, Assists};
114 pub(crate) type Handler = fn(&mut Assists, &AssistContext) -> Option<()>;
118 mod add_explicit_type;
119 mod add_from_impl_for_enum;
122 mod add_missing_impl_members;
127 mod change_return_type_to_result;
128 mod change_visibility;
130 mod extract_struct_from_enum_variant;
131 mod extract_variable;
136 mod flip_trait_bound;
137 mod inline_local_variable;
138 mod introduce_named_lifetime;
141 mod merge_match_arms;
148 mod replace_if_let_with_match;
149 mod replace_let_with_if_let;
150 mod replace_qualified_name_with_use;
151 mod replace_unwrap_with_match;
155 pub(crate) fn all() -> &'static [Handler] {
157 // These are alphabetic for the foolish consistency
158 add_custom_impl::add_custom_impl,
159 add_derive::add_derive,
160 add_explicit_type::add_explicit_type,
161 add_from_impl_for_enum::add_from_impl_for_enum,
162 add_function::add_function,
165 add_turbo_fish::add_turbo_fish,
166 apply_demorgan::apply_demorgan,
167 auto_import::auto_import,
168 change_return_type_to_result::change_return_type_to_result,
169 change_visibility::change_visibility,
170 early_return::convert_to_guarded_return,
171 extract_struct_from_enum_variant::extract_struct_from_enum_variant,
172 extract_variable::extract_variable,
173 fill_match_arms::fill_match_arms,
174 fix_visibility::fix_visibility,
175 flip_binexpr::flip_binexpr,
176 flip_comma::flip_comma,
177 flip_trait_bound::flip_trait_bound,
178 inline_local_variable::inline_local_variable,
179 introduce_named_lifetime::introduce_named_lifetime,
180 invert_if::invert_if,
181 merge_imports::merge_imports,
182 merge_match_arms::merge_match_arms,
183 move_bounds::move_bounds_to_where_clause,
184 move_guard::move_arm_cond_to_match_guard,
185 move_guard::move_guard_to_arm_body,
186 raw_string::add_hash,
187 raw_string::make_raw_string,
188 raw_string::make_usual_string,
189 raw_string::remove_hash,
190 remove_dbg::remove_dbg,
191 remove_mut::remove_mut,
192 reorder_fields::reorder_fields,
193 replace_if_let_with_match::replace_if_let_with_match,
194 replace_let_with_if_let::replace_let_with_if_let,
195 replace_qualified_name_with_use::replace_qualified_name_with_use,
196 replace_unwrap_with_match::replace_unwrap_with_match,
197 split_import::split_import,
198 unwrap_block::unwrap_block,
199 // These are manually sorted for better priorities
200 add_missing_impl_members::add_missing_impl_members,
201 add_missing_impl_members::add_missing_default_members,
202 // Are you sure you want to add new assist here, and not to the
203 // sorted list above?