laburnum 1.17.0

An LSP framework for building language servers and compilers, powered by an incremental query tree with content-addressed storage, task-based dataflow, and parallel queries.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
// Copyright Two Neutron Stars Incorporated and contributors
// SPDX-License-Identifier: BlueOak-1.0.0

use {
  crate::{
    TRACER,
    database::{DynPartition, PartitionKey, PartitionWriteContextRef},
    partitions::DocumentSymbols,
    protocol::{
      jsonrpc,
      lsp::{
        Command, Documentation, MarkupKind, PartialResultParams,
        PositionEncodingKind, Range, TagSupport, TextDocumentPositionParams,
        TextDocumentRegistrationOptions, TextEdit, WorkDoneProgressOptions,
        WorkDoneProgressParams,
      },
      macros::lsp_enum,
    },
    record::LaburnumRecordRef,
    scheduler::task::TaskContext,
    source::line_ops::LineOps,
  },
  opentelemetry::trace::FutureExt,
  serde::{Deserialize, Serialize},
  serde_json::Value,
  std::fmt::Debug,
};

/// Defines how to interpret the insert text in a completion item
#[derive(Clone, Copy, PartialEq, Eq, Deserialize, Serialize)]
#[serde(transparent)]
pub struct InsertTextFormat(i32);

lsp_enum! {
    impl InsertTextFormat {
        const PLAIN_TEXT = 1;
        const SNIPPET = 2;
    }
}

/// The kind of a completion entry.
#[derive(Clone, Copy, PartialEq, Eq, Deserialize, Serialize)]
#[serde(transparent)]
pub struct CompletionItemKind(i32);

lsp_enum! {
    impl CompletionItemKind {
        const TEXT = 1;
        const METHOD = 2;
        const FUNCTION = 3;
        const CONSTRUCTOR = 4;
        const FIELD = 5;
        const VARIABLE = 6;
        const CLASS = 7;
        const INTERFACE = 8;
        const MODULE = 9;
        const PROPERTY = 10;
        const UNIT = 11;
        const VALUE = 12;
        const ENUM = 13;
        const KEYWORD = 14;
        const SNIPPET = 15;
        const COLOR = 16;
        const FILE = 17;
        const REFERENCE = 18;
        const FOLDER = 19;
        const ENUM_MEMBER = 20;
        const CONSTANT = 21;
        const STRUCT = 22;
        const EVENT = 23;
        const OPERATOR = 24;
        const TYPE_PARAMETER = 25;
    }
}

#[derive(Debug, Default, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CompletionItemCapability {
  /// Client supports snippets as insert text.
  ///
  /// A snippet can define tab stops and placeholders with `$1`, `$2`
  /// and `${3:foo}`. `$0` defines the final tab stop, it defaults to
  /// the end of the snippet. Placeholders with equal identifiers are linked,
  /// that is typing in one will update others too.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub snippet_support: Option<bool>,

  /// Client supports commit characters on a completion item.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub commit_characters_support: Option<bool>,

  /// Client supports the follow content formats for the documentation
  /// property. The order describes the preferred format of the client.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub documentation_format: Option<Vec<MarkupKind>>,

  /// Client supports the deprecated property on a completion item.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub deprecated_support: Option<bool>,

  /// Client supports the preselect property on a completion item.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub preselect_support: Option<bool>,

  /// Client supports the tag property on a completion item. Clients supporting
  /// tags have to handle unknown tags gracefully. Clients especially need to
  /// preserve unknown tags when sending a completion item back to the server
  /// in a resolve call.
  #[serde(
    default,
    skip_serializing_if = "Option::is_none",
    deserialize_with = "TagSupport::deserialize_compat"
  )]
  pub tag_support: Option<TagSupport<CompletionItemTag>>,

  /// Client support insert replace edit to control different behavior if a
  /// completion item is inserted in the text or should replace text.
  ///
  /// @since 3.16.0
  #[serde(skip_serializing_if = "Option::is_none")]
  pub insert_replace_support: Option<bool>,

  /// Indicates which properties a client can resolve lazily on a completion
  /// item. Before version 3.16.0 only the predefined properties
  /// `documentation` and `details` could be resolved lazily.
  ///
  /// @since 3.16.0
  #[serde(skip_serializing_if = "Option::is_none")]
  pub resolve_support: Option<CompletionItemCapabilityResolveSupport>,

  /// The client supports the `insertTextMode` property on
  /// a completion item to override the whitespace handling mode
  /// as defined by the client.
  ///
  /// @since 3.16.0
  #[serde(skip_serializing_if = "Option::is_none")]
  pub insert_text_mode_support: Option<InsertTextModeSupport>,

  /// The client has support for completion item label
  /// details (see also `CompletionItemLabelDetails`).
  ///
  /// @since 3.17.0
  #[serde(skip_serializing_if = "Option::is_none")]
  pub label_details_support: Option<bool>,
}

