dsl_completion/item.rs
1//! Crate-local representation of one completion entry.
2//!
3//! Maps cleanly onto LSP `CompletionItem` in the server, but lives here
4//! so analysis/testing/inspection don't have to pull in tower-lsp.
5
6use serde::Serialize;
7
8#[derive(Debug, Clone, Default, Serialize)]
9pub struct Item {
10 pub label: String,
11 pub kind: ItemKind,
12 pub detail: Option<String>,
13 pub description: Option<String>,
14 /// Eagerly-built markdown, for items whose documentation is derived
15 /// from the catalog or a hand-written snippet blurb. Knowledge-base
16 /// items leave this `None` and set [`Item::kb_entry`] instead -- read
17 /// through [`Item::documentation`] rather than touching either field.
18 pub documentation_md: Option<String>,
19 /// Knowledge-base entry backing this item, when it has one.
20 ///
21 /// Rendering markdown for every built-in keyword, type, and function
22 /// costs ~2700 `render_markdown` calls (each one several `format!`s
23 /// plus paragraph wrapping) on *every* completion request, and the
24 /// client displays exactly one of them. Carrying the entry instead
25 /// lets the server defer rendering to `completionItem/resolve`.
26 #[serde(skip)]
27 pub kb_entry: Option<&'static dsl_knowledge::Entry>,
28 pub insert_text: String,
29 /// `true` when `insert_text` contains LSP snippet placeholders like
30 /// `$0` or `${1:arg}`. The server maps this onto
31 /// `InsertTextFormat::Snippet`. Default `false`.
32 #[serde(default)]
33 pub is_snippet: bool,
34 /// Sort priority -- lower = appears first in the completion menu.
35 /// 0 = in-scope columns (from FROM/JOIN), 1 = in-scope tables,
36 /// 2 = scoped builtins, 3 = catalog-wide tables/functions,
37 /// 4 = keywords, 5 = catch-all. Default 5 keeps old call-sites safe.
38 #[serde(default = "default_sort")]
39 pub sort_priority: u8,
40}
41
42#[allow(dead_code)]
43fn default_sort() -> u8 {
44 5
45}
46
47impl Item {
48 /// Convenience: set `sort_priority` in a builder-ish way.
49 pub fn with_sort(mut self, p: u8) -> Self {
50 self.sort_priority = p;
51 self
52 }
53
54 /// Markdown documentation for this item, rendering the knowledge-base
55 /// entry on demand. The single accessor callers should use --
56 /// `documentation_md` alone misses every built-in.
57 pub fn documentation(&self) -> Option<String> {
58 match (&self.documentation_md, self.kb_entry) {
59 (Some(md), _) => Some(md.clone()),
60 (None, Some(e)) => Some(dsl_knowledge::render_markdown(e)),
61 (None, None) => None,
62 }
63 }
64}
65
66#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Default)]
67pub enum ItemKind {
68 #[default]
69 Keyword,
70 Type,
71 Function,
72 Table,
73 View,
74 Column,
75 Schema,
76 Variable,
77 Parameter,
78}