]> git.lizzy.rs Git - rust.git/blob - crates/ide_assists/src/lib.rs
Merge #9840
[rust.git] / crates / ide_assists / src / lib.rs
1 //! `assists` crate provides a bunch of code assists, also known as code
2 //! actions (in LSP) or intentions (in IntelliJ).
3 //!
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
6 //! becomes available.
7
8 #[allow(unused)]
9 macro_rules! eprintln {
10     ($($tt:tt)*) => { stdx::eprintln!($($tt)*) };
11 }
12
13 mod assist_config;
14 mod assist_context;
15 #[cfg(test)]
16 mod tests;
17 pub mod utils;
18
19 use hir::Semantics;
20 use ide_db::{base_db::FileRange, RootDatabase};
21 use syntax::TextRange;
22
23 pub(crate) use crate::assist_context::{AssistContext, Assists};
24
25 pub use assist_config::AssistConfig;
26 pub use ide_db::assists::{
27     Assist, AssistId, AssistKind, AssistResolveStrategy, GroupLabel, SingleResolve,
28 };
29
30 /// Return all the assists applicable at the given position.
31 pub fn assists(
32     db: &RootDatabase,
33     config: &AssistConfig,
34     resolve: AssistResolveStrategy,
35     range: FileRange,
36 ) -> Vec<Assist> {
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);
42     });
43     acc.finish()
44 }
45
46 mod handlers {
47     use crate::{AssistContext, Assists};
48
49     pub(crate) type Handler = fn(&mut Assists, &AssistContext) -> Option<()>;
50
51     mod add_explicit_type;
52     mod add_lifetime_to_type;
53     mod add_missing_impl_members;
54     mod add_turbo_fish;
55     mod apply_demorgan;
56     mod auto_import;
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;
64     mod early_return;
65     mod expand_glob_import;
66     mod extract_function;
67     mod extract_struct_from_enum_variant;
68     mod extract_type_alias;
69     mod extract_variable;
70     mod fill_match_arms;
71     mod fix_visibility;
72     mod flip_binexpr;
73     mod flip_comma;
74     mod flip_trait_bound;
75     mod generate_default_from_enum_variant;
76     mod generate_default_from_new;
77     mod generate_deref;
78     mod generate_derive;
79     mod generate_enum_is_method;
80     mod generate_enum_projection_method;
81     mod generate_from_impl_for_enum;
82     mod generate_function;
83     mod generate_getter;
84     mod generate_impl;
85     mod generate_is_empty_from_len;
86     mod generate_new;
87     mod generate_setter;
88     mod infer_function_return_type;
89     mod inline_call;
90     mod inline_local_variable;
91     mod introduce_named_lifetime;
92     mod invert_if;
93     mod merge_imports;
94     mod merge_match_arms;
95     mod move_bounds;
96     mod move_guard;
97     mod move_module_to_file;
98     mod pull_assignment_up;
99     mod qualify_path;
100     mod raw_string;
101     mod remove_dbg;
102     mod remove_mut;
103     mod remove_unused_param;
104     mod reorder_fields;
105     mod reorder_impl;
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;
113     mod split_import;
114     mod sort_items;
115     mod toggle_ignore;
116     mod unmerge_use;
117     mod unwrap_block;
118     mod wrap_return_type_in_result;
119
120     pub(crate) fn all() -> &'static [Handler] {
121         &[
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,
200             //
201             replace_string_with_char::replace_string_with_char,
202             replace_string_with_char::replace_char_with_string,
203             raw_string::make_raw_string,
204             //
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?
209         ]
210     }
211 }