From fbe313779e83728b7dca7925df722c7fb4228d98 Mon Sep 17 00:00:00 2001 From: Jan Hrastnik Date: Fri, 25 Sep 2020 16:04:58 +0200 Subject: added move_line_start and move_line_end --- helix-view/src/commands.rs | 75 +++++++++++++++++++++++++--------------------- helix-view/src/keymap.rs | 8 +++++ 2 files changed, 49 insertions(+), 34 deletions(-) diff --git a/helix-view/src/commands.rs b/helix-view/src/commands.rs index be3ea0b9..42390c23 100644 --- a/helix-view/src/commands.rs +++ b/helix-view/src/commands.rs @@ -39,6 +39,45 @@ pub fn move_line_down(view: &mut View, count: usize) { .move_selection(Direction::Forward, Granularity::Line, count); } +pub fn move_line_end(view: &mut View, count: usize) { + // TODO: use a transaction + let lines = selection_lines(&view.state); + + let positions = lines + .into_iter() + .map(|index| { + // adjust all positions to the end of the line. + let line = view.state.doc.line(index); + let line_start = view.state.doc.line_to_char(index); + line_start + line.len_chars() - 1 + }) + .map(|pos| Range::new(pos, pos)); + + let selection = Selection::new(positions.collect(), 0); + + let transaction = Transaction::new(&mut view.state).with_selection(selection); + + transaction.apply(&mut view.state); +} + +pub fn move_line_start(view: &mut View, count: usize) { + let lines = selection_lines(&view.state); + + let positions = lines + .into_iter() + .map(|index| { + // adjust all positions to the start of the line. + view.state.doc.line_to_char(index) + }) + .map(|pos| Range::new(pos, pos)); + + let selection = Selection::new(positions.collect(), 0); + + let transaction = Transaction::new(&mut view.state).with_selection(selection); + + transaction.apply(&mut view.state); +} + pub fn move_next_word_start(view: &mut View, count: usize) { let pos = view.state.move_pos( view.state.selection.cursor(), @@ -127,46 +166,14 @@ fn selection_lines(state: &State) -> Vec { pub fn prepend_to_line(view: &mut View, _count: usize) { view.state.mode = Mode::Insert; - let lines = selection_lines(&view.state); - - let positions = lines - .into_iter() - .map(|index| { - // adjust all positions to the start of the line. - view.state.doc.line_to_char(index) - }) - .map(|pos| Range::new(pos, pos)); - - let selection = Selection::new(positions.collect(), 0); - - let transaction = Transaction::new(&mut view.state).with_selection(selection); - - transaction.apply(&mut view.state); - // TODO: need to store into history if successful + move_line_start(view, _count); } // A inserts at the end of each line with a selection pub fn append_to_line(view: &mut View, _count: usize) { view.state.mode = Mode::Insert; - let lines = selection_lines(&view.state); - - let positions = lines - .into_iter() - .map(|index| { - // adjust all positions to the end of the line. - let line = view.state.doc.line(index); - let line_start = view.state.doc.line_to_char(index); - line_start + line.len_chars() - 1 - }) - .map(|pos| Range::new(pos, pos)); - - let selection = Selection::new(positions.collect(), 0); - - let transaction = Transaction::new(&mut view.state).with_selection(selection); - - transaction.apply(&mut view.state); - // TODO: need to store into history if successful + move_line_end(view, _count); } // o inserts a new line after each line with a selection diff --git a/helix-view/src/keymap.rs b/helix-view/src/keymap.rs index 9fabf41d..5d9b6653 100644 --- a/helix-view/src/keymap.rs +++ b/helix-view/src/keymap.rs @@ -98,6 +98,14 @@ pub fn default() -> Keymaps { code: KeyCode::Char('k'), modifiers: Modifiers::NONE }] => commands::move_line_up as Command, + vec![Key { + code: KeyCode::Char('0'), + modifiers: Modifiers::NONE + }] => commands::move_line_start as Command, + vec![Key { + code: KeyCode::Char('$'), + modifiers: Modifiers::NONE + }] => commands::move_line_end as Command, vec![Key { code: KeyCode::Char('l'), modifiers: Modifiers::NONE -- cgit v1.2.3-70-g09d2