Deduplicate some code.
This commit is contained in:
parent
b827b41efe
commit
2027f69eae
1 changed files with 18 additions and 44 deletions
|
@ -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.
|
||||
|
||||
// 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;
|
||||
|
||||
fn selection_lines(state: &State) -> Vec<usize> {
|
||||
// calculate line numbers for each selection range
|
||||
let mut lines = state
|
||||
.selection
|
||||
|
@ -77,22 +74,24 @@ pub fn prepend_to_line(state: &mut State, _count: usize) {
|
|||
lines.sort();
|
||||
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()
|
||||
.map(|index| {
|
||||
// adjust all positions to the start of the line.
|
||||
state.doc.line_to_char(index)
|
||||
})
|
||||
.collect();
|
||||
.map(|pos| Range::new(pos, pos));
|
||||
|
||||
let selection = Selection::new(
|
||||
positions
|
||||
.iter()
|
||||
.copied()
|
||||
.map(|pos| Range::new(pos, pos))
|
||||
.collect(),
|
||||
0,
|
||||
);
|
||||
let selection = Selection::new(positions.collect(), 0);
|
||||
|
||||
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) {
|
||||
state.mode = Mode::Insert;
|
||||
|
||||
// calculate line numbers for each selection range
|
||||
let mut lines = state
|
||||
.selection
|
||||
.ranges()
|
||||
.iter()
|
||||
.map(|range| state.doc.char_to_line(range.head))
|
||||
.collect::<Vec<_>>();
|
||||
let lines = selection_lines(state);
|
||||
|
||||
lines.sort();
|
||||
lines.dedup();
|
||||
|
||||
let positions: Vec<_> = lines
|
||||
let positions = lines
|
||||
.into_iter()
|
||||
.map(|index| {
|
||||
// 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);
|
||||
line_start + line.len_chars() - 1
|
||||
})
|
||||
.collect();
|
||||
.map(|pos| Range::new(pos, pos));
|
||||
|
||||
let selection = Selection::new(
|
||||
positions
|
||||
.iter()
|
||||
.copied()
|
||||
.map(|pos| Range::new(pos, pos))
|
||||
.collect(),
|
||||
0,
|
||||
);
|
||||
let selection = Selection::new(positions.collect(), 0);
|
||||
|
||||
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) {
|
||||
state.mode = Mode::Insert;
|
||||
|
||||
// calculate line numbers for each selection range
|
||||
let mut lines = state
|
||||
.selection
|
||||
.ranges()
|
||||
.iter()
|
||||
.map(|range| state.doc.char_to_line(range.head))
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
lines.sort();
|
||||
lines.dedup();
|
||||
let lines = selection_lines(state);
|
||||
|
||||
let positions: Vec<_> = lines
|
||||
.into_iter()
|
||||
|
|
Loading…
Add table
Reference in a new issue