Let o, O take counts for multiple cursors

pull/268/head^2
Gokul Soumya 3 years ago committed by Blaž Hrastnik
parent 20d6b202d5
commit 47d2e3aefa

@ -107,6 +107,7 @@ impl<'a> Context<'a> {
self.callbacks.push(callback); self.callbacks.push(callback);
} }
/// Returns 1 if no explicit count was provided
#[inline] #[inline]
pub fn count(&self) -> usize { pub fn count(&self) -> usize {
self.count.map_or(1, |v| v.get()) self.count.map_or(1, |v| v.get())
@ -1511,14 +1512,15 @@ fn open(cx: &mut Context, open: Open) {
Open::Above => line, Open::Above => line,
}; };
let index = doc.text().line_to_char(line).saturating_sub(1); // insert newlines after this index for both Above and Below variants
let linend_index = doc.text().line_to_char(line).saturating_sub(1);
// TODO: share logic with insert_newline for indentation // TODO: share logic with insert_newline for indentation
let indent_level = indent::suggested_indent_for_pos( let indent_level = indent::suggested_indent_for_pos(
doc.language_config(), doc.language_config(),
doc.syntax(), doc.syntax(),
text, text,
index, linend_index,
true, true,
); );
let indent = doc.indent_unit().repeat(indent_level); let indent = doc.indent_unit().repeat(indent_level);
@ -1527,22 +1529,21 @@ fn open(cx: &mut Context, open: Open) {
text.push_str(&indent); text.push_str(&indent);
let text = text.repeat(count); let text = text.repeat(count);
// calculate new selection range // calculate new selection ranges
let pos = if line == 0 { let pos = if line == 0 {
0 // Required since text will have a min len of 1 (\n) 0 // Required since text will have a min len of 1 (\n)
} else { } else {
index + offs + text.chars().count() offs + linend_index + 1
}; };
ranges.push(Range::new(pos, pos)); for i in 0..count {
ranges.push(Range::new(pos + i, pos + i));
}
offs += text.chars().count(); offs += text.chars().count();
(index, index, Some(text.into())) (linend_index, linend_index, Some(text.into()))
}); });
// TODO: count actually inserts "n" new lines and starts editing on all of them.
// TODO: append "count" newlines and modify cursors to those lines
transaction = transaction.with_selection(Selection::new(ranges, selection.primary_index())); transaction = transaction.with_selection(Selection::new(ranges, selection.primary_index()));
doc.apply(&transaction, view.id); doc.apply(&transaction, view.id);

Loading…
Cancel
Save