vim: smartcase find option (#9033)

Release Notes:

- Added option `use_smartcase_find` to the vim-mode
This commit is contained in:
Rom Grk 2024-03-07 21:44:20 -05:00 committed by GitHub
parent d247086b21
commit f67abd2943
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 127 additions and 22 deletions

View file

@ -595,7 +595,8 @@
// Vim settings // Vim settings
"vim": { "vim": {
"use_system_clipboard": "always", "use_system_clipboard": "always",
"use_multiline_find": false "use_multiline_find": false,
"use_smartcase_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.

View file

@ -73,11 +73,13 @@ pub enum Motion {
before: bool, before: bool,
char: char, char: char,
mode: FindRange, mode: FindRange,
smartcase: bool,
}, },
FindBackward { FindBackward {
after: bool, after: bool,
char: char, char: char,
mode: FindRange, mode: FindRange,
smartcase: bool,
}, },
RepeatFind { RepeatFind {
last_find: Box<Motion>, last_find: Box<Motion>,
@ -604,30 +606,54 @@ impl Motion {
), ),
Matching => (matching(map, point), SelectionGoal::None), Matching => (matching(map, point), SelectionGoal::None),
// t f // t f
FindForward { before, char, mode } => { FindForward {
return find_forward(map, point, *before, *char, times, *mode) before,
char,
mode,
smartcase,
} => {
return find_forward(map, point, *before, *char, times, *mode, *smartcase)
.map(|new_point| (new_point, SelectionGoal::None)) .map(|new_point| (new_point, SelectionGoal::None))
} }
// T F // T F
FindBackward { after, char, mode } => ( FindBackward {
find_backward(map, point, *after, *char, times, *mode), after,
char,
mode,
smartcase,
} => (
find_backward(map, point, *after, *char, times, *mode, *smartcase),
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, mode } => { Motion::FindForward {
let mut new_point = find_forward(map, point, before, char, times, mode); before,
char,
mode,
smartcase,
} => {
let mut new_point =
find_forward(map, point, before, char, times, mode, smartcase);
if new_point == Some(point) { if new_point == Some(point) {
new_point = find_forward(map, point, before, char, times + 1, mode); new_point =
find_forward(map, point, before, char, times + 1, mode, smartcase);
} }
return new_point.map(|new_point| (new_point, SelectionGoal::None)); return new_point.map(|new_point| (new_point, SelectionGoal::None));
} }
Motion::FindBackward { after, char, mode } => { Motion::FindBackward {
let mut new_point = find_backward(map, point, after, char, times, mode); after,
char,
mode,
smartcase,
} => {
let mut new_point =
find_backward(map, point, after, char, times, mode, smartcase);
if new_point == point { if new_point == point {
new_point = find_backward(map, point, after, char, times + 1, mode); new_point =
find_backward(map, point, after, char, times + 1, mode, smartcase);
} }
(new_point, SelectionGoal::None) (new_point, SelectionGoal::None)
@ -636,19 +662,33 @@ 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, mode } => { Motion::FindForward {
let mut new_point = find_backward(map, point, before, char, times, mode); before,
char,
mode,
smartcase,
} => {
let mut new_point =
find_backward(map, point, before, char, times, mode, smartcase);
if new_point == point { if new_point == point {
new_point = find_backward(map, point, before, char, times + 1, mode); new_point =
find_backward(map, point, before, char, times + 1, mode, smartcase);
} }
(new_point, SelectionGoal::None) (new_point, SelectionGoal::None)
} }
Motion::FindBackward { after, char, mode } => { Motion::FindBackward {
let mut new_point = find_forward(map, point, after, char, times, mode); after,
char,
mode,
smartcase,
} => {
let mut new_point =
find_forward(map, point, after, char, times, mode, smartcase);
if new_point == Some(point) { if new_point == Some(point) {
new_point = find_forward(map, point, after, char, times + 1, mode); new_point =
find_forward(map, point, after, char, times + 1, mode, smartcase);
} }
return new_point.map(|new_point| (new_point, SelectionGoal::None)); return new_point.map(|new_point| (new_point, SelectionGoal::None));
@ -1368,6 +1408,7 @@ fn find_forward(
target: char, target: char,
times: usize, times: usize,
mode: FindRange, mode: FindRange,
smartcase: bool,
) -> Option<DisplayPoint> { ) -> Option<DisplayPoint> {
let mut to = from; let mut to = from;
let mut found = false; let mut found = false;
@ -1375,7 +1416,7 @@ fn find_forward(
for _ in 0..times { for _ in 0..times {
found = false; found = false;
let new_to = find_boundary(map, to, mode, |_, right| { let new_to = find_boundary(map, to, mode, |_, right| {
found = right == target; found = is_character_match(target, right, smartcase);
found found
}); });
if to == new_to { if to == new_to {
@ -1403,19 +1444,22 @@ fn find_backward(
target: char, target: char,
times: usize, times: usize,
mode: FindRange, mode: FindRange,
smartcase: bool,
) -> 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, mode, |_, right| {
find_preceding_boundary_display_point(map, to, mode, |_, right| right == target); is_character_match(target, right, smartcase)
});
if to == new_to { if to == new_to {
break; break;
} }
to = new_to; to = new_to;
} }
if map.buffer_snapshot.chars_at(to.to_point(map)).next() == Some(target) { let next = map.buffer_snapshot.chars_at(to.to_point(map)).next();
if next.is_some() && is_character_match(target, next.unwrap(), smartcase) {
if after { if after {
*to.column_mut() += 1; *to.column_mut() += 1;
map.clip_point(to, Bias::Right) map.clip_point(to, Bias::Right)
@ -1427,6 +1471,18 @@ fn find_backward(
} }
} }
fn is_character_match(target: char, other: char, smartcase: bool) -> bool {
if smartcase {
if target.is_uppercase() {
target == other
} else {
target == other.to_ascii_lowercase()
}
} else {
target == other
}
}
fn next_line_start(map: &DisplaySnapshot, point: DisplayPoint, times: usize) -> DisplayPoint { fn next_line_start(map: &DisplaySnapshot, point: DisplayPoint, times: usize) -> DisplayPoint {
let correct_line = start_of_relative_buffer_row(map, point, times as isize); let correct_line = start_of_relative_buffer_row(map, point, times as isize);
first_non_whitespace(map, false, correct_line) first_non_whitespace(map, false, correct_line)

View file

@ -1020,6 +1020,48 @@ mod test {
); );
} }
#[gpui::test]
async fn test_f_and_t_smartcase(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_smartcase_find = Some(true);
});
});
cx.assert_binding(
["f", "p"],
indoc! {"ˇfmt.Println(\"Hello, World!\")"},
Mode::Normal,
indoc! {"fmt.ˇPrintln(\"Hello, World!\")"},
Mode::Normal,
);
cx.assert_binding(
["shift-f", "p"],
indoc! {"fmt.Printlnˇ(\"Hello, World!\")"},
Mode::Normal,
indoc! {"fmt.ˇPrintln(\"Hello, World!\")"},
Mode::Normal,
);
cx.assert_binding(
["t", "p"],
indoc! {"ˇfmt.Println(\"Hello, World!\")"},
Mode::Normal,
indoc! {"fmtˇ.Println(\"Hello, World!\")"},
Mode::Normal,
);
cx.assert_binding(
["shift-t", "p"],
indoc! {"fmt.Printlnˇ(\"Hello, World!\")"},
Mode::Normal,
indoc! {"fmt.Pˇrintln(\"Hello, World!\")"},
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(["%"]);

View file

@ -487,6 +487,7 @@ impl Vim {
} else { } else {
FindRange::SingleLine FindRange::SingleLine
}, },
smartcase: VimSettings::get_global(cx).use_smartcase_find,
}; };
Vim::update(cx, |vim, _| { Vim::update(cx, |vim, _| {
vim.workspace_state.last_find = Some(find.clone()) vim.workspace_state.last_find = Some(find.clone())
@ -502,6 +503,7 @@ impl Vim {
} else { } else {
FindRange::SingleLine FindRange::SingleLine
}, },
smartcase: VimSettings::get_global(cx).use_smartcase_find,
}; };
Vim::update(cx, |vim, _| { Vim::update(cx, |vim, _| {
vim.workspace_state.last_find = Some(find.clone()) vim.workspace_state.last_find = Some(find.clone())
@ -642,12 +644,14 @@ struct VimSettings {
// 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, pub use_multiline_find: bool,
pub use_smartcase_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>, pub use_multiline_find: Option<bool>,
pub use_smartcase_find: Option<bool>,
} }
impl Settings for VimSettings { impl Settings for VimSettings {

View file

@ -172,7 +172,9 @@ Some vim settings are available to modify the default vim behavior:
// "on_yank": use system clipboard for yank operations // "on_yank": use system clipboard for yank operations
"use_system_clipboard": "always", "use_system_clipboard": "always",
// Enable multi-line find for `f` and `t` motions // Enable multi-line find for `f` and `t` motions
"use_multiline_find": false "use_multiline_find": false,
// Enable smartcase find for `f` and `t` motions
"use_smartcase_find": false
} }
} }
``` ```