Skip to content

Commit d0eae33

Browse files
committed
Fix behaviour of matching when tree-sitter grammar is available
If the current character is a valid bracket, resort to plaintext matching. Otherwise, use regular tree-sitter node traversal.
1 parent a2b8cfd commit d0eae33

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

helix-core/src/match_brackets.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ fn find_pair(syntax: &Syntax, doc: &Rope, pos: usize, traverse_parents: bool) ->
5050
let tree = syntax.tree();
5151
let pos = doc.char_to_byte(pos);
5252

53+
if is_valid_bracket(doc.char(pos)) {
54+
return find_matching_bracket_plaintext(doc, pos);
55+
}
56+
5357
let mut node = tree.root_node().named_descendant_for_byte_range(pos, pos)?;
5458

5559
loop {
@@ -85,10 +89,7 @@ fn find_pair(syntax: &Syntax, doc: &Rope, pos: usize, traverse_parents: bool) ->
8589
///
8690
/// If no matching bracket is found, `None` is returned.
8791
#[must_use]
88-
pub fn find_matching_bracket_current_line_plaintext(
89-
doc: &Rope,
90-
cursor_pos: usize,
91-
) -> Option<usize> {
92+
pub fn find_matching_bracket_plaintext(doc: &Rope, cursor_pos: usize) -> Option<usize> {
9293
// Don't do anything when the cursor is not on top of a bracket.
9394
let bracket = doc.char(cursor_pos);
9495
if !is_valid_bracket(bracket) {
@@ -169,10 +170,10 @@ mod tests {
169170
fn test_find_matching_bracket_current_line_plaintext() {
170171
let assert = |input: &str, pos, expected| {
171172
let input = &Rope::from(input);
172-
let actual = find_matching_bracket_current_line_plaintext(input, pos);
173+
let actual = find_matching_bracket_plaintext(input, pos);
173174
assert_eq!(expected, actual.unwrap());
174175

175-
let actual = find_matching_bracket_current_line_plaintext(input, expected);
176+
let actual = find_matching_bracket_plaintext(input, expected);
176177
assert_eq!(pos, actual.unwrap(), "expected symmetrical behaviour");
177178
};
178179

helix-term/src/commands.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4535,7 +4535,7 @@ fn match_brackets(cx: &mut Context) {
45354535
let selection = doc.selection(view.id).clone().transform(|range| {
45364536
let pos = range.cursor(text_slice);
45374537
if let Some(matched_pos) = doc.syntax().map_or_else(
4538-
|| match_brackets::find_matching_bracket_current_line_plaintext(text, pos),
4538+
|| match_brackets::find_matching_bracket_plaintext(text, pos),
45394539
|syntax| match_brackets::find_matching_bracket_fuzzy(syntax, text, pos),
45404540
) {
45414541
range.put_cursor(text_slice, matched_pos, is_select)

0 commit comments

Comments
 (0)