Fix cursor position bugs related to o and O

- `O` at the beginning of file didn't move cursor
- `o` and `O` messed up cursor position with multiple cursors

Fixes #127
imgbot
Gokul Soumya 4 years ago committed by Blaž Hrastnik
parent a364d6c383
commit 20d6b202d5

@ -1495,13 +1495,13 @@ fn open(cx: &mut Context, open: Open) {
enter_insert_mode(doc); enter_insert_mode(doc);
let text = doc.text().slice(..); let text = doc.text().slice(..);
let contents = doc.text();
let selection = doc.selection(view.id); let selection = doc.selection(view.id);
let mut ranges = SmallVec::with_capacity(selection.len()); let mut ranges = SmallVec::with_capacity(selection.len());
let mut offs = 0;
let changes: Vec<Change> = selection let mut transaction = Transaction::change_by_selection(contents, selection, |range| {
.iter()
.map(|range| {
let line = text.char_to_line(range.head); let line = text.char_to_line(range.head);
let line = match open { let line = match open {
@ -1528,20 +1528,22 @@ fn open(cx: &mut Context, open: Open) {
let text = text.repeat(count); let text = text.repeat(count);
// calculate new selection range // calculate new selection range
let pos = index + text.chars().count(); let pos = if line == 0 {
0 // Required since text will have a min len of 1 (\n)
} else {
index + offs + text.chars().count()
};
ranges.push(Range::new(pos, pos)); ranges.push(Range::new(pos, pos));
offs += text.chars().count();
(index, index, Some(text.into())) (index, index, Some(text.into()))
}) });
.collect();
// TODO: count actually inserts "n" new lines and starts editing on all of them. // TODO: count actually inserts "n" new lines and starts editing on all of them.
// TODO: append "count" newlines and modify cursors to those lines // TODO: append "count" newlines and modify cursors to those lines
let selection = Selection::new(ranges, 0); transaction = transaction.with_selection(Selection::new(ranges, selection.primary_index()));
let transaction =
Transaction::change(doc.text(), changes.into_iter()).with_selection(selection);
doc.apply(&transaction, view.id); doc.apply(&transaction, view.id);
} }

Loading…
Cancel
Save