inline_completion_provider.rs

  1use crate::Direction;
  2use gpui::{AppContext, Model, ModelContext};
  3use language::Buffer;
  4use std::ops::Range;
  5use text::{Anchor, Rope};
  6
  7pub enum InlayProposal {
  8    Hint(Anchor, project::InlayHint),
  9    Suggestion(Anchor, Rope),
 10}
 11
 12pub struct CompletionProposal {
 13    pub inlays: Vec<InlayProposal>,
 14    pub text: Rope,
 15    pub delete_range: Option<Range<Anchor>>,
 16}
 17
 18pub trait InlineCompletionProvider: 'static + Sized {
 19    fn name() -> &'static str;
 20    fn is_enabled(
 21        &self,
 22        buffer: &Model<Buffer>,
 23        cursor_position: language::Anchor,
 24        cx: &AppContext,
 25    ) -> bool;
 26    fn refresh(
 27        &mut self,
 28        buffer: Model<Buffer>,
 29        cursor_position: language::Anchor,
 30        debounce: bool,
 31        cx: &mut ModelContext<Self>,
 32    );
 33    fn cycle(
 34        &mut self,
 35        buffer: Model<Buffer>,
 36        cursor_position: language::Anchor,
 37        direction: Direction,
 38        cx: &mut ModelContext<Self>,
 39    );
 40    fn accept(&mut self, cx: &mut ModelContext<Self>);
 41    fn discard(&mut self, should_report_inline_completion_event: bool, cx: &mut ModelContext<Self>);
 42    fn active_completion_text<'a>(
 43        &'a self,
 44        buffer: &Model<Buffer>,
 45        cursor_position: language::Anchor,
 46        cx: &'a AppContext,
 47    ) -> Option<CompletionProposal>;
 48}
 49
 50pub trait InlineCompletionProviderHandle {
 51    fn is_enabled(
 52        &self,
 53        buffer: &Model<Buffer>,
 54        cursor_position: language::Anchor,
 55        cx: &AppContext,
 56    ) -> bool;
 57    fn refresh(
 58        &self,
 59        buffer: Model<Buffer>,
 60        cursor_position: language::Anchor,
 61        debounce: bool,
 62        cx: &mut AppContext,
 63    );
 64    fn cycle(
 65        &self,
 66        buffer: Model<Buffer>,
 67        cursor_position: language::Anchor,
 68        direction: Direction,
 69        cx: &mut AppContext,
 70    );
 71    fn accept(&self, cx: &mut AppContext);
 72    fn discard(&self, should_report_inline_completion_event: bool, cx: &mut AppContext);
 73    fn active_completion_text<'a>(
 74        &'a self,
 75        buffer: &Model<Buffer>,
 76        cursor_position: language::Anchor,
 77        cx: &'a AppContext,
 78    ) -> Option<CompletionProposal>;
 79}
 80
 81impl<T> InlineCompletionProviderHandle for Model<T>
 82where
 83    T: InlineCompletionProvider,
 84{
 85    fn is_enabled(
 86        &self,
 87        buffer: &Model<Buffer>,
 88        cursor_position: language::Anchor,
 89        cx: &AppContext,
 90    ) -> bool {
 91        self.read(cx).is_enabled(buffer, cursor_position, cx)
 92    }
 93
 94    fn refresh(
 95        &self,
 96        buffer: Model<Buffer>,
 97        cursor_position: language::Anchor,
 98        debounce: bool,
 99        cx: &mut AppContext,
100    ) {
101        self.update(cx, |this, cx| {
102            this.refresh(buffer, cursor_position, debounce, cx)
103        })
104    }
105
106    fn cycle(
107        &self,
108        buffer: Model<Buffer>,
109        cursor_position: language::Anchor,
110        direction: Direction,
111        cx: &mut AppContext,
112    ) {
113        self.update(cx, |this, cx| {
114            this.cycle(buffer, cursor_position, direction, cx)
115        })
116    }
117
118    fn accept(&self, cx: &mut AppContext) {
119        self.update(cx, |this, cx| this.accept(cx))
120    }
121
122    fn discard(&self, should_report_inline_completion_event: bool, cx: &mut AppContext) {
123        self.update(cx, |this, cx| {
124            this.discard(should_report_inline_completion_event, cx)
125        })
126    }
127
128    fn active_completion_text<'a>(
129        &'a self,
130        buffer: &Model<Buffer>,
131        cursor_position: language::Anchor,
132        cx: &'a AppContext,
133    ) -> Option<CompletionProposal> {
134        self.read(cx)
135            .active_completion_text(buffer, cursor_position, cx)
136    }
137}