Skip to content

Commit

Permalink
Fix behaviour of matching when tree-sitter grammar is available
Browse files Browse the repository at this point in the history
If the current character is a valid bracket, resort to plaintext matching.
Otherwise, use regular tree-sitter node traversal.
  • Loading branch information
alevinval committed Jun 5, 2023
1 parent a2b8cfd commit d0eae33
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
13 changes: 7 additions & 6 deletions helix-core/src/match_brackets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ fn find_pair(syntax: &Syntax, doc: &Rope, pos: usize, traverse_parents: bool) ->
let tree = syntax.tree();
let pos = doc.char_to_byte(pos);

if is_valid_bracket(doc.char(pos)) {
return find_matching_bracket_plaintext(doc, pos);
}

let mut node = tree.root_node().named_descendant_for_byte_range(pos, pos)?;

loop {
Expand Down Expand Up @@ -85,10 +89,7 @@ fn find_pair(syntax: &Syntax, doc: &Rope, pos: usize, traverse_parents: bool) ->
///
/// If no matching bracket is found, `None` is returned.
#[must_use]
pub fn find_matching_bracket_current_line_plaintext(
doc: &Rope,
cursor_pos: usize,
) -> Option<usize> {
pub fn find_matching_bracket_plaintext(doc: &Rope, cursor_pos: usize) -> Option<usize> {
// Don't do anything when the cursor is not on top of a bracket.
let bracket = doc.char(cursor_pos);
if !is_valid_bracket(bracket) {
Expand Down Expand Up @@ -169,10 +170,10 @@ mod tests {
fn test_find_matching_bracket_current_line_plaintext() {
let assert = |input: &str, pos, expected| {
let input = &Rope::from(input);
let actual = find_matching_bracket_current_line_plaintext(input, pos);
let actual = find_matching_bracket_plaintext(input, pos);
assert_eq!(expected, actual.unwrap());

let actual = find_matching_bracket_current_line_plaintext(input, expected);
let actual = find_matching_bracket_plaintext(input, expected);
assert_eq!(pos, actual.unwrap(), "expected symmetrical behaviour");
};

Expand Down
2 changes: 1 addition & 1 deletion helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4535,7 +4535,7 @@ fn match_brackets(cx: &mut Context) {
let selection = doc.selection(view.id).clone().transform(|range| {
let pos = range.cursor(text_slice);
if let Some(matched_pos) = doc.syntax().map_or_else(
|| match_brackets::find_matching_bracket_current_line_plaintext(text, pos),
|| match_brackets::find_matching_bracket_plaintext(text, pos),
|syntax| match_brackets::find_matching_bracket_fuzzy(syntax, text, pos),
) {
range.put_cursor(text_slice, matched_pos, is_select)
Expand Down

0 comments on commit d0eae33

Please sign in to comment.