Merge pull request #2351 from zed-industries/more-copilot-enhancements

Fix additional Copilot issues
This commit is contained in:
Antonio Scandurra 2023-04-04 14:22:12 +02:00 committed by GitHub
commit 5df5973262
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 32 additions and 49 deletions

View File

@ -176,13 +176,6 @@ impl Entity for Copilot {
} }
impl 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>> { pub fn global(cx: &AppContext) -> Option<ModelHandle<Self>> {
if cx.has_global::<ModelHandle<Self>>() { if cx.has_global::<ModelHandle<Self>>() {
Some(cx.global::<ModelHandle<Self>>().clone()) Some(cx.global::<ModelHandle<Self>>().clone())

View File

@ -228,13 +228,8 @@ 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());
let this_handle = cx.handle().downgrade(); cx.observe_global::<Settings, _>(move |_, cx| cx.notify())
cx.observe_global::<Settings, _>(move |cx| { .detach();
if let Some(handle) = this_handle.upgrade(cx) {
handle.update(cx, |_, cx| cx.notify())
}
})
.detach();
Self { Self {
popup_menu: menu, popup_menu: menu,

View File

@ -509,7 +509,7 @@ pub struct Editor {
hover_state: HoverState, hover_state: HoverState,
gutter_hovered: bool, gutter_hovered: bool,
link_go_to_definition_state: LinkGoToDefinitionState, link_go_to_definition_state: LinkGoToDefinitionState,
pub copilot_state: CopilotState, copilot_state: CopilotState,
_subscriptions: Vec<Subscription>, _subscriptions: Vec<Subscription>,
} }
@ -1255,6 +1255,7 @@ impl Editor {
cx.subscribe(&buffer, Self::on_buffer_event), cx.subscribe(&buffer, Self::on_buffer_event),
cx.observe(&display_map, Self::on_display_map_changed), cx.observe(&display_map, Self::on_display_map_changed),
cx.observe(&blink_manager, |_, _, cx| cx.notify()), cx.observe(&blink_manager, |_, _, cx| cx.notify()),
cx.observe_global::<Settings, _>(Self::on_settings_changed),
], ],
}; };
this.end_selection(cx); this.end_selection(cx);
@ -2772,33 +2773,17 @@ impl Editor {
fn refresh_copilot_suggestions(&mut self, cx: &mut ViewContext<Self>) -> Option<()> { fn refresh_copilot_suggestions(&mut self, cx: &mut ViewContext<Self>) -> Option<()> {
let copilot = Copilot::global(cx)?; let copilot = Copilot::global(cx)?;
if self.mode != EditorMode::Full || !copilot.read(cx).status().is_authorized() {
if self.mode != EditorMode::Full { self.clear_copilot_suggestions(cx);
return None; 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::<Settings>().copilot_on(language_name.as_deref());
if !copilot_enabled {
return None;
}
self.refresh_active_copilot_suggestion(cx); 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::<Settings>().copilot_on(language_name.as_deref()) {
self.clear_copilot_suggestions(cx);
return None; return None;
} }
@ -2868,9 +2853,10 @@ impl Editor {
fn refresh_active_copilot_suggestion(&mut self, cx: &mut ViewContext<Self>) { fn refresh_active_copilot_suggestion(&mut self, cx: &mut ViewContext<Self>) {
let snapshot = self.buffer.read(cx).snapshot(cx); 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 self.display_map
.update(cx, |map, cx| map.replace_suggestion::<usize>(None, cx)); .update(cx, |map, cx| map.replace_suggestion::<usize>(None, cx));
cx.notify(); cx.notify();
@ -6444,6 +6430,10 @@ impl Editor {
cx.notify(); 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) { pub fn set_searchable(&mut self, searchable: bool) {
self.searchable = searchable; self.searchable = searchable;
} }

View File

@ -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) -> Subscription pub fn observe_focus<F, V>(&mut self, handle: &ViewHandle<V>, mut callback: F) -> Subscription
where where
F: 'static + FnMut(&mut T, ViewHandle<V>, bool, &mut ViewContext<T>), F: 'static + FnMut(&mut T, ViewHandle<V>, bool, &mut ViewContext<T>),

View File

@ -191,16 +191,8 @@ impl View for WelcomePage {
impl WelcomePage { impl WelcomePage {
pub fn new(cx: &mut ViewContext<Self>) -> Self { 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 { WelcomePage {
_settings_subscription: settings_subscription, _settings_subscription: cx.observe_global::<Settings, _>(move |_, cx| cx.notify()),
} }
} }
} }