on master help sign in
make TS matching fallback to plaintext
commita0359f7f22d26548d5680fb1dc177f6d4b07ad11 authorPascal Kuthe <pascal.kuthe@semimod.de> dateTue 6 Jun 2023, 01:48 (3 years ago) parent5dba649
3 files changed+49−40
helix-core/src/match_brackets.rs+44−37 helix-term/src/commands.rs+2−2 helix-term/src/ui/editor.rs+3−1
helix-core/src/match_brackets.rs+44−37
@@ -1,8 +1,10 @@
11use std::iter;
22
3+use ropey::RopeSlice;
34use tree_sitter::Node;
45
5−use crate::{Rope, Syntax};
6+use crate::movement::Direction::{self, Backward, Forward};
7+use crate::Syntax;
68
79const MAX_PLAINTEXT_SCAN: usize = 10000;
810const MATCH_LIMIT: usize = 16;
@@ -27,7 +29,7 @@ const PAIRS: &[(char, char)] = &[
2729///
2830/// If no matching bracket is found, `None` is returned.
2931#[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> {
3133 if pos >= doc.len_chars() || !is_valid_bracket(doc.char(pos)) {
3234 return None;
3335 }
@@ -45,13 +47,18 @@ pub fn find_matching_bracket(syntax: &Syntax, doc: &Rope, pos: usize) -> Option<
4547//
4648// If no surrounding scope is found, the function returns `None`.
4749#[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> {
4951 find_pair(syntax, doc, pos, true)
5052}
5153
52−fn find_pair(syntax: &Syntax, doc: &Rope, pos: usize, traverse_parents: bool) -> Option<usize> {
54+fn find_pair(
55+ syntax: &Syntax,
56+ doc: RopeSlice,
57+ pos_: usize,
58+ traverse_parents: bool,
59+) -> Option<usize> {
5360 let tree = syntax.tree();
54− let pos = doc.char_to_byte(pos);
61+ let pos = doc.char_to_byte(pos_);
5562
5663 let mut node = tree.root_node().descendant_for_byte_range(pos, pos)?;
5764
@@ -92,7 +99,7 @@ fn find_pair(syntax: &Syntax, doc: &Rope, pos: usize, traverse_parents: bool) ->
9299 }
93100 }
94101 if node.is_named() {
95− return None;
102+ break;
96103 }
97104 }
98105
@@ -104,8 +111,16 @@ fn find_pair(syntax: &Syntax, doc: &Rope, pos: usize, traverse_parents: bool) ->
104111 return doc.try_byte_to_char(close.start_byte()).ok();
105112 }
106113 }
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;
108120 }
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)
109124}
110125
111126/// 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) ->
120135///
121136/// If no matching bracket is found, `None` is returned.
122137#[must_use]
123−pub fn find_matching_bracket_current_line_plaintext(
124− doc: &Rope,
125− cursor_pos: usize,
126−) -> Option<usize> {
138+pub fn find_matching_bracket_plaintext(doc: RopeSlice, cursor_pos: usize) -> Option<usize> {
127139 // Don't do anything when the cursor is not on top of a bracket.
128140 let bracket = doc.char(cursor_pos);
129141 if !is_valid_bracket(bracket) {
@@ -179,11 +191,11 @@ fn is_forward_bracket(c: char) -> bool {
179191 PAIRS.iter().any(|(l, _)| *l == c)
180192}
181193
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 {
183195 PAIRS.contains(&(doc.char(start_char), doc.char(end_char)))
184196}
185197
186−fn surrounding_bytes(doc: &Rope, node: &Node) -> Option<(usize, usize)> {
198+fn surrounding_bytes(doc: RopeSlice, node: &Node) -> Option<(usize, usize)> {
187199 let len = doc.len_bytes();
188200
189201 let start_byte = node.start_byte();
@@ -196,22 +208,8 @@ fn surrounding_bytes(doc: &Rope, node: &Node) -> Option<(usize, usize)> {
196208 Some((start_byte, end_byte))
197209}
198210
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−
213211/// 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> {
215213 let close = as_char(doc, node)?.1;
216214 PAIRS
217215 .iter()
@@ -223,8 +221,17 @@ fn as_close_pair(doc: &Rope, node: &Node) -> Option<char> {
223221/// # Returns
224222///
225223/// The position of the found node or `None` otherwise
226−fn find_close_pair(doc: &Rope, node: Option<Node>, close: char) -> Option<usize> {
227− iter::successors(node, |node| node.next_sibling())
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+ };
234+ iter::successors(node, advance)
228235 .take(MATCH_LIMIT)
229236 .find_map(|node| {
230237 let (pos, c) = as_char(doc, &node)?;
@@ -233,15 +240,15 @@ fn find_close_pair(doc: &Rope, node: Option<Node>, close: char) -> Option<usize>
233240}
234241
235242/// 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> {
237− let close = as_char(doc, node)?.1;
243+fn as_open_pair(doc: RopeSlice, node: &Node) -> Option<char> {
244+ let open = as_char(doc, node)?.1;
238245 PAIRS
239246 .iter()
240247 .find_map(|&(open_, close)| (open_ == open).then_some(close))
241248}
242249
243−/// 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)> {
250+/// If node is a single char return it (and its char position)
251+fn as_char(doc: RopeSlice, node: &Node) -> Option<(usize, char)> {
245252 // TODO: multi char/non ASCII pairs
246253 if node.byte_range().len() != 1 {
247254 return None;
@@ -257,11 +264,11 @@ mod tests {
257264 #[test]
258265 fn test_find_matching_bracket_current_line_plaintext() {
259266 let assert = |input: &str, pos, expected| {
260− let input = &Rope::from(input);
261− let actual = find_matching_bracket_current_line_plaintext(input, pos);
267+ let input = RopeSlice::from(input);
268+ let actual = find_matching_bracket_plaintext(input, pos);
262269 assert_eq!(expected, actual.unwrap());
263270
264− let actual = find_matching_bracket_current_line_plaintext(input, expected);
271+ let actual = find_matching_bracket_plaintext(input, expected);
265272 assert_eq!(pos, actual.unwrap(), "expected symmetrical behaviour");
266273 };
267274
helix-term/src/commands.rs+2−2
@@ -4649,8 +4649,8 @@ fn match_brackets(cx: &mut Context) {
46494649 let selection = doc.selection(view.id).clone().transform(|range| {
46504650 let pos = range.cursor(text_slice);
46514651 if let Some(matched_pos) = doc.syntax().map_or_else(
4652− || match_brackets::find_matching_bracket_current_line_plaintext(text, pos),
4653− |syntax| match_brackets::find_matching_bracket_fuzzy(syntax, text, pos),
4652+ || match_brackets::find_matching_bracket_plaintext(text.slice(..), pos),
4653+ |syntax| match_brackets::find_matching_bracket_fuzzy(syntax, text.slice(..), pos),
46544654 ) {
46554655 range.put_cursor(text_slice, matched_pos, is_select)
46564656 } else {
helix-term/src/ui/editor.rs+3−1
@@ -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)];