diff options
author | Aleksey Kladov <[email protected]> | 2020-02-07 13:53:50 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2020-02-07 13:53:50 +0000 |
commit | b831b17b3dbc475420924834b250f07bccd673d0 (patch) | |
tree | 12be36378a4652107867d40d979f5c9d01dafddd /crates/ra_assists | |
parent | 8337dcd9e277feac8e9cff621c752a3e86ba19e6 (diff) |
Simplify
Diffstat (limited to 'crates/ra_assists')
-rw-r--r-- | crates/ra_assists/src/assist_ctx.rs | 16 | ||||
-rw-r--r-- | crates/ra_assists/src/lib.rs | 55 |
2 files changed, 29 insertions, 42 deletions
diff --git a/crates/ra_assists/src/assist_ctx.rs b/crates/ra_assists/src/assist_ctx.rs index f32072dbd..b2381bd97 100644 --- a/crates/ra_assists/src/assist_ctx.rs +++ b/crates/ra_assists/src/assist_ctx.rs | |||
@@ -69,23 +69,11 @@ impl<'a> Clone for AssistCtx<'a> { | |||
69 | } | 69 | } |
70 | 70 | ||
71 | impl<'a> AssistCtx<'a> { | 71 | impl<'a> AssistCtx<'a> { |
72 | pub(crate) fn with_ctx<F, T>( | 72 | pub fn new(db: &RootDatabase, frange: FileRange, should_compute_edit: bool) -> AssistCtx { |
73 | db: &RootDatabase, | ||
74 | frange: FileRange, | ||
75 | should_compute_edit: bool, | ||
76 | f: F, | ||
77 | ) -> T | ||
78 | where | ||
79 | F: FnOnce(AssistCtx) -> T, | ||
80 | { | ||
81 | let parse = db.parse(frange.file_id); | 73 | let parse = db.parse(frange.file_id); |
82 | 74 | AssistCtx { db, frange, source_file: parse.tree(), should_compute_edit } | |
83 | let ctx = AssistCtx { db, frange, source_file: parse.tree(), should_compute_edit }; | ||
84 | f(ctx) | ||
85 | } | 75 | } |
86 | } | ||
87 | 76 | ||
88 | impl<'a> AssistCtx<'a> { | ||
89 | pub(crate) fn add_assist( | 77 | pub(crate) fn add_assist( |
90 | self, | 78 | self, |
91 | id: AssistId, | 79 | id: AssistId, |
diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs index 3f3df3f96..fcdfe6c14 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs | |||
@@ -37,6 +37,7 @@ pub struct AssistAction { | |||
37 | pub label: Option<String>, | 37 | pub label: Option<String>, |
38 | pub edit: TextEdit, | 38 | pub edit: TextEdit, |
39 | pub cursor_position: Option<TextUnit>, | 39 | pub cursor_position: Option<TextUnit>, |
40 | // FIXME: This belongs to `AssistLabel` | ||
40 | pub target: Option<TextRange>, | 41 | pub target: Option<TextRange>, |
41 | } | 42 | } |
42 | 43 | ||
@@ -60,16 +61,15 @@ impl ResolvedAssist { | |||
60 | /// Assists are returned in the "unresolved" state, that is only labels are | 61 | /// Assists are returned in the "unresolved" state, that is only labels are |
61 | /// returned, without actual edits. | 62 | /// returned, without actual edits. |
62 | pub fn unresolved_assists(db: &RootDatabase, range: FileRange) -> Vec<AssistLabel> { | 63 | pub fn unresolved_assists(db: &RootDatabase, range: FileRange) -> Vec<AssistLabel> { |
63 | AssistCtx::with_ctx(db, range, false, |ctx| { | 64 | let ctx = AssistCtx::new(db, range, false); |
64 | assists::all() | 65 | assists::all() |
65 | .iter() | 66 | .iter() |
66 | .filter_map(|f| f(ctx.clone())) | 67 | .filter_map(|f| f(ctx.clone())) |
67 | .map(|a| match a { | 68 | .map(|a| match a { |
68 | Assist::Unresolved { label } => label, | 69 | Assist::Unresolved { label } => label, |
69 | Assist::Resolved { .. } => unreachable!(), | 70 | Assist::Resolved { .. } => unreachable!(), |
70 | }) | 71 | }) |
71 | .collect() | 72 | .collect() |
72 | }) | ||
73 | } | 73 | } |
74 | 74 | ||
75 | /// Return all the assists applicable at the given position. | 75 | /// Return all the assists applicable at the given position. |
@@ -77,18 +77,17 @@ pub fn unresolved_assists(db: &RootDatabase, range: FileRange) -> Vec<AssistLabe | |||
77 | /// Assists are returned in the "resolved" state, that is with edit fully | 77 | /// Assists are returned in the "resolved" state, that is with edit fully |
78 | /// computed. | 78 | /// computed. |
79 | pub fn resolved_assists(db: &RootDatabase, range: FileRange) -> Vec<ResolvedAssist> { | 79 | pub fn resolved_assists(db: &RootDatabase, range: FileRange) -> Vec<ResolvedAssist> { |
80 | AssistCtx::with_ctx(db, range, true, |ctx| { | 80 | let ctx = AssistCtx::new(db, range, true); |
81 | let mut a = assists::all() | 81 | let mut a = assists::all() |
82 | .iter() | 82 | .iter() |
83 | .filter_map(|f| f(ctx.clone())) | 83 | .filter_map(|f| f(ctx.clone())) |
84 | .map(|a| match a { | 84 | .map(|a| match a { |
85 | Assist::Resolved { assist } => assist, | 85 | Assist::Resolved { assist } => assist, |
86 | Assist::Unresolved { .. } => unreachable!(), | 86 | Assist::Unresolved { .. } => unreachable!(), |
87 | }) | 87 | }) |
88 | .collect(); | 88 | .collect(); |
89 | sort_assists(&mut a); | 89 | sort_assists(&mut a); |
90 | a | 90 | a |
91 | }) | ||
92 | } | 91 | } |
93 | 92 | ||
94 | fn sort_assists(assists: &mut Vec<ResolvedAssist>) { | 93 | fn sort_assists(assists: &mut Vec<ResolvedAssist>) { |
@@ -192,7 +191,7 @@ mod helpers { | |||
192 | let frange = | 191 | let frange = |
193 | FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; | 192 | FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; |
194 | let assist = | 193 | let assist = |
195 | AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable"); | 194 | assist(AssistCtx::new(&db, frange, true)).expect("code action is not applicable"); |
196 | let action = match assist { | 195 | let action = match assist { |
197 | Assist::Unresolved { .. } => unreachable!(), | 196 | Assist::Unresolved { .. } => unreachable!(), |
198 | Assist::Resolved { assist } => assist.get_first_action(), | 197 | Assist::Resolved { assist } => assist.get_first_action(), |
@@ -219,7 +218,7 @@ mod helpers { | |||
219 | let (db, file_id) = with_single_file(&before); | 218 | let (db, file_id) = with_single_file(&before); |
220 | let frange = FileRange { file_id, range }; | 219 | let frange = FileRange { file_id, range }; |
221 | let assist = | 220 | let assist = |
222 | AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable"); | 221 | assist(AssistCtx::new(&db, frange, true)).expect("code action is not applicable"); |
223 | let action = match assist { | 222 | let action = match assist { |
224 | Assist::Unresolved { .. } => unreachable!(), | 223 | Assist::Unresolved { .. } => unreachable!(), |
225 | Assist::Resolved { assist } => assist.get_first_action(), | 224 | Assist::Resolved { assist } => assist.get_first_action(), |
@@ -242,7 +241,7 @@ mod helpers { | |||
242 | let frange = | 241 | let frange = |
243 | FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; | 242 | FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; |
244 | let assist = | 243 | let assist = |
245 | AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable"); | 244 | assist(AssistCtx::new(&db, frange, true)).expect("code action is not applicable"); |
246 | let action = match assist { | 245 | let action = match assist { |
247 | Assist::Unresolved { .. } => unreachable!(), | 246 | Assist::Unresolved { .. } => unreachable!(), |
248 | Assist::Resolved { assist } => assist.get_first_action(), | 247 | Assist::Resolved { assist } => assist.get_first_action(), |
@@ -261,7 +260,7 @@ mod helpers { | |||
261 | let (db, file_id) = with_single_file(&before); | 260 | let (db, file_id) = with_single_file(&before); |
262 | let frange = FileRange { file_id, range }; | 261 | let frange = FileRange { file_id, range }; |
263 | let assist = | 262 | let assist = |
264 | AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable"); | 263 | assist(AssistCtx::new(&db, frange, true)).expect("code action is not applicable"); |
265 | let action = match assist { | 264 | let action = match assist { |
266 | Assist::Unresolved { .. } => unreachable!(), | 265 | Assist::Unresolved { .. } => unreachable!(), |
267 | Assist::Resolved { assist } => assist.get_first_action(), | 266 | Assist::Resolved { assist } => assist.get_first_action(), |
@@ -279,7 +278,7 @@ mod helpers { | |||
279 | let (db, file_id) = with_single_file(&before); | 278 | let (db, file_id) = with_single_file(&before); |
280 | let frange = | 279 | let frange = |
281 | FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; | 280 | FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; |
282 | let assist = AssistCtx::with_ctx(&db, frange, true, assist); | 281 | let assist = assist(AssistCtx::new(&db, frange, true)); |
283 | assert!(assist.is_none()); | 282 | assert!(assist.is_none()); |
284 | } | 283 | } |
285 | 284 | ||
@@ -290,7 +289,7 @@ mod helpers { | |||
290 | let (range, before) = extract_range(before); | 289 | let (range, before) = extract_range(before); |
291 | let (db, file_id) = with_single_file(&before); | 290 | let (db, file_id) = with_single_file(&before); |
292 | let frange = FileRange { file_id, range }; | 291 | let frange = FileRange { file_id, range }; |
293 | let assist = AssistCtx::with_ctx(&db, frange, true, assist); | 292 | let assist = assist(AssistCtx::new(&db, frange, true)); |
294 | assert!(assist.is_none()); | 293 | assert!(assist.is_none()); |
295 | } | 294 | } |
296 | } | 295 | } |