diff --git a/crates/debugger_ui/src/debugger_panel.rs b/crates/debugger_ui/src/debugger_panel.rs index 86e8ccc496e0a75684d9128b782ccb7899f8399b..fbc6bb0b6326b0da02fb7802399682e6e8d253af 100644 --- a/crates/debugger_ui/src/debugger_panel.rs +++ b/crates/debugger_ui/src/debugger_panel.rs @@ -30,7 +30,7 @@ use project::{Project, debugger::session::ThreadStatus}; use rpc::proto::{self}; use settings::Settings; use std::sync::{Arc, LazyLock}; -use task::{DebugScenario, TaskContext}; +use task::{DebugScenario, SharedTaskContext}; use tree_sitter::{Query, StreamingIterator as _}; use ui::{ ContextMenu, Divider, PopoverMenu, PopoverMenuHandle, SplitButton, Tab, Tooltip, prelude::*, @@ -176,7 +176,7 @@ impl DebugPanel { pub fn start_session( &mut self, scenario: DebugScenario, - task_context: TaskContext, + task_context: SharedTaskContext, active_buffer: Option>, worktree_id: Option, window: &mut Window, @@ -227,9 +227,6 @@ impl DebugPanel { inventory.update(cx, |inventory, _| { inventory.scenario_scheduled( scenario.clone(), - // todo(debugger): Task context is cloned three times - // once in Session,inventory, and in resolve scenario - // we should wrap it in an RC instead to save some memory task_context.clone(), worktree_id, active_buffer.as_ref().map(|buffer| buffer.downgrade()), @@ -1957,7 +1954,7 @@ impl workspace::DebuggerProvider for DebuggerProvider { fn start_session( &self, definition: DebugScenario, - context: TaskContext, + context: SharedTaskContext, buffer: Option>, worktree_id: Option, window: &mut Window, diff --git a/crates/debugger_ui/src/new_process_modal.rs b/crates/debugger_ui/src/new_process_modal.rs index 982edd7feca720666cf90b3b771e272f628076bd..5b028671ed512a09cb90bcc4098d793a50b0fdb8 100644 --- a/crates/debugger_ui/src/new_process_modal.rs +++ b/crates/debugger_ui/src/new_process_modal.rs @@ -21,7 +21,7 @@ use gpui::{ use itertools::Itertools as _; use picker::{Picker, PickerDelegate, highlighted_match_with_paths::HighlightedMatch}; use project::{DebugScenarioContext, Project, TaskContexts, TaskSourceKind, task_store::TaskStore}; -use task::{DebugScenario, RevealTarget, VariableName, ZedDebugConfig}; +use task::{DebugScenario, RevealTarget, SharedTaskContext, VariableName, ZedDebugConfig}; use ui::{ ContextMenu, DropdownMenu, IconWithIndicator, Indicator, KeyBinding, ListItem, ListItemSpacing, Switch, SwitchLabelPosition, ToggleButtonGroup, ToggleButtonSimple, ToggleState, Tooltip, @@ -371,7 +371,11 @@ impl NewProcessModal { return; }; - let task_context = task_contexts.active_context().cloned().unwrap_or_default(); + let task_context = task_contexts + .active_context() + .cloned() + .unwrap_or_default() + .into(); let worktree_id = task_contexts.worktree(); let mode = self.mode; cx.spawn_in(window, async move |this, cx| { @@ -1292,7 +1296,7 @@ impl PickerDelegate for DebugDelegate { .as_ref() .and_then(|task_contexts| { Some(( - task_contexts.active_context().cloned()?, + SharedTaskContext::from(task_contexts.active_context().cloned()?), task_contexts.worktree(), )) }) @@ -1418,7 +1422,7 @@ impl PickerDelegate for DebugDelegate { .as_ref() .and_then(|task_contexts| { Some(DebugScenarioContext { - task_context: task_contexts.active_context().cloned()?, + task_context: task_contexts.active_context().cloned()?.into(), active_buffer: None, worktree_id: task_contexts.worktree(), }) diff --git a/crates/debugger_ui/src/session/running.rs b/crates/debugger_ui/src/session/running.rs index 94878c3f0c0ffdc910b15297cb6573e5c8cd252e..626c807ded5e0a01b086d7311cc083bab321c7f6 100644 --- a/crates/debugger_ui/src/session/running.rs +++ b/crates/debugger_ui/src/session/running.rs @@ -48,8 +48,8 @@ use serde_json::Value; use settings::Settings; use stack_frame_list::StackFrameList; use task::{ - BuildTaskDefinition, DebugScenario, Shell, ShellBuilder, SpawnInTerminal, TaskContext, - ZedDebugConfig, substitute_variables_in_str, + BuildTaskDefinition, DebugScenario, SharedTaskContext, Shell, ShellBuilder, SpawnInTerminal, + TaskContext, ZedDebugConfig, substitute_variables_in_str, }; use terminal_view::TerminalView; use ui::{ @@ -956,7 +956,7 @@ impl RunningState { pub(crate) fn resolve_scenario( &self, scenario: DebugScenario, - task_context: TaskContext, + task_context: SharedTaskContext, buffer: Option>, worktree_id: Option, window: &Window, diff --git a/crates/debugger_ui/src/tests.rs b/crates/debugger_ui/src/tests.rs index 4f020e7cd4d62250b101e01d586ca02beefecd71..5aaf3538e86054346d82b1db10aa02d8e5aa34f1 100644 --- a/crates/debugger_ui/src/tests.rs +++ b/crates/debugger_ui/src/tests.rs @@ -6,7 +6,7 @@ use dap::client::DebugAdapterClient; use gpui::{Entity, TestAppContext, WindowHandle}; use project::{Project, debugger::session::Session}; use settings::SettingsStore; -use task::TaskContext; +use task::SharedTaskContext; use terminal_view::terminal_panel::TerminalPanel; use workspace::Workspace; @@ -110,7 +110,7 @@ pub fn start_debug_session_with) + 'static>( workspace.update(cx, |workspace, window, cx| { workspace.start_debug_session( config.to_scenario(), - TaskContext::default(), + SharedTaskContext::default(), None, None, window, diff --git a/crates/debugger_ui/src/tests/attach_modal.rs b/crates/debugger_ui/src/tests/attach_modal.rs index 4df3ebf5196dea266287041e51dd65363d5f685c..b05ee591f3ac0ca2e138e25928552d93c4426152 100644 --- a/crates/debugger_ui/src/tests/attach_modal.rs +++ b/crates/debugger_ui/src/tests/attach_modal.rs @@ -9,7 +9,7 @@ use gpui::{BackgroundExecutor, TestAppContext, VisualTestContext}; use menu::Confirm; use project::{FakeFs, Project}; use serde_json::json; -use task::AttachRequest; +use task::{AttachRequest, SharedTaskContext}; use tests::{init_test, init_test_workspace}; use util::path; @@ -220,7 +220,7 @@ async fn test_attach_with_pick_pid_variable(executor: BackgroundExecutor, cx: &m tcp_connection: None, } .to_scenario(), - task::TaskContext::default(), + SharedTaskContext::default(), None, None, window, diff --git a/crates/debugger_ui/src/tests/new_process_modal.rs b/crates/debugger_ui/src/tests/new_process_modal.rs index 2f470560d5a58a1ed9e56ebe89257572d195689e..7be2b8798e38108eaa05508002624b98c1595b3f 100644 --- a/crates/debugger_ui/src/tests/new_process_modal.rs +++ b/crates/debugger_ui/src/tests/new_process_modal.rs @@ -5,7 +5,10 @@ use project::{FakeFs, Fs as _, Project}; use serde_json::json; use std::sync::Arc; use std::sync::atomic::{AtomicBool, Ordering}; -use task::{DebugRequest, DebugScenario, LaunchRequest, TaskContext, VariableName, ZedDebugConfig}; +use task::{ + DebugRequest, DebugScenario, LaunchRequest, SharedTaskContext, TaskContext, VariableName, + ZedDebugConfig, +}; use text::Point; use util::path; @@ -40,11 +43,12 @@ async fn test_debug_session_substitutes_variables_and_relativizes_paths( .into_iter() .collect(); - let task_context = TaskContext { + let task_context: SharedTaskContext = TaskContext { cwd: None, task_variables: test_variables, project_env: Default::default(), - }; + } + .into(); let home_dir = paths::home_dir(); diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index 28c17bdcd3bfa2fdcdbc3ceacf99a6825da936cf..3f1717836bcb629719d9b31c162775d5a41a0402 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -6840,7 +6840,7 @@ impl Editor { })) } CodeActionsItem::DebugScenario(scenario) => { - let context = actions_menu.actions.context; + let context = actions_menu.actions.context.into(); workspace.update(cx, |workspace, cx| { dap::send_telemetry(&scenario, TelemetrySpawnLocation::Gutter, cx); diff --git a/crates/project/src/debugger/dap_store.rs b/crates/project/src/debugger/dap_store.rs index 90d77347767708a50f1a136c2daf7c41d930c983..ab44cacf2d65607b669e160f4839642cef442238 100644 --- a/crates/project/src/debugger/dap_store.rs +++ b/crates/project/src/debugger/dap_store.rs @@ -51,7 +51,7 @@ use std::{ path::{Path, PathBuf}, sync::{Arc, Once}, }; -use task::{DebugScenario, SpawnInTerminal, TaskContext, TaskTemplate}; +use task::{DebugScenario, SharedTaskContext, SpawnInTerminal, TaskTemplate}; use util::{ResultExt as _, rel_path::RelPath}; use worktree::Worktree; @@ -451,7 +451,7 @@ impl DapStore { &mut self, label: Option, adapter: DebugAdapterName, - task_context: TaskContext, + task_context: SharedTaskContext, parent_session: Option>, quirks: SessionQuirks, cx: &mut Context, diff --git a/crates/project/src/debugger/session.rs b/crates/project/src/debugger/session.rs index 49e86a03dde26470a723909ddd28c518d0dcde4a..c3fd611e0f8b0f1d9403d91f150fbd185ca61c71 100644 --- a/crates/project/src/debugger/session.rs +++ b/crates/project/src/debugger/session.rs @@ -61,7 +61,7 @@ use std::{ path::Path, sync::Arc, }; -use task::TaskContext; +use task::SharedTaskContext; use text::{PointUtf16, ToPointUtf16}; use url::Url; use util::command::new_smol_command; @@ -711,7 +711,7 @@ pub struct Session { data_breakpoints: BTreeMap, background_tasks: Vec>, restart_task: Option>, - task_context: TaskContext, + task_context: SharedTaskContext, memory: memory::Memory, quirks: SessionQuirks, remote_client: Option>, @@ -833,7 +833,7 @@ impl Session { parent_session: Option>, label: Option, adapter: DebugAdapterName, - task_context: TaskContext, + task_context: SharedTaskContext, quirks: SessionQuirks, remote_client: Option>, node_runtime: Option, @@ -897,7 +897,7 @@ impl Session { }) } - pub fn task_context(&self) -> &TaskContext { + pub fn task_context(&self) -> &SharedTaskContext { &self.task_context } diff --git a/crates/project/src/task_inventory.rs b/crates/project/src/task_inventory.rs index c52bd0d9056f1fd7b84ee46f6a563a0e90299507..205232b523cf3773d895368ba0b1b7d2d32a7afe 100644 --- a/crates/project/src/task_inventory.rs +++ b/crates/project/src/task_inventory.rs @@ -21,8 +21,8 @@ use lsp::{LanguageServerId, LanguageServerName}; use paths::{debug_task_file_name, task_file_name}; use settings::{InvalidSettingsError, parse_json_with_comments}; use task::{ - DebugScenario, ResolvedTask, TaskContext, TaskId, TaskTemplate, TaskTemplates, TaskVariables, - VariableName, + DebugScenario, ResolvedTask, SharedTaskContext, TaskContext, TaskId, TaskTemplate, + TaskTemplates, TaskVariables, VariableName, }; use text::{BufferId, Point, ToPoint}; use util::{NumericPrefixWithSuffix, ResultExt as _, post_inc, rel_path::RelPath}; @@ -32,7 +32,7 @@ use crate::{task_store::TaskSettingsLocation, worktree_store::WorktreeStore}; #[derive(Clone, Debug, Default)] pub struct DebugScenarioContext { - pub task_context: TaskContext, + pub task_context: SharedTaskContext, pub worktree_id: Option, pub active_buffer: Option>, } @@ -252,7 +252,7 @@ impl Inventory { pub fn scenario_scheduled( &mut self, scenario: DebugScenario, - task_context: TaskContext, + task_context: SharedTaskContext, worktree_id: Option, active_buffer: Option>, ) { diff --git a/crates/task/src/task.rs b/crates/task/src/task.rs index 92d59094190de9924327722bb659058569869269..a8eccc9af1d7f8b00fb8557bb02c05281c5bb2e6 100644 --- a/crates/task/src/task.rs +++ b/crates/task/src/task.rs @@ -15,6 +15,7 @@ use serde::{Deserialize, Serialize}; use std::borrow::Cow; use std::path::PathBuf; use std::str::FromStr; +use std::sync::Arc; pub use adapter_schema::{AdapterSchema, AdapterSchemas}; pub use debug_format::{ @@ -316,6 +317,24 @@ pub struct TaskContext { pub project_env: HashMap, } +/// A shared reference to a [`TaskContext`], used to avoid cloning the context multiple times. +#[derive(Clone, Debug, Default)] +pub struct SharedTaskContext(Arc); + +impl std::ops::Deref for SharedTaskContext { + type Target = TaskContext; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl From for SharedTaskContext { + fn from(context: TaskContext) -> Self { + Self(Arc::new(context)) + } +} + /// This is a new type representing a 'tag' on a 'runnable symbol', typically a test of main() function, found via treesitter. #[derive(Clone, Debug)] pub struct RunnableTag(pub SharedString); diff --git a/crates/workspace/src/tasks.rs b/crates/workspace/src/tasks.rs index 5f52cb49e74d67619b9ba7c033a33fe8a7ad51c8..f85e1488f97491a73314297d91c597bd7d3bb841 100644 --- a/crates/workspace/src/tasks.rs +++ b/crates/workspace/src/tasks.rs @@ -5,7 +5,9 @@ use gpui::{AppContext, Context, Entity, Task}; use language::Buffer; use project::{TaskSourceKind, WorktreeId}; use remote::ConnectionState; -use task::{DebugScenario, ResolvedTask, SpawnInTerminal, TaskContext, TaskTemplate}; +use task::{ + DebugScenario, ResolvedTask, SharedTaskContext, SpawnInTerminal, TaskContext, TaskTemplate, +}; use ui::Window; use crate::{Toast, Workspace, notifications::NotificationId}; @@ -101,7 +103,7 @@ impl Workspace { pub fn start_debug_session( &mut self, scenario: DebugScenario, - task_context: TaskContext, + task_context: SharedTaskContext, active_buffer: Option>, worktree_id: Option, window: &mut Window, diff --git a/crates/workspace/src/workspace.rs b/crates/workspace/src/workspace.rs index 4341d61fbe6b6c317b16430b7f5d6ca36140561b..ae1945af5b5af032cd86c45357cc50686ee4d691 100644 --- a/crates/workspace/src/workspace.rs +++ b/crates/workspace/src/workspace.rs @@ -116,7 +116,7 @@ use std::{ }, time::Duration, }; -use task::{DebugScenario, SpawnInTerminal, TaskContext}; +use task::{DebugScenario, SharedTaskContext, SpawnInTerminal}; use theme::{ActiveTheme, GlobalTheme, SystemAppearance, ThemeSettings}; pub use toolbar::{Toolbar, ToolbarItemEvent, ToolbarItemLocation, ToolbarItemView}; pub use ui; @@ -178,7 +178,7 @@ pub trait DebuggerProvider { fn start_session( &self, definition: DebugScenario, - task_context: TaskContext, + task_context: SharedTaskContext, active_buffer: Option>, worktree_id: Option, window: &mut Window,