]> git.lizzy.rs Git - rust.git/blob - crates/ra_assists/src/lib.rs
201213c73ca63297dc73b64a5d8b637589c70ca8
[rust.git] / crates / ra_assists / src / lib.rs
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)]
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 hir::Semantics;
21 use ra_db::FileRange;
22 use ra_ide_db::{source_change::SourceChange, RootDatabase};
23 use ra_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     Refactor,
34     RefactorExtract,
35     RefactorInline,
36     RefactorRewrite,
37     Source,
38     OrganizeImports,
39 }
40
41 /// Unique identifier of the assist, should not be shown to the user
42 /// directly.
43 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
44 pub struct AssistId(pub &'static str, pub AssistKind);
45
46 #[derive(Clone, Debug)]
47 pub struct GroupLabel(pub String);
48
49 #[derive(Debug, Clone)]
50 pub struct Assist {
51     pub id: AssistId,
52     /// Short description of the assist, as shown in the UI.
53     pub label: String,
54     pub group: Option<GroupLabel>,
55     /// Target ranges are used to sort assists: the smaller the target range,
56     /// the more specific assist is, and so it should be sorted first.
57     pub target: TextRange,
58 }
59
60 #[derive(Debug, Clone)]
61 pub struct ResolvedAssist {
62     pub assist: Assist,
63     pub source_change: SourceChange,
64 }
65
66 impl Assist {
67     /// Return all the assists applicable at the given position.
68     ///
69     /// Assists are returned in the "unresolved" state, that is only labels are
70     /// returned, without actual edits.
71     pub fn unresolved(db: &RootDatabase, config: &AssistConfig, range: FileRange) -> Vec<Assist> {
72         let sema = Semantics::new(db);
73         let ctx = AssistContext::new(sema, config, range);
74         let mut acc = Assists::new_unresolved(&ctx);
75         handlers::all().iter().for_each(|handler| {
76             handler(&mut acc, &ctx);
77         });
78         acc.finish_unresolved()
79     }
80
81     /// Return all the assists applicable at the given position.
82     ///
83     /// Assists are returned in the "resolved" state, that is with edit fully
84     /// computed.
85     pub fn resolved(
86         db: &RootDatabase,
87         config: &AssistConfig,
88         range: FileRange,
89     ) -> Vec<ResolvedAssist> {
90         let sema = Semantics::new(db);
91         let ctx = AssistContext::new(sema, config, range);
92         let mut acc = Assists::new_resolved(&ctx);
93         handlers::all().iter().for_each(|handler| {
94             handler(&mut acc, &ctx);
95         });
96         acc.finish_resolved()
97     }
98
99     pub(crate) fn new(
100         id: AssistId,
101         label: String,
102         group: Option<GroupLabel>,
103         target: TextRange,
104     ) -> Assist {
105         // FIXME: make fields private, so that this invariant can't be broken
106         assert!(label.starts_with(|c: char| c.is_uppercase()));
107         Assist { id, label, group, target }
108     }
109 }
110
111 mod handlers {
112     use crate::{AssistContext, Assists};
113
114     pub(crate) type Handler = fn(&mut Assists, &AssistContext) -> Option<()>;
115
116     mod add_custom_impl;
117     mod add_derive;
118     mod add_explicit_type;
119     mod add_from_impl_for_enum;
120     mod add_function;
121     mod add_impl;
122     mod add_missing_impl_members;
123     mod add_new;
124     mod add_turbo_fish;
125     mod apply_demorgan;
126     mod auto_import;
127     mod change_return_type_to_result;
128     mod change_visibility;
129     mod early_return;
130     mod extract_struct_from_enum_variant;
131     mod extract_variable;
132     mod fill_match_arms;
133     mod fix_visibility;
134     mod flip_binexpr;
135     mod flip_comma;
136     mod flip_trait_bound;
137     mod inline_local_variable;
138     mod introduce_named_lifetime;
139     mod invert_if;
140     mod merge_imports;
141     mod merge_match_arms;
142     mod move_bounds;
143     mod move_guard;
144     mod raw_string;
145     mod remove_dbg;
146     mod remove_mut;
147     mod reorder_fields;
148     mod replace_if_let_with_match;
149     mod replace_let_with_if_let;
150     mod replace_qualified_name_with_use;
151     mod replace_unwrap_with_match;
152     mod split_import;
153     mod unwrap_block;
154
155     pub(crate) fn all() -> &'static [Handler] {
156         &[
157             // These are alphabetic for the foolish consistency
158             add_custom_impl::add_custom_impl,
159             add_derive::add_derive,
160             add_explicit_type::add_explicit_type,
161             add_from_impl_for_enum::add_from_impl_for_enum,
162             add_function::add_function,
163             add_impl::add_impl,
164             add_new::add_new,
165             add_turbo_fish::add_turbo_fish,
166             apply_demorgan::apply_demorgan,
167             auto_import::auto_import,
168             change_return_type_to_result::change_return_type_to_result,
169             change_visibility::change_visibility,
170             early_return::convert_to_guarded_return,
171             extract_struct_from_enum_variant::extract_struct_from_enum_variant,
172             extract_variable::extract_variable,
173             fill_match_arms::fill_match_arms,
174             fix_visibility::fix_visibility,
175             flip_binexpr::flip_binexpr,
176             flip_comma::flip_comma,
177             flip_trait_bound::flip_trait_bound,
178             inline_local_variable::inline_local_variable,
179             introduce_named_lifetime::introduce_named_lifetime,
180             invert_if::invert_if,
181             merge_imports::merge_imports,
182             merge_match_arms::merge_match_arms,
183             move_bounds::move_bounds_to_where_clause,
184             move_guard::move_arm_cond_to_match_guard,
185             move_guard::move_guard_to_arm_body,
186             raw_string::add_hash,
187             raw_string::make_raw_string,
188             raw_string::make_usual_string,
189             raw_string::remove_hash,
190             remove_dbg::remove_dbg,
191             remove_mut::remove_mut,
192             reorder_fields::reorder_fields,
193             replace_if_let_with_match::replace_if_let_with_match,
194             replace_let_with_if_let::replace_let_with_if_let,
195             replace_qualified_name_with_use::replace_qualified_name_with_use,
196             replace_unwrap_with_match::replace_unwrap_with_match,
197             split_import::split_import,
198             unwrap_block::unwrap_block,
199             // These are manually sorted for better priorities
200             add_missing_impl_members::add_missing_impl_members,
201             add_missing_impl_members::add_missing_default_members,
202             // Are you sure you want to add new assist here, and not to the
203             // sorted list above?
204         ]
205     }
206 }