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