From b831b17b3dbc475420924834b250f07bccd673d0 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 7 Feb 2020 14:53:50 +0100 Subject: Simplify --- crates/ra_assists/src/assist_ctx.rs | 16 ++--------- 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> { } impl<'a> AssistCtx<'a> { - pub(crate) fn with_ctx( - db: &RootDatabase, - frange: FileRange, - should_compute_edit: bool, - f: F, - ) -> T - where - F: FnOnce(AssistCtx) -> T, - { + pub fn new(db: &RootDatabase, frange: FileRange, should_compute_edit: bool) -> AssistCtx { let parse = db.parse(frange.file_id); - - let ctx = AssistCtx { db, frange, source_file: parse.tree(), should_compute_edit }; - f(ctx) + AssistCtx { db, frange, source_file: parse.tree(), should_compute_edit } } -} -impl<'a> AssistCtx<'a> { pub(crate) fn add_assist( self, 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 { pub label: Option, pub edit: TextEdit, pub cursor_position: Option, + // FIXME: This belongs to `AssistLabel` pub target: Option, } @@ -60,16 +61,15 @@ impl ResolvedAssist { /// Assists are returned in the "unresolved" state, that is only labels are /// returned, without actual edits. pub fn unresolved_assists(db: &RootDatabase, range: FileRange) -> Vec { - AssistCtx::with_ctx(db, range, false, |ctx| { - assists::all() - .iter() - .filter_map(|f| f(ctx.clone())) - .map(|a| match a { - Assist::Unresolved { label } => label, - Assist::Resolved { .. } => unreachable!(), - }) - .collect() - }) + let ctx = AssistCtx::new(db, range, false); + assists::all() + .iter() + .filter_map(|f| f(ctx.clone())) + .map(|a| match a { + Assist::Unresolved { label } => label, + Assist::Resolved { .. } => unreachable!(), + }) + .collect() } /// Return all the assists applicable at the given position. @@ -77,18 +77,17 @@ pub fn unresolved_assists(db: &RootDatabase, range: FileRange) -> Vec Vec { - AssistCtx::with_ctx(db, range, true, |ctx| { - let mut a = assists::all() - .iter() - .filter_map(|f| f(ctx.clone())) - .map(|a| match a { - Assist::Resolved { assist } => assist, - Assist::Unresolved { .. } => unreachable!(), - }) - .collect(); - sort_assists(&mut a); - a - }) + let ctx = AssistCtx::new(db, range, true); + let mut a = assists::all() + .iter() + .filter_map(|f| f(ctx.clone())) + .map(|a| match a { + Assist::Resolved { assist } => assist, + Assist::Unresolved { .. } => unreachable!(), + }) + .collect(); + sort_assists(&mut a); + a } fn sort_assists(assists: &mut Vec) { @@ -192,7 +191,7 @@ mod helpers { let frange = FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; let assist = - AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable"); + assist(AssistCtx::new(&db, frange, true)).expect("code action is not applicable"); let action = match assist { Assist::Unresolved { .. } => unreachable!(), Assist::Resolved { assist } => assist.get_first_action(), @@ -219,7 +218,7 @@ mod helpers { let (db, file_id) = with_single_file(&before); let frange = FileRange { file_id, range }; let assist = - AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable"); + assist(AssistCtx::new(&db, frange, true)).expect("code action is not applicable"); let action = match assist { Assist::Unresolved { .. } => unreachable!(), Assist::Resolved { assist } => assist.get_first_action(), @@ -242,7 +241,7 @@ mod helpers { let frange = FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; let assist = - AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable"); + assist(AssistCtx::new(&db, frange, true)).expect("code action is not applicable"); let action = match assist { Assist::Unresolved { .. } => unreachable!(), Assist::Resolved { assist } => assist.get_first_action(), @@ -261,7 +260,7 @@ mod helpers { let (db, file_id) = with_single_file(&before); let frange = FileRange { file_id, range }; let assist = - AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable"); + assist(AssistCtx::new(&db, frange, true)).expect("code action is not applicable"); let action = match assist { Assist::Unresolved { .. } => unreachable!(), Assist::Resolved { assist } => assist.get_first_action(), @@ -279,7 +278,7 @@ mod helpers { let (db, file_id) = with_single_file(&before); let frange = FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; - let assist = AssistCtx::with_ctx(&db, frange, true, assist); + let assist = assist(AssistCtx::new(&db, frange, true)); assert!(assist.is_none()); } @@ -290,7 +289,7 @@ mod helpers { let (range, before) = extract_range(before); let (db, file_id) = with_single_file(&before); let frange = FileRange { file_id, range }; - let assist = AssistCtx::with_ctx(&db, frange, true, assist); + let assist = assist(AssistCtx::new(&db, frange, true)); assert!(assist.is_none()); } } -- cgit v1.2.3