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.
pull/376/head
Nathan Vegdahl 3 years ago
parent 079d4ed86d
commit b0311f4fc2

@ -267,8 +267,15 @@ impl Selection {
} }
#[must_use] #[must_use]
pub fn cursor(&self) -> usize { pub fn cursor(&self, text: RopeSlice) -> usize {
self.primary().head let range = self.primary();
// For 1-width cursor semantics.
if range.anchor < range.head {
prev_grapheme_boundary(text, range.head)
} else {
range.head
}
} }
/// Ensure selection containing only the primary selection. /// Ensure selection containing only the primary selection.

@ -121,7 +121,7 @@ enum Align {
} }
fn align_view(doc: &Document, view: &mut View, align: Align) { fn align_view(doc: &Document, view: &mut View, align: Align) {
let pos = doc.selection(view.id).cursor(); let pos = doc.selection(view.id).cursor(doc.text().slice(..));
let line = doc.text().char_to_line(pos); let line = doc.text().char_to_line(pos);
let relative = match align { let relative = match align {
@ -868,7 +868,10 @@ fn switch_to_lowercase(cx: &mut Context) {
fn scroll(cx: &mut Context, offset: usize, direction: Direction) { fn scroll(cx: &mut Context, offset: usize, direction: Direction) {
use Direction::*; use Direction::*;
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
let cursor = coords_at_pos(doc.text().slice(..), doc.selection(view.id).cursor()); let cursor = coords_at_pos(
doc.text().slice(..),
doc.selection(view.id).cursor(doc.text().slice(..)),
);
let doc_last_line = doc.text().len_lines() - 1; let doc_last_line = doc.text().len_lines() - 1;
let last_line = view.last_line(doc); let last_line = view.last_line(doc);
@ -1038,7 +1041,7 @@ fn split_selection_on_newline(cx: &mut Context) {
fn search_impl(doc: &mut Document, view: &mut View, contents: &str, regex: &Regex, extend: bool) { fn search_impl(doc: &mut Document, view: &mut View, contents: &str, regex: &Regex, extend: bool) {
let text = doc.text(); let text = doc.text();
let selection = doc.selection(view.id); let selection = doc.selection(view.id);
let start = text.char_to_byte(selection.cursor()); let start = text.char_to_byte(selection.cursor(text.slice(..)));
// use find_at to find the next match after the cursor, loop around the end // use find_at to find the next match after the cursor, loop around the end
// Careful, `Regex` uses `bytes` as offsets, not character indices! // Careful, `Regex` uses `bytes` as offsets, not character indices!
@ -2446,7 +2449,11 @@ fn goto_definition(cx: &mut Context) {
let offset_encoding = language_server.offset_encoding(); let offset_encoding = language_server.offset_encoding();
let pos = pos_to_lsp_pos(doc.text(), doc.selection(view.id).cursor(), offset_encoding); let pos = pos_to_lsp_pos(
doc.text(),
doc.selection(view.id).cursor(doc.text().slice(..)),
offset_encoding,
);
// TODO: handle fails // TODO: handle fails
let future = language_server.goto_definition(doc.identifier(), pos, None); let future = language_server.goto_definition(doc.identifier(), pos, None);
@ -2483,7 +2490,11 @@ fn goto_type_definition(cx: &mut Context) {
let offset_encoding = language_server.offset_encoding(); let offset_encoding = language_server.offset_encoding();
let pos = pos_to_lsp_pos(doc.text(), doc.selection(view.id).cursor(), offset_encoding); let pos = pos_to_lsp_pos(
doc.text(),
doc.selection(view.id).cursor(doc.text().slice(..)),
offset_encoding,
);
// TODO: handle fails // TODO: handle fails
let future = language_server.goto_type_definition(doc.identifier(), pos, None); let future = language_server.goto_type_definition(doc.identifier(), pos, None);
@ -2520,7 +2531,11 @@ fn goto_implementation(cx: &mut Context) {
let offset_encoding = language_server.offset_encoding(); let offset_encoding = language_server.offset_encoding();
let pos = pos_to_lsp_pos(doc.text(), doc.selection(view.id).cursor(), offset_encoding); let pos = pos_to_lsp_pos(
doc.text(),
doc.selection(view.id).cursor(doc.text().slice(..)),
offset_encoding,
);
// TODO: handle fails // TODO: handle fails
let future = language_server.goto_implementation(doc.identifier(), pos, None); let future = language_server.goto_implementation(doc.identifier(), pos, None);
@ -2557,7 +2572,11 @@ fn goto_reference(cx: &mut Context) {
let offset_encoding = language_server.offset_encoding(); let offset_encoding = language_server.offset_encoding();
let pos = pos_to_lsp_pos(doc.text(), doc.selection(view.id).cursor(), offset_encoding); let pos = pos_to_lsp_pos(
doc.text(),
doc.selection(view.id).cursor(doc.text().slice(..)),
offset_encoding,
);
// TODO: handle fails // TODO: handle fails
let future = language_server.goto_reference(doc.identifier(), pos, None); let future = language_server.goto_reference(doc.identifier(), pos, None);
@ -2616,7 +2635,7 @@ fn goto_next_diag(cx: &mut Context) {
let editor = &mut cx.editor; let editor = &mut cx.editor;
let (view, doc) = current!(editor); let (view, doc) = current!(editor);
let cursor_pos = doc.selection(view.id).cursor(); let cursor_pos = doc.selection(view.id).cursor(doc.text().slice(..));
let diag = if let Some(diag) = doc let diag = if let Some(diag) = doc
.diagnostics() .diagnostics()
.iter() .iter()
@ -2637,7 +2656,7 @@ fn goto_prev_diag(cx: &mut Context) {
let editor = &mut cx.editor; let editor = &mut cx.editor;
let (view, doc) = current!(editor); let (view, doc) = current!(editor);
let cursor_pos = doc.selection(view.id).cursor(); let cursor_pos = doc.selection(view.id).cursor(doc.text().slice(..));
let diag = if let Some(diag) = doc let diag = if let Some(diag) = doc
.diagnostics() .diagnostics()
.iter() .iter()
@ -2665,7 +2684,7 @@ fn signature_help(cx: &mut Context) {
let pos = pos_to_lsp_pos( let pos = pos_to_lsp_pos(
doc.text(), doc.text(),
doc.selection(view.id).cursor(), doc.selection(view.id).cursor(doc.text().slice(..)),
language_server.offset_encoding(), language_server.offset_encoding(),
); );
@ -3403,13 +3422,14 @@ fn completion(cx: &mut Context) {
}; };
let offset_encoding = language_server.offset_encoding(); let offset_encoding = language_server.offset_encoding();
let cursor = doc.selection(view.id).cursor(doc.text().slice(..));
let pos = pos_to_lsp_pos(doc.text(), doc.selection(view.id).cursor(), offset_encoding); let pos = pos_to_lsp_pos(doc.text(), cursor, offset_encoding);
// TODO: handle fails // TODO: handle fails
let future = language_server.completion(doc.identifier(), pos, None); let future = language_server.completion(doc.identifier(), pos, None);
let trigger_offset = doc.selection(view.id).cursor(); let trigger_offset = cursor;
cx.callback( cx.callback(
future, future,
@ -3459,7 +3479,7 @@ fn hover(cx: &mut Context) {
let pos = pos_to_lsp_pos( let pos = pos_to_lsp_pos(
doc.text(), doc.text(),
doc.selection(view.id).cursor(), doc.selection(view.id).cursor(doc.text().slice(..)),
language_server.offset_encoding(), language_server.offset_encoding(),
); );
@ -3525,7 +3545,7 @@ fn match_brackets(cx: &mut Context) {
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
if let Some(syntax) = doc.syntax() { if let Some(syntax) = doc.syntax() {
let pos = doc.selection(view.id).cursor(); let pos = doc.selection(view.id).cursor(doc.text().slice(..));
if let Some(pos) = match_brackets::find(syntax, doc.text(), pos) { if let Some(pos) = match_brackets::find(syntax, doc.text(), pos) {
let selection = Selection::point(pos); let selection = Selection::point(pos);
doc.set_selection(view.id, selection); doc.set_selection(view.id, selection);
@ -3629,7 +3649,7 @@ fn align_view_bottom(cx: &mut Context) {
fn align_view_middle(cx: &mut Context) { fn align_view_middle(cx: &mut Context) {
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
let pos = doc.selection(view.id).cursor(); let pos = doc.selection(view.id).cursor(doc.text().slice(..));
let pos = coords_at_pos(doc.text().slice(..), pos); let pos = coords_at_pos(doc.text().slice(..), pos);
const OFFSET: usize = 7; // gutters const OFFSET: usize = 7; // gutters

@ -86,7 +86,7 @@ impl Completion {
let item = item.unwrap(); let item = item.unwrap();
// if more text was entered, remove it // if more text was entered, remove it
let cursor = doc.selection(view.id).cursor(); let cursor = doc.selection(view.id).cursor(doc.text().slice(..));
if trigger_offset < cursor { if trigger_offset < cursor {
let remove = Transaction::change( let remove = Transaction::change(
doc.text(), doc.text(),
@ -109,7 +109,7 @@ impl Completion {
) )
} else { } else {
let text = item.insert_text.as_ref().unwrap_or(&item.label); let text = item.insert_text.as_ref().unwrap_or(&item.label);
let cursor = doc.selection(view.id).cursor(); let cursor = doc.selection(view.id).cursor(doc.text().slice(..));
Transaction::change( Transaction::change(
doc.text(), doc.text(),
vec![(cursor, cursor, Some(text.as_str().into()))].into_iter(), vec![(cursor, cursor, Some(text.as_str().into()))].into_iter(),
@ -155,7 +155,7 @@ impl Completion {
// TODO: hooks should get processed immediately so maybe do it after select!(), before // TODO: hooks should get processed immediately so maybe do it after select!(), before
// looping? // looping?
let cursor = doc.selection(view.id).cursor(); let cursor = doc.selection(view.id).cursor(doc.text().slice(..));
if self.trigger_offset <= cursor { if self.trigger_offset <= cursor {
let fragment = doc.text().slice(self.trigger_offset..cursor); let fragment = doc.text().slice(self.trigger_offset..cursor);
let text = Cow::from(fragment); let text = Cow::from(fragment);
@ -212,7 +212,7 @@ impl Component for Completion {
.language() .language()
.and_then(|scope| scope.strip_prefix("source.")) .and_then(|scope| scope.strip_prefix("source."))
.unwrap_or(""); .unwrap_or("");
let cursor_pos = doc.selection(view.id).cursor(); let cursor_pos = doc.selection(view.id).cursor(doc.text().slice(..));
let cursor_pos = (helix_core::coords_at_pos(doc.text().slice(..), cursor_pos).row let cursor_pos = (helix_core::coords_at_pos(doc.text().slice(..), cursor_pos).row
- view.first_line) as u16; - view.first_line) as u16;

@ -398,7 +398,7 @@ impl EditorView {
// TODO: set cursor position for IME // TODO: set cursor position for IME
if let Some(syntax) = doc.syntax() { if let Some(syntax) = doc.syntax() {
use helix_core::match_brackets; use helix_core::match_brackets;
let pos = doc.selection(view.id).cursor(); let pos = doc.selection(view.id).cursor(doc.text().slice(..));
let pos = match_brackets::find(syntax, doc.text(), pos) let pos = match_brackets::find(syntax, doc.text(), pos)
.and_then(|pos| view.screen_coords_at_pos(doc, text, pos)); .and_then(|pos| view.screen_coords_at_pos(doc, text, pos));
@ -443,7 +443,7 @@ impl EditorView {
widgets::{Paragraph, Widget}, widgets::{Paragraph, Widget},
}; };
let cursor = doc.selection(view.id).cursor(); let cursor = doc.selection(view.id).cursor(doc.text().slice(..));
let diagnostics = doc.diagnostics().iter().filter(|diagnostic| { let diagnostics = doc.diagnostics().iter().filter(|diagnostic| {
diagnostic.range.start <= cursor && diagnostic.range.end >= cursor diagnostic.range.start <= cursor && diagnostic.range.end >= cursor
@ -555,7 +555,10 @@ impl EditorView {
// _ => "indent:ERROR", // _ => "indent:ERROR",
// }; // };
let position_info = { let position_info = {
let pos = coords_at_pos(doc.text().slice(..), doc.selection(view.id).cursor()); let pos = coords_at_pos(
doc.text().slice(..),
doc.selection(view.id).cursor(doc.text().slice(..)),
);
format!("{}:{}", pos.row + 1, pos.col + 1) // convert to 1-indexing format!("{}:{}", pos.row + 1, pos.col + 1) // convert to 1-indexing
}; };

@ -141,12 +141,11 @@ impl Editor {
let (view, doc) = current!(self); let (view, doc) = current!(self);
// initialize selection for view // initialize selection for view
let selection = doc doc.selections
.selections
.entry(view.id) .entry(view.id)
.or_insert_with(|| Selection::point(0)); .or_insert_with(|| Selection::point(0));
// TODO: reuse align_view // TODO: reuse align_view
let pos = selection.cursor(); let pos = doc.selection(view.id).cursor(doc.text().slice(..));
let line = doc.text().char_to_line(pos); let line = doc.text().char_to_line(pos);
view.first_line = line.saturating_sub(view.area.height as usize / 2); view.first_line = line.saturating_sub(view.area.height as usize / 2);
@ -296,7 +295,7 @@ impl Editor {
const OFFSET: u16 = 7; // 1 diagnostic + 5 linenr + 1 gutter const OFFSET: u16 = 7; // 1 diagnostic + 5 linenr + 1 gutter
let view = view!(self); let view = view!(self);
let doc = &self.documents[view.doc]; let doc = &self.documents[view.doc];
let cursor = doc.selection(view.id).cursor(); let cursor = doc.selection(view.id).cursor(doc.text().slice(..));
if let Some(mut pos) = view.screen_coords_at_pos(doc, doc.text().slice(..), cursor) { if let Some(mut pos) = view.screen_coords_at_pos(doc, doc.text().slice(..), cursor) {
pos.col += view.area.x as usize + OFFSET as usize; pos.col += view.area.x as usize + OFFSET as usize;
pos.row += view.area.y as usize; pos.row += view.area.y as usize;

@ -84,7 +84,7 @@ impl View {
} }
pub fn ensure_cursor_in_view(&mut self, doc: &Document) { pub fn ensure_cursor_in_view(&mut self, doc: &Document) {
let cursor = doc.selection(self.id).cursor(); let cursor = doc.selection(self.id).cursor(doc.text().slice(..));
let pos = coords_at_pos(doc.text().slice(..), cursor); let pos = coords_at_pos(doc.text().slice(..), cursor);
let line = pos.row; let line = pos.row;
let col = pos.col; let col = pos.col;

Loading…
Cancel
Save