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)*) };
18 pub mod ast_transform;
21 use ide_db::base_db::FileRange;
22 use ide_db::{label::Label, source_change::SourceChange, RootDatabase};
23 use 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 pub fn contains(self, other: AssistKind) -> bool {
47 AssistKind::None | AssistKind::Generate => return true,
48 AssistKind::Refactor => match other {
49 AssistKind::RefactorExtract
50 | AssistKind::RefactorInline
51 | AssistKind::RefactorRewrite => return true,
59 /// Unique identifier of the assist, should not be shown to the user
61 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
62 pub struct AssistId(pub &'static str, pub AssistKind);
64 #[derive(Clone, Debug)]
65 pub struct GroupLabel(pub String);
67 #[derive(Debug, Clone)]
70 /// Short description of the assist, as shown in the UI.
72 pub group: Option<GroupLabel>,
73 /// Target ranges are used to sort assists: the smaller the target range,
74 /// the more specific assist is, and so it should be sorted first.
75 pub target: TextRange,
76 /// Computing source change sometimes is much more costly then computing the
77 /// other fields. Additionally, the actual change is not required to show
78 /// the lightbulb UI, it only is needed when the user tries to apply an
79 /// assist. So, we compute it lazily: the API allow requesting assists with
80 /// or without source change. We could (and in fact, used to) distinguish
81 /// between resolved and unresolved assists at the type level, but this is
82 /// cumbersome, especially if you want to embed an assist into another data
83 /// structure, such as a diagnostic.
84 pub source_change: Option<SourceChange>,
88 /// Return all the assists applicable at the given position.
91 config: &AssistConfig,
95 let sema = Semantics::new(db);
96 let ctx = AssistContext::new(sema, config, range);
97 let mut acc = Assists::new(&ctx, resolve);
98 handlers::all().iter().for_each(|handler| {
99 handler(&mut acc, &ctx);
106 use crate::{AssistContext, Assists};
108 pub(crate) type Handler = fn(&mut Assists, &AssistContext) -> Option<()>;
110 mod add_explicit_type;
111 mod add_missing_impl_members;
115 mod change_visibility;
116 mod convert_integer_literal;
118 mod expand_glob_import;
119 mod extract_struct_from_enum_variant;
120 mod extract_variable;
125 mod flip_trait_bound;
126 mod generate_default_from_enum_variant;
128 mod generate_from_impl_for_enum;
129 mod generate_function;
132 mod infer_function_return_type;
134 mod inline_local_variable;
135 mod introduce_named_lifetime;
138 mod merge_match_arms;
141 mod move_module_to_file;
142 mod pull_assignment_up;
147 mod remove_unused_param;
150 mod replace_derive_with_manual_impl;
151 mod replace_if_let_with_match;
152 mod replace_impl_trait_with_generic;
153 mod replace_let_with_if_let;
154 mod replace_qualified_name_with_use;
155 mod replace_string_with_char;
156 mod replace_unwrap_with_match;
161 mod wrap_return_type_in_result;
163 pub(crate) fn all() -> &'static [Handler] {
165 // These are alphabetic for the foolish consistency
166 add_explicit_type::add_explicit_type,
167 add_turbo_fish::add_turbo_fish,
168 apply_demorgan::apply_demorgan,
169 auto_import::auto_import,
170 change_visibility::change_visibility,
171 convert_integer_literal::convert_integer_literal,
172 early_return::convert_to_guarded_return,
173 expand_glob_import::expand_glob_import,
174 move_module_to_file::move_module_to_file,
175 extract_struct_from_enum_variant::extract_struct_from_enum_variant,
176 extract_variable::extract_variable,
177 fill_match_arms::fill_match_arms,
178 fix_visibility::fix_visibility,
179 flip_binexpr::flip_binexpr,
180 flip_comma::flip_comma,
181 flip_trait_bound::flip_trait_bound,
182 generate_default_from_enum_variant::generate_default_from_enum_variant,
183 generate_derive::generate_derive,
184 generate_from_impl_for_enum::generate_from_impl_for_enum,
185 generate_function::generate_function,
186 generate_impl::generate_impl,
187 generate_new::generate_new,
188 infer_function_return_type::infer_function_return_type,
189 inline_function::inline_function,
190 inline_local_variable::inline_local_variable,
191 introduce_named_lifetime::introduce_named_lifetime,
192 invert_if::invert_if,
193 merge_imports::merge_imports,
194 merge_match_arms::merge_match_arms,
195 move_bounds::move_bounds_to_where_clause,
196 move_guard::move_arm_cond_to_match_guard,
197 move_guard::move_guard_to_arm_body,
198 pull_assignment_up::pull_assignment_up,
199 qualify_path::qualify_path,
200 raw_string::add_hash,
201 raw_string::make_usual_string,
202 raw_string::remove_hash,
203 remove_dbg::remove_dbg,
204 remove_mut::remove_mut,
205 remove_unused_param::remove_unused_param,
206 reorder_fields::reorder_fields,
207 reorder_impl::reorder_impl,
208 replace_derive_with_manual_impl::replace_derive_with_manual_impl,
209 replace_if_let_with_match::replace_if_let_with_match,
210 replace_if_let_with_match::replace_match_with_if_let,
211 replace_impl_trait_with_generic::replace_impl_trait_with_generic,
212 replace_let_with_if_let::replace_let_with_if_let,
213 replace_qualified_name_with_use::replace_qualified_name_with_use,
214 replace_unwrap_with_match::replace_unwrap_with_match,
215 split_import::split_import,
216 toggle_ignore::toggle_ignore,
217 unmerge_use::unmerge_use,
218 unwrap_block::unwrap_block,
219 wrap_return_type_in_result::wrap_return_type_in_result,
220 // These are manually sorted for better priorities
221 add_missing_impl_members::add_missing_impl_members,
222 add_missing_impl_members::add_missing_default_members,
224 replace_string_with_char::replace_string_with_char,
225 raw_string::make_raw_string,
226 // Are you sure you want to add new assist here, and not to the
227 // sorted list above?