Fix leaked handle and failure to update language in context menu

This commit is contained in:
Mikayla Maki 2023-03-30 17:40:11 -07:00
parent e38f52d595
commit 713f5f604f

View file

@ -77,7 +77,6 @@ pub struct CopilotButton {
editor_subscription: Option<(Subscription, usize)>, editor_subscription: Option<(Subscription, usize)>,
editor_enabled: Option<bool>, editor_enabled: Option<bool>,
language: Option<Arc<str>>, language: Option<Arc<str>>,
// _settings_subscription: Subscription,
} }
impl Entity for CopilotButton { impl Entity for CopilotButton {
@ -229,17 +228,19 @@ impl CopilotButton {
Copilot::global(cx).map(|copilot| cx.observe(&copilot, |_, _, cx| cx.notify()).detach()); Copilot::global(cx).map(|copilot| cx.observe(&copilot, |_, _, cx| cx.notify()).detach());
// TODO: Determine why this leaked. let this_handle = cx.handle().downgrade();
// let this_handle = cx.handle(); cx.observe_global::<Settings, _>(move |cx| {
// let sub = if let Some(handle) = this_handle.upgrade(cx) {
// cx.observe_global::<Settings, _>(move |cx| this_handle.update(cx, |_, cx| cx.notify())); handle.update(cx, |_, cx| cx.notify())
}
})
.detach();
Self { Self {
popup_menu: menu, popup_menu: menu,
editor_subscription: None, editor_subscription: None,
editor_enabled: None, editor_enabled: None,
language: None, language: None,
// _settings_subscription: sub,
} }
} }
@ -323,12 +324,6 @@ impl CopilotButton {
pub fn update_enabled(&mut self, editor: ViewHandle<Editor>, cx: &mut ViewContext<Self>) { pub fn update_enabled(&mut self, editor: ViewHandle<Editor>, cx: &mut ViewContext<Self>) {
let editor = editor.read(cx); let editor = editor.read(cx);
if let Some(enabled) = editor.copilot_state.user_enabled {
self.editor_enabled = Some(enabled);
cx.notify();
return;
}
let snapshot = editor.buffer().read(cx).snapshot(cx); let snapshot = editor.buffer().read(cx).snapshot(cx);
let settings = cx.global::<Settings>(); let settings = cx.global::<Settings>();
let suggestion_anchor = editor.selections.newest_anchor().start; let suggestion_anchor = editor.selections.newest_anchor().start;
@ -338,7 +333,13 @@ impl CopilotButton {
.map(|language| language.name()); .map(|language| language.name());
self.language = language_name.clone(); self.language = language_name.clone();
if let Some(enabled) = editor.copilot_state.user_enabled {
self.editor_enabled = Some(enabled);
} else {
self.editor_enabled = Some(settings.copilot_on(language_name.as_deref())); self.editor_enabled = Some(settings.copilot_on(language_name.as_deref()));
}
cx.notify() cx.notify()
} }
} }