aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_assists/src/lib.rs')
-rw-r--r--crates/ra_assists/src/lib.rs226
1 files changed, 0 insertions, 226 deletions
diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs
deleted file mode 100644
index 507646cc8..000000000
--- a/crates/ra_assists/src/lib.rs
+++ /dev/null
@@ -1,226 +0,0 @@
1//! `ra_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 hir::Semantics;
21use ra_db::FileRange;
22use ra_ide_db::{source_change::SourceChange, RootDatabase};
23use ra_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: 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)]
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 // FIXME: make fields private, so that this invariant can't be broken
124 assert!(label.starts_with(|c: char| c.is_uppercase()));
125 Assist { id, label, group, target }
126 }
127}
128
129mod handlers {
130 use crate::{AssistContext, Assists};
131
132 pub(crate) type Handler = fn(&mut Assists, &AssistContext) -> Option<()>;
133
134 mod add_custom_impl;
135 mod add_explicit_type;
136 mod add_missing_impl_members;
137 mod add_turbo_fish;
138 mod apply_demorgan;
139 mod auto_import;
140 mod change_return_type_to_result;
141 mod change_visibility;
142 mod early_return;
143 mod expand_glob_import;
144 mod extract_struct_from_enum_variant;
145 mod extract_variable;
146 mod fill_match_arms;
147 mod fix_visibility;
148 mod flip_binexpr;
149 mod flip_comma;
150 mod flip_trait_bound;
151 mod generate_derive;
152 mod generate_from_impl_for_enum;
153 mod generate_function;
154 mod generate_impl;
155 mod generate_new;
156 mod inline_local_variable;
157 mod introduce_named_lifetime;
158 mod invert_if;
159 mod merge_imports;
160 mod merge_match_arms;
161 mod move_bounds;
162 mod move_guard;
163 mod raw_string;
164 mod remove_dbg;
165 mod remove_mut;
166 mod reorder_fields;
167 mod replace_if_let_with_match;
168 mod replace_let_with_if_let;
169 mod replace_qualified_name_with_use;
170 mod replace_unwrap_with_match;
171 mod split_import;
172 mod unwrap_block;
173
174 pub(crate) fn all() -> &'static [Handler] {
175 &[
176 // These are alphabetic for the foolish consistency
177 add_custom_impl::add_custom_impl,
178 add_explicit_type::add_explicit_type,
179 add_turbo_fish::add_turbo_fish,
180 apply_demorgan::apply_demorgan,
181 auto_import::auto_import,
182 change_return_type_to_result::change_return_type_to_result,
183 change_visibility::change_visibility,
184 early_return::convert_to_guarded_return,
185 expand_glob_import::expand_glob_import,
186 extract_struct_from_enum_variant::extract_struct_from_enum_variant,
187 extract_variable::extract_variable,
188 fill_match_arms::fill_match_arms,
189 fix_visibility::fix_visibility,
190 flip_binexpr::flip_binexpr,
191 flip_comma::flip_comma,
192 flip_trait_bound::flip_trait_bound,
193 generate_derive::generate_derive,
194 generate_from_impl_for_enum::generate_from_impl_for_enum,
195 generate_function::generate_function,
196 generate_impl::generate_impl,
197 generate_new::generate_new,
198 inline_local_variable::inline_local_variable,
199 introduce_named_lifetime::introduce_named_lifetime,
200 invert_if::invert_if,
201 merge_imports::merge_imports,
202 merge_match_arms::merge_match_arms,
203 move_bounds::move_bounds_to_where_clause,
204 move_guard::move_arm_cond_to_match_guard,
205 move_guard::move_guard_to_arm_body,
206 raw_string::add_hash,
207 raw_string::make_raw_string,
208 raw_string::make_usual_string,
209 raw_string::remove_hash,
210 remove_dbg::remove_dbg,
211 remove_mut::remove_mut,
212 reorder_fields::reorder_fields,
213 replace_if_let_with_match::replace_if_let_with_match,
214 replace_let_with_if_let::replace_let_with_if_let,
215 replace_qualified_name_with_use::replace_qualified_name_with_use,
216 replace_unwrap_with_match::replace_unwrap_with_match,
217 split_import::split_import,
218 unwrap_block::unwrap_block,
219 // These are manually sorted for better priorities
220 add_missing_impl_members::add_missing_impl_members,
221 add_missing_impl_members::add_missing_default_members,
222 // Are you sure you want to add new assist here, and not to the
223 // sorted list above?
224 ]
225 }
226}