debugger: Fix gutter tasks display for users without the debugger feature flag (#29056)

Kirill Bulatov created

Change summary

crates/editor/src/code_context_menus.rs | 38 +++++++++++++++++---------
crates/editor/src/editor.rs             | 32 ++++++++++------------
crates/editor/src/element.rs            |  3 -
3 files changed, 41 insertions(+), 32 deletions(-)

Detailed changes

crates/editor/src/code_context_menus.rs 🔗

@@ -777,11 +777,34 @@ pub struct AvailableCodeAction {
 
 #[derive(Clone, Default)]
 pub struct CodeActionContents {
-    pub tasks: Option<Rc<ResolvedTasks>>,
-    pub actions: Option<Rc<[AvailableCodeAction]>>,
+    tasks: Option<Rc<ResolvedTasks>>,
+    pub(crate) actions: Option<Rc<[AvailableCodeAction]>>,
 }
 
 impl CodeActionContents {
+    pub fn new(
+        mut tasks: Option<ResolvedTasks>,
+        actions: Option<Rc<[AvailableCodeAction]>>,
+        cx: &App,
+    ) -> Self {
+        if !cx.has_flag::<Debugger>() {
+            if let Some(tasks) = &mut tasks {
+                tasks
+                    .templates
+                    .retain(|(_, task)| !matches!(task.task_type(), task::TaskType::Debug(_)));
+            }
+        }
+
+        Self {
+            tasks: tasks.map(Rc::new),
+            actions,
+        }
+    }
+
+    pub fn tasks(&self) -> Option<&ResolvedTasks> {
+        self.tasks.as_deref()
+    }
+
     fn len(&self) -> usize {
         match (&self.tasks, &self.actions) {
             (Some(tasks), Some(actions)) => actions.len() + tasks.templates.len(),
@@ -989,17 +1012,6 @@ impl CodeActionsMenu {
                     .iter()
                     .skip(range.start)
                     .take(range.end - range.start)
-                    .filter(|action| {
-                        if action
-                            .as_task()
-                            .map(|task| matches!(task.task_type(), task::TaskType::Debug(_)))
-                            .unwrap_or(false)
-                        {
-                            cx.has_flag::<Debugger>()
-                        } else {
-                            true
-                        }
-                    })
                     .enumerate()
                     .map(|(ix, action)| {
                         let item_ix = range.start + ix;

crates/editor/src/editor.rs 🔗

@@ -4914,26 +4914,24 @@ impl Editor {
                 Self::build_tasks_context(&project, &buffer, buffer_row, tasks, cx)
             });
 
-        cx.spawn_in(window, async move |_, _| {
+        cx.spawn_in(window, async move |_, cx| {
             let task_context = match task_context {
                 Some(task_context) => task_context.await,
                 None => None,
             };
-            let resolved_tasks = tasks.zip(task_context).map(|(tasks, task_context)| {
-                Rc::new(ResolvedTasks {
-                    templates: tasks.resolve(&task_context).collect(),
-                    position: snapshot
-                        .buffer_snapshot
-                        .anchor_before(Point::new(multibuffer_point.row, tasks.column)),
-                })
-            });
-            Some((
-                buffer,
-                CodeActionContents {
-                    actions: code_actions,
-                    tasks: resolved_tasks,
-                },
-            ))
+            let resolved_tasks =
+                tasks
+                    .zip(task_context)
+                    .map(|(tasks, task_context)| ResolvedTasks {
+                        templates: tasks.resolve(&task_context).collect(),
+                        position: snapshot
+                            .buffer_snapshot
+                            .anchor_before(Point::new(multibuffer_point.row, tasks.column)),
+                    });
+            let code_action_contents = cx
+                .update(|_, cx| CodeActionContents::new(resolved_tasks, code_actions, cx))
+                .ok()?;
+            Some((buffer, code_action_contents))
         })
     }
 
@@ -4977,7 +4975,7 @@ impl Editor {
                 Some(cx.spawn_in(window, async move |editor, cx| {
                     if let Some((buffer, code_action_contents)) = code_actions_task.await {
                         let spawn_straight_away =
-                            code_action_contents.tasks.as_ref().map_or(false, |tasks| {
+                            code_action_contents.tasks().map_or(false, |tasks| {
                                 tasks
                                     .templates
                                     .iter()

crates/editor/src/element.rs 🔗

@@ -2093,8 +2093,7 @@ impl EditorElement {
                 })) = editor.context_menu.borrow().as_ref()
                 {
                     actions
-                        .tasks
-                        .as_ref()
+                        .tasks()
                         .map(|tasks| tasks.position.to_display_point(snapshot).row())
                         .or(*deployed_from_indicator)
                 } else {