on master help sign in
Implement m / match_brackets (using tree sitter).
73c92a0Blaž Hrastnik
helix-core/src/auto_pairs.rs+2
@@ -1,6 +1,8 @@
1use crate::{Range, Rope, Selection, Tendril, Transaction};
1use crate::{Range, Rope, Selection, Tendril, Transaction};
2use smallvec::SmallVec;
2use smallvec::SmallVec;
3
3
4+// Heavily based on https://github.com/codemirror/closebrackets/
5+
4const PAIRS: &[(char, char)] = &[
6const PAIRS: &[(char, char)] = &[
5 ('(', ')'),
7 ('(', ')'),
6 ('{', '}'),
8 ('{', '}'),
helix-core/src/lib.rs+1
@@ -6,6 +6,7 @@ pub mod graphemes;
6mod history;
6mod history;
7pub mod indent;
7pub mod indent;
8pub mod macros;
8pub mod macros;
9+pub mod match_brackets;
9pub mod movement;
10pub mod movement;
10pub mod object;
11pub mod object;
11mod position;
12mod position;
helix-core/src/match_brackets.rsnew file+34
@@ -0,0 +1,34 @@
1+use crate::{Range, Rope, Selection, Syntax};
2+
3+// const PAIRS: &[(char, char)] = &[('(', ')'), ('{', '}'), ('[', ']')];
4+// limit matching pairs to only ( ) { } [ ] < >
5+
6+pub fn find(syntax: &Syntax, doc: &Rope, pos: usize) -> Option<usize> {
7+ let tree = syntax.root_layer.tree.as_ref().unwrap();
8+
9+ let byte_pos = doc.char_to_byte(pos);
10+
11+ // most naive implementation: find the innermost syntax node, if we're at the edge of a node,
12+ // return the other edge.
13+
14+ let mut node = match tree
15+ .root_node()
16+ .named_descendant_for_byte_range(byte_pos, byte_pos)
17+ {
18+ Some(node) => node,
19+ None => return None,
20+ };
21+
22+ let start_byte = node.start_byte();
23+ let end_byte = node.end_byte() - 1; // it's end exclusive
24+
25+ if start_byte == byte_pos {
26+ return Some(doc.byte_to_char(end_byte));
27+ }
28+
29+ if end_byte == byte_pos {
30+ return Some(doc.byte_to_char(start_byte));
31+ }
32+
33+ None
34+}
helix-term/src/commands.rs+13−1
@@ -1,5 +1,5 @@
1use helix_core::{
1use helix_core::{
2− comment, coords_at_pos, graphemes,
2+ comment, coords_at_pos, graphemes, match_brackets,
3 movement::{self, Direction},
3 movement::{self, Direction},
4 object, pos_at_coords,
4 object, pos_at_coords,
5 regex::{self, Regex},
5 regex::{self, Regex},
@@ -1592,3 +1592,15 @@ pub fn expand_selection(cx: &mut Context) {
1592 doc.set_selection(selection);
1592 doc.set_selection(selection);
1593 }
1593 }
1594}
1594}
1595+
1596+pub fn match_brackets(cx: &mut Context) {
1597+ let mut doc = cx.doc();
1598+
1599+ if let Some(syntax) = doc.syntax() {
1600+ let pos = doc.selection().cursor();
1601+ if let Some(pos) = match_brackets::find(syntax, doc.text(), pos) {
1602+ let selection = Selection::point(pos);
1603+ doc.set_selection(selection);
1604+ };
1605+ }
1606+}
helix-term/src/keymap.rs+3
@@ -182,6 +182,9 @@ pub fn default() -> Keymaps {
182 // or select mode X?
182 // or select mode X?
183 // extend_to_whole_line, crop_to_whole_line
183 // extend_to_whole_line, crop_to_whole_line
184
184
185+
186+ key!('m') => commands::match_brackets,
187+ // TODO: refactor into
185 // key!('m') => commands::select_to_matching,
188 // key!('m') => commands::select_to_matching,
186 // key!('M') => commands::back_select_to_matching,
189 // key!('M') => commands::back_select_to_matching,
187 // select mode extend equivalents
190 // select mode extend equivalents