on master help sign in
make TS matching fallback to plaintext
a0359f7Pascal Kuthe
helix-core/src/match_brackets.rs+44−37
@@ -1,8 +1,10 @@
1use std::iter;
1use std::iter;
2
2
3+use ropey::RopeSlice;
3use tree_sitter::Node;
4use tree_sitter::Node;
4
5
5−use crate::{Rope, Syntax};
6+use crate::movement::Direction::{self, Backward, Forward};
7+use crate::Syntax;
6
8
7const MAX_PLAINTEXT_SCAN: usize = 10000;
9const MAX_PLAINTEXT_SCAN: usize = 10000;
8const MATCH_LIMIT: usize = 16;
10const MATCH_LIMIT: usize = 16;
@@ -27,7 +29,7 @@ const PAIRS: &[(char, char)] = &[
27///
29///
28/// If no matching bracket is found, `None` is returned.
30/// If no matching bracket is found, `None` is returned.
29#[must_use]
31#[must_use]
30−pub fn find_matching_bracket(syntax: &Syntax, doc: &Rope, pos: usize) -> Option<usize> {
32+pub fn find_matching_bracket(syntax: &Syntax, doc: RopeSlice, pos: usize) -> Option<usize> {
31 if pos >= doc.len_chars() || !is_valid_bracket(doc.char(pos)) {
33 if pos >= doc.len_chars() || !is_valid_bracket(doc.char(pos)) {
32 return None;
34 return None;
33 }
35 }
@@ -45,13 +47,18 @@ pub fn find_matching_bracket(syntax: &Syntax, doc: &Rope, pos: usize) -> Option<
45//
47//
46// If no surrounding scope is found, the function returns `None`.
48// If no surrounding scope is found, the function returns `None`.
47#[must_use]
49#[must_use]
48−pub fn find_matching_bracket_fuzzy(syntax: &Syntax, doc: &Rope, pos: usize) -> Option<usize> {
50+pub fn find_matching_bracket_fuzzy(syntax: &Syntax, doc: RopeSlice, pos: usize) -> Option<usize> {
49 find_pair(syntax, doc, pos, true)
51 find_pair(syntax, doc, pos, true)
50}
52}
51
53
54+fn find_pair(
55+ syntax: &Syntax,
56+ doc: RopeSlice,
57+ pos_: usize,
52−fn find_pair(syntax: &Syntax, doc: &Rope, pos: usize, traverse_parents: bool) -> Option<usize> {
58+ traverse_parents: bool,
59+) -> Option<usize> {
53 let tree = syntax.tree();
60 let tree = syntax.tree();
54− let pos = doc.char_to_byte(pos);
61+ let pos = doc.char_to_byte(pos_);
55
62
56 let mut node = tree.root_node().descendant_for_byte_range(pos, pos)?;
63 let mut node = tree.root_node().descendant_for_byte_range(pos, pos)?;
57
64
@@ -92,7 +99,7 @@ fn find_pair(syntax: &Syntax, doc: &Rope, pos: usize, traverse_parents: bool) ->
92 }
99 }
93 }
100 }
94 if node.is_named() {
101 if node.is_named() {
95− return None;
102+ break;
96 }
103 }
97 }
104 }
98
105
@@ -104,8 +111,16 @@ fn find_pair(syntax: &Syntax, doc: &Rope, pos: usize, traverse_parents: bool) ->
104 return doc.try_byte_to_char(close.start_byte()).ok();
111 return doc.try_byte_to_char(close.start_byte()).ok();
105 }
112 }
106 }
113 }
107− node = node.parent()?;
114+ let Some(parent) = node.parent() else { break; };
115+ node = parent;
116+ }
117+ let node = tree.root_node().named_descendant_for_byte_range(pos, pos)?;
118+ if node.child_count() != 0 {
119+ return None;
108 }
120 }
121+ let node_start = doc.byte_to_char(node.start_byte());
122+ find_matching_bracket_plaintext(doc.byte_slice(node.byte_range()), pos_ - node_start)
123+ .map(|pos| pos + node_start)
109}
124}
110
125
111/// Returns the position of the matching bracket under cursor.
126/// Returns the position of the matching bracket under cursor.
@@ -120,10 +135,7 @@ fn find_pair(syntax: &Syntax, doc: &Rope, pos: usize, traverse_parents: bool) ->
120///
135///
121/// If no matching bracket is found, `None` is returned.
136/// If no matching bracket is found, `None` is returned.
122#[must_use]
137#[must_use]
123−pub fn find_matching_bracket_current_line_plaintext(
138+pub fn find_matching_bracket_plaintext(doc: RopeSlice, cursor_pos: usize) -> Option<usize> {
124− doc: &Rope,
125− cursor_pos: usize,
126−) -> Option<usize> {
127 // Don't do anything when the cursor is not on top of a bracket.
139 // Don't do anything when the cursor is not on top of a bracket.
128 let bracket = doc.char(cursor_pos);
140 let bracket = doc.char(cursor_pos);
129 if !is_valid_bracket(bracket) {
141 if !is_valid_bracket(bracket) {
@@ -179,11 +191,11 @@ fn is_forward_bracket(c: char) -> bool {
179 PAIRS.iter().any(|(l, _)| *l == c)
191 PAIRS.iter().any(|(l, _)| *l == c)
180}
192}
181
193
182−fn is_valid_pair(doc: &Rope, start_char: usize, end_char: usize) -> bool {
194+fn is_valid_pair(doc: RopeSlice, start_char: usize, end_char: usize) -> bool {
183 PAIRS.contains(&(doc.char(start_char), doc.char(end_char)))
195 PAIRS.contains(&(doc.char(start_char), doc.char(end_char)))
184}
196}
185
197
186−fn surrounding_bytes(doc: &Rope, node: &Node) -> Option<(usize, usize)> {
198+fn surrounding_bytes(doc: RopeSlice, node: &Node) -> Option<(usize, usize)> {
187 let len = doc.len_bytes();
199 let len = doc.len_bytes();
188
200
189 let start_byte = node.start_byte();
201 let start_byte = node.start_byte();
@@ -196,22 +208,8 @@ fn surrounding_bytes(doc: &Rope, node: &Node) -> Option<(usize, usize)> {
196 Some((start_byte, end_byte))
208 Some((start_byte, end_byte))
197}
209}
198
210
199−/// Checks if `node` or its siblings (at most MATCH_LIMIT nodes) is the specified openiing char
200−///
201−/// # Returns
202−///
203−/// The position of the found node or `None` otherwise
204−fn find_open_pair(doc: &Rope, node: Option<Node>, open: char) -> Option<usize> {
205− iter::successors(node, |node| node.prev_sibling())
206− .take(MATCH_LIMIT)
207− .find_map(|node| {
208− let (pos, c) = as_char(doc, &node)?;
209− (c == open).then_some(pos)
210− })
211−}
212−
213/// Tests if this node is a pair close char and returns the expected open char
211/// 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> {
212+fn as_close_pair(doc: RopeSlice, node: &Node) -> Option<char> {
215 let close = as_char(doc, node)?.1;
213 let close = as_char(doc, node)?.1;
216 PAIRS
214 PAIRS
217 .iter()
215 .iter()
@@ -223,8 +221,17 @@ fn as_close_pair(doc: &Rope, node: &Node) -> Option<char> {
223/// # Returns
221/// # Returns
224///
222///
225/// The position of the found node or `None` otherwise
223/// The position of the found node or `None` otherwise
226−fn find_close_pair(doc: &Rope, node: Option<Node>, close: char) -> Option<usize> {
224+fn find_pair_end(
225+ doc: RopeSlice,
226+ node: Option<Node>,
227+ end_char: char,
228+ direction: Direction,
229+) -> Option<usize> {
230+ let advance = match direction {
231+ Forward => Node::next_sibling,
232+ Backward => Node::prev_sibling,
233+ };
227− iter::successors(node, |node| node.next_sibling())
234+ iter::successors(node, advance)
228 .take(MATCH_LIMIT)
235 .take(MATCH_LIMIT)
229 .find_map(|node| {
236 .find_map(|node| {
230 let (pos, c) = as_char(doc, &node)?;
237 let (pos, c) = as_char(doc, &node)?;
@@ -233,15 +240,15 @@ fn find_close_pair(doc: &Rope, node: Option<Node>, close: char) -> Option<usize>
233}
240}
234
241
235/// Tests if this node is a pair close char and returns the expected open char
242/// 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> {
243+fn as_open_pair(doc: RopeSlice, node: &Node) -> Option<char> {
237− let close = as_char(doc, node)?.1;
244+ let open = as_char(doc, node)?.1;
238 PAIRS
245 PAIRS
239 .iter()
246 .iter()
240 .find_map(|&(open_, close)| (open_ == open).then_some(close))
247 .find_map(|&(open_, close)| (open_ == open).then_some(close))
241}
248}
242
249
243−/// Tests if this node is a pair opening and returns the expected close char
250+/// If node is a single char return it (and its char position)
244−fn as_char(doc: &Rope, node: &Node) -> Option<(usize, char)> {
251+fn as_char(doc: RopeSlice, node: &Node) -> Option<(usize, char)> {
245 // TODO: multi char/non ASCII pairs
252 // TODO: multi char/non ASCII pairs
246 if node.byte_range().len() != 1 {
253 if node.byte_range().len() != 1 {
247 return None;
254 return None;
@@ -257,11 +264,11 @@ mod tests {
257 #[test]
264 #[test]
258 fn test_find_matching_bracket_current_line_plaintext() {
265 fn test_find_matching_bracket_current_line_plaintext() {
259 let assert = |input: &str, pos, expected| {
266 let assert = |input: &str, pos, expected| {
260− let input = &Rope::from(input);
267+ let input = RopeSlice::from(input);
261− let actual = find_matching_bracket_current_line_plaintext(input, pos);
268+ let actual = find_matching_bracket_plaintext(input, pos);
262 assert_eq!(expected, actual.unwrap());
269 assert_eq!(expected, actual.unwrap());
263
270
264− let actual = find_matching_bracket_current_line_plaintext(input, expected);
271+ let actual = find_matching_bracket_plaintext(input, expected);
265 assert_eq!(pos, actual.unwrap(), "expected symmetrical behaviour");
272 assert_eq!(pos, actual.unwrap(), "expected symmetrical behaviour");
266 };
273 };
267
274
helix-term/src/commands.rs+2−2
@@ -4649,8 +4649,8 @@ fn match_brackets(cx: &mut Context) {
4649 let selection = doc.selection(view.id).clone().transform(|range| {
4649 let selection = doc.selection(view.id).clone().transform(|range| {
4650 let pos = range.cursor(text_slice);
4650 let pos = range.cursor(text_slice);
4651 if let Some(matched_pos) = doc.syntax().map_or_else(
4651 if let Some(matched_pos) = doc.syntax().map_or_else(
4652− || match_brackets::find_matching_bracket_current_line_plaintext(text, pos),
4652+ || match_brackets::find_matching_bracket_plaintext(text.slice(..), pos),
4653− |syntax| match_brackets::find_matching_bracket_fuzzy(syntax, text, pos),
4653+ |syntax| match_brackets::find_matching_bracket_fuzzy(syntax, text.slice(..), pos),
4654 ) {
4654 ) {
4655 range.put_cursor(text_slice, matched_pos, is_select)
4655 range.put_cursor(text_slice, matched_pos, is_select)
4656 } else {
4656 } else {
helix-term/src/ui/editor.rs+3−1
@@ -501,7 +501,9 @@ impl EditorView {
501 use helix_core::match_brackets;
501 use helix_core::match_brackets;
502 let pos = doc.selection(view.id).primary().cursor(text);
502 let pos = doc.selection(view.id).primary().cursor(text);
503
503
504+ if let Some(pos) =
504− if let Some(pos) = match_brackets::find_matching_bracket(syntax, doc.text(), pos) {
505+ match_brackets::find_matching_bracket(syntax, doc.text().slice(..), pos)
506+ {
505 // ensure col is on screen
507 // ensure col is on screen
506 if let Some(highlight) = theme.find_scope_index_exact("ui.cursor.match") {
508 if let Some(highlight) = theme.find_scope_index_exact("ui.cursor.match") {
507 return vec![(highlight, pos..pos + 1)];
509 return vec![(highlight, pos..pos + 1)];