Skip to content

Commit

Permalink
vim: Support ranges in command (#15985)
Browse files Browse the repository at this point in the history
The most requested feature here is "search and replace in visual mode",
but as a happy side effect we can now support things like :2,12j to join
those lines, and much much more.



Release Notes:

- vim: Added support for range syntax in command
([#9428](zed-industries/zed#9428)).
- vim: Prefill command with `:'<,'>` from visual mode
([#13535](zed-industries/zed#13535)).
  • Loading branch information
ConradIrwin authored Aug 8, 2024
1 parent b7d6b0a commit bd59af1
Show file tree
Hide file tree
Showing 10 changed files with 670 additions and 200 deletions.
16 changes: 9 additions & 7 deletions assets/keymaps/vim.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"bindings": {
"i": ["vim::PushOperator", { "Object": { "around": false } }],
"a": ["vim::PushOperator", { "Object": { "around": true } }],
":": "command_palette::Toggle",
"h": "vim::Left",
"left": "vim::Left",
"backspace": "vim::Backspace",
Expand Down Expand Up @@ -199,17 +198,12 @@
"ctrl-6": "pane::AlternateFile"
}
},
{
"context": "VimControl && VimCount",
"bindings": {
"0": ["vim::Number", 0]
}
},
{
"context": "vim_mode == normal",
"bindings": {
"escape": "editor::Cancel",
"ctrl-[": "editor::Cancel",
":": "command_palette::Toggle",
".": "vim::Repeat",
"c": ["vim::PushOperator", "Change"],
"shift-c": "vim::ChangeToEndOfLine",
Expand Down Expand Up @@ -257,9 +251,17 @@
"g c": ["vim::PushOperator", "ToggleComments"]
}
},
{
"context": "VimControl && VimCount",
"bindings": {
"0": ["vim::Number", 0],
":": "vim::CountCommand"
}
},
{
"context": "vim_mode == visual",
"bindings": {
":": "vim::VisualCommand",
"u": "vim::ConvertToLowerCase",
"U": "vim::ConvertToUpperCase",
"o": "vim::OtherEnd",
Expand Down
30 changes: 21 additions & 9 deletions crates/command_palette/src/command_palette.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,23 @@ fn trim_consecutive_whitespaces(input: &str) -> String {

impl CommandPalette {
fn register(workspace: &mut Workspace, _: &mut ViewContext<Workspace>) {
workspace.register_action(|workspace, _: &Toggle, cx| {
let Some(previous_focus_handle) = cx.focused() else {
return;
};
let telemetry = workspace.client().telemetry().clone();
workspace.toggle_modal(cx, move |cx| {
CommandPalette::new(previous_focus_handle, telemetry, cx)
});
workspace.register_action(|workspace, _: &Toggle, cx| Self::toggle(workspace, "", cx));
}

pub fn toggle(workspace: &mut Workspace, query: &str, cx: &mut ViewContext<Workspace>) {
let Some(previous_focus_handle) = cx.focused() else {
return;
};
let telemetry = workspace.client().telemetry().clone();
workspace.toggle_modal(cx, move |cx| {
CommandPalette::new(previous_focus_handle, telemetry, query, cx)
});
}

fn new(
previous_focus_handle: FocusHandle,
telemetry: Arc<Telemetry>,
query: &str,
cx: &mut ViewContext<Self>,
) -> Self {
let filter = CommandPaletteFilter::try_global(cx);
Expand All @@ -98,9 +101,18 @@ impl CommandPalette {
previous_focus_handle,
);

let picker = cx.new_view(|cx| Picker::uniform_list(delegate, cx));
let picker = cx.new_view(|cx| {
let picker = Picker::uniform_list(delegate, cx);
picker.set_query(query, cx);
picker
});
Self { picker }
}

pub fn set_query(&mut self, query: &str, cx: &mut ViewContext<Self>) {
self.picker
.update(cx, |picker, cx| picker.set_query(query, cx))
}
}

impl EventEmitter<DismissEvent> for CommandPalette {}
Expand Down
2 changes: 1 addition & 1 deletion crates/editor/src/selections_collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ impl<'a> MutableSelectionsCollection<'a> {
self.collection.display_map(self.cx)
}

fn buffer(&self) -> Ref<MultiBufferSnapshot> {
pub fn buffer(&self) -> Ref<MultiBufferSnapshot> {
self.collection.buffer(self.cx)
}

Expand Down
1 change: 1 addition & 0 deletions crates/vim/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ anyhow.workspace = true
async-compat = { version = "0.2.1", "optional" = true }
async-trait = { workspace = true, "optional" = true }
collections.workspace = true
command_palette.workspace = true
command_palette_hooks.workspace = true
editor.workspace = true
gpui.workspace = true
Expand Down
Loading

0 comments on commit bd59af1

Please sign in to comment.