Deduplicate some code.

pull/1/head
Blaž Hrastnik 4 years ago
parent b827b41efe
commit 2027f69eae

@ -62,10 +62,7 @@ pub fn append_mode(state: &mut State, _count: usize) {
// TODO: I, A, o and O can share a lot of the primitives. // TODO: I, A, o and O can share a lot of the primitives.
// I inserts at the start of each line with a selection fn selection_lines(state: &State) -> Vec<usize> {
pub fn prepend_to_line(state: &mut State, _count: usize) {
state.mode = Mode::Insert;
// calculate line numbers for each selection range // calculate line numbers for each selection range
let mut lines = state let mut lines = state
.selection .selection
@ -77,22 +74,24 @@ pub fn prepend_to_line(state: &mut State, _count: usize) {
lines.sort(); lines.sort();
lines.dedup(); lines.dedup();
let positions: Vec<_> = lines lines
}
// I inserts at the start of each line with a selection
pub fn prepend_to_line(state: &mut State, _count: usize) {
state.mode = Mode::Insert;
let lines = selection_lines(state);
let positions = lines
.into_iter() .into_iter()
.map(|index| { .map(|index| {
// adjust all positions to the start of the line. // adjust all positions to the start of the line.
state.doc.line_to_char(index) state.doc.line_to_char(index)
}) })
.collect(); .map(|pos| Range::new(pos, pos));
let selection = Selection::new( let selection = Selection::new(positions.collect(), 0);
positions
.iter()
.copied()
.map(|pos| Range::new(pos, pos))
.collect(),
0,
);
let transaction = Transaction::new(state).with_selection(selection); let transaction = Transaction::new(state).with_selection(selection);
@ -104,18 +103,9 @@ pub fn prepend_to_line(state: &mut State, _count: usize) {
pub fn append_to_line(state: &mut State, _count: usize) { pub fn append_to_line(state: &mut State, _count: usize) {
state.mode = Mode::Insert; state.mode = Mode::Insert;
// calculate line numbers for each selection range let lines = selection_lines(state);
let mut lines = state
.selection
.ranges()
.iter()
.map(|range| state.doc.char_to_line(range.head))
.collect::<Vec<_>>();
lines.sort(); let positions = lines
lines.dedup();
let positions: Vec<_> = lines
.into_iter() .into_iter()
.map(|index| { .map(|index| {
// adjust all positions to the end of the line. // adjust all positions to the end of the line.
@ -123,16 +113,9 @@ pub fn append_to_line(state: &mut State, _count: usize) {
let line_start = state.doc.line_to_char(index); let line_start = state.doc.line_to_char(index);
line_start + line.len_chars() - 1 line_start + line.len_chars() - 1
}) })
.collect(); .map(|pos| Range::new(pos, pos));
let selection = Selection::new( let selection = Selection::new(positions.collect(), 0);
positions
.iter()
.copied()
.map(|pos| Range::new(pos, pos))
.collect(),
0,
);
let transaction = Transaction::new(state).with_selection(selection); let transaction = Transaction::new(state).with_selection(selection);
@ -144,16 +127,7 @@ pub fn append_to_line(state: &mut State, _count: usize) {
pub fn open_below(state: &mut State, _count: usize) { pub fn open_below(state: &mut State, _count: usize) {
state.mode = Mode::Insert; state.mode = Mode::Insert;
// calculate line numbers for each selection range let lines = selection_lines(state);
let mut lines = state
.selection
.ranges()
.iter()
.map(|range| state.doc.char_to_line(range.head))
.collect::<Vec<_>>();
lines.sort();
lines.dedup();
let positions: Vec<_> = lines let positions: Vec<_> = lines
.into_iter() .into_iter()

Loading…
Cancel
Save