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