1 //! `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)*) };
20 use ide_db::{base_db::FileRange, RootDatabase};
21 use syntax::TextRange;
23 pub(crate) use crate::assist_context::{AssistContext, Assists};
25 pub use assist_config::AssistConfig;
26 pub use ide_db::assists::{
27 Assist, AssistId, AssistKind, AssistResolveStrategy, GroupLabel, SingleResolve,
30 /// Return all the assists applicable at the given position.
33 config: &AssistConfig,
34 resolve: AssistResolveStrategy,
37 let sema = Semantics::new(db);
38 let ctx = AssistContext::new(sema, config, range);
39 let mut acc = Assists::new(&ctx, resolve);
40 handlers::all().iter().for_each(|handler| {
41 handler(&mut acc, &ctx);
47 use crate::{AssistContext, Assists};
49 pub(crate) type Handler = fn(&mut Assists, &AssistContext) -> Option<()>;
51 mod add_explicit_type;
52 mod add_lifetime_to_type;
53 mod add_missing_impl_members;
57 mod change_visibility;
58 mod convert_bool_then;
59 mod convert_comment_block;
60 mod convert_integer_literal;
61 mod convert_into_to_from;
62 mod convert_iter_for_each_to_for;
63 mod convert_tuple_struct_to_named_struct;
65 mod expand_glob_import;
67 mod extract_struct_from_enum_variant;
68 mod extract_type_alias;
75 mod generate_default_from_enum_variant;
76 mod generate_default_from_new;
79 mod generate_enum_is_method;
80 mod generate_enum_projection_method;
81 mod generate_from_impl_for_enum;
82 mod generate_function;
85 mod generate_is_empty_from_len;
88 mod infer_function_return_type;
90 mod inline_local_variable;
91 mod introduce_named_lifetime;
97 mod move_module_to_file;
98 mod pull_assignment_up;
103 mod remove_unused_param;
106 mod replace_derive_with_manual_impl;
107 mod replace_for_loop_with_for_each;
108 mod replace_if_let_with_match;
109 mod replace_impl_trait_with_generic;
110 mod replace_let_with_if_let;
111 mod replace_qualified_name_with_use;
112 mod replace_string_with_char;
118 mod wrap_return_type_in_result;
120 pub(crate) fn all() -> &'static [Handler] {
122 // These are alphabetic for the foolish consistency
123 add_explicit_type::add_explicit_type,
124 add_lifetime_to_type::add_lifetime_to_type,
125 add_turbo_fish::add_turbo_fish,
126 apply_demorgan::apply_demorgan,
127 auto_import::auto_import,
128 change_visibility::change_visibility,
129 convert_bool_then::convert_if_to_bool_then,
130 convert_bool_then::convert_bool_then_to_if,
131 convert_comment_block::convert_comment_block,
132 convert_integer_literal::convert_integer_literal,
133 convert_into_to_from::convert_into_to_from,
134 convert_iter_for_each_to_for::convert_iter_for_each_to_for,
135 convert_tuple_struct_to_named_struct::convert_tuple_struct_to_named_struct,
136 early_return::convert_to_guarded_return,
137 expand_glob_import::expand_glob_import,
138 extract_struct_from_enum_variant::extract_struct_from_enum_variant,
139 extract_type_alias::extract_type_alias,
140 fill_match_arms::fill_match_arms,
141 fix_visibility::fix_visibility,
142 flip_binexpr::flip_binexpr,
143 flip_comma::flip_comma,
144 flip_trait_bound::flip_trait_bound,
145 generate_default_from_enum_variant::generate_default_from_enum_variant,
146 generate_default_from_new::generate_default_from_new,
147 generate_is_empty_from_len::generate_is_empty_from_len,
148 generate_deref::generate_deref,
149 generate_derive::generate_derive,
150 generate_enum_is_method::generate_enum_is_method,
151 generate_enum_projection_method::generate_enum_as_method,
152 generate_enum_projection_method::generate_enum_try_into_method,
153 generate_from_impl_for_enum::generate_from_impl_for_enum,
154 generate_function::generate_function,
155 generate_getter::generate_getter,
156 generate_getter::generate_getter_mut,
157 generate_impl::generate_impl,
158 generate_new::generate_new,
159 generate_setter::generate_setter,
160 infer_function_return_type::infer_function_return_type,
161 inline_call::inline_call,
162 inline_local_variable::inline_local_variable,
163 introduce_named_lifetime::introduce_named_lifetime,
164 invert_if::invert_if,
165 merge_imports::merge_imports,
166 merge_match_arms::merge_match_arms,
167 move_bounds::move_bounds_to_where_clause,
168 move_guard::move_arm_cond_to_match_guard,
169 move_guard::move_guard_to_arm_body,
170 move_module_to_file::move_module_to_file,
171 pull_assignment_up::pull_assignment_up,
172 qualify_path::qualify_path,
173 raw_string::add_hash,
174 raw_string::make_usual_string,
175 raw_string::remove_hash,
176 remove_dbg::remove_dbg,
177 remove_mut::remove_mut,
178 remove_unused_param::remove_unused_param,
179 reorder_fields::reorder_fields,
180 reorder_impl::reorder_impl,
181 replace_derive_with_manual_impl::replace_derive_with_manual_impl,
182 replace_for_loop_with_for_each::replace_for_loop_with_for_each,
183 replace_if_let_with_match::replace_if_let_with_match,
184 replace_if_let_with_match::replace_match_with_if_let,
185 replace_impl_trait_with_generic::replace_impl_trait_with_generic,
186 replace_let_with_if_let::replace_let_with_if_let,
187 replace_qualified_name_with_use::replace_qualified_name_with_use,
188 sort_items::sort_items,
189 split_import::split_import,
190 toggle_ignore::toggle_ignore,
191 unmerge_use::unmerge_use,
192 unwrap_block::unwrap_block,
193 wrap_return_type_in_result::wrap_return_type_in_result,
194 // These are manually sorted for better priorities. By default,
195 // priority is determined by the size of the target range (smaller
196 // target wins). If the ranges are equal, position in this list is
197 // used as a tie-breaker.
198 add_missing_impl_members::add_missing_impl_members,
199 add_missing_impl_members::add_missing_default_members,
201 replace_string_with_char::replace_string_with_char,
202 replace_string_with_char::replace_char_with_string,
203 raw_string::make_raw_string,
205 extract_variable::extract_variable,
206 extract_function::extract_function,
207 // Are you sure you want to add new assist here, and not to the
208 // sorted list above?