From d446f9c9a6e4a3cb49e7f586794e3e302d37c215 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandre=20Vinyals=20Valdepe=C3=B1as?= Date: Sat, 15 Oct 2022 13:51:59 +0200 Subject: [PATCH] self-review: make the implementation more concise - Introduce new variables only when needed - Simplify `is_valid_pair` call - Optimize iterations, there's no need to check if we've closed all brackets right after opening a new one... --- helix-core/src/match_brackets.rs | 37 ++++++++++++++++---------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/helix-core/src/match_brackets.rs b/helix-core/src/match_brackets.rs index 30b0d01d3bf17..1c011a2b1ab5e 100644 --- a/helix-core/src/match_brackets.rs +++ b/helix-core/src/match_brackets.rs @@ -70,26 +70,22 @@ fn find_pair(syntax: &Syntax, doc: &Rope, pos: usize, traverse_parents: bool) -> } } -// Returns the position of the bracket that is closing, searching only in -// the current line, and works on plain text, ignoring the tree-sitter grammar. -// -// If the cursor is on an opening or closing bracket, the function -// behaves equivalent to [`find_matching_bracket`]. -// -// If no matchig bracket is found on the current line, returns None. +// Returns the position of the bracket that is closing. +// This function only searches on the current line, either forwards or backwards. +// This function matches plain text, it ignores tree-sitter grammar. +// Returns None when no matching bracket is found. #[must_use] pub fn find_matching_bracket_current_line_plaintext(doc: &Rope, pos: usize) -> Option { // Don't do anything when the cursor is not on top of a bracket. - let mut c = doc.char(pos); - if !is_valid_bracket(c) { + let bracket = doc.char(pos); + if !is_valid_bracket(bracket) { return None; } - let bracket = c; let bracket_pos = pos; // Determine the direction of the matching - let is_fwd = is_forward_bracket(c); + let is_fwd = is_forward_bracket(bracket); let line = doc.byte_to_line(pos); let end = doc.line_to_byte(if is_fwd { line + 1 } else { line }); let range = if is_fwd { @@ -101,18 +97,21 @@ pub fn find_matching_bracket_current_line_plaintext(doc: &Rope, pos: usize) -> O let mut open_cnt = 1; for pos in range { - c = doc.char(pos); + let c = doc.char(pos); - if !is_valid_bracket(c) { - continue; - } else if bracket == c { + if c == bracket { open_cnt += 1; - } else if is_valid_pair(doc, bracket_pos, pos) || is_valid_pair(doc, pos, bracket_pos) { + } else if is_valid_pair( + doc, + if is_fwd { bracket_pos } else { pos }, + if is_fwd { pos } else { bracket_pos }, + ) { open_cnt -= 1; - } - if open_cnt == 0 { - return Some(pos); + // Return when all pending brackets have been closed. + if open_cnt == 0 { + return Some(pos); + } } }