inline_completion_provider.rs

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