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:
@@ -91,6 +91,12 @@ impl FeatureFlag for ThreadAutoCaptureFeatureFlag {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct JjUiFeatureFlag {}
|
||||
|
||||
impl FeatureFlag for JjUiFeatureFlag {
|
||||
const NAME: &'static str = "jj-ui";
|
||||
}
|
||||
|
||||
pub trait FeatureFlagViewExt<V: 'static> {
|
||||
fn observe_flag<T: FeatureFlag, F>(&mut self, window: &Window, callback: F) -> Subscription
|
||||
where
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
[package]
|
||||
name = "jj"
|
||||
version = "0.1.0"
|
||||
publish.workspace = true
|
||||
edition.workspace = true
|
||||
license = "GPL-3.0-or-later"
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
[lib]
|
||||
path = "src/jj.rs"
|
||||
|
||||
[dependencies]
|
||||
anyhow.workspace = true
|
||||
gpui.workspace = true
|
||||
jj-lib.workspace = true
|
||||
workspace-hack.workspace = true
|
||||
Symlink
+1
@@ -0,0 +1 @@
|
||||
../../LICENSE-GPL
|
||||
@@ -0,0 +1,5 @@
|
||||
mod jj_repository;
|
||||
mod jj_store;
|
||||
|
||||
pub use jj_repository::*;
|
||||
pub use jj_store::*;
|
||||
@@ -0,0 +1,72 @@
|
||||
use std::path::Path;
|
||||
use std::sync::Arc;
|
||||
|
||||
use anyhow::Result;
|
||||
use gpui::SharedString;
|
||||
use jj_lib::config::StackedConfig;
|
||||
use jj_lib::repo::StoreFactories;
|
||||
use jj_lib::settings::UserSettings;
|
||||
use jj_lib::workspace::{self, DefaultWorkspaceLoaderFactory, WorkspaceLoaderFactory};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Bookmark {
|
||||
pub ref_name: SharedString,
|
||||
}
|
||||
|
||||
pub trait JujutsuRepository: Send + Sync {
|
||||
fn list_bookmarks(&self) -> Vec<Bookmark>;
|
||||
}
|
||||
|
||||
pub struct RealJujutsuRepository {
|
||||
repository: Arc<jj_lib::repo::ReadonlyRepo>,
|
||||
}
|
||||
|
||||
impl RealJujutsuRepository {
|
||||
pub fn new(cwd: &Path) -> Result<Self> {
|
||||
let workspace_loader_factory = DefaultWorkspaceLoaderFactory;
|
||||
let workspace_loader = workspace_loader_factory.create(Self::find_workspace_dir(cwd))?;
|
||||
|
||||
let config = StackedConfig::with_defaults();
|
||||
let settings = UserSettings::from_config(config)?;
|
||||
|
||||
let workspace = workspace_loader.load(
|
||||
&settings,
|
||||
&StoreFactories::default(),
|
||||
&workspace::default_working_copy_factories(),
|
||||
)?;
|
||||
|
||||
let repo_loader = workspace.repo_loader();
|
||||
let repository = repo_loader.load_at_head()?;
|
||||
|
||||
Ok(Self { repository })
|
||||
}
|
||||
|
||||
fn find_workspace_dir(cwd: &Path) -> &Path {
|
||||
cwd.ancestors()
|
||||
.find(|path| path.join(".jj").is_dir())
|
||||
.unwrap_or(cwd)
|
||||
}
|
||||
}
|
||||
|
||||
impl JujutsuRepository for RealJujutsuRepository {
|
||||
fn list_bookmarks(&self) -> Vec<Bookmark> {
|
||||
let bookmarks = self
|
||||
.repository
|
||||
.view()
|
||||
.bookmarks()
|
||||
.map(|(ref_name, _target)| Bookmark {
|
||||
ref_name: ref_name.as_str().to_string().into(),
|
||||
})
|
||||
.collect();
|
||||
|
||||
bookmarks
|
||||
}
|
||||
}
|
||||
|
||||
pub struct FakeJujutsuRepository {}
|
||||
|
||||
impl JujutsuRepository for FakeJujutsuRepository {
|
||||
fn list_bookmarks(&self) -> Vec<Bookmark> {
|
||||
Vec::new()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
use std::path::Path;
|
||||
use std::sync::Arc;
|
||||
|
||||
use gpui::{App, Entity, Global, prelude::*};
|
||||
|
||||
use crate::{JujutsuRepository, RealJujutsuRepository};
|
||||
|
||||
/// Note: We won't ultimately be storing the jj store in a global, we're just doing this for exploration purposes.
|
||||
struct GlobalJujutsuStore(Entity<JujutsuStore>);
|
||||
|
||||
impl Global for GlobalJujutsuStore {}
|
||||
|
||||
pub struct JujutsuStore {
|
||||
repository: Arc<dyn JujutsuRepository>,
|
||||
}
|
||||
|
||||
impl JujutsuStore {
|
||||
pub fn init_global(cx: &mut App) {
|
||||
let Some(repository) = RealJujutsuRepository::new(&Path::new(".")).ok() else {
|
||||
return;
|
||||
};
|
||||
|
||||
let repository = Arc::new(repository);
|
||||
let jj_store = cx.new(|cx| JujutsuStore::new(repository, cx));
|
||||
|
||||
cx.set_global(GlobalJujutsuStore(jj_store));
|
||||
}
|
||||
|
||||
pub fn try_global(cx: &App) -> Option<Entity<Self>> {
|
||||
cx.try_global::<GlobalJujutsuStore>()
|
||||
.map(|global| global.0.clone())
|
||||
}
|
||||
|
||||
pub fn new(repository: Arc<dyn JujutsuRepository>, _cx: &mut Context<Self>) -> Self {
|
||||
Self { repository }
|
||||
}
|
||||
|
||||
pub fn repository(&self) -> &Arc<dyn JujutsuRepository> {
|
||||
&self.repository
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
Symlink
+1
@@ -0,0 +1 @@
|
||||
../../LICENSE-GPL
|
||||
@@ -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(),
|
||||
)),
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
@@ -67,6 +67,7 @@ image_viewer.workspace = true
|
||||
indoc.workspace = true
|
||||
inline_completion_button.workspace = true
|
||||
install_cli.workspace = true
|
||||
jj_ui.workspace = true
|
||||
journal.workspace = true
|
||||
language.workspace = true
|
||||
language_extension.workspace = true
|
||||
|
||||
@@ -565,6 +565,7 @@ fn main() {
|
||||
notifications::init(app_state.client.clone(), app_state.user_store.clone(), cx);
|
||||
collab_ui::init(&app_state, cx);
|
||||
git_ui::init(cx);
|
||||
jj_ui::init(cx);
|
||||
feedback::init(cx);
|
||||
markdown_preview::init(cx);
|
||||
welcome::init(cx);
|
||||
|
||||
@@ -142,6 +142,12 @@ pub mod git {
|
||||
action_with_deprecated_aliases!(git, Branch, ["branches::OpenRecent"]);
|
||||
}
|
||||
|
||||
pub mod jj {
|
||||
use gpui::actions;
|
||||
|
||||
actions!(jj, [BookmarkList]);
|
||||
}
|
||||
|
||||
pub mod command_palette {
|
||||
use gpui::actions;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user