aboutsummaryrefslogtreecommitdiff
path: root/crates/assists/src/lib.rs
diff options
context:
space:
mode:
authorZac Pullar-Strecker <[email protected]>2020-08-24 10:19:53 +0100
committerZac Pullar-Strecker <[email protected]>2020-08-24 10:20:13 +0100
commit7bbca7a1b3f9293d2f5cc5745199bc5f8396f2f0 (patch)
treebdb47765991cb973b2cd5481a088fac636bd326c /crates/assists/src/lib.rs
parentca464650eeaca6195891199a93f4f76cf3e7e697 (diff)
parente65d48d1fb3d4d91d9dc1148a7a836ff5c9a3c87 (diff)
Merge remote-tracking branch 'upstream/master' into 503-hover-doc-links
Diffstat (limited to 'crates/assists/src/lib.rs')
-rw-r--r--crates/assists/src/lib.rs217
1 files changed, 217 insertions, 0 deletions
diff --git a/crates/assists/src/lib.rs b/crates/assists/src/lib.rs
new file mode 100644
index 000000000..2e0d191a6
--- /dev/null
+++ b/crates/assists/src/lib.rs
@@ -0,0 +1,217 @@
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::{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/// 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 pub id: AssistId,
70 /// Short description of the assist, as shown in the UI.
71 pub label: Label,
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)]
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
118mod handlers {
119 use crate::{AssistContext, Assists};
120
121 pub(crate) type Handler = fn(&mut Assists, &AssistContext) -> Option<()>;
122
123 mod add_custom_impl;
124 mod add_explicit_type;
125 mod add_missing_impl_members;
126 mod add_turbo_fish;
127 mod apply_demorgan;
128 mod auto_import;
129 mod change_return_type_to_result;
130 mod change_visibility;
131 mod early_return;
132 mod expand_glob_import;
133 mod extract_struct_from_enum_variant;
134 mod extract_variable;
135 mod fill_match_arms;
136 mod fix_visibility;
137 mod flip_binexpr;
138 mod flip_comma;
139 mod flip_trait_bound;
140 mod generate_derive;
141 mod generate_from_impl_for_enum;
142 mod generate_function;
143 mod generate_impl;
144 mod generate_new;
145 mod inline_local_variable;
146 mod introduce_named_lifetime;
147 mod invert_if;
148 mod merge_imports;
149 mod merge_match_arms;
150 mod move_bounds;
151 mod move_guard;
152 mod raw_string;
153 mod remove_dbg;
154 mod remove_mut;
155 mod remove_unused_param;
156 mod reorder_fields;
157 mod replace_if_let_with_match;
158 mod replace_let_with_if_let;
159 mod replace_qualified_name_with_use;
160 mod replace_unwrap_with_match;
161 mod split_import;
162 mod unwrap_block;
163
164 pub(crate) fn all() -> &'static [Handler] {
165 &[
166 // These are alphabetic for the foolish consistency
167 add_custom_impl::add_custom_impl,
168 add_explicit_type::add_explicit_type,
169 add_turbo_fish::add_turbo_fish,
170 apply_demorgan::apply_demorgan,
171 auto_import::auto_import,
172 change_return_type_to_result::change_return_type_to_result,
173 change_visibility::change_visibility,
174 early_return::convert_to_guarded_return,
175 expand_glob_import::expand_glob_import,
176 extract_struct_from_enum_variant::extract_struct_from_enum_variant,
177 extract_variable::extract_variable,
178 fill_match_arms::fill_match_arms,
179 fix_visibility::fix_visibility,
180 flip_binexpr::flip_binexpr,
181 flip_comma::flip_comma,
182 flip_trait_bound::flip_trait_bound,
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 inline_local_variable::inline_local_variable,
189 introduce_named_lifetime::introduce_named_lifetime,
190 invert_if::invert_if,
191 merge_imports::merge_imports,
192 merge_match_arms::merge_match_arms,
193 move_bounds::move_bounds_to_where_clause,
194 move_guard::move_arm_cond_to_match_guard,
195 move_guard::move_guard_to_arm_body,
196 raw_string::add_hash,
197 raw_string::make_raw_string,
198 raw_string::make_usual_string,
199 raw_string::remove_hash,
200 remove_dbg::remove_dbg,
201 remove_mut::remove_mut,
202 remove_unused_param::remove_unused_param,
203 reorder_fields::reorder_fields,
204 replace_if_let_with_match::replace_if_let_with_match,
205 replace_let_with_if_let::replace_let_with_if_let,
206 replace_qualified_name_with_use::replace_qualified_name_with_use,
207 replace_unwrap_with_match::replace_unwrap_with_match,
208 split_import::split_import,
209 unwrap_block::unwrap_block,
210 // These are manually sorted for better priorities
211 add_missing_impl_members::add_missing_impl_members,
212 add_missing_impl_members::add_missing_default_members,
213 // Are you sure you want to add new assist here, and not to the
214 // sorted list above?
215 ]
216 }
217}