aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src/assist_ctx.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_assists/src/assist_ctx.rs')
-rw-r--r--crates/ra_assists/src/assist_ctx.rs25
1 files changed, 7 insertions, 18 deletions
diff --git a/crates/ra_assists/src/assist_ctx.rs b/crates/ra_assists/src/assist_ctx.rs
index f32072dbd..81f999090 100644
--- a/crates/ra_assists/src/assist_ctx.rs
+++ b/crates/ra_assists/src/assist_ctx.rs
@@ -19,6 +19,8 @@ pub(crate) enum Assist {
19 Resolved { assist: ResolvedAssist }, 19 Resolved { assist: ResolvedAssist },
20} 20}
21 21
22pub(crate) type AssistHandler = fn(AssistCtx) -> Option<Assist>;
23
22/// `AssistCtx` allows to apply an assist or check if it could be applied. 24/// `AssistCtx` allows to apply an assist or check if it could be applied.
23/// 25///
24/// Assists use a somewhat over-engineered approach, given the current needs. The 26/// Assists use a somewhat over-engineered approach, given the current needs. The
@@ -57,7 +59,7 @@ pub(crate) struct AssistCtx<'a> {
57 should_compute_edit: bool, 59 should_compute_edit: bool,
58} 60}
59 61
60impl<'a> Clone for AssistCtx<'a> { 62impl Clone for AssistCtx<'_> {
61 fn clone(&self) -> Self { 63 fn clone(&self) -> Self {
62 AssistCtx { 64 AssistCtx {
63 db: self.db, 65 db: self.db,
@@ -69,31 +71,18 @@ impl<'a> Clone for AssistCtx<'a> {
69} 71}
70 72
71impl<'a> AssistCtx<'a> { 73impl<'a> AssistCtx<'a> {
72 pub(crate) fn with_ctx<F, T>( 74 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); 75 let parse = db.parse(frange.file_id);
82 76 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 } 77 }
86}
87 78
88impl<'a> AssistCtx<'a> {
89 pub(crate) fn add_assist( 79 pub(crate) fn add_assist(
90 self, 80 self,
91 id: AssistId, 81 id: AssistId,
92 label: impl Into<String>, 82 label: impl Into<String>,
93 f: impl FnOnce(&mut ActionBuilder), 83 f: impl FnOnce(&mut ActionBuilder),
94 ) -> Option<Assist> { 84 ) -> Option<Assist> {
95 let label = AssistLabel { label: label.into(), id }; 85 let label = AssistLabel::new(label.into(), id);
96 assert!(label.label.chars().nth(0).unwrap().is_uppercase());
97 86
98 let assist = if self.should_compute_edit { 87 let assist = if self.should_compute_edit {
99 let action = { 88 let action = {
@@ -115,7 +104,7 @@ impl<'a> AssistCtx<'a> {
115 label: impl Into<String>, 104 label: impl Into<String>,
116 f: impl FnOnce() -> Vec<ActionBuilder>, 105 f: impl FnOnce() -> Vec<ActionBuilder>,
117 ) -> Option<Assist> { 106 ) -> Option<Assist> {
118 let label = AssistLabel { label: label.into(), id }; 107 let label = AssistLabel::new(label.into(), id);
119 let assist = if self.should_compute_edit { 108 let assist = if self.should_compute_edit {
120 let actions = f(); 109 let actions = f();
121 assert!(!actions.is_empty(), "Assist cannot have no"); 110 assert!(!actions.is_empty(), "Assist cannot have no");