1#[allow(unused)]
9macro_rules! eprintln {
10 ($($tt:tt)*) => { stdx::eprintln!($($tt)*) };
11}
12
13mod assist_config;
14mod assist_context;
15#[cfg(test)]
16mod tests;
17pub mod utils;
18pub mod ast_transform;
19
20use hir::Semantics;
21use ide_db::base_db::FileRange;
22use ide_db::{label::Label, source_change::SourceChange, RootDatabase};
23use syntax::TextRange;
24
25pub(crate) use crate::assist_context::{AssistContext, Assists};
26
27pub use assist_config::AssistConfig;
28
29#[derive(Debug, Clone, Copy, PartialEq, Eq)]
30pub enum AssistKind {
31 None,
32 QuickFix,
33 Generate,
34 Refactor,
35 RefactorExtract,
36 RefactorInline,
37 RefactorRewrite,
38}
39
40impl AssistKind {
41 pub fn contains(self, other: AssistKind) -> bool {
42 if self == other {
43 return true;
44 }
45
46 match self {
47 AssistKind::None | AssistKind::Generate => return true,
48 AssistKind::Refactor => match other {
49 AssistKind::RefactorExtract
50 | AssistKind::RefactorInline
51 | AssistKind::RefactorRewrite => return true,
52 _ => return false,
53 },
54 _ => return false,
55 }
56 }
57}
58
59#[derive(Debug, Clone, Copy, PartialEq, Eq)]
62pub struct AssistId(pub &'static str, pub AssistKind);
63
64#[derive(Clone, Debug)]
65pub struct GroupLabel(pub String);
66
67#[derive(Debug, Clone)]
68pub struct Assist {
69 pub id: AssistId,
70 pub label: Label,
72 pub group: Option<GroupLabel>,
73 pub target: TextRange,
76 pub source_change: Option<SourceChange>,
85}
86
87impl Assist {
88 pub fn get(
90 db: &RootDatabase,
91 config: &AssistConfig,
92 resolve: bool,
93 range: FileRange,
94 ) -> Vec<Assist> {
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);
100 });
101 acc.finish()
102 }
103}
104
105mod handlers {
106 use crate::{AssistContext, Assists};
107
108 pub(crate) type Handler = fn(&mut Assists, &AssistContext) -> Option<()>;
109
110 mod add_explicit_type;
111 mod add_lifetime_to_type;
112 mod add_missing_impl_members;
113 mod add_turbo_fish;
114 mod apply_demorgan;
115 mod auto_import;
116 mod change_visibility;
117 mod convert_integer_literal;
118 mod early_return;
119 mod expand_glob_import;
120 mod extract_function;
121 mod extract_struct_from_enum_variant;
122 mod extract_variable;
123 mod fill_match_arms;
124 mod fix_visibility;
125 mod flip_binexpr;
126 mod flip_comma;
127 mod flip_trait_bound;
128 mod generate_default_from_enum_variant;
129 mod generate_derive;
130 mod generate_enum_match_method;
131 mod generate_from_impl_for_enum;
132 mod generate_function;
133 mod generate_getter;
134 mod generate_getter_mut;
135 mod generate_impl;
136 mod generate_new;
137 mod generate_setter;
138 mod infer_function_return_type;
139 mod inline_function;
140 mod inline_local_variable;
141 mod introduce_named_lifetime;
142 mod invert_if;
143 mod merge_imports;
144 mod merge_match_arms;
145 mod move_bounds;
146 mod move_guard;
147 mod move_module_to_file;
148 mod pull_assignment_up;
149 mod qualify_path;
150 mod raw_string;
151 mod remove_dbg;
152 mod remove_mut;
153 mod remove_unused_param;
154 mod reorder_fields;
155 mod reorder_impl;
156 mod replace_derive_with_manual_impl;
157 mod replace_if_let_with_match;
158 mod replace_impl_trait_with_generic;
159 mod replace_let_with_if_let;
160 mod replace_qualified_name_with_use;
161 mod replace_string_with_char;
162 mod replace_unwrap_with_match;
163 mod split_import;
164 mod toggle_ignore;
165 mod unmerge_use;
166 mod unwrap_block;
167 mod wrap_return_type_in_result;
168
169 pub(crate) fn all() -> &'static [Handler] {
170 &[
171 add_explicit_type::add_explicit_type,
173 add_lifetime_to_type::add_lifetime_to_type,
174 add_turbo_fish::add_turbo_fish,
175 apply_demorgan::apply_demorgan,
176 auto_import::auto_import,
177 change_visibility::change_visibility,
178 convert_integer_literal::convert_integer_literal,
179 early_return::convert_to_guarded_return,
180 expand_glob_import::expand_glob_import,
181 move_module_to_file::move_module_to_file,
182 extract_function::extract_function,
183 extract_struct_from_enum_variant::extract_struct_from_enum_variant,
184 extract_variable::extract_variable,
185 fill_match_arms::fill_match_arms,
186 fix_visibility::fix_visibility,
187 flip_binexpr::flip_binexpr,
188 flip_comma::flip_comma,
189 flip_trait_bound::flip_trait_bound,
190 generate_default_from_enum_variant::generate_default_from_enum_variant,
191 generate_derive::generate_derive,
192 generate_enum_match_method::generate_enum_match_method,
193 generate_from_impl_for_enum::generate_from_impl_for_enum,
194 generate_function::generate_function,
195 generate_getter::generate_getter,
196 generate_getter_mut::generate_getter_mut,
197 generate_impl::generate_impl,
198 generate_new::generate_new,
199 generate_setter::generate_setter,
200 infer_function_return_type::infer_function_return_type,
201 inline_function::inline_function,
202 inline_local_variable::inline_local_variable,
203 introduce_named_lifetime::introduce_named_lifetime,
204 invert_if::invert_if,
205 merge_imports::merge_imports,
206 merge_match_arms::merge_match_arms,
207 move_bounds::move_bounds_to_where_clause,
208 move_guard::move_arm_cond_to_match_guard,
209 move_guard::move_guard_to_arm_body,
210 pull_assignment_up::pull_assignment_up,
211 qualify_path::qualify_path,
212 raw_string::add_hash,
213 raw_string::make_usual_string,
214 raw_string::remove_hash,
215 remove_dbg::remove_dbg,
216 remove_mut::remove_mut,
217 remove_unused_param::remove_unused_param,
218 reorder_fields::reorder_fields,
219 reorder_impl::reorder_impl,
220 replace_derive_with_manual_impl::replace_derive_with_manual_impl,
221 replace_if_let_with_match::replace_if_let_with_match,
222 replace_if_let_with_match::replace_match_with_if_let,
223 replace_impl_trait_with_generic::replace_impl_trait_with_generic,
224 replace_let_with_if_let::replace_let_with_if_let,
225 replace_qualified_name_with_use::replace_qualified_name_with_use,
226 replace_unwrap_with_match::replace_unwrap_with_match,
227 split_import::split_import,
228 toggle_ignore::toggle_ignore,
229 unmerge_use::unmerge_use,
230 unwrap_block::unwrap_block,
231 wrap_return_type_in_result::wrap_return_type_in_result,
232 add_missing_impl_members::add_missing_impl_members,
234 add_missing_impl_members::add_missing_default_members,
235 replace_string_with_char::replace_string_with_char,
237 raw_string::make_raw_string,
238 ]
241 }
242}