Skip to content

Commit f5a3f99

Browse files
committed
make TS matching fallback to plaintext
1 parent 59e7dce commit f5a3f99

File tree

3 files changed

+34
-24
lines changed

3 files changed

+34
-24
lines changed

helix-core/src/match_brackets.rs

+29-21
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use std::iter;
22

3+
use ropey::RopeSlice;
34
use tree_sitter::Node;
45

5-
use crate::{Rope, Syntax};
6+
use crate::Syntax;
67

78
const MAX_PLAINTEXT_SCAN: usize = 10000;
89
const MATCH_LIMIT: usize = 16;
@@ -27,7 +28,7 @@ const PAIRS: &[(char, char)] = &[
2728
///
2829
/// If no matching bracket is found, `None` is returned.
2930
#[must_use]
30-
pub fn find_matching_bracket(syntax: &Syntax, doc: &Rope, pos: usize) -> Option<usize> {
31+
pub fn find_matching_bracket(syntax: &Syntax, doc: RopeSlice, pos: usize) -> Option<usize> {
3132
if pos >= doc.len_chars() || !is_valid_bracket(doc.char(pos)) {
3233
return None;
3334
}
@@ -45,13 +46,18 @@ pub fn find_matching_bracket(syntax: &Syntax, doc: &Rope, pos: usize) -> Option<
4546
//
4647
// If no surrounding scope is found, the function returns `None`.
4748
#[must_use]
48-
pub fn find_matching_bracket_fuzzy(syntax: &Syntax, doc: &Rope, pos: usize) -> Option<usize> {
49+
pub fn find_matching_bracket_fuzzy(syntax: &Syntax, doc: RopeSlice, pos: usize) -> Option<usize> {
4950
find_pair(syntax, doc, pos, true)
5051
}
5152

52-
fn find_pair(syntax: &Syntax, doc: &Rope, pos: usize, traverse_parents: bool) -> Option<usize> {
53+
fn find_pair(
54+
syntax: &Syntax,
55+
doc: RopeSlice,
56+
pos_: usize,
57+
traverse_parents: bool,
58+
) -> Option<usize> {
5359
let tree = syntax.tree();
54-
let pos = doc.char_to_byte(pos);
60+
let pos = doc.char_to_byte(pos_);
5561

5662
let mut node = tree.root_node().descendant_for_byte_range(pos, pos)?;
5763

@@ -92,7 +98,7 @@ fn find_pair(syntax: &Syntax, doc: &Rope, pos: usize, traverse_parents: bool) ->
9298
}
9399
}
94100
if node.is_named() {
95-
return None;
101+
break;
96102
}
97103
}
98104

@@ -104,8 +110,13 @@ fn find_pair(syntax: &Syntax, doc: &Rope, pos: usize, traverse_parents: bool) ->
104110
return doc.try_byte_to_char(close.start_byte()).ok();
105111
}
106112
}
107-
node = node.parent()?;
113+
let Some(parent) = node.parent() else { break; };
114+
node = parent;
108115
}
116+
let node = tree.root_node().named_descendant_for_byte_range(pos, pos)?;
117+
let node_start = doc.byte_to_char(node.start_byte());
118+
find_matching_bracket_plaintext(doc.byte_slice(node.byte_range()), pos_ - node_start)
119+
.map(|pos| pos + node_start)
109120
}
110121

