on master help sign in
Make the prompt callback take a Context.
commitfd1ae35051b57e689f6e6ef7e03c552a78f3f33a authorJoe Neeman <joeneeman@gmail.com> dateTue 22 Jun 2021, 14:49 (5 years ago) parent16883e7
4 files changed+87−81
helix-term/src/commands.rs+76−70 helix-term/src/ui/mod.rs+4−4 helix-term/src/ui/picker.rs+1−1 helix-term/src/ui/prompt.rs+6−6
helix-term/src/commands.rs+76−70
@@ -28,7 +28,7 @@ use insert::*;
2828use movement::Movement;
2929
3030use crate::{
31− compositor::{Callback, Component, Compositor},
31+ compositor::{self, Callback, Component, Compositor},
3232 ui::{self, Completion, Picker, Popup, Prompt, PromptEvent},
3333};
3434
@@ -1032,30 +1032,32 @@ mod cmd {
10321032 pub alias: Option<&'static str>,
10331033 pub doc: &'static str,
10341034 // params, flags, helper, completer
1035− pub fun: fn(&mut Editor, &[&str], PromptEvent),
1035+ pub fun: fn(&mut compositor::Context, &[&str], PromptEvent),
10361036 pub completer: Option<Completer>,
10371037 }
10381038
1039− fn quit(editor: &mut Editor, args: &[&str], event: PromptEvent) {
1039+ fn quit(cx: &mut compositor::Context, args: &[&str], event: PromptEvent) {
10401040 // last view and we have unsaved changes
1041− if editor.tree.views().count() == 1 && buffers_remaining_impl(editor) {
1041+ if cx.editor.tree.views().count() == 1 && buffers_remaining_impl(cx.editor) {
10421042 return;
10431043 }
1044− editor.close(view!(editor).id, /* close_buffer */ false);
1044+ cx.editor
1045+ .close(view!(cx.editor).id, /* close_buffer */ false);
10451046 }
10461047
1047− fn force_quit(editor: &mut Editor, args: &[&str], event: PromptEvent) {
1048− editor.close(view!(editor).id, /* close_buffer */ false);
1048+ fn force_quit(cx: &mut compositor::Context, args: &[&str], event: PromptEvent) {
1049+ cx.editor
1050+ .close(view!(cx.editor).id, /* close_buffer */ false);
10491051 }
10501052
1051− fn open(editor: &mut Editor, args: &[&str], event: PromptEvent) {
1053+ fn open(cx: &mut compositor::Context, args: &[&str], event: PromptEvent) {
10521054 match args.get(0) {
10531055 Some(path) => {
10541056 // TODO: handle error
1055− editor.open(path.into(), Action::Replace);
1057+ cx.editor.open(path.into(), Action::Replace);
10561058 }
10571059 None => {
1058− editor.set_error("wrong argument count".to_string());
1060+ cx.editor.set_error("wrong argument count".to_string());
10591061 }
10601062 };
10611063 }
@@ -1086,30 +1088,30 @@ mod cmd {
10861088 Ok(())
10871089 }
10881090
1089− fn write(editor: &mut Editor, args: &[&str], event: PromptEvent) {
1090− let (view, doc) = current!(editor);
1091+ fn write(cx: &mut compositor::Context, args: &[&str], event: PromptEvent) {
1092+ let (view, doc) = current!(cx.editor);
10911093 if let Err(e) = write_impl(view, doc, args.first()) {
1092− editor.set_error(e.to_string());
1094+ cx.editor.set_error(e.to_string());
10931095 };
10941096 }
10951097
1096− fn new_file(editor: &mut Editor, args: &[&str], event: PromptEvent) {
1097− editor.new_file(Action::Replace);
1098+ fn new_file(cx: &mut compositor::Context, args: &[&str], event: PromptEvent) {
1099+ cx.editor.new_file(Action::Replace);
10981100 }
10991101
1100− fn format(editor: &mut Editor, args: &[&str], event: PromptEvent) {
1101− let (view, doc) = current!(editor);
1102+ fn format(cx: &mut compositor::Context, args: &[&str], event: PromptEvent) {
1103+ let (view, doc) = current!(cx.editor);
11021104
11031105 doc.format(view.id)
11041106 }
11051107
1106− fn set_indent_style(editor: &mut Editor, args: &[&str], event: PromptEvent) {
1108+ fn set_indent_style(cx: &mut compositor::Context, args: &[&str], event: PromptEvent) {
11071109 use IndentStyle::*;
11081110
11091111 // If no argument, report current indent style.
11101112 if args.is_empty() {
1111− let style = current!(editor).1.indent_style;
1112− editor.set_status(match style {
1113+ let style = current!(cx.editor).1.indent_style;
1114+ cx.editor.set_status(match style {
11131115 Tabs => "tabs".into(),
11141116 Spaces(1) => "1 space".into(),
11151117 Spaces(n) if (2..=8).contains(&n) => format!("{} spaces", n),
@@ -1131,22 +1133,23 @@ mod cmd {
11311133 };
11321134
11331135 if let Some(s) = style {
1134− let doc = doc_mut!(editor);
1136+ let doc = doc_mut!(cx.editor);
11351137 doc.indent_style = s;
11361138 } else {
11371139 // Invalid argument.
1138− editor.set_error(format!("invalid indent style '{}'", args[0],));
1140+ cx.editor
1141+ .set_error(format!("invalid indent style '{}'", args[0],));
11391142 }
11401143 }
11411144
11421145 /// Sets or reports the current document's line ending setting.
1143− fn set_line_ending(editor: &mut Editor, args: &[&str], event: PromptEvent) {
1146+ fn set_line_ending(cx: &mut compositor::Context, args: &[&str], event: PromptEvent) {
11441147 use LineEnding::*;
11451148
11461149 // If no argument, report current line ending setting.
11471150 if args.is_empty() {
1148− let line_ending = current!(editor).1.line_ending;
1149− editor.set_status(match line_ending {
1151+ let line_ending = current!(cx.editor).1.line_ending;
1152+ cx.editor.set_status(match line_ending {
11501153 Crlf => "crlf".into(),
11511154 LF => "line feed".into(),
11521155 FF => "form feed".into(),
@@ -1171,49 +1174,50 @@ mod cmd {
11711174 };
11721175
11731176 if let Some(le) = line_ending {
1174− doc_mut!(editor).line_ending = le;
1177+ doc_mut!(cx.editor).line_ending = le;
11751178 } else {
11761179 // Invalid argument.
1177− editor.set_error(format!("invalid line ending '{}'", args[0],));
1180+ cx.editor
1181+ .set_error(format!("invalid line ending '{}'", args[0],));
11781182 }
11791183 }
11801184
1181− fn earlier(editor: &mut Editor, args: &[&str], event: PromptEvent) {
1185+ fn earlier(cx: &mut compositor::Context, args: &[&str], event: PromptEvent) {
11821186 let uk = match args.join(" ").parse::<helix_core::history::UndoKind>() {
11831187 Ok(uk) => uk,
11841188 Err(msg) => {
1185− editor.set_error(msg);
1189+ cx.editor.set_error(msg);
11861190 return;
11871191 }
11881192 };
1189− let (view, doc) = current!(editor);
1193+ let (view, doc) = current!(cx.editor);
11901194 doc.earlier(view.id, uk)
11911195 }
11921196
1193− fn later(editor: &mut Editor, args: &[&str], event: PromptEvent) {
1197+ fn later(cx: &mut compositor::Context, args: &[&str], event: PromptEvent) {
11941198 let uk = match args.join(" ").parse::<helix_core::history::UndoKind>() {
11951199 Ok(uk) => uk,
11961200 Err(msg) => {
1197− editor.set_error(msg);
1201+ cx.editor.set_error(msg);
11981202 return;
11991203 }
12001204 };
1201− let (view, doc) = current!(editor);
1205+ let (view, doc) = current!(cx.editor);
12021206 doc.later(view.id, uk)
12031207 }
12041208
1205− fn write_quit(editor: &mut Editor, args: &[&str], event: PromptEvent) {
1206− let (view, doc) = current!(editor);
1209+ fn write_quit(cx: &mut compositor::Context, args: &[&str], event: PromptEvent) {
1210+ let (view, doc) = current!(cx.editor);
12071211 if let Err(e) = write_impl(view, doc, args.first()) {
1208− editor.set_error(e.to_string());
1212+ cx.editor.set_error(e.to_string());
12091213 return;
12101214 };
1211− quit(editor, &[], event)
1215+ quit(cx, &[], event)
12121216 }
12131217
1214− fn force_write_quit(editor: &mut Editor, args: &[&str], event: PromptEvent) {
1215− write(editor, args, event);
1216− force_quit(editor, &[], event);
1218+ fn force_write_quit(cx: &mut compositor::Context, args: &[&str], event: PromptEvent) {
1219+ write(cx, args, event);
1220+ force_quit(cx, &[], event);
12171221 }
12181222
12191223 /// Returns `true` if there are modified buffers remaining and sets editor error,
@@ -1273,16 +1277,16 @@ mod cmd {
12731277 }
12741278 }
12751279
1276− fn write_all(editor: &mut Editor, args: &[&str], event: PromptEvent) {
1277− write_all_impl(editor, args, event, false, false)
1280+ fn write_all(cx: &mut compositor::Context, args: &[&str], event: PromptEvent) {
1281+ write_all_impl(&mut cx.editor, args, event, false, false)
12781282 }
12791283
1280− fn write_all_quit(editor: &mut Editor, args: &[&str], event: PromptEvent) {
1281− write_all_impl(editor, args, event, true, false)
1284+ fn write_all_quit(cx: &mut compositor::Context, args: &[&str], event: PromptEvent) {
1285+ write_all_impl(&mut cx.editor, args, event, true, false)
12821286 }
12831287
1284− fn force_write_all_quit(editor: &mut Editor, args: &[&str], event: PromptEvent) {
1285− write_all_impl(editor, args, event, true, true)
1288+ fn force_write_all_quit(cx: &mut compositor::Context, args: &[&str], event: PromptEvent) {
1289+ write_all_impl(&mut cx.editor, args, event, true, true)
12861290 }
12871291
12881292 fn quit_all_impl(editor: &mut Editor, args: &[&str], event: PromptEvent, force: bool) {
@@ -1297,50 +1301,50 @@ mod cmd {
12971301 }
12981302 }
12991303
1300− fn quit_all(editor: &mut Editor, args: &[&str], event: PromptEvent) {
1301− quit_all_impl(editor, args, event, false)
1304+ fn quit_all(cx: &mut compositor::Context, args: &[&str], event: PromptEvent) {
1305+ quit_all_impl(&mut cx.editor, args, event, false)
13021306 }
13031307
1304− fn force_quit_all(editor: &mut Editor, args: &[&str], event: PromptEvent) {
1305− quit_all_impl(editor, args, event, true)
1308+ fn force_quit_all(cx: &mut compositor::Context, args: &[&str], event: PromptEvent) {
1309+ quit_all_impl(&mut cx.editor, args, event, true)
13061310 }
13071311
1308− fn theme(editor: &mut Editor, args: &[&str], event: PromptEvent) {
1312+ fn theme(cx: &mut compositor::Context, args: &[&str], event: PromptEvent) {
13091313 let theme = if let Some(theme) = args.first() {
13101314 theme
13111315 } else {
1312− editor.set_error("theme name not provided".into());
1316+ cx.editor.set_error("theme name not provided".into());
13131317 return;
13141318 };
13151319
1316− editor.set_theme_from_name(theme);
1320+ cx.editor.set_theme_from_name(theme);
13171321 }
13181322
1319− fn yank_main_selection_to_clipboard(editor: &mut Editor, _: &[&str], _: PromptEvent) {
1320− yank_main_selection_to_clipboard_impl(editor);
1323+ fn yank_main_selection_to_clipboard(cx: &mut compositor::Context, _: &[&str], _: PromptEvent) {
1324+ yank_main_selection_to_clipboard_impl(&mut cx.editor);
13211325 }
13221326
1323− fn yank_joined_to_clipboard(editor: &mut Editor, args: &[&str], _: PromptEvent) {
1324− let (_, doc) = current!(editor);
1327+ fn yank_joined_to_clipboard(cx: &mut compositor::Context, args: &[&str], _: PromptEvent) {
1328+ let (_, doc) = current!(cx.editor);
13251329 let separator = args
13261330 .first()
13271331 .copied()
13281332 .unwrap_or_else(|| doc.line_ending.as_str());
1329− yank_joined_to_clipboard_impl(editor, separator);
1333+ yank_joined_to_clipboard_impl(&mut cx.editor, separator);
13301334 }
13311335
1332− fn paste_clipboard_after(editor: &mut Editor, _: &[&str], _: PromptEvent) {
1333− paste_clipboard_impl(editor, Paste::After);
1336+ fn paste_clipboard_after(cx: &mut compositor::Context, _: &[&str], _: PromptEvent) {
1337+ paste_clipboard_impl(&mut cx.editor, Paste::After);
13341338 }
13351339
1336− fn paste_clipboard_before(editor: &mut Editor, _: &[&str], _: PromptEvent) {
1337− paste_clipboard_impl(editor, Paste::After);
1340+ fn paste_clipboard_before(cx: &mut compositor::Context, _: &[&str], _: PromptEvent) {
1341+ paste_clipboard_impl(&mut cx.editor, Paste::After);
13381342 }
13391343
1340− fn replace_selections_with_clipboard(editor: &mut Editor, _: &[&str], _: PromptEvent) {
1341− let (view, doc) = current!(editor);
1344+ fn replace_selections_with_clipboard(cx: &mut compositor::Context, _: &[&str], _: PromptEvent) {
1345+ let (view, doc) = current!(cx.editor);
13421346
1343− match editor.clipboard_provider.get_contents() {
1347+ match cx.editor.clipboard_provider.get_contents() {
13441348 Ok(contents) => {
13451349 let transaction =
13461350 Transaction::change_by_selection(doc.text(), doc.selection(view.id), |range| {
@@ -1356,8 +1360,9 @@ mod cmd {
13561360 }
13571361 }
13581362
1359− fn show_clipboard_provider(editor: &mut Editor, _: &[&str], _: PromptEvent) {
1360− editor.set_status(editor.clipboard_provider.name().into());
1363+ fn show_clipboard_provider(cx: &mut compositor::Context, _: &[&str], _: PromptEvent) {
1364+ cx.editor
1365+ .set_status(cx.editor.clipboard_provider.name().into());
13611366 }
13621367
13631368 fn change_current_directory(editor: &mut Editor, args: &[&str], _: PromptEvent) {
@@ -1633,7 +1638,7 @@ fn command_mode(cx: &mut Context) {
16331638 }
16341639 }
16351640 }, // completion
1636− move |editor: &mut Editor, input: &str, event: PromptEvent| {
1641+ move |cx: &mut compositor::Context, input: &str, event: PromptEvent| {
16371642 use helix_view::editor::Action;
16381643
16391644 if event != PromptEvent::Validate {
@@ -1646,9 +1651,10 @@ fn command_mode(cx: &mut Context) {
16461651 }
16471652
16481653 if let Some(cmd) = cmd::COMMANDS.get(parts[0]) {
1649− (cmd.fun)(editor, &parts[1..], event);
1654+ (cmd.fun)(cx, &parts[1..], event);
16501655 } else {
1651− editor.set_error(format!("no such command: '{}'", parts[0]));
1656+ cx.editor
1657+ .set_error(format!("no such command: '{}'", parts[0]));
16521658 };
16531659 },
16541660 );
helix-term/src/ui/mod.rs+4−4
@@ -39,10 +39,10 @@ pub fn regex_prompt(
3939 Prompt::new(
4040 prompt,
4141 |input: &str| Vec::new(), // this is fine because Vec::new() doesn't allocate
42− move |editor: &mut Editor, input: &str, event: PromptEvent| {
42+ move |cx: &mut crate::compositor::Context, input: &str, event: PromptEvent| {
4343 match event {
4444 PromptEvent::Abort => {
45− let (view, doc) = current!(editor);
45+ let (view, doc) = current!(cx.editor);
4646 doc.set_selection(view.id, snapshot.clone());
4747 }
4848 PromptEvent::Validate => {
@@ -56,8 +56,8 @@ pub fn regex_prompt(
5656
5757 match Regex::new(input) {
5858 Ok(regex) => {
59− let (view, doc) = current!(editor);
60− let registers = &mut editor.registers;
59+ let (view, doc) = current!(cx.editor);
60+ let registers = &mut cx.editor.registers;
6161
6262 // revert state to what it was before the last update
6363 doc.set_selection(view.id, snapshot.clone());
helix-term/src/ui/picker.rs+1−1
@@ -44,7 +44,7 @@ impl<T> Picker<T> {
4444 let prompt = Prompt::new(
4545 "".to_string(),
4646 |pattern: &str| Vec::new(),
47− |editor: &mut Editor, pattern: &str, event: PromptEvent| {
47+ |editor: &mut Context, pattern: &str, event: PromptEvent| {
4848 //
4949 },
5050 );
helix-term/src/ui/prompt.rs+6−6
@@ -20,7 +20,7 @@ pub struct Prompt {
2020 completion: Vec<Completion>,
2121 selection: Option<usize>,
2222 completion_fn: Box<dyn FnMut(&str) -> Vec<Completion>>,
23− callback_fn: Box<dyn FnMut(&mut Editor, &str, PromptEvent)>,
23+ callback_fn: Box<dyn FnMut(&mut Context, &str, PromptEvent)>,
2424 pub doc_fn: Box<dyn Fn(&str) -> Option<&'static str>>,
2525}
2626
@@ -54,7 +54,7 @@ impl Prompt {
5454 pub fn new(
5555 prompt: String,
5656 mut completion_fn: impl FnMut(&str) -> Vec<Completion> + 'static,
57− callback_fn: impl FnMut(&mut Editor, &str, PromptEvent) + 'static,
57+ callback_fn: impl FnMut(&mut Context, &str, PromptEvent) + 'static,
5858 ) -> Self {
5959 Self {
6060 prompt,
@@ -386,7 +386,7 @@ impl Component for Prompt {
386386 modifiers: KeyModifiers::SHIFT,
387387 } => {
388388 self.insert_char(c);
389− (self.callback_fn)(cx.editor, &self.line, PromptEvent::Update);
389+ (self.callback_fn)(cx, &self.line, PromptEvent::Update);
390390 }
391391 KeyEvent {
392392 code: KeyCode::Char('c'),
@@ -395,7 +395,7 @@ impl Component for Prompt {
395395 | KeyEvent {
396396 code: KeyCode::Esc, ..
397397 } => {
398− (self.callback_fn)(cx.editor, &self.line, PromptEvent::Abort);
398+ (self.callback_fn)(cx, &self.line, PromptEvent::Abort);
399399 return close_fn;
400400 }
401401 KeyEvent {
@@ -459,7 +459,7 @@ impl Component for Prompt {
459459 modifiers: KeyModifiers::NONE,
460460 } => {
461461 self.delete_char_backwards();
462− (self.callback_fn)(cx.editor, &self.line, PromptEvent::Update);
462+ (self.callback_fn)(cx, &self.line, PromptEvent::Update);
463463 }
464464 KeyEvent {
465465 code: KeyCode::Enter,
@@ -469,7 +469,7 @@ impl Component for Prompt {
469469 self.completion = (self.completion_fn)(&self.line);
470470 self.exit_selection();
471471 } else {
472− (self.callback_fn)(cx.editor, &self.line, PromptEvent::Validate);
472+ (self.callback_fn)(cx, &self.line, PromptEvent::Validate);
473473 return close_fn;
474474 }
475475 }