Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
Clear Copilot suggestions when it gets disabled from the settings
Antonio Scandurra79d5bb45dce1
diff --git a/crates/copilot/src/copilot.rs b/crates/copilot/src/copilot.rsindex d3a47c6068e..bcf31884822 100644--- a/crates/copilot/src/copilot.rs+++ b/crates/copilot/src/copilot.rs@@ -176,13 +176,6 @@ impl Entity for Copilot {}impl Copilot {- pub fn starting_task(&self) -> Option<Shared<Task<()>>> {- match self.server {- CopilotServer::Starting { ref task } => Some(task.clone()),- _ => None,- }- }-pub fn global(cx: &AppContext) -> Option<ModelHandle<Self>> {if cx.has_global::<ModelHandle<Self>>() {Some(cx.global::<ModelHandle<Self>>().clone())diff --git a/crates/copilot_button/src/copilot_button.rs b/crates/copilot_button/src/copilot_button.rsindex 62305553a36..589d65476fe 100644--- a/crates/copilot_button/src/copilot_button.rs+++ b/crates/copilot_button/src/copilot_button.rs@@ -228,13 +228,8 @@ impl CopilotButton {Copilot::global(cx).map(|copilot| cx.observe(&copilot, |_, _, cx| cx.notify()).detach());- let this_handle = cx.handle().downgrade();- cx.observe_global::<Settings, _>(move |cx| {- if let Some(handle) = this_handle.upgrade(cx) {- handle.update(cx, |_, cx| cx.notify())- }- })- .detach();+ cx.observe_global::<Settings, _>(move |_, cx| cx.notify())+ .detach();Self {popup_menu: menu,diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rsindex 898e94d82ad..5ce8c66563b 100644--- a/crates/editor/src/editor.rs+++ b/crates/editor/src/editor.rs@@ -509,7 +509,7 @@ pub struct Editor {hover_state: HoverState,gutter_hovered: bool,link_go_to_definition_state: LinkGoToDefinitionState,- pub copilot_state: CopilotState,+ copilot_state: CopilotState,_subscriptions: Vec<Subscription>,}@@ -1255,6 +1255,7 @@ impl Editor {cx.subscribe(&buffer, Self::on_buffer_event),cx.observe(&display_map, Self::on_display_map_changed),cx.observe(&blink_manager, |_, _, cx| cx.notify()),+ cx.observe_global::<Settings, _>(Self::on_settings_changed),],};this.end_selection(cx);@@ -2772,8 +2773,8 @@ impl Editor {fn refresh_copilot_suggestions(&mut self, cx: &mut ViewContext<Self>) -> Option<()> {let copilot = Copilot::global(cx)?;-- if self.mode != EditorMode::Full {+ if self.mode != EditorMode::Full || !copilot.read(cx).status().is_authorized() {+ self.clear_copilot_suggestions(cx);return None;}@@ -2789,19 +2790,12 @@ impl Editor {let language_name = snapshot.language_at(selection.start).map(|language| language.name());-- let copilot_enabled = cx.global::<Settings>().copilot_on(language_name.as_deref());-- if !copilot_enabled {+ if !cx.global::<Settings>().copilot_on(language_name.as_deref()) {+ self.clear_copilot_suggestions(cx);return None;}self.refresh_active_copilot_suggestion(cx);-- if !copilot.read(cx).status().is_authorized() {- return None;- }-let (buffer, buffer_position) =self.buffer.read(cx).text_anchor_for_position(cursor, cx)?;self.copilot_state.pending_refresh = cx.spawn_weak(|this, mut cx| async move {@@ -6444,6 +6438,10 @@ impl Editor {cx.notify();}+ fn on_settings_changed(&mut self, cx: &mut ViewContext<Self>) {+ self.refresh_copilot_suggestions(cx);+ }+pub fn set_searchable(&mut self, searchable: bool) {self.searchable = searchable;}diff --git a/crates/gpui/src/app.rs b/crates/gpui/src/app.rsindex 4f14d4e9d62..0923c6993e3 100644--- a/crates/gpui/src/app.rs+++ b/crates/gpui/src/app.rs@@ -3952,6 +3952,19 @@ impl<'a, T: View> ViewContext<'a, T> {})}+ pub fn observe_global<G, F>(&mut self, mut callback: F) -> Subscription+ where+ G: Any,+ F: 'static + FnMut(&mut T, &mut ViewContext<T>),+ {+ let observer = self.weak_handle();+ self.app.observe_global::<G, _>(move |cx| {+ if let Some(observer) = observer.upgrade(cx) {+ observer.update(cx, |observer, cx| callback(observer, cx));+ }+ })+ }+pub fn observe_focus<F, V>(&mut self, handle: &ViewHandle<V>, mut callback: F) -> SubscriptionwhereF: 'static + FnMut(&mut T, ViewHandle<V>, bool, &mut ViewContext<T>),diff --git a/crates/welcome/src/welcome.rs b/crates/welcome/src/welcome.rsindex fa057fdafdf..33d93dcd87f 100644--- a/crates/welcome/src/welcome.rs+++ b/crates/welcome/src/welcome.rs@@ -191,16 +191,8 @@ impl View for WelcomePage {impl WelcomePage {pub fn new(cx: &mut ViewContext<Self>) -> Self {- let handle = cx.weak_handle();-- let settings_subscription = cx.observe_global::<Settings, _>(move |cx| {- if let Some(handle) = handle.upgrade(cx) {- handle.update(cx, |_, cx| cx.notify())- }- });-WelcomePage {- _settings_subscription: settings_subscription,+ _settings_subscription: cx.observe_global::<Settings, _>(move |_, cx| cx.notify()),}}}