#[derive(Debug, Default, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CompletionItemCapabilityResolveSupport {
  /// The properties that a client can resolve lazily.
  pub properties: Vec<String>,
}

#[derive(Debug, Default, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct InsertTextModeSupport {
  pub value_set: Vec<InsertTextMode>,
}

/// How whitespace and indentation is handled during completion
/// item insertion.
///
/// @since 3.16.0
#[derive(Clone, Copy, PartialEq, Eq, Deserialize, Serialize)]
#[serde(transparent)]
pub struct InsertTextMode(i32);

lsp_enum! {
    impl InsertTextMode {
        /// The insertion or replace strings is taken as it is. If the
        /// value is multi line the lines below the cursor will be
        /// inserted using the indentation defined in the string value.
        /// The client will not apply any kind of adjustments to the
        /// string.
        const AS_IS = 1;

        /// The editor adjusts leading whitespace of new lines so that
        /// they match the indentation up to the cursor of the line for
        /// which the item is accepted.
        ///
        /// Consider a line like this: `<2tabs><cursor><3tabs>foo`. Accepting a
        /// multi line completion item is indented using 2 tabs all
        /// following lines inserted will be indented using 2 tabs as well.
        const ADJUST_INDENTATION = 2;
    }
}

#[derive(Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(transparent)]
pub struct CompletionItemTag(i32);

lsp_enum! {
    impl CompletionItemTag {
        const DEPRECATED = 1;
    }
}

#[derive(Debug, Default, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CompletionItemKindCapability {
  /// The completion item kind values the client supports. When this
  /// property exists the client also guarantees that it will
  /// handle values outside its set gracefully and falls back
  /// to a default value when unknown.
  ///
  /// If this property is not present the client only supports
  /// the completion items kinds from `Text` to `Reference` as defined in
  /// the initial version of the protocol.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub value_set: Option<Vec<CompletionItemKind>>,
}

#[derive(Debug, Default, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CompletionListCapability {
  /// The client supports the following itemDefaults on
  /// a completion list.
  ///
  /// The value lists the supported property names of the
  /// `CompletionList.itemDefaults` object. If omitted
  /// no properties are supported.
  ///
  /// @since 3.17.0
  #[serde(skip_serializing_if = "Option::is_none")]
  pub item_defaults: Option<Vec<String>>,
}

#[derive(Debug, Default, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CompletionClientCapabilities {
  /// Whether completion supports dynamic registration.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub dynamic_registration: Option<bool>,

  /// The client supports the following `CompletionItem` specific
  /// capabilities.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub completion_item: Option<CompletionItemCapability>,

  #[serde(skip_serializing_if = "Option::is_none")]
  pub completion_item_kind: Option<CompletionItemKindCapability>,

  /// The client supports to send additional context information for a
  /// `textDocument/completion` request.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub context_support: Option<bool>,

  /// The client's default when the completion item doesn't provide a
  /// `insertTextMode` property.
  ///
  /// @since 3.17.0
  #[serde(skip_serializing_if = "Option::is_none")]
  pub insert_text_mode: Option<InsertTextMode>,

  /// The client supports the following `CompletionList` specific
  /// capabilities.
  ///
  /// @since 3.17.0
  #[serde(skip_serializing_if = "Option::is_none")]
  pub completion_list: Option<CompletionListCapability>,
}

