From 8b6ae4eaae6415cf72db65890f4b7621c15f60d1 Mon Sep 17 00:00:00 2001 From: A-Walrus Date: Tue, 13 Jun 2023 11:04:38 +0300 Subject: [PATCH 1/2] Fix next/prev tree-sitter inconsistency Before there where different results going to next or previous due to sorting not dealing with multiple captures that start/end at the same pos. I chose to prefer longer matches. --- helix-core/src/movement.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/helix-core/src/movement.rs b/helix-core/src/movement.rs index 003a1f3708a7..6533f907f5e2 100644 --- a/helix-core/src/movement.rs +++ b/helix-core/src/movement.rs @@ -1,4 +1,4 @@ -use std::iter; +use std::{cmp::Reverse, iter}; use ropey::iter::Chars; use tree_sitter::{Node, QueryCursor}; @@ -509,7 +509,8 @@ pub fn goto_treesitter_object( count: usize, ) -> Range { let get_range = move |range: Range| -> Option { - let byte_pos = slice.char_to_byte(range.cursor(slice)); + let left_pos = slice.char_to_byte(range.cursor(slice)); + let right_pos = slice.char_to_byte(range.from()); let cap_name = |t: TextObject| format!("{}.{}", object_name, t); let mut cursor = QueryCursor::new(); @@ -526,11 +527,11 @@ pub fn goto_treesitter_object( let node = match dir { Direction::Forward => nodes - .filter(|n| n.start_byte() > byte_pos) - .min_by_key(|n| n.start_byte())?, + .filter(|n| n.start_byte() > left_pos) + .min_by_key(|n| (n.start_byte(), Reverse(n.end_byte())))?, Direction::Backward => nodes - .filter(|n| n.end_byte() < byte_pos) - .max_by_key(|n| n.end_byte())?, + .filter(|n| n.end_byte() < right_pos) + .max_by_key(|n| (n.end_byte(), Reverse(n.start_byte())))?, }; let len = slice.len_bytes(); From 7dad11709157c94cc3c2680b5d92c5983c4f3957 Mon Sep 17 00:00:00 2001 From: A-Walrus Date: Tue, 13 Jun 2023 11:51:27 +0300 Subject: [PATCH 2/2] Revert unnecessary change --- helix-core/src/movement.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/helix-core/src/movement.rs b/helix-core/src/movement.rs index 6533f907f5e2..2b29f36de451 100644 --- a/helix-core/src/movement.rs +++ b/helix-core/src/movement.rs @@ -509,8 +509,7 @@ pub fn goto_treesitter_object( count: usize, ) -> Range { let get_range = move |range: Range| -> Option { - let left_pos = slice.char_to_byte(range.cursor(slice)); - let right_pos = slice.char_to_byte(range.from()); + let byte_pos = slice.char_to_byte(range.cursor(slice)); let cap_name = |t: TextObject| format!("{}.{}", object_name, t); let mut cursor = QueryCursor::new(); @@ -527,10 +526,10 @@ pub fn goto_treesitter_object( let node = match dir { Direction::Forward => nodes - .filter(|n| n.start_byte() > left_pos) + .filter(|n| n.start_byte() > byte_pos) .min_by_key(|n| (n.start_byte(), Reverse(n.end_byte())))?, Direction::Backward => nodes - .filter(|n| n.end_byte() < right_pos) + .filter(|n| n.end_byte() < byte_pos) .max_by_key(|n| (n.end_byte(), Reverse(n.start_byte())))?, };