Introduce staff-only inline completion provider (#21739)

Release Notes:

- N/A

---------

Co-authored-by: Thorsten Ball <mrnugget@gmail.com>
Co-authored-by: Bennet <bennet@zed.dev>
Co-authored-by: Thorsten <thorsten@zed.dev>
This commit is contained in:
Antonio Scandurra 2024-12-09 14:26:36 +01:00 committed by GitHub
parent 39e8944dcc
commit 77b8296fbb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
39 changed files with 2890 additions and 356 deletions

View file

@ -14,5 +14,3 @@ path = "src/inline_completion.rs"
[dependencies]
gpui.workspace = true
language.workspace = true
project.workspace = true
text.workspace = true

View file

@ -1,7 +1,6 @@
use gpui::{AppContext, Model, ModelContext};
use language::Buffer;
use std::ops::Range;
use text::{Anchor, Rope};
// TODO: Find a better home for `Direction`.
//
@ -13,15 +12,9 @@ pub enum Direction {
Next,
}
pub enum InlayProposal {
Hint(Anchor, project::InlayHint),
Suggestion(Anchor, Rope),
}
pub struct CompletionProposal {
pub inlays: Vec<InlayProposal>,
pub text: Rope,
pub delete_range: Option<Range<Anchor>>,
#[derive(Clone)]
pub struct InlineCompletion {
pub edits: Vec<(Range<language::Anchor>, String)>,
}
pub trait InlineCompletionProvider: 'static + Sized {
@ -47,16 +40,17 @@ pub trait InlineCompletionProvider: 'static + Sized {
cx: &mut ModelContext<Self>,
);
fn accept(&mut self, cx: &mut ModelContext<Self>);
fn discard(&mut self, should_report_inline_completion_event: bool, cx: &mut ModelContext<Self>);
fn active_completion_text<'a>(
&'a self,
fn discard(&mut self, cx: &mut ModelContext<Self>);
fn suggest(
&mut self,
buffer: &Model<Buffer>,
cursor_position: language::Anchor,
cx: &'a AppContext,
) -> Option<CompletionProposal>;
cx: &mut ModelContext<Self>,
) -> Option<InlineCompletion>;
}
pub trait InlineCompletionProviderHandle {
fn name(&self) -> &'static str;
fn is_enabled(
&self,
buffer: &Model<Buffer>,
@ -78,19 +72,23 @@ pub trait InlineCompletionProviderHandle {
cx: &mut AppContext,
);
fn accept(&self, cx: &mut AppContext);
fn discard(&self, should_report_inline_completion_event: bool, cx: &mut AppContext);
fn active_completion_text<'a>(
&'a self,
fn discard(&self, cx: &mut AppContext);
fn suggest(
&self,
buffer: &Model<Buffer>,
cursor_position: language::Anchor,
cx: &'a AppContext,
) -> Option<CompletionProposal>;
cx: &mut AppContext,
) -> Option<InlineCompletion>;
}
impl<T> InlineCompletionProviderHandle for Model<T>
where
T: InlineCompletionProvider,
{
fn name(&self) -> &'static str {
T::name()
}
fn is_enabled(
&self,
buffer: &Model<Buffer>,
@ -128,19 +126,16 @@ where
self.update(cx, |this, cx| this.accept(cx))
}
fn discard(&self, should_report_inline_completion_event: bool, cx: &mut AppContext) {
self.update(cx, |this, cx| {
this.discard(should_report_inline_completion_event, cx)
})
fn discard(&self, cx: &mut AppContext) {
self.update(cx, |this, cx| this.discard(cx))
}
fn active_completion_text<'a>(
&'a self,
fn suggest(
&self,
buffer: &Model<Buffer>,
cursor_position: language::Anchor,
cx: &'a AppContext,
) -> Option<CompletionProposal> {
self.read(cx)
.active_completion_text(buffer, cursor_position, cx)
cx: &mut AppContext,
) -> Option<InlineCompletion> {
self.update(cx, |this, cx| this.suggest(buffer, cursor_position, cx))
}
}