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