on master help sign in
Fixed primary cursor position calculation to use 1-width semantics.

This had a bunch of knock-on effects that were buggy, such as bracket match highlighting.

commitb0311f4fc246f370bb317ab0b1fcb89823347caa authorNathan Vegdahl <cessen@cessen.com> dateMon 19 Jul 2021, 09:25 (5 years ago) parent079d4ed
6 files changed+58−29
helix-core/src/selection.rs+9−2 helix-term/src/commands.rs+35−15 helix-term/src/ui/completion.rs+4−4 helix-term/src/ui/editor.rs+6−3 helix-view/src/editor.rs+3−4 helix-view/src/view.rs+1−1
helix-core/src/selection.rs+9−2
@@ -267,8 +267,15 @@ impl Selection {
267267 }
268268
269269 #[must_use]
270− pub fn cursor(&self) -> usize {
271− self.primary().head
270+ pub fn cursor(&self, text: RopeSlice) -> usize {
271+ let range = self.primary();
272+
273+ // For 1-width cursor semantics.
274+ if range.anchor < range.head {
275+ prev_grapheme_boundary(text, range.head)
276+ } else {
277+ range.head
278+ }
272279 }
273280
274281 /// Ensure selection containing only the primary selection.
helix-term/src/commands.rs+35−15
@@ -121,7 +121,7 @@ enum Align {
121121}
122122
123123fn align_view(doc: &Document, view: &mut View, align: Align) {
124− let pos = doc.selection(view.id).cursor();
124+ let pos = doc.selection(view.id).cursor(doc.text().slice(..));
125125 let line = doc.text().char_to_line(pos);
126126
127127 let relative = match align {
@@ -868,7 +868,10 @@ fn switch_to_lowercase(cx: &mut Context) {
868868fn scroll(cx: &mut Context, offset: usize, direction: Direction) {
869869 use Direction::*;
870870 let (view, doc) = current!(cx.editor);
871− let cursor = coords_at_pos(doc.text().slice(..), doc.selection(view.id).cursor());
871+ let cursor = coords_at_pos(
872+ doc.text().slice(..),
873+ doc.selection(view.id).cursor(doc.text().slice(..)),
874+ );
872875 let doc_last_line = doc.text().len_lines() - 1;
873876
874877 let last_line = view.last_line(doc);
@@ -1038,7 +1041,7 @@ fn split_selection_on_newline(cx: &mut Context) {
10381041fn search_impl(doc: &mut Document, view: &mut View, contents: &str, regex: &Regex, extend: bool) {
10391042 let text = doc.text();
10401043 let selection = doc.selection(view.id);
1041− let start = text.char_to_byte(selection.cursor());
1044+ let start = text.char_to_byte(selection.cursor(text.slice(..)));
10421045
10431046 // use find_at to find the next match after the cursor, loop around the end
10441047 // Careful, `Regex` uses `bytes` as offsets, not character indices!
@@ -2446,7 +2449,11 @@ fn goto_definition(cx: &mut Context) {
24462449
24472450 let offset_encoding = language_server.offset_encoding();
24482451
2449− let pos = pos_to_lsp_pos(doc.text(), doc.selection(view.id).cursor(), offset_encoding);
2452+ let pos = pos_to_lsp_pos(
2453+ doc.text(),
2454+ doc.selection(view.id).cursor(doc.text().slice(..)),
2455+ offset_encoding,
2456+ );
24502457
24512458 // TODO: handle fails
24522459 let future = language_server.goto_definition(doc.identifier(), pos, None);
@@ -2483,7 +2490,11 @@ fn goto_type_definition(cx: &mut Context) {
24832490
24842491 let offset_encoding = language_server.offset_encoding();
24852492
2486− let pos = pos_to_lsp_pos(doc.text(), doc.selection(view.id).cursor(), offset_encoding);
2493+ let pos = pos_to_lsp_pos(
2494+ doc.text(),
2495+ doc.selection(view.id).cursor(doc.text().slice(..)),
2496+ offset_encoding,
2497+ );
24872498
24882499 // TODO: handle fails
24892500 let future = language_server.goto_type_definition(doc.identifier(), pos, None);
@@ -2520,7 +2531,11 @@ fn goto_implementation(cx: &mut Context) {
25202531
25212532 let offset_encoding = language_server.offset_encoding();
25222533
2523− let pos = pos_to_lsp_pos(doc.text(), doc.selection(view.id).cursor(), offset_encoding);
2534+ let pos = pos_to_lsp_pos(
2535+ doc.text(),
2536+ doc.selection(view.id).cursor(doc.text().slice(..)),
2537+ offset_encoding,
2538+ );
25242539
25252540 // TODO: handle fails
25262541 let future = language_server.goto_implementation(doc.identifier(), pos, None);
@@ -2557,7 +2572,11 @@ fn goto_reference(cx: &mut Context) {
25572572
25582573 let offset_encoding = language_server.offset_encoding();
25592574
2560− let pos = pos_to_lsp_pos(doc.text(), doc.selection(view.id).cursor(), offset_encoding);
2575+ let pos = pos_to_lsp_pos(
2576+ doc.text(),
2577+ doc.selection(view.id).cursor(doc.text().slice(..)),
2578+ offset_encoding,
2579+ );
25612580
25622581 // TODO: handle fails
25632582 let future = language_server.goto_reference(doc.identifier(), pos, None);
@@ -2616,7 +2635,7 @@ fn goto_next_diag(cx: &mut Context) {
26162635 let editor = &mut cx.editor;
26172636 let (view, doc) = current!(editor);
26182637
2619− let cursor_pos = doc.selection(view.id).cursor();
2638+ let cursor_pos = doc.selection(view.id).cursor(doc.text().slice(..));
26202639 let diag = if let Some(diag) = doc
26212640 .diagnostics()
26222641 .iter()
@@ -2637,7 +2656,7 @@ fn goto_prev_diag(cx: &mut Context) {
26372656 let editor = &mut cx.editor;
26382657 let (view, doc) = current!(editor);
26392658
2640− let cursor_pos = doc.selection(view.id).cursor();
2659+ let cursor_pos = doc.selection(view.id).cursor(doc.text().slice(..));
26412660 let diag = if let Some(diag) = doc
26422661 .diagnostics()
26432662 .iter()
@@ -2665,7 +2684,7 @@ fn signature_help(cx: &mut Context) {
26652684
26662685 let pos = pos_to_lsp_pos(
26672686 doc.text(),
2668− doc.selection(view.id).cursor(),
2687+ doc.selection(view.id).cursor(doc.text().slice(..)),
26692688 language_server.offset_encoding(),
26702689 );
26712690
@@ -3403,13 +3422,14 @@ fn completion(cx: &mut Context) {
34033422 };
34043423
34053424 let offset_encoding = language_server.offset_encoding();
3425+ let cursor = doc.selection(view.id).cursor(doc.text().slice(..));
34063426
3407− let pos = pos_to_lsp_pos(doc.text(), doc.selection(view.id).cursor(), offset_encoding);
3427+ let pos = pos_to_lsp_pos(doc.text(), cursor, offset_encoding);
34083428
34093429 // TODO: handle fails
34103430 let future = language_server.completion(doc.identifier(), pos, None);
34113431
3412− let trigger_offset = doc.selection(view.id).cursor();
3432+ let trigger_offset = cursor;
34133433
34143434 cx.callback(
34153435 future,
@@ -3459,7 +3479,7 @@ fn hover(cx: &mut Context) {
34593479
34603480 let pos = pos_to_lsp_pos(
34613481 doc.text(),
3462− doc.selection(view.id).cursor(),
3482+ doc.selection(view.id).cursor(doc.text().slice(..)),
34633483 language_server.offset_encoding(),
34643484 );
34653485
@@ -3525,7 +3545,7 @@ fn match_brackets(cx: &mut Context) {
35253545 let (view, doc) = current!(cx.editor);
35263546
35273547 if let Some(syntax) = doc.syntax() {
3528− let pos = doc.selection(view.id).cursor();
3548+ let pos = doc.selection(view.id).cursor(doc.text().slice(..));
35293549 if let Some(pos) = match_brackets::find(syntax, doc.text(), pos) {
35303550 let selection = Selection::point(pos);
35313551 doc.set_selection(view.id, selection);
@@ -3629,7 +3649,7 @@ fn align_view_bottom(cx: &mut Context) {
36293649
36303650fn align_view_middle(cx: &mut Context) {
36313651 let (view, doc) = current!(cx.editor);
3632− let pos = doc.selection(view.id).cursor();
3652+ let pos = doc.selection(view.id).cursor(doc.text().slice(..));
36333653 let pos = coords_at_pos(doc.text().slice(..), pos);
36343654
36353655 const OFFSET: usize = 7; // gutters
helix-term/src/ui/completion.rs+4−4
@@ -86,7 +86,7 @@ impl Completion {
8686 let item = item.unwrap();
8787
8888 // if more text was entered, remove it
89− let cursor = doc.selection(view.id).cursor();
89+ let cursor = doc.selection(view.id).cursor(doc.text().slice(..));
9090 if trigger_offset < cursor {
9191 let remove = Transaction::change(
9292 doc.text(),
@@ -109,7 +109,7 @@ impl Completion {
109109 )
110110 } else {
111111 let text = item.insert_text.as_ref().unwrap_or(&item.label);
112− let cursor = doc.selection(view.id).cursor();
112+ let cursor = doc.selection(view.id).cursor(doc.text().slice(..));
113113 Transaction::change(
114114 doc.text(),
115115 vec![(cursor, cursor, Some(text.as_str().into()))].into_iter(),
@@ -155,7 +155,7 @@ impl Completion {
155155 // TODO: hooks should get processed immediately so maybe do it after select!(), before
156156 // looping?
157157
158− let cursor = doc.selection(view.id).cursor();
158+ let cursor = doc.selection(view.id).cursor(doc.text().slice(..));
159159 if self.trigger_offset <= cursor {
160160 let fragment = doc.text().slice(self.trigger_offset..cursor);
161161 let text = Cow::from(fragment);
@@ -212,7 +212,7 @@ impl Component for Completion {
212212 .language()
213213 .and_then(|scope| scope.strip_prefix("source."))
214214 .unwrap_or("");
215− let cursor_pos = doc.selection(view.id).cursor();
215+ let cursor_pos = doc.selection(view.id).cursor(doc.text().slice(..));
216216 let cursor_pos = (helix_core::coords_at_pos(doc.text().slice(..), cursor_pos).row
217217 - view.first_line) as u16;
218218
helix-term/src/ui/editor.rs+6−3
@@ -398,7 +398,7 @@ impl EditorView {
398398 // TODO: set cursor position for IME
399399 if let Some(syntax) = doc.syntax() {
400400 use helix_core::match_brackets;
401− let pos = doc.selection(view.id).cursor();
401+ let pos = doc.selection(view.id).cursor(doc.text().slice(..));
402402 let pos = match_brackets::find(syntax, doc.text(), pos)
403403 .and_then(|pos| view.screen_coords_at_pos(doc, text, pos));
404404
@@ -443,7 +443,7 @@ impl EditorView {
443443 widgets::{Paragraph, Widget},
444444 };
445445
446− let cursor = doc.selection(view.id).cursor();
446+ let cursor = doc.selection(view.id).cursor(doc.text().slice(..));
447447
448448 let diagnostics = doc.diagnostics().iter().filter(|diagnostic| {
449449 diagnostic.range.start <= cursor && diagnostic.range.end >= cursor
@@ -555,7 +555,10 @@ impl EditorView {
555555 // _ => "indent:ERROR",
556556 // };
557557 let position_info = {
558− let pos = coords_at_pos(doc.text().slice(..), doc.selection(view.id).cursor());
558+ let pos = coords_at_pos(
559+ doc.text().slice(..),
560+ doc.selection(view.id).cursor(doc.text().slice(..)),
561+ );
559562 format!("{}:{}", pos.row + 1, pos.col + 1) // convert to 1-indexing
560563 };
561564
helix-view/src/editor.rs+3−4
@@ -141,12 +141,11 @@ impl Editor {
141141 let (view, doc) = current!(self);
142142
143143 // initialize selection for view
144− let selection = doc
145− .selections
144+ doc.selections
146145 .entry(view.id)
147146 .or_insert_with(|| Selection::point(0));
148147 // TODO: reuse align_view
149− let pos = selection.cursor();
148+ let pos = doc.selection(view.id).cursor(doc.text().slice(..));
150149 let line = doc.text().char_to_line(pos);
151150 view.first_line = line.saturating_sub(view.area.height as usize / 2);
152151
@@ -296,7 +295,7 @@ impl Editor {
296295 const OFFSET: u16 = 7; // 1 diagnostic + 5 linenr + 1 gutter
297296 let view = view!(self);
298297 let doc = &self.documents[view.doc];
299− let cursor = doc.selection(view.id).cursor();
298+ let cursor = doc.selection(view.id).cursor(doc.text().slice(..));
300299 if let Some(mut pos) = view.screen_coords_at_pos(doc, doc.text().slice(..), cursor) {
301300 pos.col += view.area.x as usize + OFFSET as usize;
302301 pos.row += view.area.y as usize;
helix-view/src/view.rs+1−1
@@ -84,7 +84,7 @@ impl View {
8484 }
8585
8686 pub fn ensure_cursor_in_view(&mut self, doc: &Document) {
87− let cursor = doc.selection(self.id).cursor();
87+ let cursor = doc.selection(self.id).cursor(doc.text().slice(..));
8888 let pos = coords_at_pos(doc.text().slice(..), cursor);
8989 let line = pos.row;
9090 let col = pos.col;