Skip to content

Commit

Permalink
Let o, O take counts for multiple cursors
Browse files Browse the repository at this point in the history
  • Loading branch information
sudormrfbin authored and archseer committed Jun 17, 2021
1 parent 20d6b20 commit 47d2e3a
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ impl<'a> Context<'a> {
self.callbacks.push(callback);
}

/// Returns 1 if no explicit count was provided
#[inline]
pub fn count(&self) -> usize {
self.count.map_or(1, |v| v.get())
Expand Down Expand Up @@ -1511,14 +1512,15 @@ fn open(cx: &mut Context, open: Open) {
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
let indent_level = indent::suggested_indent_for_pos(
doc.language_config(),
doc.syntax(),
text,
index,
linend_index,
true,
);
let indent = doc.indent_unit().repeat(indent_level);
Expand All @@ -1527,22 +1529,21 @@ fn open(cx: &mut Context, open: Open) {
text.push_str(&indent);
let text = text.repeat(count);

// calculate new selection range
// calculate new selection ranges
let pos = if line == 0 {
0 // Required since text will have a min len of 1 (\n)
} 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();

(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()));

doc.apply(&transaction, view.id);
Expand Down

0 comments on commit 47d2e3a

Please sign in to comment.