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:
Piotr Osiewicz 2024-02-19 17:41:43 +01:00 committed by GitHub
parent ca251babcd
commit f17d0b5729
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
30 changed files with 1394 additions and 275 deletions

View 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()))
}