mirror of
https://github.com/helix-editor/helix
synced 2024-06-01 07:46:05 +02:00
added move_line_start and move_line_end
This commit is contained in:
parent
e0785aabe7
commit
fbe313779e
|
@ -39,6 +39,45 @@ pub fn move_line_down(view: &mut View, count: usize) {
|
||||||
.move_selection(Direction::Forward, Granularity::Line, count);
|
.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) {
|
pub fn move_next_word_start(view: &mut View, count: usize) {
|
||||||
let pos = view.state.move_pos(
|
let pos = view.state.move_pos(
|
||||||
view.state.selection.cursor(),
|
view.state.selection.cursor(),
|
||||||
|
@ -127,46 +166,14 @@ fn selection_lines(state: &State) -> Vec<usize> {
|
||||||
pub fn prepend_to_line(view: &mut View, _count: usize) {
|
pub fn prepend_to_line(view: &mut View, _count: usize) {
|
||||||
view.state.mode = Mode::Insert;
|
view.state.mode = Mode::Insert;
|
||||||
|
|
||||||
let lines = selection_lines(&view.state);
|
move_line_start(view, _count);
|
||||||
|
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// A inserts at the end of each line with a selection
|
// A inserts at the end of each line with a selection
|
||||||
pub fn append_to_line(view: &mut View, _count: usize) {
|
pub fn append_to_line(view: &mut View, _count: usize) {
|
||||||
view.state.mode = Mode::Insert;
|
view.state.mode = Mode::Insert;
|
||||||
|
|
||||||
let lines = selection_lines(&view.state);
|
move_line_end(view, _count);
|
||||||
|
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// o inserts a new line after each line with a selection
|
// o inserts a new line after each line with a selection
|
||||||
|
|
|
@ -98,6 +98,14 @@ pub fn default() -> Keymaps {
|
||||||
code: KeyCode::Char('k'),
|
code: KeyCode::Char('k'),
|
||||||
modifiers: Modifiers::NONE
|
modifiers: Modifiers::NONE
|
||||||
}] => commands::move_line_up as Command,
|
}] => 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 {
|
vec![Key {
|
||||||
code: KeyCode::Char('l'),
|
code: KeyCode::Char('l'),
|
||||||
modifiers: Modifiers::NONE
|
modifiers: Modifiers::NONE
|
||||||
|
|
Loading…
Reference in New Issue