Skip to main content

ls_types/
inline_completion.rs

1use crate::{
2    Command, InsertTextFormat, Range, StaticRegistrationOptions, TextDocumentPositionParams,
3    TextDocumentRegistrationOptions, WorkDoneProgressOptions, WorkDoneProgressParams, lsp_enum,
4};
5use serde::{Deserialize, Serialize};
6
7/// Client capabilities specific to inline completions.
8///
9/// @since 3.18.0
10#[derive(Debug, Eq, PartialEq, Clone, Default, Deserialize, Serialize)]
11#[serde(rename_all = "camelCase")]
12pub struct InlineCompletionClientCapabilities {
13    /// Whether implementation supports dynamic registration for inline completion providers.
14    #[serde(skip_serializing_if = "Option::is_none")]
15    pub dynamic_registration: Option<bool>,
16}
17
18/// Inline completion options used during static registration.
19///
20/// @since 3.18.0
21#[derive(Debug, Eq, PartialEq, Clone, Default, Deserialize, Serialize)]
22pub struct InlineCompletionOptions {
23    #[serde(flatten)]
24    pub work_done_progress_options: WorkDoneProgressOptions,
25}
26
27/// Inline completion options used during static or dynamic registration.
28///
29// @since 3.18.0
30#[derive(Debug, Eq, PartialEq, Clone, Default, Deserialize, Serialize)]
31pub struct InlineCompletionRegistrationOptions {
32    #[serde(flatten)]
33    pub inline_completion_options: InlineCompletionOptions,
34
35    #[serde(flatten)]
36    pub text_document_registration_options: TextDocumentRegistrationOptions,
37
38    #[serde(flatten)]
39    pub static_registration_options: StaticRegistrationOptions,
40}
41
42/// A parameter literal used in inline completion requests.
43///
44/// @since 3.18.0
45#[derive(Debug, Eq, PartialEq, Clone, Deserialize, Serialize)]
46#[serde(rename_all = "camelCase")]
47pub struct InlineCompletionParams {
48    #[serde(flatten)]
49    pub work_done_progress_params: WorkDoneProgressParams,
50
51    #[serde(flatten)]
52    pub text_document_position: TextDocumentPositionParams,
53
54    /// Additional information about the context in which inline completions were requested.
55    pub context: InlineCompletionContext,
56}
57
58/// Describes how an [`InlineCompletionItemProvider`] was triggered.
59///
60/// @since 3.18.0
61#[derive(Eq, PartialEq, Clone, Copy, Deserialize, Serialize)]
62pub struct InlineCompletionTriggerKind(i32);
63
64lsp_enum! {
65    impl InlineCompletionTriggerKind {
66        /// Completion was triggered explicitly by a user gesture.
67        /// Return multiple completion items to enable cycling through them.
68        const INVOKED = 1;
69
70        /// Completion was triggered automatically while editing.
71        /// It is sufficient to return a single completion item in this case.
72        const AUTOMATIC = 2;
73    }
74}
75
76/// Describes the currently selected completion item.
77///
78/// @since 3.18.0
79#[derive(Debug, Eq, PartialEq, Clone, Deserialize, Serialize)]
80pub struct SelectedCompletionInfo {
81    /// The range that will be replaced if this completion item is accepted.
82    pub range: Range,
83    /// The text the range will be replaced with if this completion is
84    /// accepted.
85    pub text: String,
86}
87
88/// Provides information about the context in which an inline completion was
89/// requested.
90///
91/// @since 3.18.0
92#[derive(Debug, Eq, PartialEq, Clone, Deserialize, Serialize)]
93#[serde(rename_all = "camelCase")]
94pub struct InlineCompletionContext {
95    /// Describes how the inline completion was triggered.
96    pub trigger_kind: InlineCompletionTriggerKind,
97    /// Provides information about the currently selected item in the
98    /// autocomplete widget if it is visible.
99    ///
100    /// If set, provided inline completions must extend the text of the
101    /// selected item and use the same range, otherwise they are not shown as
102    /// preview.
103    /// As an example, if the document text is `console.` and the selected item
104    /// is `.log` replacing the `.` in the document, the inline completion must
105    /// also replace `.` and start with `.log`, for example `.log()`.
106    ///
107    /// Inline completion providers are requested again whenever the selected
108    /// item changes.
109    #[serde(skip_serializing_if = "Option::is_none")]
110    pub selected_completion_info: Option<SelectedCompletionInfo>,
111}
112
113/// `InlineCompletion` response can be multiple completion items, or a list of completion items
114#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
115#[serde(untagged)]
116pub enum InlineCompletionResponse {
117    Array(Vec<InlineCompletionItem>),
118    List(InlineCompletionList),
119}
120
121/// Represents a collection of [`InlineCompletionItem`] to be presented in the editor.
122///
123/// @since 3.18.0
124#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
125pub struct InlineCompletionList {
126    /// The inline completion items
127    pub items: Vec<InlineCompletionItem>,
128}
129
130/// An inline completion item represents a text snippet that is proposed inline
131/// to complete text that is being typed.
132///
133/// @since 3.18.0
134#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
135#[serde(rename_all = "camelCase")]
136pub struct InlineCompletionItem {
137    /// The text to replace the range with. Must be set.
138    /// Is used both for the preview and the accept operation.
139    pub insert_text: String,
140    /// A text that is used to decide if this inline completion should be
141    /// shown. When `falsy` the [`InlineCompletionItem::insertText`] is
142    /// used.
143    ///
144    /// An inline completion is shown if the text to replace is a prefix of the
145    /// filter text.
146    #[serde(skip_serializing_if = "Option::is_none")]
147    pub filter_text: Option<String>,
148    /// The range to replace.
149    /// Must begin and end on the same line.
150    ///
151    /// Prefer replacements over insertions to provide a better experience when
152    /// the user deletes typed text.
153    #[serde(skip_serializing_if = "Option::is_none")]
154    pub range: Option<Range>,
155    /// An optional command that is executed *after* inserting this
156    /// completion.
157    #[serde(skip_serializing_if = "Option::is_none")]
158    pub command: Option<Command>,
159    /// The format of the insert text. The format applies to the `insertText`.
160    /// If omitted defaults to `InsertTextFormat.PlainText`.
161    #[serde(skip_serializing_if = "Option::is_none")]
162    pub insert_text_format: Option<InsertTextFormat>,
163}