on master help sign in
Calculate the line that the range head is on correctly.
commit5841954f58702ba2eabc17d96f0bf167cb13af24 authorNathan Vegdahl <cessen@cessen.com> dateThu 22 Jul 2021, 11:17 (5 years ago) parent673338b
2 files changed+24−23
helix-core/src/selection.rs+12 helix-term/src/commands.rs+12−23
helix-core/src/selection.rs+12
@@ -73,6 +73,18 @@ impl Range {
7373 std::cmp::max(self.anchor, self.head)
7474 }
7575
76+ /// The line number that the head is on (using 1-width semantics).
77+ #[inline]
78+ #[must_use]
79+ pub fn head_line(&self, text: RopeSlice) -> usize {
80+ let head = if self.anchor < self.head {
81+ prev_grapheme_boundary(text, self.head)
82+ } else {
83+ self.head
84+ };
85+ text.char_to_line(head)
86+ }
87+
7688 /// The (inclusive) range of lines that the range overlaps.
7789 #[inline]
7890 #[must_use]
helix-term/src/commands.rs+12−23
@@ -396,19 +396,14 @@ fn goto_line_end(cx: &mut Context) {
396396 view.id,
397397 doc.selection(view.id).clone().transform(|range| {
398398 let text = doc.text().slice(..);
399−
400− let head = if range.anchor < range.head {
401− graphemes::prev_grapheme_boundary(text, range.head)
402− } else {
403− range.head
404− };
405− let line = text.char_to_line(head);
399+ let line = range.head_line(text);
406400
407401 let mut pos = line_end_char_index(&text, line);
408402 if doc.mode != Mode::Select {
409403 pos = graphemes::prev_grapheme_boundary(text, pos);
410404 }
411− pos = head.max(pos).max(text.line_to_char(line));
405+
406+ pos = range.head.max(pos).max(text.line_to_char(line));
412407
413408 range.put(text, pos, doc.mode == Mode::Select)
414409 }),
@@ -422,13 +417,7 @@ fn goto_line_end_newline(cx: &mut Context) {
422417 view.id,
423418 doc.selection(view.id).clone().transform(|range| {
424419 let text = doc.text().slice(..);
425−
426− let head = if range.anchor < range.head {
427− graphemes::prev_grapheme_boundary(text, range.head)
428− } else {
429− range.head
430− };
431− let line = text.char_to_line(head);
420+ let line = range.head_line(text);
432421
433422 let mut pos = text.line_to_char((line + 1).min(text.len_lines()));
434423 if doc.mode != Mode::Select {
@@ -445,7 +434,7 @@ fn goto_line_start(cx: &mut Context) {
445434 view.id,
446435 doc.selection(view.id).clone().transform(|range| {
447436 let text = doc.text().slice(..);
448− let line = text.char_to_line(range.head);
437+ let line = range.head_line(text);
449438
450439 // adjust to start of the line
451440 let pos = text.line_to_char(line);
@@ -460,10 +449,10 @@ fn goto_first_nonwhitespace(cx: &mut Context) {
460449 view.id,
461450 doc.selection(view.id).clone().transform(|range| {
462451 let text = doc.text().slice(..);
463− let line_idx = text.char_to_line(range.head);
452+ let line = range.head_line(text);
464453
465− if let Some(pos) = find_first_non_whitespace_char(text.line(line_idx)) {
466− let pos = pos + text.line_to_char(line_idx);
454+ if let Some(pos) = find_first_non_whitespace_char(text.line(line)) {
455+ let pos = pos + text.line_to_char(line);
467456 range.put(text, pos, doc.mode == Mode::Select)
468457 } else {
469458 range
@@ -2212,9 +2201,9 @@ fn append_to_line(cx: &mut Context) {
22122201 doc.set_selection(
22132202 view.id,
22142203 doc.selection(view.id).clone().transform(|range| {
2215− let text = &doc.text().slice(..);
2216− let line = text.char_to_line(range.head);
2217− let pos = line_end_char_index(text, line);
2204+ let text = doc.text().slice(..);
2205+ let line = range.head_line(text);
2206+ let pos = line_end_char_index(&text, line);
22182207 Range::new(pos, pos)
22192208 }),
22202209 );
@@ -2274,7 +2263,7 @@ fn open(cx: &mut Context, open: Open) {
22742263 let mut offs = 0;
22752264
22762265 let mut transaction = Transaction::change_by_selection(contents, selection, |range| {
2277− let line = text.char_to_line(range.head);
2266+ let line = range.head_line(text);
22782267
22792268 let line = match open {
22802269 // adjust position to the end of the line (next line - 1)