111122
/// Returns the position of the matching bracket under cursor.
@@ -120,10 +131,7 @@ fn find_pair(syntax: &Syntax, doc: &Rope, pos: usize, traverse_parents: bool) ->
120131
///
121132
/// If no matching bracket is found, `None` is returned.
122133
#[must_use]
123-
pub fn find_matching_bracket_current_line_plaintext(
124-
doc: &Rope,
125-
cursor_pos: usize,
126-
) -> Option<usize> {
134+
pub fn find_matching_bracket_plaintext(doc: RopeSlice, cursor_pos: usize) -> Option<usize> {
127135
// Don't do anything when the cursor is not on top of a bracket.
128136
let bracket = doc.char(cursor_pos);
129137
if !is_valid_bracket(bracket) {
@@ -179,11 +187,11 @@ fn is_forward_bracket(c: char) -> bool {
179187
PAIRS.iter().any(|(l, _)| *l == c)
180188
}
181189

182-
fn is_valid_pair(doc: &Rope, start_char: usize, end_char: usize) -> bool {
190+
fn is_valid_pair(doc: RopeSlice, start_char: usize, end_char: usize) -> bool {
183191
PAIRS.contains(&(doc.char(start_char), doc.char(end_char)))
184192
}
185193

186-
fn surrounding_bytes(doc: &Rope, node: &Node) -> Option<(usize, usize)> {
194+
fn surrounding_bytes(doc: RopeSlice, node: &Node) -> Option<(usize, usize)> {
187195
let len = doc.len_bytes();
188196

189197
let start_byte = node.start_byte();
@@ -201,7 +209,7 @@ fn surrounding_bytes(doc: &Rope, node: &Node) -> Option<(usize, usize)> {
201209
/// # Returns
202210
///
203211
/// The position of the found node or `None` otherwise
204-
fn find_open_pair(doc: &Rope, node: Option<Node>, open: char) -> Option<usize> {
212+
fn find_open_pair(doc: RopeSlice, node: Option<Node>, open: char) -> Option<usize> {
205213
iter::successors(node, |node| node.prev_sibling())
206214
.take(MATCH_LIMIT)
207215
.find_map(|node| {
@@ -211,7 +219,7 @@ fn find_open_pair(doc: &Rope, node: Option<Node>, open: char) -> Option<usize> {
211219
}
212220

213221
/// Tests if this node is a pair close char and returns the expected open char
214-
fn as_close_pair(doc: &Rope, node: &Node) -> Option<char> {
222+
fn as_close_pair(doc: RopeSlice, node: &Node) -> Option<char> {
215223
let close = as_char(doc, node)?.1;
216224
PAIRS
217225
.iter()
@@ -223,7 +231,7 @@ fn as_close_pair(doc: &Rope, node: &Node) -> Option<char> {
223231
/// # Returns
224232
///
225233
/// The position of the found node or `None` otherwise
226-
fn find_close_pair(doc: &Rope, node: Option<Node>, close: char) -> Option<usize> {
234+
fn find_close_pair(doc: RopeSlice, node: Option<Node>, close: char) -> Option<usize> {
227235
iter::successors(node, |node| node.next_sibling())
228236
.take(MATCH_LIMIT)
229237
.find_map(|node| {
@@ -233,15 +241,15 @@ fn find_close_pair(doc: &Rope, node: Option<Node>, close: char) -> Option<usize>
233241
}
234242

235243
/// Tests if this node is a pair close char and returns the expected open char
236-
fn as_open_pair(doc: &Rope, node: &Node) -> Option<char> {
244+
fn as_open_pair(doc: RopeSlice, node: &Node) -> Option<char> {
237245
let close = as_char(doc, node)?.1;
238246
PAIRS
239247
.iter()
240248
.find_map(|&(open, close_)| (close_ == close).then_some(open))
241249
}
242250

243251
/// Tests if this node is a pair opening and returns the expected close char
244-
fn as_char(doc: &Rope, node: &Node) -> Option<(usize, char)> {
252+
fn as_char(doc: RopeSlice, node: &Node) -> Option<(usize, char)> {
245253
// TODO: multi char/non ASCII pairs
246254
if node.byte_range().len() != 1 {
247255
return None;
@@ -257,11 +265,11 @@ mod tests {
257265
#[test]
258266
fn test_find_matching_bracket_current_line_plaintext() {
259267
let assert = |input: &str, pos, expected| {
260-
let input = &Rope::from(input);
261-
let actual = find_matching_bracket_current_line_plaintext(input, pos);
268+
let input = RopeSlice::from(input);
269+
let actual = find_matching_bracket_plaintext(input, pos);
262270
assert_eq!(expected, actual.unwrap());
263271

264-
let actual = find_matching_bracket_current_line_plaintext(input, expected);
272+
let actual = find_matching_bracket_plaintext(input, expected);
265273
assert_eq!(pos, actual.unwrap(), "expected symmetrical behaviour");
266274
};
267275

helix-term/src/commands.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4535,8 +4535,8 @@ 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),
4539-
|syntax| match_brackets::find_matching_bracket_fuzzy(syntax, text, pos),
4538+
|| match_brackets::find_matching_bracket_plaintext(text.slice(..), pos),
4539+
|syntax| match_brackets::find_matching_bracket_fuzzy(syntax, text.slice(..), pos),
45404540
) {
45414541
range.put_cursor(text_slice, matched_pos, is_select)
45424542
} else {

helix-term/src/ui/editor.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,9 @@ impl EditorView {
501501
use helix_core::match_brackets;
502502
let pos = doc.selection(view.id).primary().cursor(text);
503503

504-
if let Some(pos) = match_brackets::find_matching_bracket(syntax, doc.text(), pos) {
504+
if let Some(pos) =
505+
match_brackets::find_matching_bracket(syntax, doc.text().slice(..), pos)
506+
{
505507
// ensure col is on screen
506508
if let Some(highlight) = theme.find_scope_index_exact("ui.cursor.match") {
507509
return vec![(highlight, pos..pos + 1)];

0 commit comments

Comments
 (0)