diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index 2cd34a6dff..c8cd2bbc67 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -145,10 +145,19 @@ pub fn init(cx: &mut MutableAppContext, path_openers: &mut Vec) { - let position = if let Some(selection) = self.newest_anchor_selection() { - selection.head() - } else { - return; - }; - - let actions = self - .buffer - .update(cx, |buffer, cx| buffer.code_actions(position.clone(), cx)); - - cx.spawn(|this, mut cx| async move { - let actions = actions.await?; - if !actions.is_empty() { - this.update(&mut cx, |this, cx| { - if this.focused { - this.show_context_menu( - ContextMenu::CodeActions(CodeActionsMenu { - actions: actions.into(), - selected_item: 0, - list: UniformListState::default(), - }), - cx, - ); - } - }); - } - Ok::<_, anyhow::Error>(()) - }) - .detach_and_log_err(cx); - } - - pub fn confirm_completion( + fn confirm_completion( &mut self, - completion_ix: Option, + ConfirmCompletion(completion_ix): &ConfirmCompletion, cx: &mut ViewContext, ) -> Option>> { let completions_menu = if let ContextMenu::Completions(menu) = self.hide_context_menu(cx)? { @@ -2046,6 +2018,58 @@ impl Editor { })) } + fn show_code_actions(&mut self, _: &ShowCodeActions, cx: &mut ViewContext) { + let position = if let Some(selection) = self.newest_anchor_selection() { + selection.head() + } else { + return; + }; + + let actions = self + .buffer + .update(cx, |buffer, cx| buffer.code_actions(position.clone(), cx)); + + cx.spawn(|this, mut cx| async move { + let actions = actions.await?; + if !actions.is_empty() { + this.update(&mut cx, |this, cx| { + if this.focused { + this.show_context_menu( + ContextMenu::CodeActions(CodeActionsMenu { + actions: actions.into(), + selected_item: 0, + list: UniformListState::default(), + }), + cx, + ); + } + }); + } + Ok::<_, anyhow::Error>(()) + }) + .detach_and_log_err(cx); + } + + fn confirm_code_action( + &mut self, + ConfirmCodeAction(action_ix): &ConfirmCodeAction, + cx: &mut ViewContext, + ) -> Option>> { + let actions_menu = if let ContextMenu::CodeActions(menu) = self.hide_context_menu(cx)? { + menu + } else { + return None; + }; + + let action = actions_menu + .actions + .get(action_ix.unwrap_or(actions_menu.selected_item))?; + + dbg!(action); + + None + } + pub fn showing_context_menu(&self) -> bool { self.context_menu .as_ref() @@ -4801,8 +4825,14 @@ impl View for Editor { EditorMode::Full => "full", }; cx.map.insert("mode".into(), mode.into()); - if matches!(self.context_menu.as_ref(), Some(ContextMenu::Completions(_))) { - cx.set.insert("completing".into()); + match self.context_menu.as_ref() { + Some(ContextMenu::Completions(_)) => { + cx.set.insert("showing_completions".into()); + } + Some(ContextMenu::CodeActions(_)) => { + cx.set.insert("showing_code_actions".into()); + } + None => {} } cx } @@ -7497,7 +7527,9 @@ mod tests { let apply_additional_edits = editor.update(&mut cx, |editor, cx| { editor.move_down(&MoveDown, cx); - let apply_additional_edits = editor.confirm_completion(None, cx).unwrap(); + let apply_additional_edits = editor + .confirm_completion(&ConfirmCompletion(None), cx) + .unwrap(); assert_eq!( editor.text(cx), " @@ -7576,7 +7608,9 @@ mod tests { editor.next_notification(&cx).await; let apply_additional_edits = editor.update(&mut cx, |editor, cx| { - let apply_additional_edits = editor.confirm_completion(None, cx).unwrap(); + let apply_additional_edits = editor + .confirm_completion(&ConfirmCompletion(None), cx) + .unwrap(); assert_eq!( editor.text(cx), " diff --git a/crates/gpui/src/app.rs b/crates/gpui/src/app.rs index 3a2124de95..bd81f81eb1 100644 --- a/crates/gpui/src/app.rs +++ b/crates/gpui/src/app.rs @@ -831,6 +831,17 @@ impl MutableAppContext { .push(handler); } + pub fn add_async_action(&mut self, mut handler: F) + where + A: Action, + V: View, + F: 'static + FnMut(&mut V, &A, &mut ViewContext) -> Option>>, + { + self.add_action(move |view, action, cx| { + handler(view, action, cx).map(|task| task.detach_and_log_err(cx)); + }) + } + pub fn add_global_action(&mut self, mut handler: F) where A: Action,