init.rs

 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                    workspace.app_state().fs.clone(),
38                    window,
39                    cx,
40                )
41            },
42        );
43
44        workspace.register_action(|workspace, _: &ResetOnboarding, _window, cx| {
45            update_settings_file::<AllLanguageSettings>(
46                workspace.app_state().fs.clone(),
47                cx,
48                move |file, _| {
49                    file.features
50                        .get_or_insert(Default::default())
51                        .edit_prediction_provider = Some(EditPredictionProvider::None)
52                },
53            );
54        });
55    })
56    .detach();
57
58    feature_gate_predict_edits_rating_actions(cx);
59}
60
61fn feature_gate_predict_edits_rating_actions(cx: &mut App) {
62    let rate_completion_action_types = [TypeId::of::<RateCompletions>()];
63
64    CommandPaletteFilter::update_global(cx, |filter, _cx| {
65        filter.hide_action_types(&rate_completion_action_types);
66        filter.hide_action_types(&[zed_actions::OpenZedPredictOnboarding.type_id()]);
67    });
68
69    cx.observe_flag::<PredictEditsRateCompletionsFeatureFlag, _>(move |is_enabled, cx| {
70        if is_enabled {
71            CommandPaletteFilter::update_global(cx, |filter, _cx| {
72                filter.show_action_types(rate_completion_action_types.iter());
73            });
74        } else {
75            CommandPaletteFilter::update_global(cx, |filter, _cx| {
76                filter.hide_action_types(&rate_completion_action_types);
77            });
78        }
79    })
80    .detach();
81}