/// A special text edit to provide an insert and a replace operation.
///
/// @since 3.16.0
#[derive(Debug, Default, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct InsertReplaceEdit {
  /// The string to be inserted.
  pub new_text: String,

  /// The range if the insert is requested
  pub insert: Range,

  /// The range if the replace is requested.
  pub replace: Range,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(untagged)]
pub enum CompletionTextEdit {
  Edit(TextEdit),
  InsertAndReplace(InsertReplaceEdit),
}

impl From<TextEdit> for CompletionTextEdit {
  fn from(edit: TextEdit) -> Self {
    Self::Edit(edit)
  }
}

impl From<InsertReplaceEdit> for CompletionTextEdit {
  fn from(edit: InsertReplaceEdit) -> Self {
    Self::InsertAndReplace(edit)
  }
}

/// Completion options.
#[derive(Debug, Default, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CompletionOptions {
  /// The server provides support to resolve additional information for a
  /// completion item.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub resolve_provider: Option<bool>,

  /// Most tools trigger completion request automatically without explicitly
  /// requesting it using a keyboard shortcut (e.g. Ctrl+Space). Typically they
  /// do so when the user starts to type an identifier. For example if the user
  /// types `c` in a JavaScript file code complete will automatically pop up
  /// present `console` besides others as a completion item. Characters that
  /// make up identifiers don't need to be listed here.
  ///
  /// If code complete should automatically be trigger on characters not being
  /// valid inside an identifier (for example `.` in JavaScript) list them in
  /// `triggerCharacters`.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub trigger_characters: Option<Vec<String>>,

  /// The list of all possible characters that commit a completion. This field
  /// can be used if clients don't support individual commit characters per
  /// completion item. See client capability
  /// `completion.completionItem.commitCharactersSupport`.
  ///
  /// If a server provides both `allCommitCharacters` and commit characters on
  /// an individual completion item the ones on the completion item win.
  ///
  /// @since 3.2.0
  #[serde(skip_serializing_if = "Option::is_none")]
  pub all_commit_characters: Option<Vec<String>>,

  #[serde(flatten)]
  pub work_done_progress_options: WorkDoneProgressOptions,

  /// The server supports the following `CompletionItem` specific
  /// capabilities.
  ///
  /// @since 3.17.0
  #[serde(skip_serializing_if = "Option::is_none")]
  pub completion_item: Option<CompletionOptionsCompletionItem>,
}

#[derive(Debug, Default, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CompletionOptionsCompletionItem {
  /// The server has support for completion item label
  /// details (see also `CompletionItemLabelDetails`) when receiving
  /// a completion item in a resolve call.
  ///
  /// @since 3.17.0
  #[serde(skip_serializing_if = "Option::is_none")]
  pub label_details_support: Option<bool>,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
pub struct CompletionRegistrationOptions {
  #[serde(flatten)]
  pub text_document_registration_options: TextDocumentRegistrationOptions,

  #[serde(flatten)]
  pub completion_options: CompletionOptions,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(untagged)]
pub enum CompletionResponse {
  Array(Vec<CompletionItem>),
  List(CompletionList),
}

impl From<Vec<CompletionItem>> for CompletionResponse {
  fn from(items: Vec<CompletionItem>) -> Self {
    Self::Array(items)
  }
}

