on master help sign in
Bugfix
commitbc2c652fe885c7e7953224268688a6d7b6dea80e authorBlaž Hrastnik <blaz@mxxn.io> dateFri 16 Oct 2020, 16:58 (6 years ago) parent49b4cdb
4 files changed+68−36
helix-core/src/state.rs−1 helix-term/src/application.rs+35−16 helix-view/src/commands.rs+14−3 helix-view/src/prompt.rs+19−16
helix-core/src/state.rs−1
@@ -10,7 +10,6 @@ pub enum Mode {
1010 Normal,
1111 Insert,
1212 Goto,
13− Command,
1413}
1514
1615/// A state represents the current editor state of a single buffer.
helix-term/src/application.rs+35−16
@@ -225,7 +225,6 @@ impl Renderer {
225225 Mode::Insert => "INS",
226226 Mode::Normal => "NOR",
227227 Mode::Goto => "GOTO",
228− Mode::Command => "COM", // command?
229228 };
230229 // statusline
231230 self.surface.set_style(
@@ -237,13 +236,15 @@ impl Renderer {
237236 }
238237
239238 pub fn render_prompt(&mut self, prompt: &Prompt) {
240− use tui::backend::Backend;
241239 // render buffer text
242240 self.surface
243− .set_string(1, self.size.1 - 1, String::from(":"), self.text_color);
241+ .set_string(1, self.size.1 - 1, &prompt.prompt, self.text_color);
244242 self.surface
245− .set_string(2, self.size.1 - 1, &prompt.buffer, self.text_color);
243+ .set_string(2, self.size.1 - 1, &prompt.line, self.text_color);
244+ }
246245
246+ pub fn draw(&mut self) {
247+ use tui::backend::Backend;
247248 // TODO: theres probably a better place for this
248249 self.terminal
249250 .backend_mut()
@@ -259,7 +260,7 @@ impl Renderer {
259260 mode => write!(stdout, "\x1B[2 q"),
260261 };
261262 let pos = if let Some(prompt) = prompt {
262− Position::new(self.size.0 as usize, 2 + prompt.cursor_loc)
263+ Position::new(self.size.0 as usize, 2 + prompt.cursor)
263264 } else {
264265 if let Some(path) = view.state.path() {
265266 self.surface.set_string(
@@ -303,15 +304,6 @@ impl Application {
303304 Ok(app)
304305 }
305306
306− pub fn set_prompt(&mut self) {
307− let commands = |editor: &mut Editor, input: &str| match input {
308− "q" => editor.should_close = true,
309− _ => (),
310− };
311− let prompt = Prompt::new(|input| None, commands);
312− self.prompt = Some(prompt);
313− }
314−
315307 fn render(&mut self) {
316308 let viewport = Rect::new(OFFSET, 0, self.terminal.size.0, self.terminal.size.1 - 2); // - 2 for statusline and prompt
317309
@@ -323,6 +315,8 @@ impl Application {
323315 self.terminal.render_prompt(prompt);
324316 }
325317
318+ self.terminal.draw();
319+
326320 // TODO: drop unwrap
327321 self.terminal.render_cursor(
328322 self.editor.view.as_ref().unwrap(),
@@ -335,7 +329,6 @@ impl Application {
335329 let mut reader = EventStream::new();
336330 let keymap = keymap::default();
337331
338− self.set_prompt();
339332 self.render();
340333
341334 loop {
@@ -363,6 +356,8 @@ impl Application {
363356 .as_mut()
364357 .unwrap()
365358 .handle_input(event, &mut self.editor);
359+
360+ self.render();
366361 } else if let Some(view) = &mut self.editor.view {
367362 let keys = vec![event];
368363 // TODO: sequences (`gg`)
@@ -380,7 +375,31 @@ impl Application {
380375 }
381376 view.ensure_cursor_in_view();
382377 }
383− Mode::Command => unreachable!(),
378+ Mode::Normal => {
379+ if let &[KeyEvent {
380+ code: KeyCode::Char(':'),
381+ ..
382+ }] = keys.as_slice()
383+ {
384+ let prompt = Prompt::new(
385+ ":".to_owned(),
386+ |_input: &str| None, // completion
387+ |editor: &mut Editor, input: &str| match input {
388+ "q" => editor.should_close = true,
389+ _ => (),
390+ },
391+ );
392+
393+ self.prompt = Some(prompt);
394+
395+ // HAXX: special casing for command mode
396+ } else if let Some(command) = keymap[&Mode::Normal].get(&keys) {
397+ command(view, 1);
398+
399+ // TODO: simplistic ensure cursor in view for now
400+ view.ensure_cursor_in_view();
401+ }
402+ }
384403 mode => {
385404 if let Some(command) = keymap[&mode].get(&keys) {
386405 command(view, 1);
helix-view/src/commands.rs+14−3
@@ -307,8 +307,19 @@ pub fn append_mode(view: &mut View, _count: usize) {
307307 })
308308}
309309
310−pub fn command_mode(view: &mut View, _count: usize) {
311− view.state.mode = Mode::Command;
310+pub fn command_mode(_view: &mut View, _count: usize) {
311+ use crate::Editor;
312+
313+ let prompt = Prompt::new(
314+ ":".to_owned(),
315+ |_input: &str| None, // completion
316+ |editor: &mut Editor, input: &str| match input {
317+ "q" => editor.should_close = true,
318+ _ => (),
319+ },
320+ );
321+
322+ // set_prompt(prompt)
312323}
313324
314325// TODO: I, A, o and O can share a lot of the primitives.
@@ -627,7 +638,7 @@ pub fn unindent(view: &mut View, _count: usize) {
627638 append_changes_to_history(view);
628639}
629640
630−pub fn indent_selection(view: &mut View, _count: usize) {
641+pub fn indent_selection(_view: &mut View, _count: usize) {
631642 // loop over each line and recompute proper indentation
632643 unimplemented!()
633644}
helix-view/src/prompt.rs+19−16
@@ -4,54 +4,57 @@ use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
44use std::string::String;
55
66pub struct Prompt {
7− pub buffer: String,
8− pub cursor_loc: usize,
7+ pub prompt: String,
8+ pub line: String,
9+ pub cursor: usize,
910 completion_fn: Box<dyn FnMut(&str) -> Option<Vec<&str>>>,
1011 callback_fn: Box<dyn FnMut(&mut Editor, &str)>,
1112}
1213
1314impl Prompt {
1415 pub fn new(
16+ prompt: String,
1517 completion_fn: impl FnMut(&str) -> Option<Vec<&str>> + 'static,
1618 callback_fn: impl FnMut(&mut Editor, &str) + 'static,
1719 ) -> Prompt {
1820 Prompt {
19− buffer: String::from(""),
20− cursor_loc: 0,
21+ prompt,
22+ line: String::new(),
23+ cursor: 0,
2124 completion_fn: Box::new(completion_fn),
2225 callback_fn: Box::new(callback_fn),
2326 }
2427 }
2528
2629 pub fn insert_char(&mut self, c: char) {
27− self.buffer.insert(self.cursor_loc, c);
28− self.cursor_loc += 1;
30+ self.line.insert(self.cursor, c);
31+ self.cursor += 1;
2932 }
3033
3134 pub fn move_char_left(&mut self) {
32− if self.cursor_loc > 1 {
33− self.cursor_loc -= 1;
35+ if self.cursor > 1 {
36+ self.cursor -= 1;
3437 }
3538 }
3639
3740 pub fn move_char_right(&mut self) {
38− if self.cursor_loc < self.buffer.len() {
39− self.cursor_loc += 1;
41+ if self.cursor < self.line.len() {
42+ self.cursor += 1;
4043 }
4144 }
4245
4346 pub fn move_start(&mut self) {
44− self.cursor_loc = 0;
47+ self.cursor = 0;
4548 }
4649
4750 pub fn move_end(&mut self) {
48− self.cursor_loc = self.buffer.len();
51+ self.cursor = self.line.len();
4952 }
5053
5154 pub fn delete_char_backwards(&mut self) {
52− if self.cursor_loc > 0 {
53− self.buffer.remove(self.cursor_loc - 1);
54− self.cursor_loc -= 1;
55+ if self.cursor > 0 {
56+ self.line.remove(self.cursor - 1);
57+ self.cursor -= 1;
5558 }
5659 }
5760
@@ -87,7 +90,7 @@ impl Prompt {
8790 KeyEvent {
8891 code: KeyCode::Enter,
8992 ..
90− } => (self.callback_fn)(editor, &self.buffer),
93+ } => (self.callback_fn)(editor, &self.line),
9194 _ => (),
9295 }
9396 }