1 //! `assists` crate provides a bunch of code assists, also known as code actions
2 //! (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
8 //! ## Assists Guidelines
10 //! Assists are the main mechanism to deliver advanced IDE features to the user,
11 //! so we should pay extra attention to the UX.
13 //! The power of assists comes from their context-awareness. The main problem
14 //! with IDE features is that there are a lot of them, and it's hard to teach
15 //! the user what's available. Assists solve this problem nicely: 💡 signifies
16 //! that *something* is possible, and clicking on it reveals a *short* list of
17 //! actions. Contrast it with Emacs `M-x`, which just spits an infinite list of
20 //! Here are some considerations when creating a new assist:
22 //! * It's good to preserve semantics, and it's good to keep the code compiling,
23 //! but it isn't necessary. Example: "flip binary operation" might change
25 //! * Assist shouldn't necessary make the code "better". A lot of assist come in
26 //! pairs: "if let <-> match".
27 //! * Assists should have as narrow scope as possible. Each new assists greatly
28 //! improves UX for cases where the user actually invokes it, but it makes UX
29 //! worse for every case where the user clicks 💡 to invoke some *other*
30 //! assist. So, a rarely useful assist which is always applicable can be a net
32 //! * Rarely useful actions are tricky. Sometimes there are features which are
33 //! clearly useful to some users, but are just noise most of the time. We
34 //! don't have a good solution here, our current approach is to make this
35 //! functionality available only if assist is applicable to the whole
36 //! selection. Example: `sort_items` sorts items alphabetically. Naively, it
37 //! should be available more or less everywhere, which isn't useful. So
38 //! instead we only show it if the user *selects* the items they want to sort.
39 //! * Consider grouping related assists together (see [`Assists::add_group`]).
40 //! * Make assists robust. If the assist depends on results of type-inference to
41 //! much, it might only fire in fully-correct code. This makes assist less
42 //! useful and (worse) less predictable. The user should have a clear
43 //! intuition when each particular assist is available.
44 //! * Make small assists, which compose. Example: rather than auto-importing
45 //! enums in `fill_match_arms`, we use fully-qualified names. There's a
46 //! separate assist to shorten a fully-qualified name.
47 //! * Distinguish between assists and fixits for diagnostics. Internally, fixits
48 //! and assists are equivalent. They have the same "show a list + invoke a
49 //! single element" workflow, and both use [`Assist`] data structure. The main
50 //! difference is in the UX: while 💡 looks only at the cursor position,
51 //! diagnostics squigglies and fixits are calculated for the whole file and
52 //! are presented to the user eagerly. So, diagnostics should be fixable
53 //! errors, while assists can be just suggestions for an alternative way to do
54 //! something. If something *could* be a diagnostic, it should be a
55 //! diagnostic. Conversely, it might be valuable to turn a diagnostic with a
56 //! lot of false errors into an assist.
59 //! See also this post:
60 //! <https://rust-analyzer.github.io/blog/2020/09/28/how-to-make-a-light-bulb.html>
62 macro_rules! eprintln {
63 ($($tt:tt)*) => { stdx::eprintln!($($tt)*) };
73 use ide_db::{base_db::FileRange, RootDatabase};
74 use syntax::TextRange;
76 pub(crate) use crate::assist_context::{AssistContext, Assists};
78 pub use assist_config::AssistConfig;
79 pub use ide_db::assists::{
80 Assist, AssistId, AssistKind, AssistResolveStrategy, GroupLabel, SingleResolve,
83 /// Return all the assists applicable at the given position.
85 // NOTE: We don't have a `Feature: ` section for assists, they are special-cased
89 config: &AssistConfig,
90 resolve: AssistResolveStrategy,
93 let sema = Semantics::new(db);
94 let ctx = AssistContext::new(sema, config, range);
95 let mut acc = Assists::new(&ctx, resolve);
96 handlers::all().iter().for_each(|handler| {
97 handler(&mut acc, &ctx);
103 use crate::{AssistContext, Assists};
105 pub(crate) type Handler = fn(&mut Assists, &AssistContext) -> Option<()>;
107 mod add_explicit_type;
108 mod add_lifetime_to_type;
109 mod add_missing_impl_members;
113 mod change_visibility;
114 mod convert_bool_then;
115 mod convert_comment_block;
116 mod convert_integer_literal;
117 mod convert_into_to_from;
118 mod convert_iter_for_each_to_for;
119 mod convert_tuple_struct_to_named_struct;
120 mod convert_to_guarded_return;
121 mod convert_while_to_loop;
122 mod destructure_tuple_binding;
123 mod expand_glob_import;
124 mod extract_function;
125 mod extract_struct_from_enum_variant;
126 mod extract_type_alias;
127 mod extract_variable;
128 mod add_missing_match_arms;
132 mod flip_trait_bound;
133 mod generate_default_from_enum_variant;
134 mod generate_default_from_new;
137 mod generate_enum_is_method;
138 mod generate_enum_projection_method;
139 mod generate_from_impl_for_enum;
140 mod generate_function;
143 mod generate_is_empty_from_len;
148 mod inline_local_variable;
149 mod introduce_named_lifetime;
152 mod merge_match_arms;
155 mod move_module_to_file;
156 mod pull_assignment_up;
161 mod remove_unused_param;
164 mod replace_derive_with_manual_impl;
165 mod replace_if_let_with_match;
166 mod introduce_named_generic;
167 mod replace_let_with_if_let;
168 mod replace_qualified_name_with_use;
169 mod replace_string_with_char;
175 mod wrap_return_type_in_result;
177 pub(crate) fn all() -> &'static [Handler] {
179 // These are alphabetic for the foolish consistency
180 add_explicit_type::add_explicit_type,
181 add_missing_match_arms::add_missing_match_arms,
182 add_lifetime_to_type::add_lifetime_to_type,
183 add_return_type::add_return_type,
184 add_turbo_fish::add_turbo_fish,
185 apply_demorgan::apply_demorgan,
186 auto_import::auto_import,
187 change_visibility::change_visibility,
188 convert_bool_then::convert_bool_then_to_if,
189 convert_bool_then::convert_if_to_bool_then,
190 convert_comment_block::convert_comment_block,
191 convert_integer_literal::convert_integer_literal,
192 convert_into_to_from::convert_into_to_from,
193 convert_iter_for_each_to_for::convert_iter_for_each_to_for,
194 convert_iter_for_each_to_for::convert_for_loop_with_for_each,
195 convert_to_guarded_return::convert_to_guarded_return,
196 convert_tuple_struct_to_named_struct::convert_tuple_struct_to_named_struct,
197 convert_while_to_loop::convert_while_to_loop,
198 destructure_tuple_binding::destructure_tuple_binding,
199 expand_glob_import::expand_glob_import,
200 extract_struct_from_enum_variant::extract_struct_from_enum_variant,
201 extract_type_alias::extract_type_alias,
202 fix_visibility::fix_visibility,
203 flip_binexpr::flip_binexpr,
204 flip_comma::flip_comma,
205 flip_trait_bound::flip_trait_bound,
206 generate_default_from_enum_variant::generate_default_from_enum_variant,
207 generate_default_from_new::generate_default_from_new,
208 generate_deref::generate_deref,
209 generate_derive::generate_derive,
210 generate_enum_is_method::generate_enum_is_method,
211 generate_enum_projection_method::generate_enum_as_method,
212 generate_enum_projection_method::generate_enum_try_into_method,
213 generate_from_impl_for_enum::generate_from_impl_for_enum,
214 generate_function::generate_function,
215 generate_impl::generate_impl,
216 generate_is_empty_from_len::generate_is_empty_from_len,
217 generate_new::generate_new,
218 inline_call::inline_call,
219 inline_local_variable::inline_local_variable,
220 introduce_named_generic::introduce_named_generic,
221 introduce_named_lifetime::introduce_named_lifetime,
222 invert_if::invert_if,
223 merge_imports::merge_imports,
224 merge_match_arms::merge_match_arms,
225 move_bounds::move_bounds_to_where_clause,
226 move_guard::move_arm_cond_to_match_guard,
227 move_guard::move_guard_to_arm_body,
228 move_module_to_file::move_module_to_file,
229 pull_assignment_up::pull_assignment_up,
230 qualify_path::qualify_path,
231 raw_string::add_hash,
232 raw_string::make_usual_string,
233 raw_string::remove_hash,
234 remove_dbg::remove_dbg,
235 remove_mut::remove_mut,
236 remove_unused_param::remove_unused_param,
237 reorder_fields::reorder_fields,
238 reorder_impl::reorder_impl,
239 replace_derive_with_manual_impl::replace_derive_with_manual_impl,
240 replace_if_let_with_match::replace_if_let_with_match,
241 replace_if_let_with_match::replace_match_with_if_let,
242 replace_let_with_if_let::replace_let_with_if_let,
243 replace_qualified_name_with_use::replace_qualified_name_with_use,
244 sort_items::sort_items,
245 split_import::split_import,
246 toggle_ignore::toggle_ignore,
247 unmerge_use::unmerge_use,
248 unwrap_block::unwrap_block,
249 wrap_return_type_in_result::wrap_return_type_in_result,
250 // These are manually sorted for better priorities. By default,
251 // priority is determined by the size of the target range (smaller
252 // target wins). If the ranges are equal, position in this list is
253 // used as a tie-breaker.
254 add_missing_impl_members::add_missing_impl_members,
255 add_missing_impl_members::add_missing_default_members,
257 replace_string_with_char::replace_string_with_char,
258 replace_string_with_char::replace_char_with_string,
259 raw_string::make_raw_string,
261 extract_variable::extract_variable,
262 extract_function::extract_function,
264 generate_getter::generate_getter,
265 generate_getter::generate_getter_mut,
266 generate_setter::generate_setter,
267 // Are you sure you want to add new assist here, and not to the
268 // sorted list above?