diff options
author | Aleksey Kladov <[email protected]> | 2020-05-06 17:45:35 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2020-05-07 15:27:54 +0100 |
commit | 4867968d22899395e6551f22641b3617e995140c (patch) | |
tree | 4f3ab3a70fbbb901ccec3cd162da00eaa9cbad09 /crates/ra_assists/src/lib.rs | |
parent | f4cd75ac06dff7f5a95065a6c3868669e5c2ab27 (diff) |
Refactor assists API to be more convenient for adding new assists
It now duplicates completion API in its shape.
Diffstat (limited to 'crates/ra_assists/src/lib.rs')
-rw-r--r-- | crates/ra_assists/src/lib.rs | 42 |
1 files changed, 19 insertions, 23 deletions
diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs index 0473fd8c2..011613762 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs | |||
@@ -10,7 +10,7 @@ macro_rules! eprintln { | |||
10 | ($($tt:tt)*) => { stdx::eprintln!($($tt)*) }; | 10 | ($($tt:tt)*) => { stdx::eprintln!($($tt)*) }; |
11 | } | 11 | } |
12 | 12 | ||
13 | mod assist_ctx; | 13 | mod assist_context; |
14 | mod marks; | 14 | mod marks; |
15 | #[cfg(test)] | 15 | #[cfg(test)] |
16 | mod tests; | 16 | mod tests; |
@@ -22,7 +22,7 @@ use ra_db::FileRange; | |||
22 | use ra_ide_db::{source_change::SourceChange, RootDatabase}; | 22 | use ra_ide_db::{source_change::SourceChange, RootDatabase}; |
23 | use ra_syntax::TextRange; | 23 | use ra_syntax::TextRange; |
24 | 24 | ||
25 | pub(crate) use crate::assist_ctx::{Assist, AssistCtx}; | 25 | pub(crate) use crate::assist_context::{AssistContext, Assists}; |
26 | 26 | ||
27 | /// Unique identifier of the assist, should not be shown to the user | 27 | /// Unique identifier of the assist, should not be shown to the user |
28 | /// directly. | 28 | /// directly. |
@@ -68,13 +68,12 @@ pub struct ResolvedAssist { | |||
68 | /// returned, without actual edits. | 68 | /// returned, without actual edits. |
69 | pub fn unresolved_assists(db: &RootDatabase, range: FileRange) -> Vec<AssistLabel> { | 69 | pub fn unresolved_assists(db: &RootDatabase, range: FileRange) -> Vec<AssistLabel> { |
70 | let sema = Semantics::new(db); | 70 | let sema = Semantics::new(db); |
71 | let ctx = AssistCtx::new(&sema, range, false); | 71 | let ctx = AssistContext::new(sema, range); |
72 | handlers::all() | 72 | let mut acc = Assists::new_unresolved(&ctx); |
73 | .iter() | 73 | handlers::all().iter().for_each(|handler| { |
74 | .filter_map(|f| f(ctx.clone())) | 74 | handler(&mut acc, &ctx); |
75 | .flat_map(|it| it.0) | 75 | }); |
76 | .map(|a| a.label) | 76 | acc.finish_unresolved() |
77 | .collect() | ||
78 | } | 77 | } |
79 | 78 | ||
80 | /// Return all the assists applicable at the given position. | 79 | /// Return all the assists applicable at the given position. |
@@ -83,31 +82,30 @@ pub fn unresolved_assists(db: &RootDatabase, range: FileRange) -> Vec<AssistLabe | |||
83 | /// computed. | 82 | /// computed. |
84 | pub fn resolved_assists(db: &RootDatabase, range: FileRange) -> Vec<ResolvedAssist> { | 83 | pub fn resolved_assists(db: &RootDatabase, range: FileRange) -> Vec<ResolvedAssist> { |
85 | let sema = Semantics::new(db); | 84 | let sema = Semantics::new(db); |
86 | let ctx = AssistCtx::new(&sema, range, true); | 85 | let ctx = AssistContext::new(sema, range); |
87 | let mut a = handlers::all() | 86 | let mut acc = Assists::new_resolved(&ctx); |
88 | .iter() | 87 | handlers::all().iter().for_each(|handler| { |
89 | .filter_map(|f| f(ctx.clone())) | 88 | handler(&mut acc, &ctx); |
90 | .flat_map(|it| it.0) | 89 | }); |
91 | .map(|it| it.into_resolved().unwrap()) | 90 | acc.finish_resolved() |
92 | .collect::<Vec<_>>(); | ||
93 | a.sort_by_key(|it| it.label.target.len()); | ||
94 | a | ||
95 | } | 91 | } |
96 | 92 | ||
97 | mod handlers { | 93 | mod handlers { |
98 | use crate::{Assist, AssistCtx}; | 94 | use crate::{AssistContext, Assists}; |
99 | 95 | ||
100 | pub(crate) type Handler = fn(AssistCtx) -> Option<Assist>; | 96 | pub(crate) type Handler = fn(&mut Assists, &AssistContext) -> Option<()>; |
101 | 97 | ||
102 | mod add_custom_impl; | 98 | mod add_custom_impl; |
103 | mod add_derive; | 99 | mod add_derive; |
104 | mod add_explicit_type; | 100 | mod add_explicit_type; |
101 | mod add_from_impl_for_enum; | ||
105 | mod add_function; | 102 | mod add_function; |
106 | mod add_impl; | 103 | mod add_impl; |
107 | mod add_missing_impl_members; | 104 | mod add_missing_impl_members; |
108 | mod add_new; | 105 | mod add_new; |
109 | mod apply_demorgan; | 106 | mod apply_demorgan; |
110 | mod auto_import; | 107 | mod auto_import; |
108 | mod change_return_type_to_result; | ||
111 | mod change_visibility; | 109 | mod change_visibility; |
112 | mod early_return; | 110 | mod early_return; |
113 | mod fill_match_arms; | 111 | mod fill_match_arms; |
@@ -124,14 +122,12 @@ mod handlers { | |||
124 | mod raw_string; | 122 | mod raw_string; |
125 | mod remove_dbg; | 123 | mod remove_dbg; |
126 | mod remove_mut; | 124 | mod remove_mut; |
125 | mod reorder_fields; | ||
127 | mod replace_if_let_with_match; | 126 | mod replace_if_let_with_match; |
128 | mod replace_let_with_if_let; | 127 | mod replace_let_with_if_let; |
129 | mod replace_qualified_name_with_use; | 128 | mod replace_qualified_name_with_use; |
130 | mod replace_unwrap_with_match; | 129 | mod replace_unwrap_with_match; |
131 | mod split_import; | 130 | 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; | 131 | mod unwrap_block; |
136 | 132 | ||
137 | pub(crate) fn all() -> &'static [Handler] { | 133 | pub(crate) fn all() -> &'static [Handler] { |