Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): match inside/around closest pair inconsitency #10611

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 1 addition & 3 deletions helix-core/src/surround.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ fn find_nth_closest_pairs_ts(
mut skip: usize,
) -> Result<(usize, usize)> {
let mut opening = range.from();
// We want to expand the selection if we are already on the found pair,
// otherwise we would need to subtract "-1" from "range.to()".
let mut closing = range.to();
let mut closing = range.to() - 1;

while skip > 0 {
closing = find_matching_bracket_fuzzy(syntax, text, closing).ok_or(Error::PairNotFound)?;
Expand Down
14 changes: 13 additions & 1 deletion helix-core/src/textobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,19 @@ fn textobject_pair_surround_impl(
) -> Range {
let pair_pos = match ch {
Some(ch) => surround::find_nth_pairs_pos(slice, ch, range, count),
None => surround::find_nth_closest_pairs_pos(syntax, slice, range, count),
None => {
surround::find_nth_closest_pairs_pos(syntax, slice, range, count).and_then(|closest| {
// Since `find_nth_closest_pairs_pos` takes into accout the closest
// pair even if the range included it entirely, we need to handle this
// edge case separetely. In such cases, we want to actually find the
// next pair.
if Range::new(closest.0, closest.1).len() == range.len() - 1 && count == 1 {
surround::find_nth_closest_pairs_pos(syntax, slice, range, count + 1)
} else {
Ok(closest)
}
})
}
};
pair_pos
.map(|(anchor, head)| match textobject {
Expand Down
30 changes: 30 additions & 0 deletions helix-term/tests/test/movement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,36 @@ async fn match_around_closest_ts() -> anyhow::Result<()> {
Ok(())
}

#[tokio::test(flavor = "multi_thread")]
async fn match_inside_closest_ts() -> anyhow::Result<()> {
test_with_config(
AppBuilder::new().with_file("foo.rs", None),
(
r#"fn main() {func(#["|]#string");}"#,
"mim",
r##"fn main() {func("#[string|]#");}"##,
),
)
.await?;

Ok(())
}

#[tokio::test(flavor = "multi_thread")]
async fn match_mix_closest_ts() -> anyhow::Result<()> {
test_with_config(
AppBuilder::new().with_file("foo.rs", None),
(
r#"fn main() {func("st#[|r]#ing");}"#,
"mimmammimmam",
r#"fn main() {func#[|("string")]#;}"#,
),
)
.await?;

Ok(())
}

/// Ensure the very initial cursor in an opened file is the width of
/// the first grapheme
#[tokio::test(flavor = "multi_thread")]
Expand Down