Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

Picker: Support child and parent navigation ( menu::SelectChild and menu::SelectParent in PickerDelegate ) fixes (#61053) (#61314)

vinod akshatcb8a0170ff5d
diff --git a/crates/open_path_prompt/src/open_path_prompt.rs b/crates/open_path_prompt/src/open_path_prompt.rs
index 5fa29d453ac..cbbebeb6a7f 100644
--- a/crates/open_path_prompt/src/open_path_prompt.rs
+++ b/crates/open_path_prompt/src/open_path_prompt.rs
@@ -577,7 +577,45 @@ impl PickerDelegate for OpenPathDelegate {
.ok();
})
}
+ fn select_child(
+ &mut self,
+ _window: &mut Window,
+ _cx: &mut Context<Picker<Self>>,
+ ) -> Option<String> {
+ let candidate = self.get_entry(self.selected_index)?;
+ if candidate.path.string.is_empty() || !candidate.is_dir {
+ return None;
+ }
+ let path_style = self.path_style;
+ match &self.directory_state {
+ DirectoryState::List { parent_path, .. }
+ | DirectoryState::Create { parent_path, .. } => Some(format!(
+ "{}{}{}",
+ parent_path,
+ candidate.path.string,
+ path_style.primary_separator()
+ )),
+ DirectoryState::None { .. } => None,
+ }
+ }
+ fn select_parent(
+ &mut self,
+ _window: &mut Window,
+ _cx: &mut Context<Picker<Self>>,
+ ) -> Option<String> {
+ let parent_path = match &self.directory_state {
+ DirectoryState::List { parent_path, .. }
+ | DirectoryState::Create { parent_path, .. } => parent_path,
+ DirectoryState::None { .. } => return None,
+ };
+ if parent_path == &self.prompt_root {
+ return None;
+ }
+ let trimmed = parent_path.trim_end_matches(['/', '\\']);
+ let (dir, _) = get_dir_and_suffix(trimmed.to_string(), self.path_style);
+ Some(dir)
+ }
fn confirm_completion(
&mut self,
query: String,
diff --git a/crates/picker/src/picker.rs b/crates/picker/src/picker.rs
index 9f8baac0cba..ad20dfc28ce 100644
--- a/crates/picker/src/picker.rs
+++ b/crates/picker/src/picker.rs
@@ -301,6 +301,27 @@ pub trait PickerDelegate: Sized + 'static {
) -> Option<String> {
None
}
+ /// Called when `SelectChild` fires (e.g. shift-right-arrow). Return `Some(query)`
+ /// to step into the currently selected item (e.g. a directory); the picker
+ /// will set the query and refresh matches.
+ fn select_child(
+ &mut self,
+ _window: &mut Window,
+ _cx: &mut Context<Picker<Self>>,
+ ) -> Option<String> {
+ None
+ }
+
+ /// Called when `SelectParent` fires (e.g. shift-left-arrow). Return `Some(query)`
+ /// to step back to the parent; the picker will set the query and refresh
+ /// matches.
+ fn select_parent(
+ &mut self,
+ _window: &mut Window,
+ _cx: &mut Context<Picker<Self>>,
+ ) -> Option<String> {
+ None
+ }
fn editor_position(&self) -> PickerEditorPosition {
PickerEditorPosition::default()
@@ -1002,6 +1023,26 @@ impl<D: PickerDelegate> Picker<D> {
cx.propagate()
}
}
+ fn select_child(&mut self, _: &menu::SelectChild, window: &mut Window, cx: &mut Context<Self>) {
+ if let Some(new_query) = self.delegate.select_child(window, cx) {
+ self.set_query(&new_query, window, cx);
+ } else {
+ cx.propagate()
+ }
+ }
+
+ fn select_parent(
+ &mut self,
+ _: &menu::SelectParent,
+ window: &mut Window,
+ cx: &mut Context<Self>,
+ ) {
+ if let Some(new_query) = self.delegate.select_parent(window, cx) {
+ self.set_query(&new_query, window, cx);
+ } else {
+ cx.propagate()
+ }
+ }
fn set_preview_right(
&mut self,
diff --git a/crates/picker/src/render.rs b/crates/picker/src/render.rs
index 6abdf571139..d9bf085707e 100644
--- a/crates/picker/src/render.rs
+++ b/crates/picker/src/render.rs
@@ -192,6 +192,8 @@ impl<D: PickerDelegate> Picker<D> {
.on_action(cx.listener(Self::editor_move_up))
.on_action(cx.listener(Self::select_first))
.on_action(cx.listener(Self::select_last))
+ .on_action(cx.listener(Self::select_child))
+ .on_action(cx.listener(Self::select_parent))
.on_action(cx.listener(Self::cancel))
.on_action(cx.listener(Self::confirm))
.on_action(cx.listener(Self::secondary_confirm))