on master help sign in
Deduplicate some code.
2027f69Blaž Hrastnik
helix-core/src/commands.rs+18−44
@@ -62,10 +62,7 @@ pub fn append_mode(state: &mut State, _count: usize) {
62
62
63// TODO: I, A, o and O can share a lot of the primitives.
63// TODO: I, A, o and O can share a lot of the primitives.
64
64
65−// I inserts at the start of each line with a selection
65+fn selection_lines(state: &State) -> Vec<usize> {
66−pub fn prepend_to_line(state: &mut State, _count: usize) {
67− state.mode = Mode::Insert;
68−
69 // calculate line numbers for each selection range
66 // calculate line numbers for each selection range
70 let mut lines = state
67 let mut lines = state
71 .selection
68 .selection
@@ -77,22 +74,24 @@ pub fn prepend_to_line(state: &mut State, _count: usize) {
77 lines.sort();
74 lines.sort();
78 lines.dedup();
75 lines.dedup();
79
76
77+ lines
78+}
79+
80+// I inserts at the start of each line with a selection
81+pub fn prepend_to_line(state: &mut State, _count: usize) {
82+ state.mode = Mode::Insert;
83+
84+ let lines = selection_lines(state);
85+
80− let positions: Vec<_> = lines
86+ let positions = lines
81 .into_iter()
87 .into_iter()
82 .map(|index| {
88 .map(|index| {
83 // adjust all positions to the start of the line.
89 // adjust all positions to the start of the line.
84 state.doc.line_to_char(index)
90 state.doc.line_to_char(index)
85 })
91 })
86− .collect();
92+ .map(|pos| Range::new(pos, pos));
87
93
88− let selection = Selection::new(
94+ let selection = Selection::new(positions.collect(), 0);
89− positions
90− .iter()
91− .copied()
92− .map(|pos| Range::new(pos, pos))
93− .collect(),
94− 0,
95− );
96
95
97 let transaction = Transaction::new(state).with_selection(selection);
96 let transaction = Transaction::new(state).with_selection(selection);
98
97
@@ -104,18 +103,9 @@ pub fn prepend_to_line(state: &mut State, _count: usize) {
104pub fn append_to_line(state: &mut State, _count: usize) {
103pub fn append_to_line(state: &mut State, _count: usize) {
105 state.mode = Mode::Insert;
104 state.mode = Mode::Insert;
106
105
107− // calculate line numbers for each selection range
106+ let lines = selection_lines(state);
108− let mut lines = state
109− .selection
110− .ranges()
111− .iter()
112− .map(|range| state.doc.char_to_line(range.head))
113− .collect::<Vec<_>>();
114
107
115− lines.sort();
116− lines.dedup();
117−
118− let positions: Vec<_> = lines
108+ let positions = lines
119 .into_iter()
109 .into_iter()
120 .map(|index| {
110 .map(|index| {
121 // adjust all positions to the end of the line.
111 // adjust all positions to the end of the line.
@@ -123,16 +113,9 @@ pub fn append_to_line(state: &mut State, _count: usize) {
123 let line_start = state.doc.line_to_char(index);
113 let line_start = state.doc.line_to_char(index);
124 line_start + line.len_chars() - 1
114 line_start + line.len_chars() - 1
125 })
115 })
126− .collect();
116+ .map(|pos| Range::new(pos, pos));
127
117
128− let selection = Selection::new(
118+ let selection = Selection::new(positions.collect(), 0);
129− positions
130− .iter()
131− .copied()
132− .map(|pos| Range::new(pos, pos))
133− .collect(),
134− 0,
135− );
136
119
137 let transaction = Transaction::new(state).with_selection(selection);
120 let transaction = Transaction::new(state).with_selection(selection);
138
121
@@ -144,16 +127,7 @@ pub fn append_to_line(state: &mut State, _count: usize) {
144pub fn open_below(state: &mut State, _count: usize) {
127pub fn open_below(state: &mut State, _count: usize) {
145 state.mode = Mode::Insert;
128 state.mode = Mode::Insert;
146
129
147− // calculate line numbers for each selection range
130+ let lines = selection_lines(state);
148− let mut lines = state
149− .selection
150− .ranges()
151− .iter()
152− .map(|range| state.doc.char_to_line(range.head))
153− .collect::<Vec<_>>();
154−
155− lines.sort();
156− lines.dedup();
157
131
158 let positions: Vec<_> = lines
132 let positions: Vec<_> = lines
159 .into_iter()
133 .into_iter()