Allow clients to run Zed tasks on remote projects (#12199)
Release Notes: - Enabled Zed tasks on remote projects with ssh connection string specified --------- Co-authored-by: Conrad Irwin <conrad@zed.dev>
This commit is contained in:
parent
df35fd0026
commit
055a13a9b6
16 changed files with 1250 additions and 600 deletions
|
@ -9,6 +9,7 @@ license = "GPL-3.0-or-later"
|
|||
workspace = true
|
||||
|
||||
[dependencies]
|
||||
anyhow.workspace = true
|
||||
editor.workspace = true
|
||||
file_icons.workspace = true
|
||||
fuzzy.workspace = true
|
||||
|
|
|
@ -1,11 +1,8 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use ::settings::Settings;
|
||||
use editor::{tasks::task_context, Editor};
|
||||
use gpui::{AppContext, ViewContext, WindowContext};
|
||||
use language::Language;
|
||||
use gpui::{AppContext, Task as AsyncTask, ViewContext, WindowContext};
|
||||
use modal::TasksModal;
|
||||
use project::WorktreeId;
|
||||
use project::{Location, WorktreeId};
|
||||
use workspace::tasks::schedule_task;
|
||||
use workspace::{tasks::schedule_resolved_task, Workspace};
|
||||
|
||||
|
@ -34,15 +31,23 @@ pub fn init(cx: &mut AppContext) {
|
|||
if let Some(use_new_terminal) = action.use_new_terminal {
|
||||
original_task.use_new_terminal = use_new_terminal;
|
||||
}
|
||||
let task_context = task_context(workspace, cx);
|
||||
schedule_task(
|
||||
workspace,
|
||||
task_source_kind,
|
||||
&original_task,
|
||||
&task_context,
|
||||
false,
|
||||
cx,
|
||||
)
|
||||
let context_task = task_context(workspace, cx);
|
||||
cx.spawn(|workspace, mut cx| async move {
|
||||
let task_context = context_task.await;
|
||||
workspace
|
||||
.update(&mut cx, |workspace, cx| {
|
||||
schedule_task(
|
||||
workspace,
|
||||
task_source_kind,
|
||||
&original_task,
|
||||
&task_context,
|
||||
false,
|
||||
cx,
|
||||
)
|
||||
})
|
||||
.ok()
|
||||
})
|
||||
.detach()
|
||||
} else {
|
||||
if let Some(resolved) = last_scheduled_task.resolved.as_mut() {
|
||||
if let Some(allow_concurrent_runs) = action.allow_concurrent_runs {
|
||||
|
@ -62,7 +67,7 @@ pub fn init(cx: &mut AppContext) {
|
|||
);
|
||||
}
|
||||
} else {
|
||||
toggle_modal(workspace, cx);
|
||||
toggle_modal(workspace, cx).detach();
|
||||
};
|
||||
});
|
||||
},
|
||||
|
@ -72,33 +77,52 @@ pub fn init(cx: &mut AppContext) {
|
|||
|
||||
fn spawn_task_or_modal(workspace: &mut Workspace, action: &Spawn, cx: &mut ViewContext<Workspace>) {
|
||||
match &action.task_name {
|
||||
Some(name) => spawn_task_with_name(name.clone(), cx),
|
||||
None => toggle_modal(workspace, cx),
|
||||
Some(name) => spawn_task_with_name(name.clone(), cx).detach_and_log_err(cx),
|
||||
None => toggle_modal(workspace, cx).detach(),
|
||||
}
|
||||
}
|
||||
|
||||
fn toggle_modal(workspace: &mut Workspace, cx: &mut ViewContext<'_, Workspace>) {
|
||||
let inventory = workspace.project().read(cx).task_inventory().clone();
|
||||
fn toggle_modal(workspace: &mut Workspace, cx: &mut ViewContext<'_, Workspace>) -> AsyncTask<()> {
|
||||
let project = workspace.project().clone();
|
||||
let workspace_handle = workspace.weak_handle();
|
||||
let task_context = task_context(workspace, cx);
|
||||
workspace.toggle_modal(cx, |cx| {
|
||||
TasksModal::new(inventory, task_context, workspace_handle, cx)
|
||||
let context_task = task_context(workspace, cx);
|
||||
cx.spawn(|workspace, mut cx| async move {
|
||||
let task_context = context_task.await;
|
||||
workspace
|
||||
.update(&mut cx, |workspace, cx| {
|
||||
if workspace.project().update(cx, |project, cx| {
|
||||
project.is_local() || project.ssh_connection_string(cx).is_some()
|
||||
}) {
|
||||
workspace.toggle_modal(cx, |cx| {
|
||||
TasksModal::new(project, task_context, workspace_handle, cx)
|
||||
})
|
||||
}
|
||||
})
|
||||
.ok();
|
||||
})
|
||||
}
|
||||
|
||||
fn spawn_task_with_name(name: String, cx: &mut ViewContext<Workspace>) {
|
||||
fn spawn_task_with_name(
|
||||
name: String,
|
||||
cx: &mut ViewContext<Workspace>,
|
||||
) -> AsyncTask<anyhow::Result<()>> {
|
||||
cx.spawn(|workspace, mut cx| async move {
|
||||
let context_task =
|
||||
workspace.update(&mut cx, |workspace, cx| task_context(workspace, cx))?;
|
||||
let task_context = context_task.await;
|
||||
let tasks = workspace
|
||||
.update(&mut cx, |workspace, cx| {
|
||||
let (worktree, location) = active_item_selection_properties(workspace, cx);
|
||||
workspace.project().update(cx, |project, cx| {
|
||||
project.task_templates(worktree, location, cx)
|
||||
})
|
||||
})?
|
||||
.await?;
|
||||
|
||||
let did_spawn = workspace
|
||||
.update(&mut cx, |workspace, cx| {
|
||||
let (worktree, language) = active_item_selection_properties(workspace, cx);
|
||||
let tasks = workspace.project().update(cx, |project, cx| {
|
||||
project
|
||||
.task_inventory()
|
||||
.update(cx, |inventory, _| inventory.list_tasks(language, worktree))
|
||||
});
|
||||
let (task_source_kind, target_task) =
|
||||
tasks.into_iter().find(|(_, task)| task.label == name)?;
|
||||
let task_context = task_context(workspace, cx);
|
||||
schedule_task(
|
||||
workspace,
|
||||
task_source_kind,
|
||||
|
@ -108,9 +132,7 @@ fn spawn_task_with_name(name: String, cx: &mut ViewContext<Workspace>) {
|
|||
cx,
|
||||
);
|
||||
Some(())
|
||||
})
|
||||
.ok()
|
||||
.flatten()
|
||||
})?
|
||||
.is_some();
|
||||
if !did_spawn {
|
||||
workspace
|
||||
|
@ -119,32 +141,38 @@ fn spawn_task_with_name(name: String, cx: &mut ViewContext<Workspace>) {
|
|||
})
|
||||
.ok();
|
||||
}
|
||||
|
||||
Ok(())
|
||||
})
|
||||
.detach();
|
||||
}
|
||||
|
||||
fn active_item_selection_properties(
|
||||
workspace: &Workspace,
|
||||
cx: &mut WindowContext,
|
||||
) -> (Option<WorktreeId>, Option<Arc<Language>>) {
|
||||
) -> (Option<WorktreeId>, Option<Location>) {
|
||||
let active_item = workspace.active_item(cx);
|
||||
let worktree_id = active_item
|
||||
.as_ref()
|
||||
.and_then(|item| item.project_path(cx))
|
||||
.map(|path| path.worktree_id);
|
||||
let language = active_item
|
||||
let location = active_item
|
||||
.and_then(|active_item| active_item.act_as::<Editor>(cx))
|
||||
.and_then(|editor| {
|
||||
editor.update(cx, |editor, cx| {
|
||||
let selection = editor.selections.newest::<usize>(cx);
|
||||
let (buffer, buffer_position, _) = editor
|
||||
.buffer()
|
||||
.read(cx)
|
||||
.point_to_buffer_offset(selection.start, cx)?;
|
||||
buffer.read(cx).language_at(buffer_position)
|
||||
let selection = editor.selections.newest_anchor();
|
||||
let multi_buffer = editor.buffer().clone();
|
||||
let multi_buffer_snapshot = multi_buffer.read(cx).snapshot(cx);
|
||||
let (buffer_snapshot, buffer_offset) =
|
||||
multi_buffer_snapshot.point_to_buffer_offset(selection.head())?;
|
||||
let buffer_anchor = buffer_snapshot.anchor_before(buffer_offset);
|
||||
let buffer = multi_buffer.read(cx).buffer(buffer_snapshot.remote_id())?;
|
||||
Some(Location {
|
||||
buffer,
|
||||
range: buffer_anchor..buffer_anchor,
|
||||
})
|
||||
})
|
||||
});
|
||||
(worktree_id, language)
|
||||
(worktree_id, location)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
@ -250,69 +278,84 @@ mod tests {
|
|||
.unwrap();
|
||||
buffer2.update(cx, |this, cx| this.set_language(Some(rust_language), cx));
|
||||
let editor2 = cx.new_view(|cx| Editor::for_buffer(buffer2, Some(project), cx));
|
||||
workspace.update(cx, |this, cx| {
|
||||
this.add_item_to_center(Box::new(editor1.clone()), cx);
|
||||
this.add_item_to_center(Box::new(editor2.clone()), cx);
|
||||
assert_eq!(this.active_item(cx).unwrap().item_id(), editor2.entity_id());
|
||||
assert_eq!(
|
||||
task_context(this, cx),
|
||||
TaskContext {
|
||||
cwd: Some("/dir".into()),
|
||||
task_variables: TaskVariables::from_iter([
|
||||
(VariableName::File, "/dir/rust/b.rs".into()),
|
||||
(VariableName::Filename, "b.rs".into()),
|
||||
(VariableName::RelativeFile, "rust/b.rs".into()),
|
||||
(VariableName::Dirname, "/dir/rust".into()),
|
||||
(VariableName::Stem, "b".into()),
|
||||
(VariableName::WorktreeRoot, "/dir".into()),
|
||||
(VariableName::Row, "1".into()),
|
||||
(VariableName::Column, "1".into()),
|
||||
])
|
||||
}
|
||||
);
|
||||
// And now, let's select an identifier.
|
||||
editor2.update(cx, |this, cx| {
|
||||
this.change_selections(None, cx, |selections| selections.select_ranges([14..18]))
|
||||
});
|
||||
assert_eq!(
|
||||
task_context(this, cx),
|
||||
TaskContext {
|
||||
cwd: Some("/dir".into()),
|
||||
task_variables: TaskVariables::from_iter([
|
||||
(VariableName::File, "/dir/rust/b.rs".into()),
|
||||
(VariableName::Filename, "b.rs".into()),
|
||||
(VariableName::RelativeFile, "rust/b.rs".into()),
|
||||
(VariableName::Dirname, "/dir/rust".into()),
|
||||
(VariableName::Stem, "b".into()),
|
||||
(VariableName::WorktreeRoot, "/dir".into()),
|
||||
(VariableName::Row, "1".into()),
|
||||
(VariableName::Column, "15".into()),
|
||||
(VariableName::SelectedText, "is_i".into()),
|
||||
(VariableName::Symbol, "this_is_a_rust_file".into()),
|
||||
])
|
||||
}
|
||||
);
|
||||
|
||||
// Now, let's switch the active item to .ts file.
|
||||
this.activate_item(&editor1, cx);
|
||||
assert_eq!(
|
||||
task_context(this, cx),
|
||||
TaskContext {
|
||||
cwd: Some("/dir".into()),
|
||||
task_variables: TaskVariables::from_iter([
|
||||
(VariableName::File, "/dir/a.ts".into()),
|
||||
(VariableName::Filename, "a.ts".into()),
|
||||
(VariableName::RelativeFile, "a.ts".into()),
|
||||
(VariableName::Dirname, "/dir".into()),
|
||||
(VariableName::Stem, "a".into()),
|
||||
(VariableName::WorktreeRoot, "/dir".into()),
|
||||
(VariableName::Row, "1".into()),
|
||||
(VariableName::Column, "1".into()),
|
||||
(VariableName::Symbol, "this_is_a_test".into()),
|
||||
])
|
||||
}
|
||||
);
|
||||
let first_context = workspace
|
||||
.update(cx, |workspace, cx| {
|
||||
workspace.add_item_to_center(Box::new(editor1.clone()), cx);
|
||||
workspace.add_item_to_center(Box::new(editor2.clone()), cx);
|
||||
assert_eq!(
|
||||
workspace.active_item(cx).unwrap().item_id(),
|
||||
editor2.entity_id()
|
||||
);
|
||||
task_context(workspace, cx)
|
||||
})
|
||||
.await;
|
||||
assert_eq!(
|
||||
first_context,
|
||||
TaskContext {
|
||||
cwd: Some("/dir".into()),
|
||||
task_variables: TaskVariables::from_iter([
|
||||
(VariableName::File, "/dir/rust/b.rs".into()),
|
||||
(VariableName::Filename, "b.rs".into()),
|
||||
(VariableName::RelativeFile, "rust/b.rs".into()),
|
||||
(VariableName::Dirname, "/dir/rust".into()),
|
||||
(VariableName::Stem, "b".into()),
|
||||
(VariableName::WorktreeRoot, "/dir".into()),
|
||||
(VariableName::Row, "1".into()),
|
||||
(VariableName::Column, "1".into()),
|
||||
])
|
||||
}
|
||||
);
|
||||
|
||||
// And now, let's select an identifier.
|
||||
editor2.update(cx, |editor, cx| {
|
||||
editor.change_selections(None, cx, |selections| selections.select_ranges([14..18]))
|
||||
});
|
||||
|
||||
assert_eq!(
|
||||
workspace
|
||||
.update(cx, |workspace, cx| { task_context(workspace, cx) })
|
||||
.await,
|
||||
TaskContext {
|
||||
cwd: Some("/dir".into()),
|
||||
task_variables: TaskVariables::from_iter([
|
||||
(VariableName::File, "/dir/rust/b.rs".into()),
|
||||
(VariableName::Filename, "b.rs".into()),
|
||||
(VariableName::RelativeFile, "rust/b.rs".into()),
|
||||
(VariableName::Dirname, "/dir/rust".into()),
|
||||
(VariableName::Stem, "b".into()),
|
||||
(VariableName::WorktreeRoot, "/dir".into()),
|
||||
(VariableName::Row, "1".into()),
|
||||
(VariableName::Column, "15".into()),
|
||||
(VariableName::SelectedText, "is_i".into()),
|
||||
(VariableName::Symbol, "this_is_a_rust_file".into()),
|
||||
])
|
||||
}
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
workspace
|
||||
.update(cx, |workspace, cx| {
|
||||
// Now, let's switch the active item to .ts file.
|
||||
workspace.activate_item(&editor1, cx);
|
||||
task_context(workspace, cx)
|
||||
})
|
||||
.await,
|
||||
TaskContext {
|
||||
cwd: Some("/dir".into()),
|
||||
task_variables: TaskVariables::from_iter([
|
||||
(VariableName::File, "/dir/a.ts".into()),
|
||||
(VariableName::Filename, "a.ts".into()),
|
||||
(VariableName::RelativeFile, "a.ts".into()),
|
||||
(VariableName::Dirname, "/dir".into()),
|
||||
(VariableName::Stem, "a".into()),
|
||||
(VariableName::WorktreeRoot, "/dir".into()),
|
||||
(VariableName::Row, "1".into()),
|
||||
(VariableName::Column, "1".into()),
|
||||
(VariableName::Symbol, "this_is_a_test".into()),
|
||||
])
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
pub(crate) fn init_test(cx: &mut TestAppContext) -> Arc<AppState> {
|
||||
|
|
|
@ -4,11 +4,11 @@ use crate::active_item_selection_properties;
|
|||
use fuzzy::{StringMatch, StringMatchCandidate};
|
||||
use gpui::{
|
||||
impl_actions, rems, Action, AnyElement, AppContext, DismissEvent, EventEmitter, FocusableView,
|
||||
InteractiveElement, Model, ParentElement, Render, SharedString, Styled, Subscription, View,
|
||||
ViewContext, VisualContext, WeakView,
|
||||
InteractiveElement, Model, ParentElement, Render, SharedString, Styled, Subscription, Task,
|
||||
View, ViewContext, VisualContext, WeakView,
|
||||
};
|
||||
use picker::{highlighted_match_with_paths::HighlightedText, Picker, PickerDelegate};
|
||||
use project::{Inventory, TaskSourceKind};
|
||||
use project::{Project, TaskSourceKind};
|
||||
use task::{ResolvedTask, TaskContext, TaskTemplate};
|
||||
use ui::{
|
||||
div, h_flex, v_flex, ActiveTheme, Button, ButtonCommon, ButtonSize, Clickable, Color,
|
||||
|
@ -60,7 +60,7 @@ impl_actions!(task, [Rerun, Spawn]);
|
|||
|
||||
/// A modal used to spawn new tasks.
|
||||
pub(crate) struct TasksModalDelegate {
|
||||
inventory: Model<Inventory>,
|
||||
project: Model<Project>,
|
||||
candidates: Option<Vec<(TaskSourceKind, ResolvedTask)>>,
|
||||
last_used_candidate_index: Option<usize>,
|
||||
divider_index: Option<usize>,
|
||||
|
@ -74,12 +74,12 @@ pub(crate) struct TasksModalDelegate {
|
|||
|
||||
impl TasksModalDelegate {
|
||||
fn new(
|
||||
inventory: Model<Inventory>,
|
||||
project: Model<Project>,
|
||||
task_context: TaskContext,
|
||||
workspace: WeakView<Workspace>,
|
||||
) -> Self {
|
||||
Self {
|
||||
inventory,
|
||||
project,
|
||||
workspace,
|
||||
candidates: None,
|
||||
matches: Vec::new(),
|
||||
|
@ -121,8 +121,10 @@ impl TasksModalDelegate {
|
|||
// it doesn't make sense to requery the inventory for new candidates, as that's potentially costly and more often than not it should just return back
|
||||
// the original list without a removed entry.
|
||||
candidates.remove(ix);
|
||||
self.inventory.update(cx, |inventory, _| {
|
||||
inventory.delete_previously_used(&task.id);
|
||||
self.project.update(cx, |project, cx| {
|
||||
project.task_inventory().update(cx, |inventory, _| {
|
||||
inventory.delete_previously_used(&task.id);
|
||||
})
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -134,14 +136,14 @@ pub(crate) struct TasksModal {
|
|||
|
||||
impl TasksModal {
|
||||
pub(crate) fn new(
|
||||
inventory: Model<Inventory>,
|
||||
project: Model<Project>,
|
||||
task_context: TaskContext,
|
||||
workspace: WeakView<Workspace>,
|
||||
cx: &mut ViewContext<Self>,
|
||||
) -> Self {
|
||||
let picker = cx.new_view(|cx| {
|
||||
Picker::uniform_list(
|
||||
TasksModalDelegate::new(inventory, task_context, workspace),
|
||||
TasksModalDelegate::new(project, task_context, workspace),
|
||||
cx,
|
||||
)
|
||||
});
|
||||
|
@ -197,53 +199,85 @@ impl PickerDelegate for TasksModalDelegate {
|
|||
&mut self,
|
||||
query: String,
|
||||
cx: &mut ViewContext<picker::Picker<Self>>,
|
||||
) -> gpui::Task<()> {
|
||||
) -> Task<()> {
|
||||
cx.spawn(move |picker, mut cx| async move {
|
||||
let Some(candidates) = picker
|
||||
let Some(candidates_task) = picker
|
||||
.update(&mut cx, |picker, cx| {
|
||||
let candidates = match &mut picker.delegate.candidates {
|
||||
Some(candidates) => candidates,
|
||||
match &mut picker.delegate.candidates {
|
||||
Some(candidates) => {
|
||||
Task::ready(Ok(string_match_candidates(candidates.iter())))
|
||||
}
|
||||
None => {
|
||||
let Ok((worktree, language)) =
|
||||
let Ok((worktree, location)) =
|
||||
picker.delegate.workspace.update(cx, |workspace, cx| {
|
||||
active_item_selection_properties(workspace, cx)
|
||||
})
|
||||
else {
|
||||
return Vec::new();
|
||||
};
|
||||
let (used, current) =
|
||||
picker.delegate.inventory.update(cx, |inventory, _| {
|
||||
inventory.used_and_current_resolved_tasks(
|
||||
language,
|
||||
worktree,
|
||||
&picker.delegate.task_context,
|
||||
)
|
||||
});
|
||||
picker.delegate.last_used_candidate_index = if used.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(used.len() - 1)
|
||||
return Task::ready(Ok(Vec::new()));
|
||||
};
|
||||
|
||||
let mut new_candidates = used;
|
||||
new_candidates.extend(current);
|
||||
picker.delegate.candidates.insert(new_candidates)
|
||||
let resolved_task =
|
||||
picker.delegate.project.update(cx, |project, cx| {
|
||||
let ssh_connection_string = project.ssh_connection_string(cx);
|
||||
if project.is_remote() && ssh_connection_string.is_none() {
|
||||
Task::ready((Vec::new(), Vec::new()))
|
||||
} else {
|
||||
let remote_templates = if project.is_local() {
|
||||
None
|
||||
} else {
|
||||
project
|
||||
.remote_id()
|
||||
.filter(|_| ssh_connection_string.is_some())
|
||||
.map(|project_id| {
|
||||
project.query_remote_task_templates(
|
||||
project_id,
|
||||
worktree,
|
||||
location.as_ref(),
|
||||
cx,
|
||||
)
|
||||
})
|
||||
};
|
||||
project
|
||||
.task_inventory()
|
||||
.read(cx)
|
||||
.used_and_current_resolved_tasks(
|
||||
remote_templates,
|
||||
worktree,
|
||||
location,
|
||||
&picker.delegate.task_context,
|
||||
cx,
|
||||
)
|
||||
}
|
||||
});
|
||||
cx.spawn(|picker, mut cx| async move {
|
||||
let (used, current) = resolved_task.await;
|
||||
picker.update(&mut cx, |picker, _| {
|
||||
picker.delegate.last_used_candidate_index = if used.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(used.len() - 1)
|
||||
};
|
||||
|
||||
let mut new_candidates = used;
|
||||
new_candidates.extend(current);
|
||||
let match_candidates =
|
||||
string_match_candidates(new_candidates.iter());
|
||||
let _ = picker.delegate.candidates.insert(new_candidates);
|
||||
match_candidates
|
||||
})
|
||||
})
|
||||
}
|
||||
};
|
||||
candidates
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(index, (_, candidate))| StringMatchCandidate {
|
||||
id: index,
|
||||
char_bag: candidate.resolved_label.chars().collect(),
|
||||
string: candidate.display_label().to_owned(),
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
}
|
||||
})
|
||||
.ok()
|
||||
else {
|
||||
return;
|
||||
};
|
||||
let Some(candidates): Option<Vec<StringMatchCandidate>> =
|
||||
candidates_task.await.log_err()
|
||||
else {
|
||||
return;
|
||||
};
|
||||
let matches = fuzzy::match_strings(
|
||||
&candidates,
|
||||
&query,
|
||||
|
@ -534,6 +568,19 @@ impl PickerDelegate for TasksModalDelegate {
|
|||
}
|
||||
}
|
||||
|
||||
fn string_match_candidates<'a>(
|
||||
candidates: impl Iterator<Item = &'a (TaskSourceKind, ResolvedTask)> + 'a,
|
||||
) -> Vec<StringMatchCandidate> {
|
||||
candidates
|
||||
.enumerate()
|
||||
.map(|(index, (_, candidate))| StringMatchCandidate {
|
||||
id: index,
|
||||
char_bag: candidate.resolved_label.chars().collect(),
|
||||
string: candidate.display_label().to_owned(),
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::{path::PathBuf, sync::Arc};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue