vim: f
and t
multiline option (#8448)
I'm not sure how compliant you're aiming to be with vim, but the `f` behavior is more useful when it can search on multiple lines instead of a single one, so I'd like to propose this change. This change is quite frequent in vim/neovim as a plugin (e.g. [clever-f](https://github.com/VSCodeVim/Vim), [improved-ft](https://github.com/backdround/improved-ft.nvim), etc), and in other vim emulations (e.g. [vscode-vim](https://github.com/VSCodeVim/Vim)).
This commit is contained in:
parent
bd8896a3dc
commit
9a7a267203
6 changed files with 186 additions and 37 deletions
|
@ -571,7 +571,8 @@
|
||||||
},
|
},
|
||||||
// Vim settings
|
// Vim settings
|
||||||
"vim": {
|
"vim": {
|
||||||
"use_system_clipboard": "always"
|
"use_system_clipboard": "always",
|
||||||
|
"use_multiline_find": false
|
||||||
},
|
},
|
||||||
// The server to connect to. If the environment variable
|
// The server to connect to. If the environment variable
|
||||||
// ZED_SERVER_URL is set, it will override this setting.
|
// ZED_SERVER_URL is set, it will override this setting.
|
||||||
|
|
|
@ -12,7 +12,7 @@ use std::{ops::Range, sync::Arc};
|
||||||
/// Defines search strategy for items in `movement` module.
|
/// Defines search strategy for items in `movement` module.
|
||||||
/// `FindRange::SingeLine` only looks for a match on a single line at a time, whereas
|
/// `FindRange::SingeLine` only looks for a match on a single line at a time, whereas
|
||||||
/// `FindRange::MultiLine` keeps going until the end of a string.
|
/// `FindRange::MultiLine` keeps going until the end of a string.
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||||
pub enum FindRange {
|
pub enum FindRange {
|
||||||
SingleLine,
|
SingleLine,
|
||||||
MultiLine,
|
MultiLine,
|
||||||
|
|
|
@ -22,27 +22,57 @@ use crate::{
|
||||||
pub enum Motion {
|
pub enum Motion {
|
||||||
Left,
|
Left,
|
||||||
Backspace,
|
Backspace,
|
||||||
Down { display_lines: bool },
|
Down {
|
||||||
Up { display_lines: bool },
|
display_lines: bool,
|
||||||
|
},
|
||||||
|
Up {
|
||||||
|
display_lines: bool,
|
||||||
|
},
|
||||||
Right,
|
Right,
|
||||||
Space,
|
Space,
|
||||||
NextWordStart { ignore_punctuation: bool },
|
NextWordStart {
|
||||||
NextWordEnd { ignore_punctuation: bool },
|
ignore_punctuation: bool,
|
||||||
PreviousWordStart { ignore_punctuation: bool },
|
},
|
||||||
PreviousWordEnd { ignore_punctuation: bool },
|
NextWordEnd {
|
||||||
FirstNonWhitespace { display_lines: bool },
|
ignore_punctuation: bool,
|
||||||
|
},
|
||||||
|
PreviousWordStart {
|
||||||
|
ignore_punctuation: bool,
|
||||||
|
},
|
||||||
|
PreviousWordEnd {
|
||||||
|
ignore_punctuation: bool,
|
||||||
|
},
|
||||||
|
FirstNonWhitespace {
|
||||||
|
display_lines: bool,
|
||||||
|
},
|
||||||
CurrentLine,
|
CurrentLine,
|
||||||
StartOfLine { display_lines: bool },
|
StartOfLine {
|
||||||
EndOfLine { display_lines: bool },
|
display_lines: bool,
|
||||||
|
},
|
||||||
|
EndOfLine {
|
||||||
|
display_lines: bool,
|
||||||
|
},
|
||||||
StartOfParagraph,
|
StartOfParagraph,
|
||||||
EndOfParagraph,
|
EndOfParagraph,
|
||||||
StartOfDocument,
|
StartOfDocument,
|
||||||
EndOfDocument,
|
EndOfDocument,
|
||||||
Matching,
|
Matching,
|
||||||
FindForward { before: bool, char: char },
|
FindForward {
|
||||||
FindBackward { after: bool, char: char },
|
before: bool,
|
||||||
RepeatFind { last_find: Box<Motion> },
|
char: char,
|
||||||
RepeatFindReversed { last_find: Box<Motion> },
|
mode: FindRange,
|
||||||
|
},
|
||||||
|
FindBackward {
|
||||||
|
after: bool,
|
||||||
|
char: char,
|
||||||
|
mode: FindRange,
|
||||||
|
},
|
||||||
|
RepeatFind {
|
||||||
|
last_find: Box<Motion>,
|
||||||
|
},
|
||||||
|
RepeatFindReversed {
|
||||||
|
last_find: Box<Motion>,
|
||||||
|
},
|
||||||
NextLineStart,
|
NextLineStart,
|
||||||
StartOfLineDownward,
|
StartOfLineDownward,
|
||||||
EndOfLineDownward,
|
EndOfLineDownward,
|
||||||
|
@ -481,30 +511,30 @@ impl Motion {
|
||||||
),
|
),
|
||||||
Matching => (matching(map, point), SelectionGoal::None),
|
Matching => (matching(map, point), SelectionGoal::None),
|
||||||
// t f
|
// t f
|
||||||
FindForward { before, char } => {
|
FindForward { before, char, mode } => {
|
||||||
return find_forward(map, point, *before, *char, times)
|
return find_forward(map, point, *before, *char, times, *mode)
|
||||||
.map(|new_point| (new_point, SelectionGoal::None))
|
.map(|new_point| (new_point, SelectionGoal::None))
|
||||||
}
|
}
|
||||||
// T F
|
// T F
|
||||||
FindBackward { after, char } => (
|
FindBackward { after, char, mode } => (
|
||||||
find_backward(map, point, *after, *char, times),
|
find_backward(map, point, *after, *char, times, *mode),
|
||||||
SelectionGoal::None,
|
SelectionGoal::None,
|
||||||
),
|
),
|
||||||
// ; -- repeat the last find done with t, f, T, F
|
// ; -- repeat the last find done with t, f, T, F
|
||||||
RepeatFind { last_find } => match **last_find {
|
RepeatFind { last_find } => match **last_find {
|
||||||
Motion::FindForward { before, char } => {
|
Motion::FindForward { before, char, mode } => {
|
||||||
let mut new_point = find_forward(map, point, before, char, times);
|
let mut new_point = find_forward(map, point, before, char, times, mode);
|
||||||
if new_point == Some(point) {
|
if new_point == Some(point) {
|
||||||
new_point = find_forward(map, point, before, char, times + 1);
|
new_point = find_forward(map, point, before, char, times + 1, mode);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new_point.map(|new_point| (new_point, SelectionGoal::None));
|
return new_point.map(|new_point| (new_point, SelectionGoal::None));
|
||||||
}
|
}
|
||||||
|
|
||||||
Motion::FindBackward { after, char } => {
|
Motion::FindBackward { after, char, mode } => {
|
||||||
let mut new_point = find_backward(map, point, after, char, times);
|
let mut new_point = find_backward(map, point, after, char, times, mode);
|
||||||
if new_point == point {
|
if new_point == point {
|
||||||
new_point = find_backward(map, point, after, char, times + 1);
|
new_point = find_backward(map, point, after, char, times + 1, mode);
|
||||||
}
|
}
|
||||||
|
|
||||||
(new_point, SelectionGoal::None)
|
(new_point, SelectionGoal::None)
|
||||||
|
@ -513,19 +543,19 @@ impl Motion {
|
||||||
},
|
},
|
||||||
// , -- repeat the last find done with t, f, T, F, in opposite direction
|
// , -- repeat the last find done with t, f, T, F, in opposite direction
|
||||||
RepeatFindReversed { last_find } => match **last_find {
|
RepeatFindReversed { last_find } => match **last_find {
|
||||||
Motion::FindForward { before, char } => {
|
Motion::FindForward { before, char, mode } => {
|
||||||
let mut new_point = find_backward(map, point, before, char, times);
|
let mut new_point = find_backward(map, point, before, char, times, mode);
|
||||||
if new_point == point {
|
if new_point == point {
|
||||||
new_point = find_backward(map, point, before, char, times + 1);
|
new_point = find_backward(map, point, before, char, times + 1, mode);
|
||||||
}
|
}
|
||||||
|
|
||||||
(new_point, SelectionGoal::None)
|
(new_point, SelectionGoal::None)
|
||||||
}
|
}
|
||||||
|
|
||||||
Motion::FindBackward { after, char } => {
|
Motion::FindBackward { after, char, mode } => {
|
||||||
let mut new_point = find_forward(map, point, after, char, times);
|
let mut new_point = find_forward(map, point, after, char, times, mode);
|
||||||
if new_point == Some(point) {
|
if new_point == Some(point) {
|
||||||
new_point = find_forward(map, point, after, char, times + 1);
|
new_point = find_forward(map, point, after, char, times + 1, mode);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new_point.map(|new_point| (new_point, SelectionGoal::None));
|
return new_point.map(|new_point| (new_point, SelectionGoal::None));
|
||||||
|
@ -1011,13 +1041,14 @@ fn find_forward(
|
||||||
before: bool,
|
before: bool,
|
||||||
target: char,
|
target: char,
|
||||||
times: usize,
|
times: usize,
|
||||||
|
mode: FindRange,
|
||||||
) -> Option<DisplayPoint> {
|
) -> Option<DisplayPoint> {
|
||||||
let mut to = from;
|
let mut to = from;
|
||||||
let mut found = false;
|
let mut found = false;
|
||||||
|
|
||||||
for _ in 0..times {
|
for _ in 0..times {
|
||||||
found = false;
|
found = false;
|
||||||
let new_to = find_boundary(map, to, FindRange::SingleLine, |_, right| {
|
let new_to = find_boundary(map, to, mode, |_, right| {
|
||||||
found = right == target;
|
found = right == target;
|
||||||
found
|
found
|
||||||
});
|
});
|
||||||
|
@ -1045,14 +1076,13 @@ fn find_backward(
|
||||||
after: bool,
|
after: bool,
|
||||||
target: char,
|
target: char,
|
||||||
times: usize,
|
times: usize,
|
||||||
|
mode: FindRange,
|
||||||
) -> DisplayPoint {
|
) -> DisplayPoint {
|
||||||
let mut to = from;
|
let mut to = from;
|
||||||
|
|
||||||
for _ in 0..times {
|
for _ in 0..times {
|
||||||
let new_to =
|
let new_to =
|
||||||
find_preceding_boundary_display_point(map, to, FindRange::SingleLine, |_, right| {
|
find_preceding_boundary_display_point(map, to, mode, |_, right| right == target);
|
||||||
right == target
|
|
||||||
});
|
|
||||||
if to == new_to {
|
if to == new_to {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -379,10 +379,12 @@ pub(crate) fn normal_replace(text: Arc<str>, cx: &mut WindowContext) {
|
||||||
mod test {
|
mod test {
|
||||||
use gpui::TestAppContext;
|
use gpui::TestAppContext;
|
||||||
use indoc::indoc;
|
use indoc::indoc;
|
||||||
|
use settings::SettingsStore;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
state::Mode::{self},
|
state::Mode::{self},
|
||||||
test::NeovimBackedTestContext,
|
test::{NeovimBackedTestContext, VimTestContext},
|
||||||
|
VimSettings,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[gpui::test]
|
#[gpui::test]
|
||||||
|
@ -903,6 +905,90 @@ mod test {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[gpui::test]
|
||||||
|
async fn test_f_and_t_multiline(cx: &mut gpui::TestAppContext) {
|
||||||
|
let mut cx = VimTestContext::new(cx, true).await;
|
||||||
|
cx.update_global(|store: &mut SettingsStore, cx| {
|
||||||
|
store.update_user_settings::<VimSettings>(cx, |s| {
|
||||||
|
s.use_multiline_find = Some(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
cx.assert_binding(
|
||||||
|
["f", "l"],
|
||||||
|
indoc! {"
|
||||||
|
ˇfunction print() {
|
||||||
|
console.log('ok')
|
||||||
|
}
|
||||||
|
"},
|
||||||
|
Mode::Normal,
|
||||||
|
indoc! {"
|
||||||
|
function print() {
|
||||||
|
consoˇle.log('ok')
|
||||||
|
}
|
||||||
|
"},
|
||||||
|
Mode::Normal,
|
||||||
|
);
|
||||||
|
|
||||||
|
cx.assert_binding(
|
||||||
|
["t", "l"],
|
||||||
|
indoc! {"
|
||||||
|
ˇfunction print() {
|
||||||
|
console.log('ok')
|
||||||
|
}
|
||||||
|
"},
|
||||||
|
Mode::Normal,
|
||||||
|
indoc! {"
|
||||||
|
function print() {
|
||||||
|
consˇole.log('ok')
|
||||||
|
}
|
||||||
|
"},
|
||||||
|
Mode::Normal,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[gpui::test]
|
||||||
|
async fn test_capital_f_and_capital_t_multiline(cx: &mut gpui::TestAppContext) {
|
||||||
|
let mut cx = VimTestContext::new(cx, true).await;
|
||||||
|
cx.update_global(|store: &mut SettingsStore, cx| {
|
||||||
|
store.update_user_settings::<VimSettings>(cx, |s| {
|
||||||
|
s.use_multiline_find = Some(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
cx.assert_binding(
|
||||||
|
["shift-f", "p"],
|
||||||
|
indoc! {"
|
||||||
|
function print() {
|
||||||
|
console.ˇlog('ok')
|
||||||
|
}
|
||||||
|
"},
|
||||||
|
Mode::Normal,
|
||||||
|
indoc! {"
|
||||||
|
function ˇprint() {
|
||||||
|
console.log('ok')
|
||||||
|
}
|
||||||
|
"},
|
||||||
|
Mode::Normal,
|
||||||
|
);
|
||||||
|
|
||||||
|
cx.assert_binding(
|
||||||
|
["shift-t", "p"],
|
||||||
|
indoc! {"
|
||||||
|
function print() {
|
||||||
|
console.ˇlog('ok')
|
||||||
|
}
|
||||||
|
"},
|
||||||
|
Mode::Normal,
|
||||||
|
indoc! {"
|
||||||
|
function pˇrint() {
|
||||||
|
console.log('ok')
|
||||||
|
}
|
||||||
|
"},
|
||||||
|
Mode::Normal,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[gpui::test]
|
#[gpui::test]
|
||||||
async fn test_percent(cx: &mut TestAppContext) {
|
async fn test_percent(cx: &mut TestAppContext) {
|
||||||
let mut cx = NeovimBackedTestContext::new(cx).await.binding(["%"]);
|
let mut cx = NeovimBackedTestContext::new(cx).await.binding(["%"]);
|
||||||
|
|
|
@ -17,7 +17,10 @@ mod visual;
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use collections::HashMap;
|
use collections::HashMap;
|
||||||
use command_palette_hooks::{CommandPaletteFilter, CommandPaletteInterceptor};
|
use command_palette_hooks::{CommandPaletteFilter, CommandPaletteInterceptor};
|
||||||
use editor::{movement, Editor, EditorEvent, EditorMode};
|
use editor::{
|
||||||
|
movement::{self, FindRange},
|
||||||
|
Editor, EditorEvent, EditorMode,
|
||||||
|
};
|
||||||
use gpui::{
|
use gpui::{
|
||||||
actions, impl_actions, Action, AppContext, EntityId, Global, Subscription, View, ViewContext,
|
actions, impl_actions, Action, AppContext, EntityId, Global, Subscription, View, ViewContext,
|
||||||
WeakView, WindowContext,
|
WeakView, WindowContext,
|
||||||
|
@ -482,6 +485,11 @@ impl Vim {
|
||||||
let find = Motion::FindForward {
|
let find = Motion::FindForward {
|
||||||
before,
|
before,
|
||||||
char: text.chars().next().unwrap(),
|
char: text.chars().next().unwrap(),
|
||||||
|
mode: if VimSettings::get_global(cx).use_multiline_find {
|
||||||
|
FindRange::MultiLine
|
||||||
|
} else {
|
||||||
|
FindRange::SingleLine
|
||||||
|
},
|
||||||
};
|
};
|
||||||
Vim::update(cx, |vim, _| {
|
Vim::update(cx, |vim, _| {
|
||||||
vim.workspace_state.last_find = Some(find.clone())
|
vim.workspace_state.last_find = Some(find.clone())
|
||||||
|
@ -492,6 +500,11 @@ impl Vim {
|
||||||
let find = Motion::FindBackward {
|
let find = Motion::FindBackward {
|
||||||
after,
|
after,
|
||||||
char: text.chars().next().unwrap(),
|
char: text.chars().next().unwrap(),
|
||||||
|
mode: if VimSettings::get_global(cx).use_multiline_find {
|
||||||
|
FindRange::MultiLine
|
||||||
|
} else {
|
||||||
|
FindRange::SingleLine
|
||||||
|
},
|
||||||
};
|
};
|
||||||
Vim::update(cx, |vim, _| {
|
Vim::update(cx, |vim, _| {
|
||||||
vim.workspace_state.last_find = Some(find.clone())
|
vim.workspace_state.last_find = Some(find.clone())
|
||||||
|
@ -628,11 +641,13 @@ struct VimSettings {
|
||||||
// vim always uses system cliupbaord
|
// vim always uses system cliupbaord
|
||||||
// some magic where yy is system and dd is not.
|
// some magic where yy is system and dd is not.
|
||||||
pub use_system_clipboard: UseSystemClipboard,
|
pub use_system_clipboard: UseSystemClipboard,
|
||||||
|
pub use_multiline_find: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Default, Serialize, Deserialize, JsonSchema)]
|
#[derive(Clone, Default, Serialize, Deserialize, JsonSchema)]
|
||||||
struct VimSettingsContent {
|
struct VimSettingsContent {
|
||||||
pub use_system_clipboard: Option<UseSystemClipboard>,
|
pub use_system_clipboard: Option<UseSystemClipboard>,
|
||||||
|
pub use_multiline_find: Option<bool>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Settings for VimSettings {
|
impl Settings for VimSettings {
|
||||||
|
|
|
@ -144,6 +144,23 @@ Currently supported vim-specific commands (as of Zed 0.106):
|
||||||
to sort the current selection (with i, case-insensitively)
|
to sort the current selection (with i, case-insensitively)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Vim settings
|
||||||
|
|
||||||
|
Some vim settings are available to modify the default vim behavior:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"vim": {
|
||||||
|
// "always": use system clipboard
|
||||||
|
// "never": don't use system clipboard
|
||||||
|
// "on_yank": use system clipboard for yank operations
|
||||||
|
"use_system_clipboard": "always",
|
||||||
|
// Enable multi-line find for `f` and `t` motions
|
||||||
|
"use_multiline_find": false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
## Related settings
|
## Related settings
|
||||||
|
|
||||||
There are a few Zed settings that you may also enjoy if you use vim mode:
|
There are a few Zed settings that you may also enjoy if you use vim mode:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue