started work on goto mode

This commit is contained in:
Jan Hrastnik 2020-10-04 23:47:37 +02:00
parent b7e1c0cf82
commit 038201647c
6 changed files with 1049 additions and 0 deletions

1
Cargo.lock generated
View File

@ -225,6 +225,7 @@ checksum = "4bd1061998a501ee7d4b6d449020df3266ca3124b941ec56cf2005c3779ca142"
dependencies = [
"bitflags",
"indexmap",
"lazy_static",
"os_str_bytes",
"textwrap",
"unicode-width",

View File

@ -9,6 +9,7 @@
pub enum Mode {
Normal,
Insert,
Goto,
}
/// A state represents the current editor state of a single buffer.

View File

@ -254,6 +254,7 @@ fn render(&mut self) {
let mode = match view.state.mode() {
Mode::Insert => "INS",
Mode::Normal => "NOR",
Mode::Goto => "GOTO",
};
self.surface.set_style(
Rect::new(0, self.size.1 - 1, self.size.0, 1),
@ -278,6 +279,7 @@ fn render(&mut self) {
match view.state.mode() {
Mode::Insert => write!(stdout, "\x1B[6 q"),
Mode::Normal => write!(stdout, "\x1B[2 q"),
Mode::Goto => write!(stdout, "\x1B[2 q"),
};
// render the cursor
@ -326,6 +328,7 @@ pub async fn event_loop(&mut self) {
}))) => {
break;
}
Some(Ok(Event::Key(event))) => {
if let Some(view) = &mut self.view {
match view.state.mode() {
@ -356,6 +359,19 @@ pub async fn event_loop(&mut self) {
// TODO: simplistic ensure cursor in view for now
view.ensure_cursor_in_view();
self.render();
}
}
Mode::Goto => {
// TODO: handle modes and sequences (`gg`)
let keys = vec![event];
if let Some(command) = keymap[&Mode::Goto].get(&keys) {
// TODO: handle count other than 1
command(view, 1);
// TODO: simplistic ensure cursor in view for now
view.ensure_cursor_in_view();
self.render();
}
}

1000
helix-term/test.txt Normal file

File diff suppressed because it is too large Load Diff

View File

@ -117,6 +117,24 @@ pub fn move_next_word_end(view: &mut View, count: usize) {
view.state.selection = Selection::single(pos, pos);
}
pub fn move_file_start(view: &mut View, count: usize) {
// TODO: use a transaction
view.state.selection = view
.state
.move_selection(Direction::Backward, Granularity::Line, count);
view.state.mode = Mode::Normal;
}
pub fn move_file_end(view: &mut View, count: usize) {
// TODO: use a transaction
view.state.selection = view
.state
.move_selection(Direction::Forward, Granularity::Line, count);
view.state.mode = Mode::Normal;
}
// avoid select by default by having a visual mode switch that makes movements into selects
pub fn extend_char_left(view: &mut View, count: usize) {
@ -292,6 +310,10 @@ pub fn normal_mode(view: &mut View, _count: usize) {
}
}
pub fn goto_mode(view: &mut View, _count: usize) {
view.state.mode = Mode::Goto;
}
// TODO: insert means add text just before cursor, on exit we should be on the last letter.
pub fn insert_char(view: &mut View, c: char) {
let c = Tendril::from_char(c);

View File

@ -126,6 +126,7 @@ pub fn default() -> Keymaps {
vec![key!('w')] => commands::move_next_word_start,
vec![key!('b')] => commands::move_prev_word_start,
vec![key!('e')] => commands::move_next_word_end,
vec![key!('g')] => commands::goto_mode,
vec![key!('i')] => commands::insert_mode,
vec![shift!('I')] => commands::prepend_to_line,
vec![key!('a')] => commands::append_mode,
@ -161,6 +162,14 @@ pub fn default() -> Keymaps {
code: KeyCode::Tab,
modifiers: Modifiers::NONE
}] => commands::insert_tab,
),
state::Mode::Goto => hashmap!(
vec![Key {
code: KeyCode::Esc,
modifiers: Modifiers::NONE
}] => commands::normal_mode as Command,
vec![key!('g')] => commands::move_file_start as Command,
vec![key!('e')] => commands::move_file_end as Command,
)
)
}