vim: Update change surrounds to match vim's behavior (#38721)
These changes refactor the whitespace handling logic for Vim's change surrounds command (`cs`), making its behavior closely match [tpope/vim-surround](https://github.com/tpope/vim-surround), following [this discussion](https://github.com/zed-industries/zed/issues/38169#issuecomment-3304129461). Zed's current implementation has two main differences when compared to [tpope/vim-surround](https://github.com/tpope/vim-surround): - It only considers whether a single space should be added or removed, instead of all the space that is between the surrounding character and the content - It only takes into consideration the new surrounding characters in order to determine whether to add or remove that space A review of [tpope/vim-surround](https://github.com/tpope/vim-surround)'s behavior reveals these rules for whitespace: * Quote to Quote * Whitespace is never changed * Quote to Bracket * If opening bracket, add one space * If closing bracket, do not add space * Bracket to Bracket * If opening to opening, keep only one space * If opening to closing, remove all space * If closing to opening, add one space * If closing to closing, do not change space * Bracket to Quote * If opening, remove all space * If closing, preserve all space Below is a table with examples for each scenario. A new test has also been added to specifically check the scenarios outlined above, `vim::surrounds::test::test_change_surrounds_vim`. | Type | Before | Command | After | |-------------------|-------------|---------|---------------| | Quote → Quote | `' a '` | `cs'"` | `" a "` | | Quote → Quote | `" a "` | `cs"'` | `' a '` | | Quote → Bracket | `' a '` | `cs'{` | `{ a }` | | Quote → Bracket | `' a '` | `cs'}` | `{ a }` | | Bracket → Bracket | `[ a ]` | `cs[{` | `{ a }` | | Bracket → Bracket | `[ a ]` | `cs[}` | `{a}` | | Bracket → Bracket | `[ a ]` | `cs]{` | `{ a }` | | Bracket → Bracket | `[ a ]` | `cs]}` | `{ a }` | | Bracket → Quote | `[ a ]` | `cs['` | `'a'` | | Bracket → Quote | `[ a ]` | `cs]'` | `' a '` | These changes diverge from [tpope/vim-surround](https://github.com/tpope/vim-surround) when handling newlines. For example, with the following snippet: ```rust fn test_surround() { if 2 > 1 { println!("place cursor here"); } }; ``` Placing the cursor inside the string and running any combination of `cs{[`, `cs{]`, `cs}[`, or `cs}]` would previously remove newline characters. With these changes, using commands like `cs}]` will now preserve newlines. Related to #38169 Closes #39334 Release Notes: - Improved Vim’s change surround command to closely match [tpope/vim-surround](https://github.com/tpope/vim-surround) behavior. --------- Co-authored-by: Conrad Irwin <conrad.irwin@gmail.com>
This commit is contained in:
@@ -109,6 +109,9 @@ pub enum Operator {
|
||||
},
|
||||
ChangeSurrounds {
|
||||
target: Option<Object>,
|
||||
/// Represents whether the opening bracket was used for the target
|
||||
/// object.
|
||||
opening: bool,
|
||||
},
|
||||
DeleteSurrounds,
|
||||
Mark,
|
||||
@@ -1077,7 +1080,9 @@ impl Operator {
|
||||
| Operator::Replace
|
||||
| Operator::Digraph { .. }
|
||||
| Operator::Literal { .. }
|
||||
| Operator::ChangeSurrounds { target: Some(_) }
|
||||
| Operator::ChangeSurrounds {
|
||||
target: Some(_), ..
|
||||
}
|
||||
| Operator::DeleteSurrounds => true,
|
||||
Operator::Change
|
||||
| Operator::Delete
|
||||
@@ -1094,7 +1099,7 @@ impl Operator {
|
||||
| Operator::ReplaceWithRegister
|
||||
| Operator::Exchange
|
||||
| Operator::Object { .. }
|
||||
| Operator::ChangeSurrounds { target: None }
|
||||
| Operator::ChangeSurrounds { target: None, .. }
|
||||
| Operator::OppositeCase
|
||||
| Operator::ToggleComments
|
||||
| Operator::HelixMatch
|
||||
@@ -1121,7 +1126,7 @@ impl Operator {
|
||||
| Operator::Rewrap
|
||||
| Operator::ShellCommand
|
||||
| Operator::AddSurrounds { target: None }
|
||||
| Operator::ChangeSurrounds { target: None }
|
||||
| Operator::ChangeSurrounds { target: None, .. }
|
||||
| Operator::DeleteSurrounds
|
||||
| Operator::Exchange
|
||||
| Operator::HelixNext { .. }
|
||||
|
||||
Reference in New Issue
Block a user