mirror of
https://github.com/helix-editor/helix.git
synced 2024-11-10 10:35:16 +03:00
Implement m / match_brackets (using tree sitter).
This commit is contained in:
parent
bd607b4cbd
commit
73c92a0bc1
@ -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)] = &[
|
||||
('(', ')'),
|
||||
('{', '}'),
|
||||
|
@ -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;
|
||||
|
34
helix-core/src/match_brackets.rs
Normal file
34
helix-core/src/match_brackets.rs
Normal file
@ -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
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
use helix_core::{
|
||||
comment, coords_at_pos, graphemes,
|
||||
comment, coords_at_pos, graphemes, match_brackets,
|
||||
movement::{self, Direction},
|
||||
object, pos_at_coords,
|
||||
regex::{self, Regex},
|
||||
@ -1592,3 +1592,15 @@ pub fn expand_selection(cx: &mut Context) {
|
||||
doc.set_selection(selection);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn match_brackets(cx: &mut Context) {
|
||||
let mut doc = cx.doc();
|
||||
|
||||
if let Some(syntax) = doc.syntax() {
|
||||
let pos = doc.selection().cursor();
|
||||
if let Some(pos) = match_brackets::find(syntax, doc.text(), pos) {
|
||||
let selection = Selection::point(pos);
|
||||
doc.set_selection(selection);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -182,6 +182,9 @@ pub fn default() -> Keymaps {
|
||||
// or select mode X?
|
||||
// extend_to_whole_line, crop_to_whole_line
|
||||
|
||||
|
||||
key!('m') => commands::match_brackets,
|
||||
// TODO: refactor into
|
||||
// key!('m') => commands::select_to_matching,
|
||||
// key!('M') => commands::back_select_to_matching,
|
||||
// select mode extend equivalents
|
||||
|
Loading…
Reference in New Issue
Block a user