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 project::DisableAiSettings;
  8use settings::{Settings, SettingsStore, update_settings_file};
  9use ui::App;
 10use workspace::Workspace;
 11
 12use crate::{RateCompletionModal, onboarding_modal::ZedPredictModal};
 13
 14actions!(
 15    edit_prediction,
 16    [
 17        /// Resets the edit prediction onboarding state.
 18        ResetOnboarding,
 19        /// Opens the rate completions modal.
 20        RateCompletions
 21    ]
 22);
 23
 24pub fn init(cx: &mut App) {
 25    feature_gate_predict_edits_actions(cx);
 26
 27    cx.observe_new(move |workspace: &mut Workspace, _, _cx| {
 28        workspace.register_action(|workspace, _: &RateCompletions, window, cx| {
 29            if cx.has_flag::<PredictEditsRateCompletionsFeatureFlag>() {
 30                RateCompletionModal::toggle(workspace, window, cx);
 31            }
 32        });
 33
 34        workspace.register_action(
 35            move |workspace, _: &zed_actions::OpenZedPredictOnboarding, window, cx| {
 36                ZedPredictModal::toggle(
 37                    workspace,
 38                    workspace.user_store().clone(),
 39                    workspace.client().clone(),
 40                    window,
 41                    cx,
 42                )
 43            },
 44        );
 45
 46        workspace.register_action(|workspace, _: &ResetOnboarding, _window, cx| {
 47            update_settings_file::<AllLanguageSettings>(
 48                workspace.app_state().fs.clone(),
 49                cx,
 50                move |file, _| {
 51                    file.features
 52                        .get_or_insert(Default::default())
 53                        .edit_prediction_provider = Some(EditPredictionProvider::None)
 54                },
 55            );
 56        });
 57    })
 58    .detach();
 59}
 60
 61fn feature_gate_predict_edits_actions(cx: &mut App) {
 62    let rate_completion_action_types = [TypeId::of::<RateCompletions>()];
 63    let reset_onboarding_action_types = [TypeId::of::<ResetOnboarding>()];
 64    let zeta_all_action_types = [
 65        TypeId::of::<RateCompletions>(),
 66        TypeId::of::<ResetOnboarding>(),
 67        zed_actions::OpenZedPredictOnboarding.type_id(),
 68        TypeId::of::<crate::ClearHistory>(),
 69        TypeId::of::<crate::ThumbsUpActiveCompletion>(),
 70        TypeId::of::<crate::ThumbsDownActiveCompletion>(),
 71        TypeId::of::<crate::NextEdit>(),
 72        TypeId::of::<crate::PreviousEdit>(),
 73    ];
 74
 75    CommandPaletteFilter::update_global(cx, |filter, _cx| {
 76        filter.hide_action_types(&rate_completion_action_types);
 77        filter.hide_action_types(&reset_onboarding_action_types);
 78        filter.hide_action_types(&[zed_actions::OpenZedPredictOnboarding.type_id()]);
 79    });
 80
 81    cx.observe_global::<SettingsStore>(move |cx| {
 82        let is_ai_disabled = DisableAiSettings::get_global(cx).disable_ai;
 83        let has_feature_flag = cx.has_flag::<PredictEditsRateCompletionsFeatureFlag>();
 84
 85        CommandPaletteFilter::update_global(cx, |filter, _cx| {
 86            if is_ai_disabled {
 87                filter.hide_action_types(&zeta_all_action_types);
 88            } else if has_feature_flag {
 89                filter.show_action_types(&rate_completion_action_types);
 90            } else {
 91                filter.hide_action_types(&rate_completion_action_types);
 92            }
 93        });
 94    })
 95    .detach();
 96
 97    cx.observe_flag::<PredictEditsRateCompletionsFeatureFlag, _>(move |is_enabled, cx| {
 98        if !DisableAiSettings::get_global(cx).disable_ai {
 99            if is_enabled {
100                CommandPaletteFilter::update_global(cx, |filter, _cx| {
101                    filter.show_action_types(&rate_completion_action_types);
102                });
103            } else {
104                CommandPaletteFilter::update_global(cx, |filter, _cx| {
105                    filter.hide_action_types(&rate_completion_action_types);
106                });
107            }
108        }
109    })
110    .detach();
111}