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
use tower_lsp::lsp_types::{
SemanticToken, SemanticTokenModifier, SemanticTokenType, SemanticTokensFullOptions,
SemanticTokensLegend, SemanticTokensOptions, SemanticTokensServerCapabilities,
};
use crate::document::Document;
// Token type indices
const TOKEN_INGREDIENT: u32 = 0;
const TOKEN_COOKWARE: u32 = 1;
const TOKEN_TIMER: u32 = 2;
#[allow(dead_code)]
const TOKEN_QUANTITY: u32 = 3; // Reserved for future use
#[allow(dead_code)]
const TOKEN_UNIT: u32 = 4; // Reserved for future use
const TOKEN_COMMENT: u32 = 5;
const TOKEN_METADATA_KEY: u32 = 6;
#[allow(dead_code)]
const TOKEN_METADATA_VALUE: u32 = 7; // Reserved for future use
const TOKEN_SECTION: u32 = 8;
pub const TOKEN_TYPES: &[SemanticTokenType] = &[
SemanticTokenType::VARIABLE, // 0: Ingredients (@)
SemanticTokenType::CLASS, // 1: Cookware (#)
SemanticTokenType::FUNCTION, // 2: Timers (~)
SemanticTokenType::NUMBER, // 3: Quantities
SemanticTokenType::STRING, // 4: Units
SemanticTokenType::COMMENT, // 5: Comments
SemanticTokenType::KEYWORD, // 6: Metadata keys
SemanticTokenType::PROPERTY, // 7: Metadata values
SemanticTokenType::NAMESPACE, // 8: Sections
];
pub const TOKEN_MODIFIERS: &[SemanticTokenModifier] = &[];
pub fn legend() -> SemanticTokensLegend {
SemanticTokensLegend {
token_types: TOKEN_TYPES.to_vec(),
token_modifiers: TOKEN_MODIFIERS.to_vec(),
}
}
pub fn capabilities() -> SemanticTokensServerCapabilities {
SemanticTokensServerCapabilities::SemanticTokensOptions(SemanticTokensOptions {
legend: legend(),
full: Some(SemanticTokensFullOptions::Bool(true)),
range: Some(false),
work_done_progress_options: Default::default(),
})
}
struct TokenBuilder {
tokens: Vec<SemanticToken>,
prev_line: u32,
prev_start: u32,
}
impl TokenBuilder {
fn new() -> Self {
Self {
tokens: Vec::new(),
prev_line: 0,
prev_start: 0,
}
}
fn push(&mut self, line: u32, start: u32, length: u32, token_type: u32) {
if length == 0 {
return;
}
let delta_line = line - self.prev_line;
let delta_start = if delta_line == 0 {
start.saturating_sub(self.prev_start)
} else {
start
};
self.tokens.push(SemanticToken {
delta_line,
delta_start,
length,
token_type,
token_modifiers_bitset: 0,
});
self.prev_line = line;
self.prev_start = start;
}
fn build(self) -> Vec<SemanticToken> {
self.tokens
}
}
pub fn get_semantic_tokens(doc: &Document) -> Vec<SemanticToken> {
let mut builder = TokenBuilder::new();
let content = &doc.content;
let line_index = &doc.line_index;
// Scan through the document and identify tokens
let mut chars = content.char_indices().peekable();
while let Some((idx, ch)) = chars.next() {
match ch {
// Ingredient: @name or @name{...}
'@' => {
let start = idx;
let mut end = idx + 1;
// Collect the ingredient name (no spaces allowed outside braces)
while let Some(&(i, c)) = chars.peek() {
if c == '{' {
// Include until closing brace (spaces allowed inside)
chars.next();
end = i + 1;
while let Some(&(i2, c2)) = chars.peek() {
end = i2 + c2.len_utf8();
chars.next();
if c2 == '}' {
break;
}
}
break;
} else if c.is_alphanumeric() || c == '_' {
end = i + c.len_utf8();
chars.next();
} else {
break;
}
}
let (line, col) = line_index.line_col(start as u32);
let length = line_index.utf16_len(start, end);
builder.push(line, col, length, TOKEN_INGREDIENT);
}
// Cookware: #name or #name{}
'#' => {
let start = idx;
let mut end = idx + 1;
while let Some(&(i, c)) = chars.peek() {
if c == '{' {
chars.next();
end = i + 1;
while let Some(&(i2, c2)) = chars.peek() {
end = i2 + c2.len_utf8();
chars.next();
if c2 == '}' {
break;
}
}
break;
} else if c.is_alphanumeric() || c == '_' {
end = i + c.len_utf8();
chars.next();
} else {
break;
}
}
let (line, col) = line_index.line_col(start as u32);
let length = line_index.utf16_len(start, end);
builder.push(line, col, length, TOKEN_COOKWARE);
}
// Timer: ~name{...} or ~{...}
'~' => {
let start = idx;
let mut end = idx + 1;
while let Some(&(i, c)) = chars.peek() {
if c == '{' {
chars.next();
end = i + 1;
while let Some(&(i2, c2)) = chars.peek() {
end = i2 + c2.len_utf8();
chars.next();
if c2 == '}' {
break;
}
}
break;
} else if c.is_alphanumeric() || c == '_' {
end = i + c.len_utf8();
chars.next();
} else {
break;
}
}
let (line, col) = line_index.line_col(start as u32);
let length = line_index.utf16_len(start, end);
builder.push(line, col, length, TOKEN_TIMER);
}
// Line comment: -- ... OR YAML front matter: ---
'-' => {
let is_line_start =
idx == 0 || content.as_bytes().get(idx.saturating_sub(1)) == Some(&b'\n');
if let Some(&(_, '-')) = chars.peek() {
let start = idx;
chars.next();
// Check for YAML front matter (--- at start of line)
if is_line_start {
if let Some(&(_, '-')) = chars.peek() {
chars.next();
// This is ---, check if it's only dashes until end of line
let mut is_yaml_delimiter = true;
let mut end = idx + 3;
while let Some(&(i, c)) = chars.peek() {
if c == '\n' {
break;
}
if c != '-' && !c.is_whitespace() {
is_yaml_delimiter = false;
}
end = i + c.len_utf8();
chars.next();
}
if is_yaml_delimiter {
// Highlight the --- line as metadata
let (line, col) = line_index.line_col(start as u32);
let length = line_index.utf16_len(start, end);
builder.push(line, col, length, TOKEN_METADATA_KEY);
continue;
}
}
}
// Regular comment: --
let mut end = idx + 2;
while let Some(&(i, c)) = chars.peek() {
if c == '\n' {
break;
}
end = i + c.len_utf8();
chars.next();
}
let (line, col) = line_index.line_col(start as u32);
let length = line_index.utf16_len(start, end);
builder.push(line, col, length, TOKEN_COMMENT);
}
}
// Section: = Section Name = (must start at beginning of line)
'=' => {
// Check if this is at the start of a line
let is_line_start =
idx == 0 || content.as_bytes().get(idx.saturating_sub(1)) == Some(&b'\n');
if is_line_start {
let start = idx;
let mut end = idx + 1;
let mut found_closing = false;
while let Some(&(i, c)) = chars.peek() {
if c == '\n' {
break;
}
end = i + c.len_utf8();
chars.next();
if c == '=' {
found_closing = true;
break;
}
}
if found_closing {
let (line, col) = line_index.line_col(start as u32);
let length = line_index.utf16_len(start, end);
builder.push(line, col, length, TOKEN_SECTION);
}
}
}
// Metadata: >> key: value
'>' => {
if let Some(&(_, '>')) = chars.peek() {
let start = idx;
chars.next();
let mut end = idx + 2;
// Read until end of line
while let Some(&(i, c)) = chars.peek() {
if c == '\n' {
break;
}
end = i + c.len_utf8();
chars.next();
}
let (line, col) = line_index.line_col(start as u32);
let length = line_index.utf16_len(start, end);
builder.push(line, col, length, TOKEN_METADATA_KEY);
}
}
_ => {}
}
}
builder.build()
}