Introduce InlineCompletionProvider (#9777)

This pull request introduces a new `InlineCompletionProvider` trait,
which enables making `Editor` copilot-agnostic and lets us push all the
copilot functionality into the `copilot_ui` module. Long-term, I would
like to merge `copilot` and `copilot_ui`, but right now `project`
depends on `copilot`, which makes this impossible.

The reason for adding this new trait is so that we can experiment with
other inline completion providers and swap them at runtime using config
settings.

Please, note also that we renamed some of the existing copilot actions
to be more agnostic (see release notes below). We still kept the old
actions bound for backwards-compatibility, but we should probably remove
them at some later version.

Also, as a drive-by, we added new methods to the `Global` trait that let
you read or mutate a global directly, e.g.:

```rs
MyGlobal::update(cx, |global, cx| {
});
```

Release Notes:

- Renamed the `copilot::Suggest` action to
`editor::ShowInlineCompletion`
- Renamed the `copilot::NextSuggestion` action to
`editor::NextInlineCompletion`
- Renamed the `copilot::PreviousSuggestion` action to
`editor::PreviousInlineCompletion`
- Renamed the `editor::AcceptPartialCopilotSuggestion` action to
`editor::AcceptPartialInlineCompletion`

---------

Co-authored-by: Nathan <nathan@zed.dev>
Co-authored-by: Kyle <kylek@zed.dev>
Co-authored-by: Kyle Kelley <rgbkrk@gmail.com>
This commit is contained in:
Antonio Scandurra 2024-03-26 13:28:06 +01:00 committed by GitHub
parent b8663e56a9
commit fb6cff89d7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
45 changed files with 1472 additions and 1138 deletions

View file

@ -261,6 +261,10 @@ pub trait EventEmitter<E: Any>: 'static {}
pub trait BorrowAppContext {
/// Set a global value on the context.
fn set_global<T: Global>(&mut self, global: T);
/// Updates the global state of the given type.
fn update_global<G, R>(&mut self, f: impl FnOnce(&mut G, &mut Self) -> R) -> R
where
G: Global;
}
impl<C> BorrowAppContext for C
@ -270,6 +274,16 @@ where
fn set_global<G: Global>(&mut self, global: G) {
self.borrow_mut().set_global(global)
}
fn update_global<G, R>(&mut self, f: impl FnOnce(&mut G, &mut Self) -> R) -> R
where
G: Global,
{
let mut global = self.borrow_mut().lease_global::<G>();
let result = f(&mut global, self);
self.borrow_mut().end_global_lease(global);
result
}
}
/// A flatten equivalent for anyhow `Result`s.
@ -293,4 +307,18 @@ impl<T> Flatten<T> for Result<T> {
/// A marker trait for types that can be stored in GPUI's global state.
///
/// Implement this on types you want to store in the context as a global.
pub trait Global: 'static {}
pub trait Global: 'static + Sized {
/// Access the global of the implementing type. Panics if a global for that type has not been assigned.
fn get(cx: &AppContext) -> &Self {
cx.global()
}
/// Updates the global of the implementing type with a closure. Unlike `global_mut`, this method provides
/// your closure with mutable access to the `AppContext` and the global simultaneously.
fn update<C, R>(cx: &mut C, f: impl FnOnce(&mut Self, &mut C) -> R) -> R
where
C: BorrowAppContext,
{
cx.update_global(f)
}
}