impl From<CompletionList> for CompletionResponse {
  fn from(list: CompletionList) -> Self {
    Self::List(list)
  }
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CompletionParams {
  // This field was "mixed-in" from TextDocumentPositionParams
  #[serde(flatten)]
  pub text_document_position: TextDocumentPositionParams,

  #[serde(flatten)]
  pub work_done_progress_params: WorkDoneProgressParams,

  #[serde(flatten)]
  pub partial_result_params: PartialResultParams,

  // CompletionParams properties:
  #[serde(skip_serializing_if = "Option::is_none")]
  pub context: Option<CompletionContext>,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CompletionContext {
  /// How the completion was triggered.
  pub trigger_kind: CompletionTriggerKind,

  /// The trigger character (a single character) that has trigger code
  /// complete. Is undefined if `triggerKind !==
  /// CompletionTriggerKind.TriggerCharacter`
  #[serde(skip_serializing_if = "Option::is_none")]
  pub trigger_character: Option<String>,
}

/// How a completion was triggered.
#[derive(Clone, Copy, PartialEq, Eq, Deserialize, Serialize)]
#[serde(transparent)]
pub struct CompletionTriggerKind(i32);

lsp_enum! {
    impl CompletionTriggerKind {
        const INVOKED = 1;
        const TRIGGER_CHARACTER = 2;
        const TRIGGER_FOR_INCOMPLETE_COMPLETIONS = 3;
    }
}

/// Represents a collection of [completion items](#CompletionItem) to be
/// presented in the editor.
#[derive(Debug, Default, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CompletionList {
  /// This list it not complete. Further typing should result in recomputing
  /// this list.
  pub is_incomplete: bool,

  /// The completion items.
  pub items: Vec<CompletionItem>,
}

#[derive(Debug, Default, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CompletionItem {
  /// The label of this completion item. By default
  /// also the text that is inserted when selecting
  /// this completion.
  pub label: String,

  /// Additional details for the label
  ///
  /// @since 3.17.0
  #[serde(skip_serializing_if = "Option::is_none")]
  pub label_details: Option<CompletionItemLabelDetails>,

  /// The kind of this completion item. Based of the kind
  /// an icon is chosen by the editor.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub kind: Option<CompletionItemKind>,

  /// A human-readable string with additional information
  /// about this item, like type or symbol information.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub detail: Option<String>,

  /// A human-readable string that represents a doc-comment.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub documentation: Option<Documentation>,

  /// Indicates if this item is deprecated.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub deprecated: Option<bool>,

  /// Select this item when showing.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub preselect: Option<bool>,

  /// A string that should be used when comparing this item
  /// with other items. When `falsy` the label is used
  /// as the sort text for this item.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub sort_text: Option<String>,

  /// A string that should be used when filtering a set of
  /// completion items. When `falsy` the label is used as the
  /// filter text for this item.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub filter_text: Option<String>,

  /// A string that should be inserted into a document when selecting
  /// this completion. When `falsy` the label is used as the insert text
  /// for this item.
  ///
  /// The `insertText` is subject to interpretation by the client side.
  /// Some tools might not take the string literally. For example
  /// VS Code when code complete is requested in this example
  /// `con<cursor position>` and a completion item with an `insertText` of
  /// `console` is provided it will only insert `sole`. Therefore it is
  /// recommended to use `textEdit` instead since it avoids additional client
  /// side interpretation.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub insert_text: Option<String>,

  /// The format of the insert text. The format applies to both the
  /// `insertText` property and the `newText` property of a provided
  /// `textEdit`. If omitted defaults to `InsertTextFormat.PlainText`.
  ///
  /// @since 3.16.0
  #[serde(skip_serializing_if = "Option::is_none")]
  pub insert_text_format: Option<InsertTextFormat>,

  /// How whitespace and indentation is handled during completion
  /// item insertion. If not provided the client's default value depends on
  /// the `textDocument.completion.insertTextMode` client capability.
  ///
  /// @since 3.16.0
  /// @since 3.17.0 - support for `textDocument.completion.insertTextMode`
  #[serde(skip_serializing_if = "Option::is_none")]
  pub insert_text_mode: Option<InsertTextMode>,

