Add a picker for jj bookmark list
(#30883)
This PR adds a new picker for viewing a list of jj bookmarks, like you would with `jj bookmark list`. This is an exploration around what it would look like to begin adding some dedicated jj features to Zed. This is behind the `jj-ui` feature flag. Release Notes: - N/A
This commit is contained in:
parent
122d6c9e4d
commit
dd3956eaf1
16 changed files with 1644 additions and 152 deletions
25
crates/jj_ui/Cargo.toml
Normal file
25
crates/jj_ui/Cargo.toml
Normal file
|
@ -0,0 +1,25 @@
|
|||
[package]
|
||||
name = "jj_ui"
|
||||
version = "0.1.0"
|
||||
publish.workspace = true
|
||||
edition.workspace = true
|
||||
license = "GPL-3.0-or-later"
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
[lib]
|
||||
path = "src/jj_ui.rs"
|
||||
|
||||
[dependencies]
|
||||
command_palette_hooks.workspace = true
|
||||
feature_flags.workspace = true
|
||||
fuzzy.workspace = true
|
||||
gpui.workspace = true
|
||||
jj.workspace = true
|
||||
picker.workspace = true
|
||||
ui.workspace = true
|
||||
util.workspace = true
|
||||
workspace-hack.workspace = true
|
||||
workspace.workspace = true
|
||||
zed_actions.workspace = true
|
1
crates/jj_ui/LICENSE-GPL
Symbolic link
1
crates/jj_ui/LICENSE-GPL
Symbolic link
|
@ -0,0 +1 @@
|
|||
../../LICENSE-GPL
|
197
crates/jj_ui/src/bookmark_picker.rs
Normal file
197
crates/jj_ui/src/bookmark_picker.rs
Normal file
|
@ -0,0 +1,197 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use fuzzy::{StringMatchCandidate, match_strings};
|
||||
use gpui::{
|
||||
App, DismissEvent, Entity, EventEmitter, FocusHandle, Focusable, Task, WeakEntity, Window,
|
||||
prelude::*,
|
||||
};
|
||||
use jj::{Bookmark, JujutsuStore};
|
||||
use picker::{Picker, PickerDelegate};
|
||||
use ui::{HighlightedLabel, ListItem, ListItemSpacing, prelude::*};
|
||||
use util::ResultExt as _;
|
||||
use workspace::{ModalView, Workspace};
|
||||
|
||||
pub fn register(workspace: &mut Workspace) {
|
||||
workspace.register_action(open);
|
||||
}
|
||||
|
||||
fn open(
|
||||
workspace: &mut Workspace,
|
||||
_: &zed_actions::jj::BookmarkList,
|
||||
window: &mut Window,
|
||||
cx: &mut Context<Workspace>,
|
||||
) {
|
||||
let Some(jj_store) = JujutsuStore::try_global(cx) else {
|
||||
return;
|
||||
};
|
||||
|
||||
workspace.toggle_modal(window, cx, |window, cx| {
|
||||
let delegate = BookmarkPickerDelegate::new(cx.entity().downgrade(), jj_store, cx);
|
||||
BookmarkPicker::new(delegate, window, cx)
|
||||
});
|
||||
}
|
||||
|
||||
pub struct BookmarkPicker {
|
||||
picker: Entity<Picker<BookmarkPickerDelegate>>,
|
||||
}
|
||||
|
||||
impl BookmarkPicker {
|
||||
pub fn new(
|
||||
delegate: BookmarkPickerDelegate,
|
||||
window: &mut Window,
|
||||
cx: &mut Context<Self>,
|
||||
) -> Self {
|
||||
let picker = cx.new(|cx| Picker::uniform_list(delegate, window, cx));
|
||||
Self { picker }
|
||||
}
|
||||
}
|
||||
|
||||
impl ModalView for BookmarkPicker {}
|
||||
|
||||
impl EventEmitter<DismissEvent> for BookmarkPicker {}
|
||||
|
||||
impl Focusable for BookmarkPicker {
|
||||
fn focus_handle(&self, cx: &App) -> FocusHandle {
|
||||
self.picker.focus_handle(cx)
|
||||
}
|
||||
}
|
||||
|
||||
impl Render for BookmarkPicker {
|
||||
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
|
||||
v_flex().w(rems(34.)).child(self.picker.clone())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct BookmarkEntry {
|
||||
bookmark: Bookmark,
|
||||
positions: Vec<usize>,
|
||||
}
|
||||
|
||||
pub struct BookmarkPickerDelegate {
|
||||
picker: WeakEntity<BookmarkPicker>,
|
||||
matches: Vec<BookmarkEntry>,
|
||||
all_bookmarks: Vec<Bookmark>,
|
||||
selected_index: usize,
|
||||
}
|
||||
|
||||
impl BookmarkPickerDelegate {
|
||||
fn new(
|
||||
picker: WeakEntity<BookmarkPicker>,
|
||||
jj_store: Entity<JujutsuStore>,
|
||||
cx: &mut Context<BookmarkPicker>,
|
||||
) -> Self {
|
||||
let bookmarks = jj_store.read(cx).repository().list_bookmarks();
|
||||
|
||||
Self {
|
||||
picker,
|
||||
matches: Vec::new(),
|
||||
all_bookmarks: bookmarks,
|
||||
selected_index: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PickerDelegate for BookmarkPickerDelegate {
|
||||
type ListItem = ListItem;
|
||||
|
||||
fn placeholder_text(&self, _window: &mut Window, _cx: &mut App) -> Arc<str> {
|
||||
"Select Bookmark…".into()
|
||||
}
|
||||
|
||||
fn match_count(&self) -> usize {
|
||||
self.matches.len()
|
||||
}
|
||||
|
||||
fn selected_index(&self) -> usize {
|
||||
self.selected_index
|
||||
}
|
||||
|
||||
fn set_selected_index(
|
||||
&mut self,
|
||||
ix: usize,
|
||||
_window: &mut Window,
|
||||
_cx: &mut Context<Picker<Self>>,
|
||||
) {
|
||||
self.selected_index = ix;
|
||||
}
|
||||
|
||||
fn update_matches(
|
||||
&mut self,
|
||||
query: String,
|
||||
window: &mut Window,
|
||||
cx: &mut Context<Picker<Self>>,
|
||||
) -> Task<()> {
|
||||
let background = cx.background_executor().clone();
|
||||
let all_bookmarks = self.all_bookmarks.clone();
|
||||
|
||||
cx.spawn_in(window, async move |this, cx| {
|
||||
let matches = if query.is_empty() {
|
||||
all_bookmarks
|
||||
.into_iter()
|
||||
.map(|bookmark| BookmarkEntry {
|
||||
bookmark,
|
||||
positions: Vec::new(),
|
||||
})
|
||||
.collect()
|
||||
} else {
|
||||
let candidates = all_bookmarks
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(ix, bookmark)| StringMatchCandidate::new(ix, &bookmark.ref_name))
|
||||
.collect::<Vec<_>>();
|
||||
match_strings(
|
||||
&candidates,
|
||||
&query,
|
||||
false,
|
||||
100,
|
||||
&Default::default(),
|
||||
background,
|
||||
)
|
||||
.await
|
||||
.into_iter()
|
||||
.map(|mat| BookmarkEntry {
|
||||
bookmark: all_bookmarks[mat.candidate_id].clone(),
|
||||
positions: mat.positions,
|
||||
})
|
||||
.collect()
|
||||
};
|
||||
|
||||
this.update(cx, |this, _cx| {
|
||||
this.delegate.matches = matches;
|
||||
})
|
||||
.log_err();
|
||||
})
|
||||
}
|
||||
|
||||
fn confirm(&mut self, _secondary: bool, _window: &mut Window, _cx: &mut Context<Picker<Self>>) {
|
||||
//
|
||||
}
|
||||
|
||||
fn dismissed(&mut self, _window: &mut Window, cx: &mut Context<Picker<Self>>) {
|
||||
self.picker
|
||||
.update(cx, |_, cx| cx.emit(DismissEvent))
|
||||
.log_err();
|
||||
}
|
||||
|
||||
fn render_match(
|
||||
&self,
|
||||
ix: usize,
|
||||
selected: bool,
|
||||
_window: &mut Window,
|
||||
_cx: &mut Context<Picker<Self>>,
|
||||
) -> Option<Self::ListItem> {
|
||||
let entry = &self.matches[ix];
|
||||
|
||||
Some(
|
||||
ListItem::new(ix)
|
||||
.inset(true)
|
||||
.spacing(ListItemSpacing::Sparse)
|
||||
.toggle_state(selected)
|
||||
.child(HighlightedLabel::new(
|
||||
entry.bookmark.ref_name.clone(),
|
||||
entry.positions.clone(),
|
||||
)),
|
||||
)
|
||||
}
|
||||
}
|
39
crates/jj_ui/src/jj_ui.rs
Normal file
39
crates/jj_ui/src/jj_ui.rs
Normal file
|
@ -0,0 +1,39 @@
|
|||
mod bookmark_picker;
|
||||
|
||||
use command_palette_hooks::CommandPaletteFilter;
|
||||
use feature_flags::FeatureFlagAppExt as _;
|
||||
use gpui::App;
|
||||
use jj::JujutsuStore;
|
||||
use workspace::Workspace;
|
||||
|
||||
pub fn init(cx: &mut App) {
|
||||
JujutsuStore::init_global(cx);
|
||||
|
||||
cx.observe_new(|workspace: &mut Workspace, _window, _cx| {
|
||||
bookmark_picker::register(workspace);
|
||||
})
|
||||
.detach();
|
||||
|
||||
feature_gate_jj_ui_actions(cx);
|
||||
}
|
||||
|
||||
fn feature_gate_jj_ui_actions(cx: &mut App) {
|
||||
const JJ_ACTION_NAMESPACE: &str = "jj";
|
||||
|
||||
CommandPaletteFilter::update_global(cx, |filter, _cx| {
|
||||
filter.hide_namespace(JJ_ACTION_NAMESPACE);
|
||||
});
|
||||
|
||||
cx.observe_flag::<feature_flags::JjUiFeatureFlag, _>({
|
||||
move |is_enabled, cx| {
|
||||
CommandPaletteFilter::update_global(cx, |filter, _cx| {
|
||||
if is_enabled {
|
||||
filter.show_namespace(JJ_ACTION_NAMESPACE);
|
||||
} else {
|
||||
filter.hide_namespace(JJ_ACTION_NAMESPACE);
|
||||
}
|
||||
});
|
||||
}
|
||||
})
|
||||
.detach();
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue