on master help sign in
modified editor.render() to prepare for command mode rendering
0c0c2c7Jan Hrastnik
helix-core/src/state.rs+1
@@ -10,6 +10,7 @@ pub enum Mode {
10 Normal,
10 Normal,
11 Insert,
11 Insert,
12 Goto,
12 Goto,
13+ Command,
13}
14}
14
15
15/// A state represents the current editor state of a single buffer.
16/// A state represents the current editor state of a single buffer.
helix-term/src/editor.rs+40−19
@@ -1,5 +1,5 @@
1use clap::ArgMatches as Args;
1use 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};
3use helix_view::{commands, keymap, View};
3use helix_view::{commands, keymap, View};
4
4
5use std::{
5use std::{
@@ -242,6 +242,7 @@ impl Editor {
242 Mode::Insert => "INS",
242 Mode::Insert => "INS",
243 Mode::Normal => "NOR",
243 Mode::Normal => "NOR",
244 Mode::Goto => "GOTO",
244 Mode::Goto => "GOTO",
245+ Mode::Command => ":",
245 };
246 };
246 self.surface.set_style(
247 self.surface.set_style(
247 Rect::new(0, self.size.1 - 1, self.size.0, 1),
248 Rect::new(0, self.size.1 - 1, self.size.0, 1),
@@ -251,35 +252,45 @@ impl Editor {
251 let text_color = Style::default().fg(Color::Rgb(219, 191, 239)); // lilac
252 let text_color = Style::default().fg(Color::Rgb(219, 191, 239)); // lilac
252 self.surface
253 self.surface
253 .set_string(1, self.size.1 - 1, mode, text_color);
254 .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);
264
255
265 // set cursor shape
256 // set cursor shape
266 match view.state.mode() {
257 match view.state.mode() {
267 Mode::Insert => write!(stdout, "\x1B[6 q"),
258 Mode::Insert => write!(stdout, "\x1B[6 q"),
268 Mode::Normal => write!(stdout, "\x1B[2 q"),
259 Mode::Normal => write!(stdout, "\x1B[2 q"),
269 Mode::Goto => write!(stdout, "\x1B[2 q"),
260 Mode::Goto => write!(stdout, "\x1B[2 q"),
261+ Mode::Command => write!(stdout, "\x1B[2 q"),
270 };
262 };
271
263
272 // render the cursor
264 // 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+ }
274
277
275− let pos = view
278+ let cursor = view.state.selection().cursor();
276− .screen_coords_at_pos(&view.state.doc().slice(..), pos)
277− .expect("Cursor is out of bounds.");
278
279
279− execute!(
280+ pos = view
280− stdout,
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+
281− cursor::MoveTo(pos.col as u16 + viewport.x, pos.row as u16 + viewport.y,)
293+ execute!(stdout, cursor::MoveTo(pos.col as u16, pos.row as u16));
282− );
283 }
294 }
284 None => (),
295 None => (),
285 }
296 }
@@ -357,6 +368,16 @@ impl Editor {
357 // TODO: simplistic ensure cursor in view for now
368 // TODO: simplistic ensure cursor in view for now
358 view.ensure_cursor_in_view();
369 view.ensure_cursor_in_view();
359
370
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+
360 self.render();
381 self.render();
361 }
382 }
362 }
383 }
helix-view/src/commands.rs+4
@@ -304,6 +304,10 @@ pub fn append_mode(view: &mut View, _count: usize) {
304 })
304 })
305}
305}
306
306
307+pub fn command_mode(view: &mut View, _count: usize) {
308+ view.state.mode = Mode::Command;
309+}
310+
307// TODO: I, A, o and O can share a lot of the primitives.
311// TODO: I, A, o and O can share a lot of the primitives.
308
312
309// calculate line numbers for each selection range
313// calculate line numbers for each selection range
helix-view/src/keymap.rs+7
@@ -163,6 +163,7 @@ pub fn default() -> Keymaps {
163 vec![key!('p')] => commands::paste,
163 vec![key!('p')] => commands::paste,
164 vec![key!('>')] => commands::indent,
164 vec![key!('>')] => commands::indent,
165 vec![key!('<')] => commands::unindent,
165 vec![key!('<')] => commands::unindent,
166+ vec![key!(':')] => commands::command_mode,
166 vec![Key {
167 vec![Key {
167 code: KeyCode::Esc,
168 code: KeyCode::Esc,
168 modifiers: Modifiers::NONE
169 modifiers: Modifiers::NONE
@@ -207,6 +208,12 @@ pub fn default() -> Keymaps {
207 }] => commands::normal_mode as Command,
208 }] => commands::normal_mode as Command,
208 vec![key!('g')] => commands::move_file_start as Command,
209 vec![key!('g')] => commands::move_file_start as Command,
209 vec![key!('e')] => commands::move_file_end as Command,
210 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,
210 )
217 )
211 )
218 )
212}
219}