  /// An edit which is applied to a document when selecting
  /// this completion. When an edit is provided the value of
  /// insertText is ignored.
  ///
  /// Most editors support two different operation when accepting a completion
  /// item. One is to insert a

  /// completion text and the other is to replace an existing text with a
  /// completion text. Since this can usually not predetermined by a server
  /// it can report both ranges. Clients need to signal support for
  /// `InsertReplaceEdits` via the
  /// `textDocument.completion.insertReplaceSupport` client capability
  /// property.
  ///
  /// *Note 1:* The text edit's range as well as both ranges from a insert
  /// replace edit must be a [single line] and they must contain the position
  /// at which completion has been requested. *Note 2:* If an
  /// `InsertReplaceEdit` is returned the edit's insert range must be a prefix
  /// of the edit's replace range, that means it must be contained and
  /// starting at the same position.
  ///
  /// @since 3.16.0 additional type `InsertReplaceEdit`
  #[serde(skip_serializing_if = "Option::is_none")]
  pub text_edit: Option<CompletionTextEdit>,

  /// An optional array of additional text edits that are applied when
  /// selecting this completion. Edits must not overlap with the main edit
  /// nor with themselves.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub additional_text_edits: Option<Vec<TextEdit>>,

  /// An optional command that is executed *after* inserting this completion.
  /// *Note* that additional modifications to the current document should be
  /// described with the additionalTextEdits-property.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub command: Option<Command>,

  /// An optional set of characters that when pressed while this completion is
  /// active will accept it first and then type that character. *Note* that all
  /// commit characters should have `length=1` and that superfluous characters
  /// will be ignored.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub commit_characters: Option<Vec<String>>,

  /// An data entry field that is preserved on a completion item between
  /// a completion and a completion resolve request.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub data: Option<Value>,

  /// Tags for this completion item.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub tags: Option<Vec<CompletionItemTag>>,
}

impl CompletionItem {
  /// Create a `CompletionItem` with the minimum possible info (label and
  /// detail).
  #[must_use]
  pub fn new_simple(label: String, detail: String) -> Self {
    Self {
      label,
      detail: Some(detail),
      ..Self::default()
    }
  }
}

/// Additional details for a completion item label.
///
/// @since 3.17.0
#[derive(Debug, Default, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CompletionItemLabelDetails {
  /// An optional string which is rendered less prominently directly after
  /// {@link CompletionItemLabel.label label}, without any spacing. Should be
  /// used for function signatures or type annotations.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub detail: Option<String>,

  /// An optional string which is rendered less prominently after
  /// {@link CompletionItemLabel.detail}. Should be used for fully qualified
  /// names or file path.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub description: Option<String>,
}

pub trait CompletionService<
  P: crate::database::storage::Partitions,
  T: crate::protocol::lsp::LanguageServer<P>,
