Add static Runnables (#8009)
Part of #7108 This PR includes just the static runnables part. We went with **not** having a dedicated panel for runnables. This is just a 1st PR out of N, as we want to start exploring the dynamic runnables front. Still, all that work is going to happen once this gets merged. Release Notes: - Added initial, static Runnables support to Zed. Such runnables are defined in `runnables.json` file (accessible via `zed: open runnables` action) and they can be spawned with `runnables: spawn` action. --------- Co-authored-by: Kirill Bulatov <kirill@zed.dev> Co-authored-by: Pitor <pitor@zed.dev> Co-authored-by: Beniamin <beniamin@zagan.be>
This commit is contained in:
parent
ca251babcd
commit
f17d0b5729
30 changed files with 1394 additions and 275 deletions
24
crates/runnables_ui/Cargo.toml
Normal file
24
crates/runnables_ui/Cargo.toml
Normal file
|
@ -0,0 +1,24 @@
|
|||
[package]
|
||||
name = "runnables_ui"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
anyhow.workspace = true
|
||||
db.workspace = true
|
||||
editor.workspace = true
|
||||
fs.workspace = true
|
||||
futures.workspace = true
|
||||
fuzzy.workspace = true
|
||||
gpui.workspace = true
|
||||
log.workspace = true
|
||||
picker.workspace = true
|
||||
project.workspace = true
|
||||
runnable.workspace = true
|
||||
schemars.workspace = true
|
||||
serde.workspace = true
|
||||
serde_json.workspace = true
|
||||
theme.workspace = true
|
||||
ui.workspace = true
|
||||
util.workspace = true
|
||||
workspace.workspace = true
|
89
crates/runnables_ui/src/lib.rs
Normal file
89
crates/runnables_ui/src/lib.rs
Normal file
|
@ -0,0 +1,89 @@
|
|||
use std::path::PathBuf;
|
||||
|
||||
use gpui::{AppContext, ViewContext, WindowContext};
|
||||
use modal::RunnablesModal;
|
||||
use runnable::Runnable;
|
||||
use util::ResultExt;
|
||||
use workspace::Workspace;
|
||||
|
||||
mod modal;
|
||||
|
||||
pub fn init(cx: &mut AppContext) {
|
||||
cx.observe_new_views(
|
||||
|workspace: &mut Workspace, _: &mut ViewContext<Workspace>| {
|
||||
workspace
|
||||
.register_action(|workspace, _: &modal::Spawn, cx| {
|
||||
let inventory = workspace.project().read(cx).runnable_inventory().clone();
|
||||
let workspace_handle = workspace.weak_handle();
|
||||
workspace.toggle_modal(cx, |cx| {
|
||||
RunnablesModal::new(inventory, workspace_handle, cx)
|
||||
})
|
||||
})
|
||||
.register_action(move |workspace, _: &modal::Rerun, cx| {
|
||||
if let Some(runnable) = workspace.project().update(cx, |project, cx| {
|
||||
project
|
||||
.runnable_inventory()
|
||||
.update(cx, |inventory, cx| inventory.last_scheduled_runnable(cx))
|
||||
}) {
|
||||
schedule_runnable(workspace, runnable.as_ref(), cx)
|
||||
};
|
||||
});
|
||||
},
|
||||
)
|
||||
.detach();
|
||||
}
|
||||
|
||||
fn schedule_runnable(
|
||||
workspace: &Workspace,
|
||||
runnable: &dyn Runnable,
|
||||
cx: &mut ViewContext<'_, Workspace>,
|
||||
) {
|
||||
let cwd = match runnable.cwd() {
|
||||
Some(cwd) => Some(cwd.to_path_buf()),
|
||||
None => runnable_cwd(workspace, cx).log_err().flatten(),
|
||||
};
|
||||
let spawn_in_terminal = runnable.exec(cwd);
|
||||
if let Some(spawn_in_terminal) = spawn_in_terminal {
|
||||
workspace.project().update(cx, |project, cx| {
|
||||
project.runnable_inventory().update(cx, |inventory, _| {
|
||||
inventory.last_scheduled_runnable = Some(runnable.id().clone());
|
||||
})
|
||||
});
|
||||
cx.emit(workspace::Event::SpawnRunnable(spawn_in_terminal));
|
||||
}
|
||||
}
|
||||
|
||||
fn runnable_cwd(workspace: &Workspace, cx: &mut WindowContext) -> anyhow::Result<Option<PathBuf>> {
|
||||
let project = workspace.project().read(cx);
|
||||
let available_worktrees = project
|
||||
.worktrees()
|
||||
.filter(|worktree| {
|
||||
let worktree = worktree.read(cx);
|
||||
worktree.is_visible()
|
||||
&& worktree.is_local()
|
||||
&& worktree.root_entry().map_or(false, |e| e.is_dir())
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
let cwd = match available_worktrees.len() {
|
||||
0 => None,
|
||||
1 => Some(available_worktrees[0].read(cx).abs_path()),
|
||||
_ => {
|
||||
let cwd_for_active_entry = project.active_entry().and_then(|entry_id| {
|
||||
available_worktrees.into_iter().find_map(|worktree| {
|
||||
let worktree = worktree.read(cx);
|
||||
if worktree.contains_entry(entry_id) {
|
||||
Some(worktree.abs_path())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
});
|
||||
anyhow::ensure!(
|
||||
cwd_for_active_entry.is_some(),
|
||||
"Cannot determine runnable cwd for multiple worktrees"
|
||||
);
|
||||
cwd_for_active_entry
|
||||
}
|
||||
};
|
||||
Ok(cwd.map(|path| path.to_path_buf()))
|
||||
}
|
201
crates/runnables_ui/src/modal.rs
Normal file
201
crates/runnables_ui/src/modal.rs
Normal file
|
@ -0,0 +1,201 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use fuzzy::{StringMatch, StringMatchCandidate};
|
||||
use gpui::{
|
||||
actions, rems, DismissEvent, EventEmitter, FocusableView, InteractiveElement, Model,
|
||||
ParentElement, Render, SharedString, Styled, Subscription, Task, View, ViewContext,
|
||||
VisualContext, WeakView,
|
||||
};
|
||||
use picker::{Picker, PickerDelegate};
|
||||
use project::Inventory;
|
||||
use runnable::Runnable;
|
||||
use ui::{v_flex, HighlightedLabel, ListItem, ListItemSpacing, Selectable};
|
||||
use util::ResultExt;
|
||||
use workspace::{ModalView, Workspace};
|
||||
|
||||
use crate::schedule_runnable;
|
||||
|
||||
actions!(runnables, [Spawn, Rerun]);
|
||||
|
||||
/// A modal used to spawn new runnables.
|
||||
pub(crate) struct RunnablesModalDelegate {
|
||||
inventory: Model<Inventory>,
|
||||
candidates: Vec<Arc<dyn Runnable>>,
|
||||
matches: Vec<StringMatch>,
|
||||
selected_index: usize,
|
||||
placeholder_text: Arc<str>,
|
||||
workspace: WeakView<Workspace>,
|
||||
}
|
||||
|
||||
impl RunnablesModalDelegate {
|
||||
fn new(inventory: Model<Inventory>, workspace: WeakView<Workspace>) -> Self {
|
||||
Self {
|
||||
inventory,
|
||||
workspace,
|
||||
candidates: Vec::new(),
|
||||
matches: Vec::new(),
|
||||
selected_index: 0,
|
||||
placeholder_text: Arc::from("Select runnable..."),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct RunnablesModal {
|
||||
picker: View<Picker<RunnablesModalDelegate>>,
|
||||
_subscription: Subscription,
|
||||
}
|
||||
|
||||
impl RunnablesModal {
|
||||
pub(crate) fn new(
|
||||
inventory: Model<Inventory>,
|
||||
workspace: WeakView<Workspace>,
|
||||
cx: &mut ViewContext<Self>,
|
||||
) -> Self {
|
||||
let picker = cx.new_view(|cx| {
|
||||
Picker::uniform_list(RunnablesModalDelegate::new(inventory, workspace), cx)
|
||||
});
|
||||
let _subscription = cx.subscribe(&picker, |_, _, _, cx| {
|
||||
cx.emit(DismissEvent);
|
||||
});
|
||||
Self {
|
||||
picker,
|
||||
_subscription,
|
||||
}
|
||||
}
|
||||
}
|
||||
impl Render for RunnablesModal {
|
||||
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl gpui::prelude::IntoElement {
|
||||
v_flex()
|
||||
.w(rems(34.))
|
||||
.child(self.picker.clone())
|
||||
.on_mouse_down_out(cx.listener(|modal, _, cx| {
|
||||
modal.picker.update(cx, |picker, cx| {
|
||||
picker.cancel(&Default::default(), cx);
|
||||
})
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
impl EventEmitter<DismissEvent> for RunnablesModal {}
|
||||
impl FocusableView for RunnablesModal {
|
||||
fn focus_handle(&self, cx: &gpui::AppContext) -> gpui::FocusHandle {
|
||||
self.picker.read(cx).focus_handle(cx)
|
||||
}
|
||||
}
|
||||
impl ModalView for RunnablesModal {}
|
||||
|
||||
impl PickerDelegate for RunnablesModalDelegate {
|
||||
type ListItem = ListItem;
|
||||
|
||||
fn match_count(&self) -> usize {
|
||||
self.matches.len()
|
||||
}
|
||||
|
||||
fn selected_index(&self) -> usize {
|
||||
self.selected_index
|
||||
}
|
||||
|
||||
fn set_selected_index(&mut self, ix: usize, _cx: &mut ViewContext<picker::Picker<Self>>) {
|
||||
self.selected_index = ix;
|
||||
}
|
||||
|
||||
fn placeholder_text(&self) -> Arc<str> {
|
||||
self.placeholder_text.clone()
|
||||
}
|
||||
|
||||
fn update_matches(
|
||||
&mut self,
|
||||
query: String,
|
||||
cx: &mut ViewContext<picker::Picker<Self>>,
|
||||
) -> Task<()> {
|
||||
cx.spawn(move |picker, mut cx| async move {
|
||||
let Some(candidates) = picker
|
||||
.update(&mut cx, |picker, cx| {
|
||||
picker.delegate.candidates = picker
|
||||
.delegate
|
||||
.inventory
|
||||
.update(cx, |inventory, cx| inventory.list_runnables(None, cx));
|
||||
picker
|
||||
.delegate
|
||||
.candidates
|
||||
.sort_by(|a, b| a.name().cmp(&b.name()));
|
||||
|
||||
picker
|
||||
.delegate
|
||||
.candidates
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(index, candidate)| StringMatchCandidate {
|
||||
id: index,
|
||||
char_bag: candidate.name().chars().collect(),
|
||||
string: candidate.name().into(),
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
})
|
||||
.ok()
|
||||
else {
|
||||
return;
|
||||
};
|
||||
let matches = fuzzy::match_strings(
|
||||
&candidates,
|
||||
&query,
|
||||
true,
|
||||
1000,
|
||||
&Default::default(),
|
||||
cx.background_executor().clone(),
|
||||
)
|
||||
.await;
|
||||
picker
|
||||
.update(&mut cx, |picker, _| {
|
||||
let delegate = &mut picker.delegate;
|
||||
delegate.matches = matches;
|
||||
|
||||
if delegate.matches.is_empty() {
|
||||
delegate.selected_index = 0;
|
||||
} else {
|
||||
delegate.selected_index =
|
||||
delegate.selected_index.min(delegate.matches.len() - 1);
|
||||
}
|
||||
})
|
||||
.log_err();
|
||||
})
|
||||
}
|
||||
|
||||
fn confirm(&mut self, _secondary: bool, cx: &mut ViewContext<picker::Picker<Self>>) {
|
||||
let current_match_index = self.selected_index();
|
||||
let Some(current_match) = self.matches.get(current_match_index) else {
|
||||
return;
|
||||
};
|
||||
|
||||
let ix = current_match.candidate_id;
|
||||
let runnable = &self.candidates[ix];
|
||||
self.workspace
|
||||
.update(cx, |workspace, cx| {
|
||||
schedule_runnable(workspace, runnable.as_ref(), cx);
|
||||
})
|
||||
.ok();
|
||||
cx.emit(DismissEvent);
|
||||
}
|
||||
|
||||
fn dismissed(&mut self, cx: &mut ViewContext<picker::Picker<Self>>) {
|
||||
cx.emit(DismissEvent);
|
||||
}
|
||||
|
||||
fn render_match(
|
||||
&self,
|
||||
ix: usize,
|
||||
selected: bool,
|
||||
_cx: &mut ViewContext<picker::Picker<Self>>,
|
||||
) -> Option<Self::ListItem> {
|
||||
let hit = &self.matches[ix];
|
||||
//let runnable = self.candidates[target_index].metadata();
|
||||
let highlights: Vec<_> = hit.positions.iter().copied().collect();
|
||||
Some(
|
||||
ListItem::new(SharedString::from(format!("runnables-modal-{ix}")))
|
||||
.inset(true)
|
||||
.spacing(ListItemSpacing::Sparse)
|
||||
.selected(selected)
|
||||
.start_slot(HighlightedLabel::new(hit.string.clone(), highlights)),
|
||||
)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue