vim: Add exchange (#24678)

Implements [vim-exchange](https://github.com/tommcdo/vim-exchange)
functionality.

Lets you swap the content of one selection/object/motion with another.

The default key bindings are the same as in exchange:
- `cx` to begin the exchange in normal mode. Visual mode does not have a
default binding due to conflicts.
- `cxx` selects the current line
- `cxc` clears the selection
- If the previous operation was an exchange, `.` will repeat that
operation.

Closes #22759

## Overlapping regions

According to the vim exchange readme:

> If one region is fully contained within the other, it will replace the
containing region.

Zed does the following:
- If one range is completely contained within another: the smaller
region replaces the larger region (as in exchange.vim)
- If the ranges only partially overlap, then we abort and cancel the
exchange. I don't think we can do anything sensible with that. Not sure
what the original does, evil-exchange aborts.

## Not implemented: cross-window exchange

Emacs's evil-exchange allows you to exchange across buffers. There is no
code to accommodate that in this PR. Personally, it'd never occurred to
me before working on this and I've never needed it. As such, I'll leave
that implementation for whomever needs it.

As an upside; this allows you to have concurrent exchange states per
buffer, which may come in handy.

## Bonus

Also adds "replace with register" for the full line with `grr` 🐕 This
was an oversight from a previous PR.

Release notes:

- Added an implementation of `vim-exchange`
- Fixed: Added missing default key binding for `Vim::CurrentLine` for
replace with register mode (`grr`)

---------

Co-authored-by: Conrad Irwin <conrad.irwin@gmail.com>
This commit is contained in:
Thomas Heartman 2025-02-22 21:36:21 +01:00 committed by GitHub
parent 04732b23fb
commit 084a0233b6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 220 additions and 3 deletions

View file

@ -126,6 +126,7 @@ actions!(
SwitchToVisualBlockMode,
SwitchToHelixNormalMode,
ClearOperators,
ClearExchange,
Tab,
Enter,
InnerObject,
@ -138,6 +139,7 @@ actions!(
ResizePaneDown,
PushChange,
PushDelete,
Exchange,
PushYank,
PushReplace,
PushDeleteSurrounds,
@ -637,6 +639,18 @@ impl Vim {
},
);
Vim::action(editor, cx, |vim, _: &Exchange, window, cx| {
if vim.mode.is_visual() {
vim.exchange_visual(window, cx)
} else {
vim.push_operator(Operator::Exchange, window, cx)
}
});
Vim::action(editor, cx, |vim, _: &ClearExchange, window, cx| {
vim.clear_exchange(window, cx)
});
Vim::action(editor, cx, |vim, _: &PushToggleComments, window, cx| {
vim.push_operator(Operator::ToggleComments, window, cx)
});