aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNathan Vegdahl2021-07-20 18:58:56 +0000
committerNathan Vegdahl2021-07-20 18:58:56 +0000
commit1c6b5581f01371a00dc7f6f6e1720ad8af61ec7a (patch)
treec1758df277987266251500fc5c864f1def5c715c
parente8a3980e464a9c98c3f76cada6c46a66498dc2bf (diff)
Fix various bugs related to goto-end-of-line command.
This also fixes a bug with `Selection::normalize()`, that could result in an out-of-bounds primary index.
-rw-r--r--helix-core/src/selection.rs6
-rw-r--r--helix-term/src/commands.rs29
2 files changed, 26 insertions, 9 deletions
diff --git a/helix-core/src/selection.rs b/helix-core/src/selection.rs
index f1625f99..c08f504d 100644
--- a/helix-core/src/selection.rs
+++ b/helix-core/src/selection.rs
@@ -357,14 +357,14 @@ impl Selection {
let mut prev_i = 0;
for i in 1..self.ranges.len() {
if self.ranges[prev_i].overlaps(&self.ranges[i]) {
- if i == self.primary_index {
- self.primary_index = prev_i;
- }
self.ranges[prev_i] = self.ranges[prev_i].merge(self.ranges[i]);
} else {
prev_i += 1;
self.ranges[prev_i] = self.ranges[i];
}
+ if i == self.primary_index {
+ self.primary_index = prev_i;
+ }
}
self.ranges.truncate(prev_i + 1);
diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs
index 61c5096d..a155e19e 100644
--- a/helix-term/src/commands.rs
+++ b/helix-term/src/commands.rs
@@ -396,11 +396,19 @@ fn goto_line_end(cx: &mut Context) {
view.id,
doc.selection(view.id).clone().transform(|range| {
let text = doc.text().slice(..);
- let line = text.char_to_line(range.head);
- let pos = line_end_char_index(&text, line);
- let pos = graphemes::nth_prev_grapheme_boundary(text, pos, 1);
- let pos = range.head.max(pos).max(text.line_to_char(line));
+ let head = if range.anchor < range.head {
+ graphemes::prev_grapheme_boundary(text, range.head)
+ } else {
+ range.head
+ };
+ let line = text.char_to_line(head);
+
+ let mut pos = line_end_char_index(&text, line);
+ if doc.mode != Mode::Select {
+ pos = graphemes::prev_grapheme_boundary(text, pos);
+ }
+ pos = head.max(pos).max(text.line_to_char(line));
range.put(text, pos, doc.mode == Mode::Select)
}),
@@ -414,9 +422,18 @@ fn goto_line_end_newline(cx: &mut Context) {
view.id,
doc.selection(view.id).clone().transform(|range| {
let text = doc.text().slice(..);
- let line = text.char_to_line(range.head);
- let pos = line_end_char_index(&text, line);
+ let head = if range.anchor < range.head {
+ graphemes::prev_grapheme_boundary(text, range.head)
+ } else {
+ range.head
+ };
+ let line = text.char_to_line(head);
+
+ let mut pos = text.line_to_char((line + 1).min(text.len_lines()));
+ if doc.mode != Mode::Select {
+ pos = graphemes::prev_grapheme_boundary(text, pos);
+ }
range.put(text, pos, doc.mode == Mode::Select)
}),
);