Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions helix-core/src/surround.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ pub fn find_nth_pairs_pos(
if text.len_chars() < 2 {
return Err(Error::PairNotFound);
}
if range.to() >= text.len_chars() {
if range.to() > text.len_chars() {
return Err(Error::RangeExceedsText);
}

Expand Down Expand Up @@ -332,7 +332,7 @@ pub fn get_surround_pos(
#[cfg(test)]
mod test {
use super::*;
use crate::Range;
use crate::{Range, Transaction};

use ropey::Rope;
use smallvec::SmallVec;
Expand All @@ -352,6 +352,43 @@ mod test {
);
}

#[test]
fn test_get_surround_pos_full_range_ending_at_eof_can_be_deleted() {
let mut doc = Rope::from("(hello world)");
let selection = Selection::single(0, doc.len_chars());

let change_pos = get_surround_pos(None, doc.slice(..), &selection, Some('('), 1).unwrap();

assert_eq!(change_pos, [0, doc.len_chars() - 1]);

let transaction =
Transaction::change(&doc, change_pos.into_iter().map(|pos| (pos, pos + 1, None)));
assert!(transaction.apply(&mut doc));
assert_eq!(doc, "hello world");
}

#[test]
fn test_find_nth_pairs_pos_rejects_range_beyond_eof() {
let doc = Rope::from("(hello world)");
let range = Range::new(0, doc.len_chars() + 1);

assert_eq!(
find_nth_pairs_pos(None, doc.slice(..), '(', range, 1),
Err(Error::RangeExceedsText)
);
}

#[test]
fn test_find_nth_pairs_pos_accepts_range_within_text() {
let doc = Rope::from("(hello world)");
let range = Range::new(1, doc.len_chars() - 1);

assert_eq!(
find_nth_pairs_pos(None, doc.slice(..), '(', range, 1),
Ok((0, doc.len_chars() - 1))
);
}

#[test]
fn test_get_surround_pos_bail_different_surround_chars() {
#[rustfmt::skip]
Expand Down