From 4743fe84151f8a014591f65a60da06529245abbc Mon Sep 17 00:00:00 2001 From: Dino Date: Fri, 19 Sep 2025 16:50:33 +0100 Subject: [PATCH] vim: Fix regression in surround behavior (#38344) Fix an issue introduced in https://github.com/zed-industries/zed/pull/37321 where vim's surround wouldn't work as expected when replacing quotes with non-quotes, with whitespace always being added, regardless of whether the opening or closing bracket was used. This is not the intended, or previous, behavior, where only the opening bracket would trigger whitespace to be added. Closes #38169 Release Notes: - Fixed regression in vim's surround plugin that ignored whether the opening or closing bracket was being used when replacing quotes, so space would always be added --- crates/vim/src/surrounds.rs | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/crates/vim/src/surrounds.rs b/crates/vim/src/surrounds.rs index 8b3359c8f08046cf995db077a9a5ff0d36a97b95..5e25b08dd8656887b2013df52a5e7d62fce5dbe0 100644 --- a/crates/vim/src/surrounds.rs +++ b/crates/vim/src/surrounds.rs @@ -241,21 +241,15 @@ impl Vim { }, }; - // Determines whether space should be added/removed after + // Determines whether space should be added after // and before the surround pairs. - // For example, using `cs{[` will add a space before and - // after the pair, while using `cs{]` will not, notice the - // use of the closing bracket instead of the opening bracket - // on the target object. - // In the case of quotes, the opening and closing is the - // same, so no space will ever be added or removed. - let surround = match target { - Object::Quotes - | Object::BackQuotes - | Object::AnyQuotes - | Object::MiniQuotes - | Object::DoubleQuotes => true, - _ => pair.end != surround_alias((*text).as_ref()), + // Space is only added in the following cases: + // - new surround is not quote and is opening bracket (({[<) + // - new surround is quote and original was also quote + let surround = if pair.start != pair.end { + pair.end != surround_alias((*text).as_ref()) + } else { + will_replace_pair.start == will_replace_pair.end }; let (display_map, selections) = editor.selections.all_adjusted_display(cx); @@ -1241,6 +1235,15 @@ mod test { "}, Mode::Normal, ); + + // test quote to bracket spacing. + cx.set_state(indoc! {"'ˇfoobar'"}, Mode::Normal); + cx.simulate_keystrokes("c s ' {"); + cx.assert_state(indoc! {"ˇ{ foobar }"}, Mode::Normal); + + cx.set_state(indoc! {"'ˇfoobar'"}, Mode::Normal); + cx.simulate_keystrokes("c s ' }"); + cx.assert_state(indoc! {"ˇ{foobar}"}, Mode::Normal); } #[gpui::test]