on master help sign in
added move_line_start and move_line_end
commitfbe313779e83728b7dca7925df722c7fb4228d98 authorJan Hrastnik <jan.hrastnik2@gmail.com> dateFri 25 Sep 2020, 16:04 (6 years ago) parente0785aa
2 files changed+49−34
helix-view/src/commands.rs+41−34 helix-view/src/keymap.rs+8
helix-view/src/commands.rs+41−34
@@ -39,6 +39,45 @@ pub fn move_line_down(view: &mut View, count: usize) {
3939 .move_selection(Direction::Forward, Granularity::Line, count);
4040}
4141
42+pub fn move_line_end(view: &mut View, count: usize) {
43+ // TODO: use a transaction
44+ let lines = selection_lines(&view.state);
45+
46+ let positions = lines
47+ .into_iter()
48+ .map(|index| {
49+ // adjust all positions to the end of the line.
50+ let line = view.state.doc.line(index);
51+ let line_start = view.state.doc.line_to_char(index);
52+ line_start + line.len_chars() - 1
53+ })
54+ .map(|pos| Range::new(pos, pos));
55+
56+ let selection = Selection::new(positions.collect(), 0);
57+
58+ let transaction = Transaction::new(&mut view.state).with_selection(selection);
59+
60+ transaction.apply(&mut view.state);
61+}
62+
63+pub fn move_line_start(view: &mut View, count: usize) {
64+ let lines = selection_lines(&view.state);
65+
66+ let positions = lines
67+ .into_iter()
68+ .map(|index| {
69+ // adjust all positions to the start of the line.
70+ view.state.doc.line_to_char(index)
71+ })
72+ .map(|pos| Range::new(pos, pos));
73+
74+ let selection = Selection::new(positions.collect(), 0);
75+
76+ let transaction = Transaction::new(&mut view.state).with_selection(selection);
77+
78+ transaction.apply(&mut view.state);
79+}
80+
4281pub fn move_next_word_start(view: &mut View, count: usize) {
4382 let pos = view.state.move_pos(
4483 view.state.selection.cursor(),
@@ -127,46 +166,14 @@ fn selection_lines(state: &State) -> Vec<usize> {
127166pub fn prepend_to_line(view: &mut View, _count: usize) {
128167 view.state.mode = Mode::Insert;
129168
130− let lines = selection_lines(&view.state);
131−
132− let positions = lines
133− .into_iter()
134− .map(|index| {
135− // adjust all positions to the start of the line.
136− view.state.doc.line_to_char(index)
137− })
138− .map(|pos| Range::new(pos, pos));
139−
140− let selection = Selection::new(positions.collect(), 0);
141−
142− let transaction = Transaction::new(&mut view.state).with_selection(selection);
143−
144− transaction.apply(&mut view.state);
145− // TODO: need to store into history if successful
169+ move_line_start(view, _count);
146170}
147171
148172// A inserts at the end of each line with a selection
149173pub fn append_to_line(view: &mut View, _count: usize) {
150174 view.state.mode = Mode::Insert;
151175
152− let lines = selection_lines(&view.state);
153−
154− let positions = lines
155− .into_iter()
156− .map(|index| {
157− // adjust all positions to the end of the line.
158− let line = view.state.doc.line(index);
159− let line_start = view.state.doc.line_to_char(index);
160− line_start + line.len_chars() - 1
161− })
162− .map(|pos| Range::new(pos, pos));
163−
164− let selection = Selection::new(positions.collect(), 0);
165−
166− let transaction = Transaction::new(&mut view.state).with_selection(selection);
167−
168− transaction.apply(&mut view.state);
169− // TODO: need to store into history if successful
176+ move_line_end(view, _count);
170177}
171178
172179// o inserts a new line after each line with a selection
helix-view/src/keymap.rs+8
@@ -98,6 +98,14 @@ pub fn default() -> Keymaps {
9898 code: KeyCode::Char('k'),
9999 modifiers: Modifiers::NONE
100100 }] => commands::move_line_up as Command,
101+ vec![Key {
102+ code: KeyCode::Char('0'),
103+ modifiers: Modifiers::NONE
104+ }] => commands::move_line_start as Command,
105+ vec![Key {
106+ code: KeyCode::Char('$'),
107+ modifiers: Modifiers::NONE
108+ }] => commands::move_line_end as Command,
101109 vec![Key {
102110 code: KeyCode::Char('l'),
103111 modifiers: Modifiers::NONE