diff options
author | Blaž Hrastnik | 2021-03-22 08:58:49 +0000 |
---|---|---|
committer | Blaž Hrastnik | 2021-03-22 08:58:49 +0000 |
commit | 73c92a0bc1499f02092a7425c45d4992f4e573a1 (patch) | |
tree | 1de38a0f16e3acd44998b1f8368df726a7962e0c /helix-core | |
parent | bd607b4cbd58e2bff06e36f614ff61b7d1a721c3 (diff) |
Implement m / match_brackets (using tree sitter).
Diffstat (limited to 'helix-core')
-rw-r--r-- | helix-core/src/auto_pairs.rs | 2 | ||||
-rw-r--r-- | helix-core/src/lib.rs | 1 | ||||
-rw-r--r-- | helix-core/src/match_brackets.rs | 34 |
3 files changed, 37 insertions, 0 deletions
diff --git a/helix-core/src/auto_pairs.rs b/helix-core/src/auto_pairs.rs index 52a45075..e6b8f667 100644 --- a/helix-core/src/auto_pairs.rs +++ b/helix-core/src/auto_pairs.rs @@ -1,6 +1,8 @@ use crate::{Range, Rope, Selection, Tendril, Transaction}; use smallvec::SmallVec; +// Heavily based on https://github.com/codemirror/closebrackets/ + const PAIRS: &[(char, char)] = &[ ('(', ')'), ('{', '}'), diff --git a/helix-core/src/lib.rs b/helix-core/src/lib.rs index a8a449ca..30b3d37f 100644 --- a/helix-core/src/lib.rs +++ b/helix-core/src/lib.rs @@ -6,6 +6,7 @@ pub mod graphemes; mod history; pub mod indent; pub mod macros; +pub mod match_brackets; pub mod movement; pub mod object; mod position; diff --git a/helix-core/src/match_brackets.rs b/helix-core/src/match_brackets.rs new file mode 100644 index 00000000..bacd764f --- /dev/null +++ b/helix-core/src/match_brackets.rs @@ -0,0 +1,34 @@ +use crate::{Range, Rope, Selection, Syntax}; + +// const PAIRS: &[(char, char)] = &[('(', ')'), ('{', '}'), ('[', ']')]; +// limit matching pairs to only ( ) { } [ ] < > + +pub fn find(syntax: &Syntax, doc: &Rope, pos: usize) -> Option<usize> { + let tree = syntax.root_layer.tree.as_ref().unwrap(); + + let byte_pos = doc.char_to_byte(pos); + + // most naive implementation: find the innermost syntax node, if we're at the edge of a node, + // return the other edge. + + let mut node = match tree + .root_node() + .named_descendant_for_byte_range(byte_pos, byte_pos) + { + Some(node) => node, + None => return None, + }; + + let start_byte = node.start_byte(); + let end_byte = node.end_byte() - 1; // it's end exclusive + + if start_byte == byte_pos { + return Some(doc.byte_to_char(end_byte)); + } + + if end_byte == byte_pos { + return Some(doc.byte_to_char(start_byte)); + } + + None +} |