From 4213fd8e8a4894619b1ffcc1c1c1a29e3d083da9 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Tue, 4 Apr 2023 14:22:12 +0200 Subject: [PATCH] Merge pull request #2351 from zed-industries/more-copilot-enhancements Fix additional Copilot issues --- crates/copilot/src/copilot.rs | 7 ---- crates/copilot_button/src/copilot_button.rs | 9 +---- crates/editor/src/editor.rs | 42 ++++++++------------- crates/gpui/src/app.rs | 13 +++++++ crates/welcome/src/welcome.rs | 10 +---- 5 files changed, 32 insertions(+), 49 deletions(-) diff --git a/crates/copilot/src/copilot.rs b/crates/copilot/src/copilot.rs index d3a47c6068e613145c6a36a6e75883e56e7a86b9..bcf31884822eef90bb62e6075890ae11b434eec2 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>> { - match self.server { - CopilotServer::Starting { ref task } => Some(task.clone()), - _ => None, - } - } - pub fn global(cx: &AppContext) -> Option> { if cx.has_global::>() { Some(cx.global::>().clone()) diff --git a/crates/copilot_button/src/copilot_button.rs b/crates/copilot_button/src/copilot_button.rs index 62305553a368c7fde455380350c60c1660ec0eab..589d65476fed7b6f00c7f3292b70833af2267303 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::(move |cx| { - if let Some(handle) = this_handle.upgrade(cx) { - handle.update(cx, |_, cx| cx.notify()) - } - }) - .detach(); + cx.observe_global::(move |_, cx| cx.notify()) + .detach(); Self { popup_menu: menu, diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index 30df3dbb0a7e6d3f24b4454fba68bebae3e822c0..36615978cfb96e5177c875810dc04e2a7e0256a6 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, } @@ -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::(Self::on_settings_changed), ], }; this.end_selection(cx); @@ -2772,33 +2773,17 @@ impl Editor { fn refresh_copilot_suggestions(&mut self, cx: &mut ViewContext) -> Option<()> { let copilot = Copilot::global(cx)?; - - if self.mode != EditorMode::Full { - return None; - } - - let selection = self.selections.newest_anchor(); - let snapshot = self.buffer.read(cx).snapshot(cx); - - let cursor = if selection.start == selection.end { - selection.start.bias_left(&snapshot) - } else { - return None; - }; - - let language_name = snapshot - .language_at(selection.start) - .map(|language| language.name()); - - let copilot_enabled = cx.global::().copilot_on(language_name.as_deref()); - - if !copilot_enabled { + if self.mode != EditorMode::Full || !copilot.read(cx).status().is_authorized() { + self.clear_copilot_suggestions(cx); return None; } - self.refresh_active_copilot_suggestion(cx); - if !copilot.read(cx).status().is_authorized() { + let snapshot = self.buffer.read(cx).snapshot(cx); + let cursor = self.selections.newest_anchor().head(); + let language_name = snapshot.language_at(cursor).map(|language| language.name()); + if !cx.global::().copilot_on(language_name.as_deref()) { + self.clear_copilot_suggestions(cx); return None; } @@ -2868,9 +2853,10 @@ impl Editor { fn refresh_active_copilot_suggestion(&mut self, cx: &mut ViewContext) { let snapshot = self.buffer.read(cx).snapshot(cx); - let cursor = self.selections.newest_anchor().head(); + let selection = self.selections.newest_anchor(); + let cursor = selection.head(); - if self.context_menu.is_some() { + if self.context_menu.is_some() || selection.start != selection.end { self.display_map .update(cx, |map, cx| map.replace_suggestion::(None, cx)); cx.notify(); @@ -6444,6 +6430,10 @@ impl Editor { cx.notify(); } + fn on_settings_changed(&mut self, cx: &mut ViewContext) { + 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.rs index a744018e1feb9ae5d9d75c5b4ea6cbbd0b07cfce..b9bd17688382308f56017435519d9d1a19c63ff1 100644 --- a/crates/gpui/src/app.rs +++ b/crates/gpui/src/app.rs @@ -3951,6 +3951,19 @@ impl<'a, T: View> ViewContext<'a, T> { }) } + pub fn observe_global(&mut self, mut callback: F) -> Subscription + where + G: Any, + F: 'static + FnMut(&mut T, &mut ViewContext), + { + let observer = self.weak_handle(); + self.app.observe_global::(move |cx| { + if let Some(observer) = observer.upgrade(cx) { + observer.update(cx, |observer, cx| callback(observer, cx)); + } + }) + } + pub fn observe_focus(&mut self, handle: &ViewHandle, mut callback: F) -> Subscription where F: 'static + FnMut(&mut T, ViewHandle, bool, &mut ViewContext), diff --git a/crates/welcome/src/welcome.rs b/crates/welcome/src/welcome.rs index fb55c79a51e09a37d02c1ffa1e26f0522f5e282c..27022429e0b07a6d32d9f7a7372d81d9fa355d25 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 { - let handle = cx.weak_handle(); - - let settings_subscription = cx.observe_global::(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::(move |_, cx| cx.notify()), } } }