Skip to content

Commit

Permalink
make TS matching fallback to plaintext
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalkuthe committed Jun 6, 2023
1 parent 59e7dce commit f5a3f99
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 24 deletions.
50 changes: 29 additions & 21 deletions helix-core/src/match_brackets.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::iter;

use ropey::RopeSlice;
use tree_sitter::Node;

use crate::{Rope, Syntax};
use crate::Syntax;

const MAX_PLAINTEXT_SCAN: usize = 10000;
const MATCH_LIMIT: usize = 16;
Expand All @@ -27,7 +28,7 @@ const PAIRS: &[(char, char)] = &[
///
/// If no matching bracket is found, `None` is returned.
#[must_use]
pub fn find_matching_bracket(syntax: &Syntax, doc: &Rope, pos: usize) -> Option<usize> {
pub fn find_matching_bracket(syntax: &Syntax, doc: RopeSlice, pos: usize) -> Option<usize> {
if pos >= doc.len_chars() || !is_valid_bracket(doc.char(pos)) {
return None;
}
Expand All @@ -45,13 +46,18 @@ pub fn find_matching_bracket(syntax: &Syntax, doc: &Rope, pos: usize) -> Option<
//
// If no surrounding scope is found, the function returns `None`.
#[must_use]
pub fn find_matching_bracket_fuzzy(syntax: &Syntax, doc: &Rope, pos: usize) -> Option<usize> {
pub fn find_matching_bracket_fuzzy(syntax: &Syntax, doc: RopeSlice, pos: usize) -> Option<usize> {
find_pair(syntax, doc, pos, true)
}

fn find_pair(syntax: &Syntax, doc: &Rope, pos: usize, traverse_parents: bool) -> Option<usize> {
fn find_pair(
syntax: &Syntax,
doc: RopeSlice,
pos_: usize,
traverse_parents: bool,
) -> Option<usize> {
let tree = syntax.tree();
let pos = doc.char_to_byte(pos);
let pos = doc.char_to_byte(pos_);

let mut node = tree.root_node().descendant_for_byte_range(pos, pos)?;

Expand Down Expand Up @@ -92,7 +98,7 @@ fn find_pair(syntax: &Syntax, doc: &Rope, pos: usize, traverse_parents: bool) ->
}
}
if node.is_named() {
return None;
break;
}
}

Expand All @@ -104,8 +110,13 @@ fn find_pair(syntax: &Syntax, doc: &Rope, pos: usize, traverse_parents: bool) ->
return doc.try_byte_to_char(close.start_byte()).ok();
}
}
node = node.parent()?;
let Some(parent) = node.parent() else { break; };
node = parent;
}
let node = tree.root_node().named_descendant_for_byte_range(pos, pos)?;
let node_start = doc.byte_to_char(node.start_byte());
find_matching_bracket_plaintext(doc.byte_slice(node.byte_range()), pos_ - node_start)
.map(|pos| pos + node_start)
}

