on master help sign in
Deduplicate some code.
commit2027f69eae5a8b7d25a4613dfddc382f01bcdb78 authorBlaž Hrastnik <blaz@mxxn.io> dateSun 13 Sep 2020, 20:11 (6 years ago) parentb827b41
1 file changed+18−44
helix-core/src/commands.rs+18−44
helix-core/src/commands.rs+18−44
@@ -62,10 +62,7 @@ pub fn append_mode(state: &mut State, _count: usize) {
6262
6363// TODO: I, A, o and O can share a lot of the primitives.
6464
65−// I inserts at the start of each line with a selection
66−pub fn prepend_to_line(state: &mut State, _count: usize) {
67− state.mode = Mode::Insert;
68−
65+fn selection_lines(state: &State) -> Vec<usize> {
6966 // calculate line numbers for each selection range
7067 let mut lines = state
7168 .selection
@@ -77,22 +74,24 @@ pub fn prepend_to_line(state: &mut State, _count: usize) {
7774 lines.sort();
7875 lines.dedup();
7976
80− let positions: Vec<_> = lines
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+
86+ let positions = lines
8187 .into_iter()
8288 .map(|index| {
8389 // adjust all positions to the start of the line.
8490 state.doc.line_to_char(index)
8591 })
86− .collect();
92+ .map(|pos| Range::new(pos, pos));
8793
88− let selection = Selection::new(
89− positions
90− .iter()
91− .copied()
92− .map(|pos| Range::new(pos, pos))
93− .collect(),
94− 0,
95− );
94+ let selection = Selection::new(positions.collect(), 0);
9695
9796 let transaction = Transaction::new(state).with_selection(selection);
9897
@@ -104,18 +103,9 @@ pub fn prepend_to_line(state: &mut State, _count: usize) {
104103pub fn append_to_line(state: &mut State, _count: usize) {
105104 state.mode = Mode::Insert;
106105
107− // calculate line numbers for each selection range
108− let mut lines = state
109− .selection
110− .ranges()
111− .iter()
112− .map(|range| state.doc.char_to_line(range.head))
113− .collect::<Vec<_>>();
106+ let lines = selection_lines(state);
114107
115− lines.sort();
116− lines.dedup();
117−
118− let positions: Vec<_> = lines
108+ let positions = lines
119109 .into_iter()
120110 .map(|index| {
121111 // adjust all positions to the end of the line.
@@ -123,16 +113,9 @@ pub fn append_to_line(state: &mut State, _count: usize) {
123113 let line_start = state.doc.line_to_char(index);
124114 line_start + line.len_chars() - 1
125115 })
126− .collect();
116+ .map(|pos| Range::new(pos, pos));
127117
128− let selection = Selection::new(
129− positions
130− .iter()
131− .copied()
132− .map(|pos| Range::new(pos, pos))
133− .collect(),
134− 0,
135− );
118+ let selection = Selection::new(positions.collect(), 0);
136119
137120 let transaction = Transaction::new(state).with_selection(selection);
138121
@@ -144,16 +127,7 @@ pub fn append_to_line(state: &mut State, _count: usize) {
144127pub fn open_below(state: &mut State, _count: usize) {
145128 state.mode = Mode::Insert;
146129
147− // calculate line numbers for each selection range
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();
130+ let lines = selection_lines(state);
157131
158132 let positions: Vec<_> = lines
159133 .into_iter()