on master help sign in
Refactor command calling.
49b4cdbBlaž Hrastnik
helix-term/src/application.rs+21−21
@@ -37,7 +37,6 @@ static EX: smol::Executor = smol::Executor::new();
37pub struct Application {
37pub struct Application {
38 editor: Editor,
38 editor: Editor,
39 prompt: Option<Prompt>,
39 prompt: Option<Prompt>,
40− should_close: bool,
41 terminal: Renderer,
40 terminal: Renderer,
42}
41}
43
42
@@ -299,19 +298,18 @@ impl Application {
299 terminal,
298 terminal,
300 // TODO; move to state
299 // TODO; move to state
301 prompt: None,
300 prompt: None,
302− should_close: false,
303 };
301 };
304
302
305 Ok(app)
303 Ok(app)
306 }
304 }
307
305
308 pub fn set_prompt(&mut self) {
306 pub fn set_prompt(&mut self) {
309− // let commands = |input| match input {
307+ let commands = |editor: &mut Editor, input: &str| match input {
310− // "q" => self.should_close = true,
308+ "q" => editor.should_close = true,
311− // _ => (),
309+ _ => (),
312− // };
310+ };
313− // let prompt = Prompt::new(|input| None, commands);
311+ let prompt = Prompt::new(|input| None, commands);
314− // self.prompt = Some(prompt);
312+ self.prompt = Some(prompt);
315 }
313 }
316
314
317 fn render(&mut self) {
315 fn render(&mut self) {
@@ -341,12 +339,12 @@ impl Application {
341 self.render();
339 self.render();
342
340
343 loop {
341 loop {
344− // Handle key events
345− if self.should_close {
342+ if self.editor.should_close {
346 break;
343 break;
347 }
344 }
345+
346+ // Handle key events
348− let mut event = reader.next().await;
347+ match reader.next().await {
349− match event {
350 Some(Ok(Event::Resize(width, height))) => {
348 Some(Ok(Event::Resize(width, height))) => {
351 self.terminal.resize(width, height);
349 self.terminal.resize(width, height);
352
350
@@ -359,10 +357,16 @@ impl Application {
359 self.render();
357 self.render();
360 }
358 }
361 Some(Ok(Event::Key(event))) => {
359 Some(Ok(Event::Key(event))) => {
362− // TODO: sequences (`gg`)
360+ // if there's a prompt, it takes priority
363− // TODO: handle count other than 1
361+ if let Some(prompt) = &mut self.prompt {
362+ self.prompt
363+ .as_mut()
364+ .unwrap()
365+ .handle_input(event, &mut self.editor);
364− if let Some(view) = &mut self.editor.view {
366+ } else if let Some(view) = &mut self.editor.view {
365 let keys = vec![event];
367 let keys = vec![event];
368+ // TODO: sequences (`gg`)
369+ // TODO: handle count other than 1
366 match view.state.mode() {
370 match view.state.mode() {
367 Mode::Insert => {
371 Mode::Insert => {
368 if let Some(command) = keymap[&Mode::Insert].get(&keys) {
372 if let Some(command) = keymap[&Mode::Insert].get(&keys) {
@@ -376,9 +380,7 @@ impl Application {
376 }
380 }
377 view.ensure_cursor_in_view();
381 view.ensure_cursor_in_view();
378 }
382 }
379− Mode::Command => {
383+ Mode::Command => unreachable!(),
380− self.prompt.as_mut().unwrap().handle_input(event, view);
381− }
382 mode => {
384 mode => {
383 if let Some(command) = keymap[&mode].get(&keys) {
385 if let Some(command) = keymap[&mode].get(&keys) {
384 command(view, 1);
386 command(view, 1);
@@ -391,9 +393,7 @@ impl Application {
391 self.render();
393 self.render();
392 }
394 }
393 }
395 }
394− Some(Ok(_)) => {
395− // unhandled event
396+ Some(Ok(Event::Mouse(_))) => (), // unhandled
396− }
397 Some(Err(x)) => panic!(x),
397 Some(Err(x)) => panic!(x),
398 None => break,
398 None => break,
399 }
399 }
helix-view/src/editor.rs+5−1
@@ -6,11 +6,15 @@ use anyhow::Error;
6
6
7pub struct Editor {
7pub struct Editor {
8 pub view: Option<View>,
8 pub view: Option<View>,
9+ pub should_close: bool,
9}
10}
10
11
11impl Editor {
12impl Editor {
12 pub fn new() -> Self {
13 pub fn new() -> Self {
14+ Self {
13− Self { view: None }
15+ view: None,
16+ should_close: false,
17+ }
14 }
18 }
15
19
16 pub fn open(&mut self, path: PathBuf, size: (u16, u16)) -> Result<(), Error> {
20 pub fn open(&mut self, path: PathBuf, size: (u16, u16)) -> Result<(), Error> {
helix-view/src/prompt.rs+6−6
@@ -1,5 +1,5 @@
1use crate::commands;
1use crate::commands;
2−use crate::View;
2+use crate::{Editor, View};
3use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
3use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
4use std::string::String;
4use std::string::String;
5
5
@@ -7,13 +7,13 @@ pub struct Prompt {
7 pub buffer: String,
7 pub buffer: String,
8 pub cursor_loc: usize,
8 pub cursor_loc: usize,
9 completion_fn: Box<dyn FnMut(&str) -> Option<Vec<&str>>>,
9 completion_fn: Box<dyn FnMut(&str) -> Option<Vec<&str>>>,
10− callback_fn: Box<dyn FnMut(&str)>,
10+ callback_fn: Box<dyn FnMut(&mut Editor, &str)>,
11}
11}
12
12
13impl Prompt {
13impl Prompt {
14 pub fn new(
14 pub fn new(
15 completion_fn: impl FnMut(&str) -> Option<Vec<&str>> + 'static,
15 completion_fn: impl FnMut(&str) -> Option<Vec<&str>> + 'static,
16− callback_fn: impl FnMut(&str) + 'static,
16+ callback_fn: impl FnMut(&mut Editor, &str) + 'static,
17 ) -> Prompt {
17 ) -> Prompt {
18 Prompt {
18 Prompt {
19 buffer: String::from(""),
19 buffer: String::from(""),
@@ -55,7 +55,7 @@ impl Prompt {
55 }
55 }
56 }
56 }
57
57
58− pub fn handle_input(&mut self, key_event: KeyEvent, view: &mut View) {
58+ pub fn handle_input(&mut self, key_event: KeyEvent, editor: &mut Editor) {
59 match key_event {
59 match key_event {
60 KeyEvent {
60 KeyEvent {
61 code: KeyCode::Char(c),
61 code: KeyCode::Char(c),
@@ -63,7 +63,7 @@ impl Prompt {
63 } => self.insert_char(c),
63 } => self.insert_char(c),
64 KeyEvent {
64 KeyEvent {
65 code: KeyCode::Esc, ..
65 code: KeyCode::Esc, ..
66− } => commands::normal_mode(view, 1),
66+ } => unimplemented!("Exit prompt!"),
67 KeyEvent {
67 KeyEvent {
68 code: KeyCode::Right,
68 code: KeyCode::Right,
69 ..
69 ..
@@ -87,7 +87,7 @@ impl Prompt {
87 KeyEvent {
87 KeyEvent {
88 code: KeyCode::Enter,
88 code: KeyCode::Enter,
89 ..
89 ..
90− } => (self.callback_fn)(&self.buffer),
90+ } => (self.callback_fn)(editor, &self.buffer),
91 _ => (),
91 _ => (),
92 }
92 }
93 }
93 }