1 //! `ra_assists` crate provides a bunch of code assists, also known as code
2 //! actions (in LSP) or intentions (in IntelliJ).
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
9 macro_rules! eprintln {
10 ($($tt:tt)*) => { stdx::eprintln!($($tt)*) };
18 pub mod ast_transform;
22 use ra_ide_db::{source_change::SourceChange, RootDatabase};
23 use ra_syntax::TextRange;
25 pub(crate) use crate::assist_ctx::{Assist, AssistCtx};
27 /// Unique identifier of the assist, should not be shown to the user
29 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
30 pub struct AssistId(pub &'static str);
32 #[derive(Debug, Clone)]
33 pub struct AssistLabel {
35 /// Short description of the assist, as shown in the UI.
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,
43 #[derive(Clone, Debug)]
44 pub struct GroupLabel(pub String);
50 group: Option<GroupLabel>,
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 }
59 #[derive(Debug, Clone)]
60 pub struct ResolvedAssist {
61 pub label: AssistLabel,
62 pub source_change: SourceChange,
65 /// Return all the assists applicable at the given position.
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);
74 .filter_map(|f| f(ctx.clone()))
80 /// Return all the assists applicable at the given position.
82 /// Assists are returned in the "resolved" state, that is with edit fully
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()
89 .filter_map(|f| f(ctx.clone()))
91 .map(|it| it.into_resolved().unwrap())
93 a.sort_by_key(|it| it.label.target.len());
98 use crate::{Assist, AssistCtx};
100 pub(crate) type Handler = fn(AssistCtx) -> Option<Assist>;
104 mod add_explicit_type;
107 mod add_missing_impl_members;
111 mod change_visibility;
116 mod flip_trait_bound;
117 mod inline_local_variable;
118 mod introduce_variable;
121 mod merge_match_arms;
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;
132 mod change_return_type_to_result;
133 mod add_from_impl_for_enum;
137 pub(crate) fn all() -> &'static [Handler] {
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,
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?