Skip to content

Commit b831b17

Browse files
committed
Simplify
1 parent 8337dcd commit b831b17

File tree

2 files changed

+29
-42
lines changed

2 files changed

+29
-42
lines changed

crates/ra_assists/src/assist_ctx.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,23 +69,11 @@ impl<'a> Clone for AssistCtx<'a> {
6969
}
7070

7171
impl<'a> AssistCtx<'a> {
72-
pub(crate) fn with_ctx<F, T>(
73-
db: &RootDatabase,
74-
frange: FileRange,
75-
should_compute_edit: bool,
76-
f: F,
77-
) -> T
78-
where
79-
F: FnOnce(AssistCtx) -> T,
80-
{
72+
pub fn new(db: &RootDatabase, frange: FileRange, should_compute_edit: bool) -> AssistCtx {
8173
let parse = db.parse(frange.file_id);
82-
83-
let ctx = AssistCtx { db, frange, source_file: parse.tree(), should_compute_edit };
84-
f(ctx)
74+
AssistCtx { db, frange, source_file: parse.tree(), should_compute_edit }
8575
}
86-
}
8776

88-
impl<'a> AssistCtx<'a> {
8977
pub(crate) fn add_assist(
9078
self,
9179
id: AssistId,

crates/ra_assists/src/lib.rs

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ pub struct AssistAction {
3737
pub label: Option<String>,
3838
pub edit: TextEdit,
3939
pub cursor_position: Option<TextUnit>,
40+
// FIXME: This belongs to `AssistLabel`
4041
pub target: Option<TextRange>,
4142
}
4243

@@ -60,35 +61,33 @@ impl ResolvedAssist {
6061
/// Assists are returned in the "unresolved" state, that is only labels are
6162
/// returned, without actual edits.
6263
pub fn unresolved_assists(db: &RootDatabase, range: FileRange) -> Vec<AssistLabel> {
63-
AssistCtx::with_ctx(db, range, false, |ctx| {
64-
assists::all()
65-
.iter()
66-
.filter_map(|f| f(ctx.clone()))
67-
.map(|a| match a {
68-
Assist::Unresolved { label } => label,
69-
Assist::Resolved { .. } => unreachable!(),
70-
})
71-
.collect()
72-
})
64+
let ctx = AssistCtx::new(db, range, false);
65+
assists::all()
66+
.iter()
67+
.filter_map(|f| f(ctx.clone()))
68+
.map(|a| match a {
69+
Assist::Unresolved { label } => label,
70+
Assist::Resolved { .. } => unreachable!(),
71+
})
72+
.collect()
7373
}
7474

7575
/// Return all the assists applicable at the given position.
7676
///
7777
/// Assists are returned in the "resolved" state, that is with edit fully
7878
/// computed.
7979
pub fn resolved_assists(db: &RootDatabase, range: FileRange) -> Vec<ResolvedAssist> {
80-
AssistCtx::with_ctx(db, range, true, |ctx| {
81-
let mut a = assists::all()
82-
.iter()
83-
.filter_map(|f| f(ctx.clone()))
84-
.map(|a| match a {
85-
Assist::Resolved { assist } => assist,
86-
Assist::Unresolved { .. } => unreachable!(),
87-
})
88-
.collect();
89-
sort_assists(&mut a);
90-
a
91-
})
80+
let ctx = AssistCtx::new(db, range, true);
81+
let mut a = assists::all()
82+
.iter()
83+
.filter_map(|f| f(ctx.clone()))
84+
.map(|a| match a {
85+
Assist::Resolved { assist } => assist,
86+
Assist::Unresolved { .. } => unreachable!(),
87+
})
88+
.collect();
89+
sort_assists(&mut a);
90+
a
9291
}
9392

9493
fn sort_assists(assists: &mut Vec<ResolvedAssist>) {
@@ -192,7 +191,7 @@ mod helpers {
192191
let frange =
193192
FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) };
194193
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");
196195
let action = match assist {
197196
Assist::Unresolved { .. } => unreachable!(),
198197
Assist::Resolved { assist } => assist.get_first_action(),
@@ -219,7 +218,7 @@ mod helpers {
219218
let (db, file_id) = with_single_file(&before);
220219
let frange = FileRange { file_id, range };
221220
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");
223222
let action = match assist {
224223
Assist::Unresolved { .. } => unreachable!(),
225224
Assist::Resolved { assist } => assist.get_first_action(),
@@ -242,7 +241,7 @@ mod helpers {
242241
let frange =
243242
FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) };
244243
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");
246245
let action = match assist {
247246
Assist::Unresolved { .. } => unreachable!(),
248247
Assist::Resolved { assist } => assist.get_first_action(),
@@ -261,7 +260,7 @@ mod helpers {
261260
let (db, file_id) = with_single_file(&before);
262261
let frange = FileRange { file_id, range };
263262
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");
265264
let action = match assist {
266265
Assist::Unresolved { .. } => unreachable!(),
267266
Assist::Resolved { assist } => assist.get_first_action(),
@@ -279,7 +278,7 @@ mod helpers {
279278
let (db, file_id) = with_single_file(&before);
280279
let frange =
281280
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));
283282
assert!(assist.is_none());
284283
}
285284

@@ -290,7 +289,7 @@ mod helpers {
290289
let (range, before) = extract_range(before);
291290
let (db, file_id) = with_single_file(&before);
292291
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));
294293
assert!(assist.is_none());
295294
}
296295
}

0 commit comments

Comments
 (0)