diff options
author | Benjamin Coenen <[email protected]> | 2020-05-10 11:31:55 +0100 |
---|---|---|
committer | Benjamin Coenen <[email protected]> | 2020-05-10 11:31:55 +0100 |
commit | e80903a96564c2239489a8c630a4748bf21a3659 (patch) | |
tree | 12b31a1fd12deb2120065cea5a558425c8c1984f /crates/ra_assists/src/lib.rs | |
parent | 6203e9c4faee288f16d93dbb7dd0f1f8df487d83 (diff) | |
parent | 4578154b608fa075595103d0c933da60d55b25c8 (diff) |
Merge branch 'master' of github.com:rust-analyzer/rust-analyzer into feat_4348
Diffstat (limited to 'crates/ra_assists/src/lib.rs')
-rw-r--r-- | crates/ra_assists/src/lib.rs | 98 |
1 files changed, 47 insertions, 51 deletions
diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs index 0473fd8c2..b6dc7cb1b 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,15 +22,18 @@ 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. |
29 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | 29 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
30 | pub struct AssistId(pub &'static str); | 30 | pub struct AssistId(pub &'static str); |
31 | 31 | ||
32 | #[derive(Clone, Debug)] | ||
33 | pub struct GroupLabel(pub String); | ||
34 | |||
32 | #[derive(Debug, Clone)] | 35 | #[derive(Debug, Clone)] |
33 | pub struct AssistLabel { | 36 | pub struct Assist { |
34 | pub id: AssistId, | 37 | pub id: AssistId, |
35 | /// Short description of the assist, as shown in the UI. | 38 | /// Short description of the assist, as shown in the UI. |
36 | pub label: String, | 39 | pub label: String, |
@@ -40,74 +43,69 @@ pub struct AssistLabel { | |||
40 | pub target: TextRange, | 43 | pub target: TextRange, |
41 | } | 44 | } |
42 | 45 | ||
43 | #[derive(Clone, Debug)] | 46 | #[derive(Debug, Clone)] |
44 | pub struct GroupLabel(pub String); | 47 | pub struct ResolvedAssist { |
48 | pub assist: Assist, | ||
49 | pub source_change: SourceChange, | ||
50 | } | ||
51 | |||
52 | impl Assist { | ||
53 | /// Return all the assists applicable at the given position. | ||
54 | /// | ||
55 | /// Assists are returned in the "unresolved" state, that is only labels are | ||
56 | /// returned, without actual edits. | ||
57 | pub fn unresolved(db: &RootDatabase, range: FileRange) -> Vec<Assist> { | ||
58 | let sema = Semantics::new(db); | ||
59 | let ctx = AssistContext::new(sema, range); | ||
60 | let mut acc = Assists::new_unresolved(&ctx); | ||
61 | handlers::all().iter().for_each(|handler| { | ||
62 | handler(&mut acc, &ctx); | ||
63 | }); | ||
64 | acc.finish_unresolved() | ||
65 | } | ||
66 | |||
67 | /// Return all the assists applicable at the given position. | ||
68 | /// | ||
69 | /// Assists are returned in the "resolved" state, that is with edit fully | ||
70 | /// computed. | ||
71 | pub fn resolved(db: &RootDatabase, range: FileRange) -> Vec<ResolvedAssist> { | ||
72 | let sema = Semantics::new(db); | ||
73 | let ctx = AssistContext::new(sema, range); | ||
74 | let mut acc = Assists::new_resolved(&ctx); | ||
75 | handlers::all().iter().for_each(|handler| { | ||
76 | handler(&mut acc, &ctx); | ||
77 | }); | ||
78 | acc.finish_resolved() | ||
79 | } | ||
45 | 80 | ||
46 | impl AssistLabel { | ||
47 | pub(crate) fn new( | 81 | pub(crate) fn new( |
48 | id: AssistId, | 82 | id: AssistId, |
49 | label: String, | 83 | label: String, |
50 | group: Option<GroupLabel>, | 84 | group: Option<GroupLabel>, |
51 | target: TextRange, | 85 | target: TextRange, |
52 | ) -> AssistLabel { | 86 | ) -> Assist { |
53 | // FIXME: make fields private, so that this invariant can't be broken | 87 | // FIXME: make fields private, so that this invariant can't be broken |
54 | assert!(label.starts_with(|c: char| c.is_uppercase())); | 88 | assert!(label.starts_with(|c: char| c.is_uppercase())); |
55 | AssistLabel { id, label, group, target } | 89 | Assist { id, label, group, target } |
56 | } | 90 | } |
57 | } | 91 | } |
58 | 92 | ||
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 { | 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] { |