on master help sign in
modified editor.render() to prepare for command mode rendering
commit0c0c2c71034e264204a2ce6d4fd7a17ddbc2245f authorJan Hrastnik <jan.hrastnik2@gmail.com> dateThu 8 Oct 2020, 00:02 (6 years ago) parent16828d3
4 files changed+52−19
helix-core/src/state.rs+1 helix-term/src/editor.rs+40−19 helix-view/src/commands.rs+4 helix-view/src/keymap.rs+7
helix-core/src/state.rs+1
@@ -10,6 +10,7 @@ pub enum Mode {
1010 Normal,
1111 Insert,
1212 Goto,
13+ Command,
1314}
1415
1516/// A state represents the current editor state of a single buffer.
helix-term/src/editor.rs+40−19
@@ -1,5 +1,5 @@
11use clap::ArgMatches as Args;
2−use helix_core::{indent::TAB_WIDTH, state::Mode, syntax::HighlightEvent, Range, State};
2+use helix_core::{indent::TAB_WIDTH, state::Mode, syntax::HighlightEvent, Position, Range, State};
33use helix_view::{commands, keymap, View};
44
55use std::{
@@ -242,6 +242,7 @@ impl Editor {
242242 Mode::Insert => "INS",
243243 Mode::Normal => "NOR",
244244 Mode::Goto => "GOTO",
245+ Mode::Command => ":",
245246 };
246247 self.surface.set_style(
247248 Rect::new(0, self.size.1 - 1, self.size.0, 1),
@@ -251,35 +252,45 @@ impl Editor {
251252 let text_color = Style::default().fg(Color::Rgb(219, 191, 239)); // lilac
252253 self.surface
253254 .set_string(1, self.size.1 - 1, mode, text_color);
254− if let Some(path) = view.state.path() {
255− self.surface
256− .set_string(6, self.size.1 - 1, path.to_string_lossy(), text_color);
257− }
258−
259− self.terminal
260− .backend_mut()
261− .draw(self.cache.diff(&self.surface).into_iter());
262− // swap the buffer
263− std::mem::swap(&mut self.surface, &mut self.cache);
264255
265256 // set cursor shape
266257 match view.state.mode() {
267258 Mode::Insert => write!(stdout, "\x1B[6 q"),
268259 Mode::Normal => write!(stdout, "\x1B[2 q"),
269260 Mode::Goto => write!(stdout, "\x1B[2 q"),
261+ Mode::Command => write!(stdout, "\x1B[2 q"),
270262 };
271263
272264 // render the cursor
273− let pos = view.state.selection().cursor();
265+ let mut pos: Position;
266+ if view.state.mode() == Mode::Command {
267+ pos = Position::new(self.size.0 as usize, 2);
268+ } else {
269+ if let Some(path) = view.state.path() {
270+ self.surface.set_string(
271+ 6,
272+ self.size.1 - 1,
273+ path.to_string_lossy(),
274+ text_color,
275+ );
276+ }
274277
275− let pos = view
276− .screen_coords_at_pos(&view.state.doc().slice(..), pos)
277− .expect("Cursor is out of bounds.");
278+ let cursor = view.state.selection().cursor();
278279
279− execute!(
280− stdout,
281− cursor::MoveTo(pos.col as u16 + viewport.x, pos.row as u16 + viewport.y,)
282− );
280+ pos = view
281+ .screen_coords_at_pos(&view.state.doc().slice(..), cursor)
282+ .expect("Cursor is out of bounds.");
283+ pos.col += viewport.x as usize;
284+ pos.row += viewport.y as usize;
285+ }
286+
287+ self.terminal
288+ .backend_mut()
289+ .draw(self.cache.diff(&self.surface).into_iter());
290+ // swap the buffer
291+ std::mem::swap(&mut self.surface, &mut self.cache);
292+
293+ execute!(stdout, cursor::MoveTo(pos.col as u16, pos.row as u16));
283294 }
284295 None => (),
285296 }
@@ -357,6 +368,16 @@ impl Editor {
357368 // TODO: simplistic ensure cursor in view for now
358369 view.ensure_cursor_in_view();
359370
371+ self.render();
372+ }
373+ }
374+ Mode::Command => {
375+ // TODO: handle modes and sequences (`gg`)
376+ let keys = vec![event];
377+ if let Some(command) = keymap[&Mode::Goto].get(&keys) {
378+ // TODO: handle count other than 1
379+ command(view, 1);
380+
360381 self.render();
361382 }
362383 }
helix-view/src/commands.rs+4
@@ -304,6 +304,10 @@ pub fn append_mode(view: &mut View, _count: usize) {
304304 })
305305}
306306
307+pub fn command_mode(view: &mut View, _count: usize) {
308+ view.state.mode = Mode::Command;
309+}
310+
307311// TODO: I, A, o and O can share a lot of the primitives.
308312
309313// calculate line numbers for each selection range
helix-view/src/keymap.rs+7
@@ -163,6 +163,7 @@ pub fn default() -> Keymaps {
163163 vec![key!('p')] => commands::paste,
164164 vec![key!('>')] => commands::indent,
165165 vec![key!('<')] => commands::unindent,
166+ vec![key!(':')] => commands::command_mode,
166167 vec![Key {
167168 code: KeyCode::Esc,
168169 modifiers: Modifiers::NONE
@@ -207,6 +208,12 @@ pub fn default() -> Keymaps {
207208 }] => commands::normal_mode as Command,
208209 vec![key!('g')] => commands::move_file_start as Command,
209210 vec![key!('e')] => commands::move_file_end as Command,
211+ ),
212+ state::Mode::Command => hashmap!(
213+ vec![Key {
214+ code: KeyCode::Esc,
215+ modifiers: Modifiers::NONE
216+ }] => commands::normal_mode as Command,
210217 )
211218 )
212219}