From ad3fe316c616cbc5640cff19048eabac8941ed01 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 27 Oct 2019 18:22:14 +0300 Subject: further simplify assists --- crates/ra_assists/src/assist_ctx.rs | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) (limited to 'crates/ra_assists/src/assist_ctx.rs') diff --git a/crates/ra_assists/src/assist_ctx.rs b/crates/ra_assists/src/assist_ctx.rs index c5e9056af..1908bdec9 100644 --- a/crates/ra_assists/src/assist_ctx.rs +++ b/crates/ra_assists/src/assist_ctx.rs @@ -14,8 +14,8 @@ use crate::{AssistAction, AssistId, AssistLabel}; #[derive(Clone, Debug)] pub(crate) enum Assist { - Unresolved(Vec), - Resolved(Vec<(AssistLabel, AssistAction)>), + Unresolved { label: AssistLabel }, + Resolved { label: AssistLabel, action: AssistAction }, } /// `AssistCtx` allows to apply an assist or check if it could be applied. @@ -54,7 +54,6 @@ pub(crate) struct AssistCtx<'a, DB> { pub(crate) frange: FileRange, source_file: SourceFile, should_compute_edit: bool, - assist: Assist, } impl<'a, DB> Clone for AssistCtx<'a, DB> { @@ -64,7 +63,6 @@ impl<'a, DB> Clone for AssistCtx<'a, DB> { frange: self.frange, source_file: self.source_file.clone(), should_compute_edit: self.should_compute_edit, - assist: self.assist.clone(), } } } @@ -75,32 +73,30 @@ impl<'a, DB: HirDatabase> AssistCtx<'a, DB> { F: FnOnce(AssistCtx) -> T, { let parse = db.parse(frange.file_id); - let assist = - if should_compute_edit { Assist::Resolved(vec![]) } else { Assist::Unresolved(vec![]) }; - let ctx = AssistCtx { db, frange, source_file: parse.tree(), should_compute_edit, assist }; + let ctx = AssistCtx { db, frange, source_file: parse.tree(), should_compute_edit }; f(ctx) } pub(crate) fn add_assist( - mut self, + self, id: AssistId, label: impl Into, f: impl FnOnce(&mut AssistBuilder), ) -> Option { let label = AssistLabel { label: label.into(), id }; - match &mut self.assist { - Assist::Unresolved(labels) => labels.push(label), - Assist::Resolved(labels_actions) => { - let action = { - let mut edit = AssistBuilder::default(); - f(&mut edit); - edit.build() - }; - labels_actions.push((label, action)); - } - } - Some(self.assist) + let assist = if self.should_compute_edit { + let action = { + let mut edit = AssistBuilder::default(); + f(&mut edit); + edit.build() + }; + Assist::Resolved { label, action } + } else { + Assist::Unresolved { label } + }; + + Some(assist) } pub(crate) fn token_at_offset(&self) -> TokenAtOffset { -- cgit v1.2.3