diff options
Diffstat (limited to 'crates/assists/src/lib.rs')
-rw-r--r-- | crates/assists/src/lib.rs | 224 |
1 files changed, 224 insertions, 0 deletions
diff --git a/crates/assists/src/lib.rs b/crates/assists/src/lib.rs new file mode 100644 index 000000000..c589b08dc --- /dev/null +++ b/crates/assists/src/lib.rs | |||
@@ -0,0 +1,224 @@ | |||
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 | pub mod ast_transform; | ||
19 | |||
20 | use base_db::FileRange; | ||
21 | use hir::Semantics; | ||
22 | use ide_db::{source_change::SourceChange, RootDatabase}; | ||
23 | use syntax::TextRange; | ||
24 | |||
25 | pub(crate) use crate::assist_context::{AssistContext, Assists}; | ||
26 | |||
27 | pub use assist_config::AssistConfig; | ||
28 | |||
29 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
30 | pub enum AssistKind { | ||
31 | None, | ||
32 | QuickFix, | ||
33 | Generate, | ||
34 | Refactor, | ||
35 | RefactorExtract, | ||
36 | RefactorInline, | ||
37 | RefactorRewrite, | ||
38 | } | ||
39 | |||
40 | impl 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 | /// Unique identifier of the assist, should not be shown to the user | ||
60 | /// directly. | ||
61 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
62 | pub struct AssistId(pub &'static str, pub AssistKind); | ||
63 | |||
64 | #[derive(Clone, Debug)] | ||
65 | pub struct GroupLabel(pub String); | ||
66 | |||
67 | #[derive(Debug, Clone)] | ||
68 | pub struct Assist { | ||
69 | pub id: AssistId, | ||
70 | /// Short description of the assist, as shown in the UI. | ||
71 | label: String, | ||
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 | } | ||
77 | |||
78 | #[derive(Debug, Clone)] | ||
79 | pub struct ResolvedAssist { | ||
80 | pub assist: Assist, | ||
81 | pub source_change: SourceChange, | ||
82 | } | ||
83 | |||
84 | impl Assist { | ||
85 | fn new(id: AssistId, label: String, group: Option<GroupLabel>, target: TextRange) -> Assist { | ||
86 | assert!(label.starts_with(char::is_uppercase)); | ||
87 | Assist { id, label, group, target } | ||
88 | } | ||
89 | |||
90 | /// Return all the assists applicable at the given position. | ||
91 | /// | ||
92 | /// Assists are returned in the "unresolved" state, that is only labels are | ||
93 | /// returned, without actual edits. | ||
94 | pub fn unresolved(db: &RootDatabase, config: &AssistConfig, range: FileRange) -> Vec<Assist> { | ||
95 | let sema = Semantics::new(db); | ||
96 | let ctx = AssistContext::new(sema, config, range); | ||
97 | let mut acc = Assists::new_unresolved(&ctx); | ||
98 | handlers::all().iter().for_each(|handler| { | ||
99 | handler(&mut acc, &ctx); | ||
100 | }); | ||
101 | acc.finish_unresolved() | ||
102 | } | ||
103 | |||
104 | /// Return all the assists applicable at the given position. | ||
105 | /// | ||
106 | /// Assists are returned in the "resolved" state, that is with edit fully | ||
107 | /// computed. | ||
108 | pub fn resolved( | ||
109 | db: &RootDatabase, | ||
110 | config: &AssistConfig, | ||
111 | range: FileRange, | ||
112 | ) -> Vec<ResolvedAssist> { | ||
113 | let sema = Semantics::new(db); | ||
114 | let ctx = AssistContext::new(sema, config, range); | ||
115 | let mut acc = Assists::new_resolved(&ctx); | ||
116 | handlers::all().iter().for_each(|handler| { | ||
117 | handler(&mut acc, &ctx); | ||
118 | }); | ||
119 | acc.finish_resolved() | ||
120 | } | ||
121 | |||
122 | pub fn label(&self) -> &str { | ||
123 | self.label.as_str() | ||
124 | } | ||
125 | } | ||
126 | |||
127 | mod handlers { | ||
128 | use crate::{AssistContext, Assists}; | ||
129 | |||
130 | pub(crate) type Handler = fn(&mut Assists, &AssistContext) -> Option<()>; | ||
131 | |||
132 | mod add_custom_impl; | ||
133 | mod add_explicit_type; | ||
134 | mod add_missing_impl_members; | ||
135 | mod add_turbo_fish; | ||
136 | mod apply_demorgan; | ||
137 | mod auto_import; | ||
138 | mod change_return_type_to_result; | ||
139 | mod change_visibility; | ||
140 | mod early_return; | ||
141 | mod expand_glob_import; | ||
142 | mod extract_struct_from_enum_variant; | ||
143 | mod extract_variable; | ||
144 | mod fill_match_arms; | ||
145 | mod fix_visibility; | ||
146 | mod flip_binexpr; | ||
147 | mod flip_comma; | ||
148 | mod flip_trait_bound; | ||
149 | mod generate_derive; | ||
150 | mod generate_from_impl_for_enum; | ||
151 | mod generate_function; | ||
152 | mod generate_impl; | ||
153 | mod generate_new; | ||
154 | mod inline_local_variable; | ||
155 | mod introduce_named_lifetime; | ||
156 | mod invert_if; | ||
157 | mod merge_imports; | ||
158 | mod merge_match_arms; | ||
159 | mod move_bounds; | ||
160 | mod move_guard; | ||
161 | mod raw_string; | ||
162 | mod remove_dbg; | ||
163 | mod remove_mut; | ||
164 | mod reorder_fields; | ||
165 | mod replace_if_let_with_match; | ||
166 | mod replace_let_with_if_let; | ||
167 | mod replace_qualified_name_with_use; | ||
168 | mod replace_unwrap_with_match; | ||
169 | mod split_import; | ||
170 | mod unwrap_block; | ||
171 | |||
172 | pub(crate) fn all() -> &'static [Handler] { | ||
173 | &[ | ||
174 | // These are alphabetic for the foolish consistency | ||
175 | add_custom_impl::add_custom_impl, | ||
176 | add_explicit_type::add_explicit_type, | ||
177 | add_turbo_fish::add_turbo_fish, | ||
178 | apply_demorgan::apply_demorgan, | ||
179 | auto_import::auto_import, | ||
180 | change_return_type_to_result::change_return_type_to_result, | ||
181 | change_visibility::change_visibility, | ||
182 | early_return::convert_to_guarded_return, | ||
183 | expand_glob_import::expand_glob_import, | ||
184 | extract_struct_from_enum_variant::extract_struct_from_enum_variant, | ||
185 | extract_variable::extract_variable, | ||
186 | fill_match_arms::fill_match_arms, | ||
187 | fix_visibility::fix_visibility, | ||
188 | flip_binexpr::flip_binexpr, | ||
189 | flip_comma::flip_comma, | ||
190 | flip_trait_bound::flip_trait_bound, | ||
191 | generate_derive::generate_derive, | ||
192 | generate_from_impl_for_enum::generate_from_impl_for_enum, | ||
193 | generate_function::generate_function, | ||
194 | generate_impl::generate_impl, | ||
195 | generate_new::generate_new, | ||
196 | inline_local_variable::inline_local_variable, | ||
197 | introduce_named_lifetime::introduce_named_lifetime, | ||
198 | invert_if::invert_if, | ||
199 | merge_imports::merge_imports, | ||
200 | merge_match_arms::merge_match_arms, | ||
201 | move_bounds::move_bounds_to_where_clause, | ||
202 | move_guard::move_arm_cond_to_match_guard, | ||
203 | move_guard::move_guard_to_arm_body, | ||
204 | raw_string::add_hash, | ||
205 | raw_string::make_raw_string, | ||
206 | raw_string::make_usual_string, | ||
207 | raw_string::remove_hash, | ||
208 | remove_dbg::remove_dbg, | ||
209 | remove_mut::remove_mut, | ||
210 | reorder_fields::reorder_fields, | ||
211 | replace_if_let_with_match::replace_if_let_with_match, | ||
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 | unwrap_block::unwrap_block, | ||
217 | // These are manually sorted for better priorities | ||
218 | add_missing_impl_members::add_missing_impl_members, | ||
219 | add_missing_impl_members::add_missing_default_members, | ||
220 | // Are you sure you want to add new assist here, and not to the | ||
221 | // sorted list above? | ||
222 | ] | ||
223 | } | ||
224 | } | ||