>: Send + Sync + 'static
{
  /// The [`textDocument/completion`] request is sent from the client to the
  /// server to compute completion items at a given cursor position.
  ///
  /// If computing full completion items is expensive, servers can additionally
  /// provide a handler for the completion item resolve request
  /// (`completionItem/resolve`). This request is sent when a completion item
  /// is selected in the user interface.
  ///
  /// [`textDocument/completion`]: https://microsoft.github.io/language-server-protocol/specification#textDocument_completion
  ///
  /// # Compatibility
  ///
  /// Since 3.16.0, the client can signal that it can resolve more properties
  /// lazily. This is done using the `completion_item.resolve_support` client
  /// capability which lists all properties that can be filled in during a
  /// `completionItem/resolve` request.
  ///
  /// All other properties (usually `sort_text`, `filter_text`, `insert_text`,
  /// and `text_edit`) must be provided in the `textDocument/completion`
  /// response and must not be changed during resolve.
  fn completion(
    &self,
    params: CompletionParams,
    ctx: &mut TaskContext<P, T>,
    _writer: &mut PartitionWriteContextRef<'_, P>,
  ) -> impl std::future::Future<
    Output = jsonrpc::Result<Option<CompletionResponse>>,
  > + Send {
    let cx = otel::span!(^
      "laburnum.lsp.completion",
      "document.uri" = params.text_document_position.text_document.uri.to_string(),
      "position.line" = params.text_document_position.position.line as i64,
      "position.character" = params.text_document_position.position.character as i64
    );
    async move {
      let uri = params.text_document_position.text_document.uri;
      let position = params.text_document_position.position;

      let source_key = {
        let cache = ctx.source_cache();
        let guard = cache.read();
        match guard.latest_key(&uri) {
          | Some(key) => key,
          | None => return Ok(None),
        }
      };

      // Get the position encoding from the client
      let encoding = ctx.position_encoding();

      // Extract prefix in a scoped block to drop SourceView (DashMap guard) before await
      let prefix = {
        let reader = ctx.source_cache_reader();
        let source = match reader.get_source(source_key) {
          | Some(s) => s,
          | None => return Ok(None),
        };
        if source.is_evicted() {
          return Ok(None);
        }

        // Convert cursor position to byte offset via LineOps (works on both String and Rope)
        let offset = match source.line_col_to_byte(position.line, position.character, &encoding) {
          | Some(o) => o,
          | None => return Ok(None),
        };

        // Find the start of the current line to bound the backward scan
        let line_start = source
          .line_col_to_byte(position.line, 0, &PositionEncodingKind::UTF8)
          .unwrap_or(0);

        // Slice only from line start to cursor — avoids materializing the entire file
        let slice = match source.slice(line_start, offset) {
          | Some(s) => s,
          | None => return Ok(None),
        };

        // Scan backward through the slice to find the word prefix
        let bytes = slice.as_bytes();
        let mut start = bytes.len();
        while start > 0 && (bytes[start - 1].is_ascii_alphanumeric() || bytes[start - 1] == b'_') {
          start -= 1;
        }
        slice[start..].to_string()
      };

      let file_prefix = crate::partitions::document_symbols::DocumentSymbolSortKey::FilePrefix {
        source_key,
      }
      .to_string();

      let query_results = ctx
        .query_client()
        .prefix_internal(DocumentSymbols::KEY, file_prefix)
        .await;

      let reader = ctx.source_cache_reader();
      let mut items: Vec<CompletionItem> = Vec::new();

      for record_meta in query_results.records().iter() {
        if let Some(record_ref) = query_results.get(record_meta)
          && let Some(symbol) = record_ref.as_document_symbol()
        {
          let ident = symbol.get_ident();
          let ident_str = ident.to_string();

          if !prefix.is_empty() && !ident_str.starts_with(&prefix) {
            continue;
          }

          let doc_symbol = symbol.to_document_symbol(&reader, &encoding);
          let completion_kind = symbol_kind_to_completion_kind(doc_symbol.kind);

          let sort_text = if ident_str.starts_with(&prefix) {
            format!("0{}", ident_str)
          } else {
            format!("1{}", ident_str)
          };

          items.push(CompletionItem {
            label: ident_str.clone(),
            kind: Some(completion_kind),
            detail: doc_symbol.detail,
            sort_text: Some(sort_text),
            filter_text: Some(ident_str),
            ..CompletionItem::default()
          });
        }
      }

      if items.is_empty() {
        Ok(None)
      } else {
        Ok(Some(CompletionResponse::Array(items)))
      }
    }.with_context(cx)
  }
  const COMPLETION_LANE: crate::scheduler::lanes::Lane =
    crate::scheduler::lanes::INPUT_CONTINUOUS_LANE;

  /// The [`completionItem/resolve`] request is sent from the client to the
  /// server to resolve additional information for a given completion item.
  ///
  /// [`completionItem/resolve`]: https://microsoft.github.io/language-server-protocol/specification#completionItem_resolve
  fn completion_resolve(
    &self,
    params: CompletionItem,
    ctx: &mut TaskContext<P, T>,
    writer: &mut PartitionWriteContextRef<'_, P>,
  ) -> impl std::future::Future<Output = jsonrpc::Result<CompletionItem>> + Send
  {
    async move {
      otel::span!("laburnum.lsp.completion_resolve");
      Err(jsonrpc::Error::method_not_found())
    }
  }
  const COMPLETION_RESOLVE_LANE: crate::scheduler::lanes::Lane =
    crate::scheduler::lanes::DEFAULT_LANE;
}