/// Returns the position of the matching bracket under cursor.
Expand All @@ -120,10 +131,7 @@ fn find_pair(syntax: &Syntax, doc: &Rope, pos: usize, traverse_parents: bool) ->
///
/// If no matching bracket is found, `None` is returned.
#[must_use]
pub fn find_matching_bracket_current_line_plaintext(
doc: &Rope,
cursor_pos: usize,
) -> Option<usize> {
pub fn find_matching_bracket_plaintext(doc: RopeSlice, cursor_pos: usize) -> Option<usize> {
// Don't do anything when the cursor is not on top of a bracket.
let bracket = doc.char(cursor_pos);
if !is_valid_bracket(bracket) {
Expand Down Expand Up @@ -179,11 +187,11 @@ fn is_forward_bracket(c: char) -> bool {
PAIRS.iter().any(|(l, _)| *l == c)
}

fn is_valid_pair(doc: &Rope, start_char: usize, end_char: usize) -> bool {
fn is_valid_pair(doc: RopeSlice, start_char: usize, end_char: usize) -> bool {
PAIRS.contains(&(doc.char(start_char), doc.char(end_char)))
}

fn surrounding_bytes(doc: &Rope, node: &Node) -> Option<(usize, usize)> {
fn surrounding_bytes(doc: RopeSlice, node: &Node) -> Option<(usize, usize)> {
let len = doc.len_bytes();

let start_byte = node.start_byte();
Expand All @@ -201,7 +209,7 @@ fn surrounding_bytes(doc: &Rope, node: &Node) -> Option<(usize, usize)> {
/// # Returns
///
/// The position of the found node or `None` otherwise
fn find_open_pair(doc: &Rope, node: Option<Node>, open: char) -> Option<usize> {
fn find_open_pair(doc: RopeSlice, node: Option<Node>, open: char) -> Option<usize> {
iter::successors(node, |node| node.prev_sibling())
.take(MATCH_LIMIT)
.find_map(|node| {
Expand All @@ -211,7 +219,7 @@ fn find_open_pair(doc: &Rope, node: Option<Node>, open: char) -> Option<usize> {
}

/// Tests if this node is a pair close char and returns the expected open char
fn as_close_pair(doc: &Rope, node: &Node) -> Option<char> {
fn as_close_pair(doc: RopeSlice, node: &Node) -> Option<char> {
let close = as_char(doc, node)?.1;
PAIRS
.iter()
Expand All @@ -223,7 +231,7 @@ fn as_close_pair(doc: &Rope, node: &Node) -> Option<char> {
/// # Returns
///
/// The position of the found node or `None` otherwise
fn find_close_pair(doc: &Rope, node: Option<Node>, close: char) -> Option<usize> {
fn find_close_pair(doc: RopeSlice, node: Option<Node>, close: char) -> Option<usize> {
iter::successors(node, |node| node.next_sibling())
.take(MATCH_LIMIT)
.find_map(|node| {
Expand All @@ -233,15 +241,15 @@ fn find_close_pair(doc: &Rope, node: Option<Node>, close: char) -> Option<usize>
}

/// Tests if this node is a pair close char and returns the expected open char
fn as_open_pair(doc: &Rope, node: &Node) -> Option<char> {
fn as_open_pair(doc: RopeSlice, node: &Node) -> Option<char> {
let close = as_char(doc, node)?.1;
PAIRS
.iter()
.find_map(|&(open, close_)| (close_ == close).then_some(open))
}

/// Tests if this node is a pair opening and returns the expected close char
fn as_char(doc: &Rope, node: &Node) -> Option<(usize, char)> {
fn as_char(doc: RopeSlice, node: &Node) -> Option<(usize, char)> {
// TODO: multi char/non ASCII pairs
if node.byte_range().len() != 1 {
return None;
Expand All @@ -257,11 +265,11 @@ mod tests {
#[test]
fn test_find_matching_bracket_current_line_plaintext() {
let assert = |input: &str, pos, expected| {
let input = &Rope::from(input);
let actual = find_matching_bracket_current_line_plaintext(input, pos);
let input = RopeSlice::from(input);
let actual = find_matching_bracket_plaintext(input, pos);
assert_eq!(expected, actual.unwrap());

let actual = find_matching_bracket_current_line_plaintext(input, expected);
let actual = find_matching_bracket_plaintext(input, expected);
assert_eq!(pos, actual.unwrap(), "expected symmetrical behaviour");
};

Expand Down
4 changes: 2 additions & 2 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4535,8 +4535,8 @@ fn match_brackets(cx: &mut Context) {
let selection = doc.selection(view.id).clone().transform(|range| {
let pos = range.cursor(text_slice);
if let Some(matched_pos) = doc.syntax().map_or_else(
|| match_brackets::find_matching_bracket_current_line_plaintext(text, pos),
|syntax| match_brackets::find_matching_bracket_fuzzy(syntax, text, pos),
|| match_brackets::find_matching_bracket_plaintext(text.slice(..), pos),
|syntax| match_brackets::find_matching_bracket_fuzzy(syntax, text.slice(..), pos),
) {
range.put_cursor(text_slice, matched_pos, is_select)
} else {
Expand Down
4 changes: 3 additions & 1 deletion helix-term/src/ui/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,9 @@ impl EditorView {
use helix_core::match_brackets;
let pos = doc.selection(view.id).primary().cursor(text);

if let Some(pos) = match_brackets::find_matching_bracket(syntax, doc.text(), pos) {
if let Some(pos) =
match_brackets::find_matching_bracket(syntax, doc.text().slice(..), pos)
{
// ensure col is on screen
if let Some(highlight) = theme.find_scope_index_exact("ui.cursor.match") {
return vec![(highlight, pos..pos + 1)];
Expand Down

0 comments on commit f5a3f99

Please sign in to comment.