Skip to content

Commit

Permalink
use Rope::get_char instead of doing our own bounds checking
Browse files Browse the repository at this point in the history
  • Loading branch information
dead10ck committed Dec 12, 2021
1 parent 22202a7 commit ed4cffc
Showing 1 changed file with 4 additions and 11 deletions.
15 changes: 4 additions & 11 deletions helix-core/src/auto_pairs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,14 @@ pub fn hook(doc: &Rope, selection: &Selection, ch: char) -> Option<Transaction>
None
}

fn next_char(doc: &Rope, pos: usize) -> Option<char> {
if pos >= doc.len_chars() {
return None;
}
Some(doc.char(pos))
}

fn prev_char(doc: &Rope, mut pos: usize) -> Option<char> {
if pos == 0 {
return None;
}

pos -= 1;

next_char(doc, pos)
doc.get_char(pos)
}

fn handle_open(
Expand All @@ -89,7 +82,7 @@ fn handle_open(
let transaction = Transaction::change_by_selection(doc, selection, |start_range| {
let start_head = start_range.head;

let next = next_char(doc, start_head);
let next = doc.get_char(start_head);
let end_head = start_head + offs + open.len_utf8();

let end_anchor = if start_range.is_empty() {
Expand Down Expand Up @@ -132,7 +125,7 @@ fn handle_close(doc: &Rope, selection: &Selection, _open: char, close: char) ->

let transaction = Transaction::change_by_selection(doc, selection, |start_range| {
let start_head = start_range.head;
let next = next_char(doc, start_head);
let next = doc.get_char(start_head);
let end_head = start_head + offs + close.len_utf8();

let end_anchor = if start_range.is_empty() {
Expand Down Expand Up @@ -182,7 +175,7 @@ fn handle_same(

end_ranges.push(Range::new(end_anchor, end_head));

let next = next_char(doc, start_head);
let next = doc.get_char(start_head);
let prev = prev_char(doc, start_head);

if next == Some(token) {
Expand Down

0 comments on commit ed4cffc

Please sign in to comment.