1use std::any::{Any, TypeId};
2
3use command_palette_hooks::CommandPaletteFilter;
4use feature_flags::{FeatureFlagAppExt as _, PredictEditsRateCompletionsFeatureFlag};
5use gpui::actions;
6use language::language_settings::{AllLanguageSettings, EditPredictionProvider};
7use settings::update_settings_file;
8use ui::App;
9use workspace::Workspace;
10
11use crate::{RateCompletionModal, onboarding_modal::ZedPredictModal};
12
13actions!(
14 edit_prediction,
15 [
16 /// Resets the edit prediction onboarding state.
17 ResetOnboarding,
18 /// Opens the rate completions modal.
19 RateCompletions
20 ]
21);
22
23pub fn init(cx: &mut App) {
24 cx.observe_new(move |workspace: &mut Workspace, _, _cx| {
25 workspace.register_action(|workspace, _: &RateCompletions, window, cx| {
26 if cx.has_flag::<PredictEditsRateCompletionsFeatureFlag>() {
27 RateCompletionModal::toggle(workspace, window, cx);
28 }
29 });
30
31 workspace.register_action(
32 move |workspace, _: &zed_actions::OpenZedPredictOnboarding, window, cx| {
33 ZedPredictModal::toggle(
34 workspace,
35 workspace.user_store().clone(),
36 workspace.client().clone(),
37 window,
38 cx,
39 )
40 },
41 );
42
43 workspace.register_action(|workspace, _: &ResetOnboarding, _window, cx| {
44 update_settings_file::<AllLanguageSettings>(
45 workspace.app_state().fs.clone(),
46 cx,
47 move |file, _| {
48 file.features
49 .get_or_insert(Default::default())
50 .edit_prediction_provider = Some(EditPredictionProvider::None)
51 },
52 );
53 });
54 })
55 .detach();
56
57 feature_gate_predict_edits_rating_actions(cx);
58}
59
60fn feature_gate_predict_edits_rating_actions(cx: &mut App) {
61 let rate_completion_action_types = [TypeId::of::<RateCompletions>()];
62
63 CommandPaletteFilter::update_global(cx, |filter, _cx| {
64 filter.hide_action_types(&rate_completion_action_types);
65 filter.hide_action_types(&[zed_actions::OpenZedPredictOnboarding.type_id()]);
66 });
67
68 cx.observe_flag::<PredictEditsRateCompletionsFeatureFlag, _>(move |is_enabled, cx| {
69 if is_enabled {
70 CommandPaletteFilter::update_global(cx, |filter, _cx| {
71 filter.show_action_types(rate_completion_action_types.iter());
72 });
73 } else {
74 CommandPaletteFilter::update_global(cx, |filter, _cx| {
75 filter.hide_action_types(&rate_completion_action_types);
76 });
77 }
78 })
79 .detach();
80}