fn symbol_kind_to_completion_kind(
  symbol_kind: crate::protocol::lsp::SymbolKind,
) -> CompletionItemKind {
  use crate::protocol::lsp::SymbolKind;

  match symbol_kind {
    | SymbolKind::FILE => CompletionItemKind::FILE,
    | SymbolKind::MODULE | SymbolKind::NAMESPACE | SymbolKind::PACKAGE => {
      CompletionItemKind::MODULE
    },
    | SymbolKind::CLASS => CompletionItemKind::CLASS,
    | SymbolKind::METHOD => CompletionItemKind::METHOD,
    | SymbolKind::PROPERTY | SymbolKind::KEY => CompletionItemKind::PROPERTY,
    | SymbolKind::FIELD => CompletionItemKind::FIELD,
    | SymbolKind::CONSTRUCTOR => CompletionItemKind::CONSTRUCTOR,
    | SymbolKind::ENUM => CompletionItemKind::ENUM,
    | SymbolKind::INTERFACE => CompletionItemKind::INTERFACE,
    | SymbolKind::FUNCTION => CompletionItemKind::FUNCTION,
    | SymbolKind::VARIABLE => CompletionItemKind::VARIABLE,
    | SymbolKind::CONSTANT => CompletionItemKind::CONSTANT,
    | SymbolKind::STRING
    | SymbolKind::NUMBER
    | SymbolKind::BOOLEAN
    | SymbolKind::ARRAY
    | SymbolKind::OBJECT
    | SymbolKind::NULL => CompletionItemKind::VALUE,
    | SymbolKind::ENUM_MEMBER => CompletionItemKind::ENUM_MEMBER,
    | SymbolKind::STRUCT => CompletionItemKind::STRUCT,
    | SymbolKind::EVENT => CompletionItemKind::EVENT,
    | SymbolKind::OPERATOR => CompletionItemKind::OPERATOR,
    | SymbolKind::TYPE_PARAMETER => CompletionItemKind::TYPE_PARAMETER,
    | _ => CompletionItemKind::TEXT,
  }
}

#[cfg(test)]
mod tests {
  use {super::*, crate::protocol::tests::test_deserialization};

  #[test]
  fn test_tag_support_deserialization() {
    let empty = CompletionItemCapability {
      tag_support: None,
      ..Default::default()
    };
    test_deserialization(r"{}", &empty);
    test_deserialization(r#"{"tagSupport": false}"#, &empty);

    let t = CompletionItemCapability {
      tag_support: Some(TagSupport { value_set: vec![] }),
      ..Default::default()
    };
    test_deserialization(r#"{"tagSupport": true}"#, &t);

    let t = CompletionItemCapability {
      tag_support: Some(TagSupport {
        value_set: vec![CompletionItemTag::DEPRECATED],
      }),
      ..Default::default()
    };
    test_deserialization(r#"{"tagSupport": {"valueSet": [1]}}"#, &t);
  }

  #[test]
  fn test_debug_enum() {
    assert_eq!(format!("{:?}", CompletionItemKind::TEXT), "Text");
    assert_eq!(
      format!("{:?}", CompletionItemKind::TYPE_PARAMETER),
      "TypeParameter"
    );
  }

  #[test]
  fn test_try_from_enum() {
    use std::convert::TryInto;
    assert_eq!("Text".try_into(), Ok(CompletionItemKind::TEXT));
    assert_eq!(
      "TypeParameter".try_into(),
      Ok(CompletionItemKind::TYPE_PARAMETER)
    );
  }
}