Skip to content

Commit

Permalink
optimize: make sure we only resort to plaintext search on cases where…
Browse files Browse the repository at this point in the history
… we are

not on top of the boundary of the node *and* we have a valid pair in the node
start/ending bytes.
  • Loading branch information
alevinval committed Jun 5, 2023
1 parent d0eae33 commit 1fb75bb
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions helix-core/src/match_brackets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,18 @@ 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)?;
let mut node_positions = get_node_positions(doc, &node);

loop {
let (start_byte, end_byte) = surrounding_bytes(doc, &node)?;
let (start_char, end_char) = (doc.byte_to_char(start_byte), doc.byte_to_char(end_byte));
if is_valid_bracket(doc.char(pos)) {
if let Some((_, _, start_char, end_char)) = node_positions {
if !is_valid_pair(doc, start_char, end_char) {
return find_matching_bracket_plaintext(doc, pos);
}
}
}

while let Some((_, end_byte, start_char, end_char)) = node_positions {
if is_valid_pair(doc, start_char, end_char) {
if end_byte == pos {
return Some(start_char);
Expand All @@ -71,10 +73,19 @@ fn find_pair(syntax: &Syntax, doc: &Rope, pos: usize, traverse_parents: bool) ->

if traverse_parents {
node = node.parent()?;
node_positions = get_node_positions(doc, &node);
} else {
return None;
break;
}
}

None
}

fn get_node_positions(doc: &Rope, node: &Node) -> Option<(usize, usize, usize, usize)> {
let (start_byte, end_byte) = surrounding_bytes(doc, node)?;
let (start_char, end_char) = (doc.byte_to_char(start_byte), doc.byte_to_char(end_byte));
Some((start_byte, end_byte, start_char, end_char))
}

/// Returns the position of the matching bracket under cursor.
Expand Down

0 comments on commit 1fb75bb

Please sign in to comment.