on master help sign in
added move_line_start and move_line_end
fbe3137Jan Hrastnik
helix-view/src/commands.rs+41−34
@@ -39,6 +39,45 @@ pub fn move_line_down(view: &mut View, count: usize) {
39 .move_selection(Direction::Forward, Granularity::Line, count);
39 .move_selection(Direction::Forward, Granularity::Line, count);
40}
40}
41
41
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+
42pub fn move_next_word_start(view: &mut View, count: usize) {
81pub fn move_next_word_start(view: &mut View, count: usize) {
43 let pos = view.state.move_pos(
82 let pos = view.state.move_pos(
44 view.state.selection.cursor(),
83 view.state.selection.cursor(),
@@ -127,46 +166,14 @@ fn selection_lines(state: &State) -> Vec<usize> {
127pub fn prepend_to_line(view: &mut View, _count: usize) {
166pub fn prepend_to_line(view: &mut View, _count: usize) {
128 view.state.mode = Mode::Insert;
167 view.state.mode = Mode::Insert;
129
168
130− let lines = selection_lines(&view.state);
169+ move_line_start(view, _count);
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
146}
170}
147
171
148// A inserts at the end of each line with a selection
172// A inserts at the end of each line with a selection
149pub fn append_to_line(view: &mut View, _count: usize) {
173pub fn append_to_line(view: &mut View, _count: usize) {
150 view.state.mode = Mode::Insert;
174 view.state.mode = Mode::Insert;
151
175
152− let lines = selection_lines(&view.state);
176+ move_line_end(view, _count);
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
170}
177}
171
178
172// o inserts a new line after each line with a selection
179// 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 {
98 code: KeyCode::Char('k'),
98 code: KeyCode::Char('k'),
99 modifiers: Modifiers::NONE
99 modifiers: Modifiers::NONE
100 }] => commands::move_line_up as Command,
100 }] => 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,
101 vec![Key {
109 vec![Key {
102 code: KeyCode::Char('l'),
110 code: KeyCode::Char('l'),
103 modifiers: Modifiers::NONE
111 modifiers: Modifiers::NONE