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
//! Computes completions at a cursor position from the schema + document IDs.
use std::collections::BTreeSet;
use async_lsp::lsp_types::{CompletionItem, CompletionItemKind};
use disposition_input_model::theme::ThemeAttr;
use serde_json::Value;
use crate::completion::{
completion_target::CompletionTarget, cursor_context::CursorContext,
diagram_schema::DiagramSchema, dynamic_completions::DynamicCompletions,
id_category::IdCategory, key_category::KeyCategory,
};
/// Produces YAML key / value completions for an `InputDiagram` buffer.
pub struct CompletionEngine;
impl CompletionEngine {
/// Returns the completion items for the cursor at `line` / `character`
/// (zero-based) within `text`.
pub fn completions(text: &str, line: u32, character: u32) -> Vec<CompletionItem> {
let schema = DiagramSchema::get();
let cursor_context = CursorContext::at(text, line, character);
let Some(container) = schema.schema_at(&cursor_context.path) else {
return Vec::new();
};
// The def name (`$ref`) of the container *before* dereferencing -- e.g.
// `ThingNames` -- identifies which dynamic map-key suggestions to offer.
let container_ref_name = DiagramSchema::ref_name(container);
let container = schema.deref(container);
match &cursor_context.target {
CompletionTarget::Key => Self::key_completions(
schema,
container,
container_ref_name,
&cursor_context.sibling_keys,
text,
),
CompletionTarget::Value {
key,
in_sequence,
needs_space,
} => Self::value_completions(
schema,
container,
container_ref_name,
key,
*in_sequence,
*needs_space,
text,
),
}
}
/// Offers the known fields of `container` as map-key completions.
///
/// In addition to the struct fields (`properties`), this offers the keys an
/// arbitrary-map container constrains its entries to via `propertyNames`
/// (e.g. the `ThemeAttr` keys of a `CssClassPartials` map -- `shape_color`,
/// `stroke_style`, ..).
fn key_completions(
schema: &DiagramSchema,
container: &Value,
container_ref_name: Option<&str>,
sibling_keys: &BTreeSet<String>,
text: &str,
) -> Vec<CompletionItem> {
let mut items = schema
.property_entries(container)
.into_iter()
.map(|property| CompletionItem {
label: property.name.to_string(),
kind: Some(CompletionItemKind::FIELD),
detail: property.description.map(first_line),
..CompletionItem::default()
})
.collect::<Vec<CompletionItem>>();
// Keys constrained to an enum, e.g. a `Map<ThemeAttr, _>`'s theme
// attribute keys.
if let Some(property_names) = container.get("propertyNames") {
items.extend(
schema
.enum_entries(property_names)
.into_iter()
.map(|entry| CompletionItem {
label: entry.value.to_string(),
kind: Some(CompletionItemKind::FIELD),
detail: entry.description.map(first_line),
..CompletionItem::default()
}),
);
}
// Dynamic keys for maps whose keys are document-defined IDs, ID
// templates, or known literal keys (e.g. a `ThingNames` map keyed by
// `ThingId`).
if let Some(key_category) = container_ref_name.and_then(KeyCategory::from_ref_name) {
items.extend(Self::dynamic_key_completions(schema, key_category, text));
}
// A map key can only be declared once, so drop any already present as a
// sibling of the cursor.
items.retain(|item| !sibling_keys.contains(&item.label));
items
}
/// Offers the dynamic map-key suggestions for `key_category`.
///
/// Combines the document-derived / templated / literal labels from
/// [`DynamicCompletions`] with any schema-derived built-in keys (the
/// `StyleAlias` / `EntityType` enum values).
fn dynamic_key_completions(
schema: &DiagramSchema,
key_category: KeyCategory,
text: &str,
) -> Vec<CompletionItem> {
let dynamic_completions = DynamicCompletions::from_text(text);
let mut items = dynamic_completions
.key_suggestions(key_category)
.into_iter()
.map(|label| CompletionItem {
label,
kind: Some(CompletionItemKind::VALUE),
..CompletionItem::default()
})
.collect::<Vec<CompletionItem>>();
// Built-in enum keys defined in the schema.
let builtin_def_name = match key_category {
KeyCategory::StyleAlias => Some("StyleAlias"),
KeyCategory::EntityType => Some("EntityType"),
_ => None,
};
if let Some(def) = builtin_def_name.and_then(|name| schema.def(name)) {
items.extend(
schema
.enum_entries(def)
.into_iter()
.map(|entry| CompletionItem {
label: entry.value.to_string(),
kind: Some(CompletionItemKind::ENUM_MEMBER),
detail: entry.description.map(first_line),
..CompletionItem::default()
}),
);
}
items
}
/// Offers enum values and/or document-defined IDs for `key`'s value.
///
/// `in_sequence` is `true` when the cursor is already inside a sequence (a
/// `- ` item or `[ .. ]` flow brackets). For an array-valued field
/// completed at the `key:` position (`in_sequence == false`), each
/// element value is inserted as a flow list (e.g. `[t_a]`) so the YAML
/// stays a valid sequence. `needs_space` prepends a separator space
/// when the cursor sits immediately after `key:`.
#[allow(clippy::fn_params_excessive_bools)]
fn value_completions(
schema: &DiagramSchema,
container: &Value,
container_ref_name: Option<&str>,
key: &str,
in_sequence: bool,
needs_space: bool,
text: &str,
) -> Vec<CompletionItem> {
// `CssClassPartials` values keyed by a `ThemeAttr` are partial Tailwind
// values whose vocabulary (colors, shades, styles, ..) depends on the
// attribute and is not expressible in the JSON schema. The `key` may
// instead be the `style_aliases_applied` property, which falls through
// to the normal schema-driven completion below.
if container_ref_name == Some("CssClassPartials")
&& let Some(items) = Self::theme_attr_value_completions(key, needs_space)
{
return items;
}
let Some(value_schema) = schema.field_schema(container, key) else {
return Vec::new();
};
// For an array-valued field (e.g. `things`), complete its element type.
let array_items = schema.array_items(value_schema);
let element_schema = array_items.unwrap_or(value_schema);
// An array value typed at the `key:` position (not already in a
// sequence) is wrapped in flow-list brackets so the result is a valid
// sequence.
let wrap_in_list = array_items.is_some() && !in_sequence;
let insert_text = |label: &str| value_insert_text(label, wrap_in_list, needs_space);
let mut items = Vec::new();
// Fixed enum values (e.g. `row`, `cyclic`, `top_to_bottom`).
items.extend(
schema
.enum_entries(element_schema)
.into_iter()
.map(|entry| CompletionItem {
label: entry.value.to_string(),
kind: Some(CompletionItemKind::ENUM_MEMBER),
detail: entry.description.map(first_line),
insert_text: insert_text(entry.value),
..CompletionItem::default()
}),
);
// Document-defined IDs, when the value references an ID type.
if let Some(category) =
DiagramSchema::ref_name(element_schema).and_then(IdCategory::from_ref_name)
{
let dynamic_completions = DynamicCompletions::from_text(text);
items.extend(dynamic_completions.ids_for(category).into_iter().map(|id| {
CompletionItem {
label: id.to_string(),
kind: Some(CompletionItemKind::VALUE),
insert_text: insert_text(id),
..CompletionItem::default()
}
}));
}
items
}
/// Offers the partial Tailwind values for a `CssClassPartials` value keyed
/// by `key`, if `key` is a `ThemeAttr`.
///
/// Returns `None` when `key` is not a theme attribute (e.g. the
/// `style_aliases_applied` property), so the caller can fall back to the
/// schema-driven completion. A theme attribute with no enumerable values
/// (numeric / freeform, e.g. `padding`) yields `Some(<empty>)`.
///
/// `needs_space` prepends a separator space when the cursor sits
/// immediately after `key:`.
fn theme_attr_value_completions(key: &str, needs_space: bool) -> Option<Vec<CompletionItem>> {
let theme_attr =
serde_json::from_value::<ThemeAttr>(Value::String(key.to_string())).ok()?;
let items = theme_attr
.value_suggestions()
.iter()
.map(|value| CompletionItem {
label: (*value).to_string(),
kind: Some(CompletionItemKind::VALUE),
insert_text: value_insert_text(value, false, needs_space),
..CompletionItem::default()
})
.collect();
Some(items)
}
}
/// Builds the `insert_text` for a value completion `label`.
///
/// Returns `None` when the bare `label` can be inserted as-is. Otherwise the
/// value is wrapped in flow-list brackets (`wrap_in_list`) and/or prefixed with
/// a separator space (`needs_space`).
fn value_insert_text(label: &str, wrap_in_list: bool, needs_space: bool) -> Option<String> {
if !wrap_in_list && !needs_space {
return None;
}
let space = if needs_space { " " } else { "" };
if wrap_in_list {
Some(format!("{space}[{label}]"))
} else {
Some(format!("{space}{label}"))
}
}
/// Returns the first non-empty line of `description`, trimmed -- used as the
/// short completion detail.
fn first_line(description: &str) -> String {
description
.lines()
.map(str::trim)
.find(|line| !line.is_empty())
.unwrap_or_default()
.to_string()
}