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.
This commit is contained in:
parent
079d4ed86d
commit
b0311f4fc2
6 changed files with 58 additions and 29 deletions
helix-core/src
helix-term/src
helix-view/src
|
@ -267,8 +267,15 @@ impl Selection {
|
|||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn cursor(&self) -> usize {
|
||||
self.primary().head
|
||||
pub fn cursor(&self, text: RopeSlice) -> usize {
|
||||
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.
|
||||
|
|
|
@ -121,7 +121,7 @@ enum 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 relative = match align {
|
||||
|
@ -868,7 +868,10 @@ fn switch_to_lowercase(cx: &mut Context) {
|
|||
fn scroll(cx: &mut Context, offset: usize, direction: Direction) {
|
||||
use Direction::*;
|
||||
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 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) {
|
||||
let text = doc.text();
|
||||
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
|
||||
// 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 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
|
||||
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 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
|
||||
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 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
|
||||
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 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
|
||||
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 (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
|
||||
.diagnostics()
|
||||
.iter()
|
||||
|
@ -2637,7 +2656,7 @@ fn goto_prev_diag(cx: &mut Context) {
|
|||
let editor = &mut cx.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
|
||||
.diagnostics()
|
||||
.iter()
|
||||
|
@ -2665,7 +2684,7 @@ fn signature_help(cx: &mut Context) {
|
|||
|
||||
let pos = pos_to_lsp_pos(
|
||||
doc.text(),
|
||||
doc.selection(view.id).cursor(),
|
||||
doc.selection(view.id).cursor(doc.text().slice(..)),
|
||||
language_server.offset_encoding(),
|
||||
);
|
||||
|
||||
|
@ -3403,13 +3422,14 @@ fn completion(cx: &mut Context) {
|
|||
};
|
||||
|
||||
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
|
||||
let future = language_server.completion(doc.identifier(), pos, None);
|
||||
|
||||
let trigger_offset = doc.selection(view.id).cursor();
|
||||
let trigger_offset = cursor;
|
||||
|
||||
cx.callback(
|
||||
future,
|
||||
|
@ -3459,7 +3479,7 @@ fn hover(cx: &mut Context) {
|
|||
|
||||
let pos = pos_to_lsp_pos(
|
||||
doc.text(),
|
||||
doc.selection(view.id).cursor(),
|
||||
doc.selection(view.id).cursor(doc.text().slice(..)),
|
||||
language_server.offset_encoding(),
|
||||
);
|
||||
|
||||
|
@ -3525,7 +3545,7 @@ fn match_brackets(cx: &mut Context) {
|
|||
let (view, doc) = current!(cx.editor);
|
||||
|
||||
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) {
|
||||
let selection = Selection::point(pos);
|
||||
doc.set_selection(view.id, selection);
|
||||
|
@ -3629,7 +3649,7 @@ fn align_view_bottom(cx: &mut Context) {
|
|||
|
||||
fn align_view_middle(cx: &mut Context) {
|
||||
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);
|
||||
|
||||
const OFFSET: usize = 7; // gutters
|
||||
|
|
|
@ -86,7 +86,7 @@ impl Completion {
|
|||
let item = item.unwrap();
|
||||
|
||||
// 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 {
|
||||
let remove = Transaction::change(
|
||||
doc.text(),
|
||||
|
@ -109,7 +109,7 @@ impl Completion {
|
|||
)
|
||||
} else {
|
||||
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(
|
||||
doc.text(),
|
||||
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
|
||||
// looping?
|
||||
|
||||
let cursor = doc.selection(view.id).cursor();
|
||||
let cursor = doc.selection(view.id).cursor(doc.text().slice(..));
|
||||
if self.trigger_offset <= cursor {
|
||||
let fragment = doc.text().slice(self.trigger_offset..cursor);
|
||||
let text = Cow::from(fragment);
|
||||
|
@ -212,7 +212,7 @@ impl Component for Completion {
|
|||
.language()
|
||||
.and_then(|scope| scope.strip_prefix("source."))
|
||||
.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
|
||||
- view.first_line) as u16;
|
||||
|
||||
|
|
|
@ -398,7 +398,7 @@ impl EditorView {
|
|||
// TODO: set cursor position for IME
|
||||
if let Some(syntax) = doc.syntax() {
|
||||
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)
|
||||
.and_then(|pos| view.screen_coords_at_pos(doc, text, pos));
|
||||
|
||||
|
@ -443,7 +443,7 @@ impl EditorView {
|
|||
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| {
|
||||
diagnostic.range.start <= cursor && diagnostic.range.end >= cursor
|
||||
|
@ -555,7 +555,10 @@ impl EditorView {
|
|||
// _ => "indent:ERROR",
|
||||
// };
|
||||
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
|
||||
};
|
||||
|
||||
|
|
|
@ -141,12 +141,11 @@ impl Editor {
|
|||
let (view, doc) = current!(self);
|
||||
|
||||
// initialize selection for view
|
||||
let selection = doc
|
||||
.selections
|
||||
doc.selections
|
||||
.entry(view.id)
|
||||
.or_insert_with(|| Selection::point(0));
|
||||
// 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);
|
||||
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
|
||||
let view = view!(self);
|
||||
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) {
|
||||
pos.col += view.area.x as usize + OFFSET 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) {
|
||||
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 line = pos.row;
|
||||
let col = pos.col;
|
||||
|
|
Loading…
Add table
Reference in a new issue