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!(edit_prediction, [ResetOnboarding, RateCompletions]);
14
15pub fn init(cx: &mut App) {
16    cx.observe_new(move |workspace: &mut Workspace, _, _cx| {
17        workspace.register_action(|workspace, _: &RateCompletions, window, cx| {
18            if cx.has_flag::<PredictEditsRateCompletionsFeatureFlag>() {
19                RateCompletionModal::toggle(workspace, window, cx);
20            }
21        });
22
23        workspace.register_action(
24            move |workspace, _: &zed_actions::OpenZedPredictOnboarding, window, cx| {
25                ZedPredictModal::toggle(
26                    workspace,
27                    workspace.user_store().clone(),
28                    workspace.client().clone(),
29                    workspace.app_state().fs.clone(),
30                    window,
31                    cx,
32                )
33            },
34        );
35
36        workspace.register_action(|workspace, _: &ResetOnboarding, _window, cx| {
37            update_settings_file::<AllLanguageSettings>(
38                workspace.app_state().fs.clone(),
39                cx,
40                move |file, _| {
41                    file.features
42                        .get_or_insert(Default::default())
43                        .edit_prediction_provider = Some(EditPredictionProvider::None)
44                },
45            );
46        });
47    })
48    .detach();
49
50    feature_gate_predict_edits_rating_actions(cx);
51}
52
53fn feature_gate_predict_edits_rating_actions(cx: &mut App) {
54    let rate_completion_action_types = [TypeId::of::<RateCompletions>()];
55
56    CommandPaletteFilter::update_global(cx, |filter, _cx| {
57        filter.hide_action_types(&rate_completion_action_types);
58        filter.hide_action_types(&[zed_actions::OpenZedPredictOnboarding.type_id()]);
59    });
60
61    cx.observe_flag::<PredictEditsRateCompletionsFeatureFlag, _>(move |is_enabled, cx| {
62        if is_enabled {
63            CommandPaletteFilter::update_global(cx, |filter, _cx| {
64                filter.show_action_types(rate_completion_action_types.iter());
65            });
66        } else {
67            CommandPaletteFilter::update_global(cx, |filter, _cx| {
68                filter.hide_action_types(&rate_completion_action_types);
69            });
70        }
71    })
72    .detach();
73}