Add surrounds support for vim (#9400)

For #4965

There are still some minor issues: 
1. When change the surround and delete the surround, we should also
decide whether there are spaces inside after deleting/replacing
according to whether it is open parentheses, and replace them
accordingly, but at present, delete and change, haven't done this
adaptation for current pr, I'm not sure if I can fit it in the back or
if it needs to be fitted together.
2. In the selection mode, pressing s plus brackets should also trigger
the Add Surrounds function, but this MR has not adapted the selection
mode for the time being, I think we need to support different add
behaviors for the three selection modes.(Currently in select mode, s is
used for Substitute)
3. For the current change surrounds, if the user does not find the
bracket that needs to be matched after entering cs, but it is a valid
bracket, and will wait for the second input before failing, the better
practice here should be to return to normal mode if the first bracket is
not found
4. I reused BracketPair in language, but two of its properties weren't
used in this mr, so I'm not sure if I should create a new struct with
only start and end, which would have less code

I'm not sure which ones need to be changed in the first issue, and which
ones can be revised in the future, and it seems that they can be solved

---------

Co-authored-by: Conrad Irwin <conrad.irwin@gmail.com>
This commit is contained in:
Hans 2024-04-09 01:41:06 +08:00 committed by GitHub
parent e826ef83e2
commit 44aed4a0cb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 1033 additions and 15 deletions

View file

@ -12,6 +12,7 @@ mod normal;
mod object;
mod replace;
mod state;
mod surrounds;
mod utils;
mod visual;
@ -37,6 +38,7 @@ use serde_derive::Serialize;
use settings::{update_settings_file, Settings, SettingsStore};
use state::{EditorState, Mode, Operator, RecordedSelection, WorkspaceState};
use std::{ops::Range, sync::Arc};
use surrounds::{add_surrounds, change_surrounds, delete_surrounds};
use ui::BorrowAppContext;
use visual::{visual_block_motion, visual_replace};
use workspace::{self, Workspace};
@ -170,7 +172,14 @@ fn observe_keystrokes(keystroke_event: &KeystrokeEvent, cx: &mut WindowContext)
}
Vim::update(cx, |vim, cx| match vim.active_operator() {
Some(Operator::FindForward { .. } | Operator::FindBackward { .. } | Operator::Replace) => {}
Some(
Operator::FindForward { .. }
| Operator::FindBackward { .. }
| Operator::Replace
| Operator::AddSurrounds { .. }
| Operator::ChangeSurrounds { .. }
| Operator::DeleteSurrounds,
) => {}
Some(_) => {
vim.clear_operator(cx);
}
@ -622,6 +631,31 @@ impl Vim {
Mode::Visual | Mode::VisualLine | Mode::VisualBlock => visual_replace(text, cx),
_ => Vim::update(cx, |vim, cx| vim.clear_operator(cx)),
},
Some(Operator::AddSurrounds { target }) => match Vim::read(cx).state().mode {
Mode::Normal => {
if let Some(target) = target {
add_surrounds(text, target, cx);
Vim::update(cx, |vim, cx| vim.clear_operator(cx));
}
}
_ => Vim::update(cx, |vim, cx| vim.clear_operator(cx)),
},
Some(Operator::ChangeSurrounds { target }) => match Vim::read(cx).state().mode {
Mode::Normal => {
if let Some(target) = target {
change_surrounds(text, target, cx);
Vim::update(cx, |vim, cx| vim.clear_operator(cx));
}
}
_ => Vim::update(cx, |vim, cx| vim.clear_operator(cx)),
},
Some(Operator::DeleteSurrounds) => match Vim::read(cx).state().mode {
Mode::Normal => {
delete_surrounds(text, cx);
Vim::update(cx, |vim, cx| vim.clear_operator(cx));
}
_ => Vim::update(cx, |vim, cx| vim.clear_operator(cx)),
},
_ => match Vim::read(cx).state().mode {
Mode::Replace => multi_replace(text, cx),
_ => {}