compmd 0.4.0

The CompMD processor: parses, validates and expands CompMD (Component Markdown) documents. In development.
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
//! Hints on component lines, inline components and slot lines (spec §3, §4): a component's name, its params and their
//! values, the params a list takes, and slots. The line is read again as the parser reads it, so a line being typed
//! has a hint too.

use super::{Completion, Found, ParameterHint, Place, Subject, expression};
use crate::parse::line::{Form, classify, matching_bracket};
use crate::parse::params::{self, List, Written, name_len};
use crate::resolve::Renderer;
use crate::tree::{Code, Container, Node, ParamValue, SlotDeclaration};

/// The hint at the caret of `place`, on a component line or in an inline component.
pub(super) fn hint(place: &Place) -> Option<Found> {
    let indent = indent(&place.text);
    let line = &place.text[indent..];
    let on_line = place.at >= indent && line.starts_with(&[':', ':']);
    let found = on_line
        .then(|| component_line(place, indent))
        .flatten()
        .map(|found| Found {
            start: found.start + indent,
            end: found.end + indent,
            ..found
        });
    found.or_else(|| inline(place))
}

/// The hint on the opening line of a code block a code renderer renders (spec §7.7): its language stands for the
/// renderer, and the param list after it is the renderer's, as a leaf's is.
pub(super) fn code_block(place: &Place, code: &Code) -> Option<Found> {
    let start = code.position?.start;
    if place.line != start.line {
        return None;
    }
    let language = code.lang.as_deref()?;
    let Some(Renderer::Component(name, _)) = place.document.renderers.get(language) else {
        return None;
    };
    let at = place.document.lines().language_at(start).column - 1;
    let end = at + language.chars().count();
    if (at..=end).contains(&place.at) {
        let languages = place
            .document
            .renderers
            .iter()
            .filter_map(|(language, renderer)| {
                let Renderer::Component(name, _) = renderer else {
                    return None;
                };
                Some(Completion::new(
                    language,
                    Some(format!("drawn by `{name}`")),
                ))
            });
        return Some(Found {
            completions: languages.collect(),
            ..component(place, name, at, end)
        });
    }
    let brace = end + place.text[end..].iter().position(|c| *c == '{')?;
    if place.at <= brace {
        return None;
    }
    let list = params::braced(&place.text, brace);
    let params = place.block_params(name);
    params_taking(place, &place.text, place.at, name, &list, brace + 1, params)
}

/// How many code points of `line` come before what a component line starts with: indentation, and the markers of
/// block quotes and list items.
fn indent(line: &[char]) -> usize {
    let space = |c: Option<&char>| matches!(c, Some(' ' | '\t'));
    let mut at = 0;
    loop {
        while space(line.get(at)) {
            at += 1;
        }
        let digits = line[at..].iter().take_while(|c| c.is_ascii_digit()).count();
        match line.get(at) {
            Some('>') => at += 1,
            Some('-' | '*' | '+') if space(line.get(at + 1)) => at += 1,
            Some(_)
                if digits > 0
                    && matches!(line.get(at + digits), Some('.' | ')'))
                    && space(line.get(at + digits + 1)) =>
            {
                at += digits + 1;
            }
            _ => return at,
        }
    }
}

/// The hint on a line that starts with `::`, whose caret is at `place.at - indent` of `line`; its indexes are the
/// line's past `indent`.
fn component_line(place: &Place, indent: usize) -> Option<Found> {
    let line = &place.text[indent..];
    let at = place.at - indent;
    let colons = line.iter().take_while(|c| **c == ':').count();
    let name_start = colons
        + line[colons..]
            .iter()
            .take_while(|c| matches!(c, ' ' | '\t'))
            .count();
    match classify(line) {
        Form::Opener { name, params } => {
            let end = name_start + name.chars().count();
            named(place, line, at, &name, name_start, Some((end, &params)))
        }
        Form::Leaf {
            name,
            fallback,
            params,
        } => {
            if fallback.is_some_and(|(start, end)| start <= at && at <= end) {
                return None;
            }
            let end = name_start + name.chars().count();
            let after = fallback.map_or(end, |(_, close)| close + 1);
            named(place, line, at, &name, name_start, Some((after, &params)))
        }
        Form::Closer { name } => named(place, line, at, &name, name_start + 1, None),
        Form::Continuation { params } => {
            params_of(place, line, at, &continued(place)?, &params, name_start + 1)
        }
        Form::Slot { name } => slot(place, at, &name, name_start + 1),
        Form::Malformed { .. } => {
            let len = name_len(line, name_start)?;
            let name: String = line[name_start..name_start + len].iter().collect();
            named(place, line, at, &name, name_start, None)
        }
    }
}

/// The hint on a component's name at `start` of `line`, or in its param list, which follows `after`.
fn named(
    place: &Place,
    line: &[char],
    at: usize,
    name: &str,
    start: usize,
    list: Option<(usize, &List)>,
) -> Option<Found> {
    let end = start + name.chars().count();
    if (start..=end).contains(&at) {
        return Some(component(place, name, start, end));
    }
    let (after, list) = list?;
    let brace = after
        + line[after..]
            .iter()
            .take_while(|c| matches!(c, ' ' | '\t'))
            .count();
    if line.get(brace) != Some(&'{') || at <= brace {
        return None;
    }
    params_of(place, line, at, name, list, brace + 1)
}

/// The hint on the name `name` at `start..end`.
fn component(place: &Place, name: &str, start: usize, end: usize) -> Found {
    Found {
        start,
        end,
        subject: Subject::Component {
            name: name.to_string(),
            component: place.component_hint(name),
        },
        completions: place.component_names(),
    }
}

/// The name of the component whose line the continuation line `place` continues.
fn continued(place: &Place) -> Option<String> {
    let lines = place.document.lines();
    (1..place.line)
        .rev()
        .find_map(|number| {
            let text: Vec<char> = lines.line(number)?.chars().collect();
            let line = &text[indent(&text)..];
            if !line.starts_with(&[':', ':']) {
                return Some(None);
            }
            match classify(line) {
                Form::Continuation { .. } => None,
                Form::Opener { name, .. } | Form::Leaf { name, .. } => Some(Some(name)),
                _ => Some(None),
            }
        })
        .flatten()
}

/// Where a param's key and value are on its line.
struct Spot {
    key: String,
    /// The key's code points.
    key_at: (usize, usize),
    /// The value's code points, past the `=`; `None` for a flag, or a key with no `=` yet.
    value_at: Option<(usize, usize)>,
}

/// The hint in the param list of the component `component`, which starts at `from` of `line`.
fn params_of(
    place: &Place,
    line: &[char],
    at: usize,
    component: &str,
    list: &List,
    from: usize,
) -> Option<Found> {
    params_taking(
        place,
        line,
        at,
        component,
        list,
        from,
        place.params(component),
    )
}

/// [`params_of`], for a list that takes `all`.
fn params_taking(
    place: &Place,
    line: &[char],
    at: usize,
    component: &str,
    list: &List,
    from: usize,
    all: Vec<ParameterHint>,
) -> Option<Found> {
    let closed = list.broke_at.is_none()
        && list
            .end
            .checked_sub(1)
            .is_some_and(|last| line.get(last) == Some(&'}'));
    if at < from || (closed && at >= list.end) {
        return None;
    }
    let spot = list
        .params
        .iter()
        .find(|param| param.start <= at && at <= param.end)
        .map(spot_of)
        .or_else(|| broken(line, list, at))
        .unwrap_or_else(|| token(line, at));
    if let Some(found) = in_expression(place, line, at, &spot) {
        return Some(found);
    }
    let written: Vec<&str> = list.params.iter().filter_map(key_of).collect();
    let others: Vec<ParameterHint> = all
        .iter()
        .filter(|param| param.name == spot.key || !written.contains(&param.name.as_str()))
        .cloned()
        .collect();
    let keys = others
        .iter()
        .map(|param| Completion::new(&param.name, Some(param.takes.description.clone())))
        .collect();
    let component = component.to_string();
    let (key_start, key_end) = spot.key_at;
    let found = match (
        all.into_iter().find(|param| param.name == spot.key),
        spot.value_at,
    ) {
        (Some(param), Some((start, end))) if at > key_end => Found {
            start,
            end,
            completions: param
                .takes
                .tokens
                .iter()
                .map(|token| Completion::new(token, None))
                .collect(),
            subject: Subject::Param {
                component,
                param,
                value: Some(line[start..end].iter().collect()),
            },
        },
        (Some(param), _) => Found {
            start: key_start,
            end: key_end,
            subject: Subject::Param {
                component,
                param,
                value: None,
            },
            completions: keys,
        },
        (None, _) => Found {
            start: key_start,
            end: key_end,
            subject: Subject::Params {
                component,
                params: others
                    .into_iter()
                    .filter(|param| param.name != spot.key)
                    .collect(),
            },
            completions: keys,
        },
    };
    Some(found)
}

/// The hint inside the expression that is the value at `spot`, `(…)`, when the caret is in it.
fn in_expression(place: &Place, line: &[char], at: usize, spot: &Spot) -> Option<Found> {
    let (start, end) = spot.value_at?;
    if line.get(start) != Some(&'(') {
        return None;
    }
    let inner = start + 1;
    let inner_end = if end > inner && line[end - 1] == ')' {
        end - 1
    } else {
        end
    };
    if at < inner || at > inner_end {
        return None;
    }
    let found = expression::expression(place, &line[inner..inner_end], at - inner)?;
    Some(Found {
        start: found.start + inner,
        end: found.end + inner,
        ..found
    })
}

/// The key a written param gives, `class` and `id` for `.name` and `#name`.
fn key_of(param: &Written) -> Option<&str> {
    match param.value {
        ParamValue::Class(_) => Some("class"),
        ParamValue::Id(_) => Some("id"),
        _ => param.key.as_deref(),
    }
}

/// Where a param the list read is.
fn spot_of(param: &Written) -> Spot {
    let key = key_of(param).unwrap_or_default().to_string();
    if matches!(param.value, ParamValue::Class(_) | ParamValue::Id(_)) {
        return Spot {
            key,
            key_at: (param.start, param.start),
            value_at: Some((param.start + 1, param.end)),
        };
    }
    let key_start = param.start
        + param
            .breakpoint
            .as_ref()
            .map_or(0, |breakpoint| breakpoint.chars().count() + 1);
    let key_end = key_start + key.chars().count();
    Spot {
        key,
        key_at: (key_start, key_end),
        value_at: (param.value != ParamValue::Flag).then_some((key_end + 1, param.end)),
    }
}

/// Where the param is whose quoted string, expression or JSON value opens at the list's break and doesn't close
/// before the caret: its value runs to the end of the line.
fn broken(line: &[char], list: &List, at: usize) -> Option<Spot> {
    let broke = list
        .broke_at
        .filter(|broke| at > *broke && matches!(line.get(*broke), Some('"' | '(' | '[' | '{')))?;
    let mut spot = token(line, broke);
    spot.value_at = spot.value_at.map(|(start, _)| (start, line.len()));
    Some(spot)
}

/// Where the param is that the caret is in when the list didn't read it: the text around the caret up to a space or
/// a brace, such as `border=` or a key being typed. Between params it is empty.
fn token(line: &[char], at: usize) -> Spot {
    let stop = |c: &&char| matches!(c, ' ' | '\t' | '{' | '}');
    let start = at - line[..at].iter().rev().take_while(|c| !stop(c)).count();
    let end = at + line[at..].iter().take_while(|c| !stop(c)).count();
    match line.get(start) {
        Some(marker @ ('.' | '#')) if start < end => {
            return Spot {
                key: if *marker == '.' { "class" } else { "id" }.to_string(),
                key_at: (start, start),
                value_at: Some((start + 1, end)),
            };
        }
        _ => {}
    }
    let equals = line[start..end].iter().position(|c| *c == '=');
    let head_end = equals.map_or(end, |equals| start + equals);
    let key_start = line[start..head_end]
        .iter()
        .position(|c| *c == ':')
        .map_or(start, |colon| start + colon + 1);
    Spot {
        key: line[key_start..head_end].iter().collect(),
        key_at: (key_start, head_end),
        value_at: equals.map(|equals| (start + equals + 1, end)),
    }
}

/// The hint on a slot line, whose name is at `start`.
fn slot(place: &Place, at: usize, name: &str, start: usize) -> Option<Found> {
    let end = start + name.chars().count();
    if at + 1 < start {
        return None;
    }
    let container = container_of(&place.document.root.children, place.line)?;
    let slots: Vec<SlotDeclaration> = place
        .component(&container.name)
        .and_then(|component| component.signature.as_ref()?.slots.clone())
        .unwrap_or_default();
    let completions = slots
        .iter()
        .map(|slot| match slot {
            SlotDeclaration::Name(slot) | SlotDeclaration::Template { name: slot, .. } => slot,
        })
        .filter(|slot| *slot != "default")
        .map(|slot| Completion::new(slot, None))
        .collect();
    Some(Found {
        start,
        end,
        subject: Subject::Slot {
            component: container.name.clone(),
            slot: name.to_string(),
            slots,
        },
        completions,
    })
}

/// The container whose slot line is on `line`.
fn container_of(nodes: &[Node], line: usize) -> Option<&Container> {
    nodes.iter().find_map(|node| match node {
        Node::ComponentContainer(container) => {
            let here = container.slots.iter().any(|slot| {
                slot.position
                    .is_some_and(|position| position.start.line == line)
            });
            if here {
                return Some(container);
            }
            container_of(&container.children, line).or_else(|| {
                container
                    .slots
                    .iter()
                    .find_map(|slot| container_of(&slot.children, line))
            })
        }
        _ => container_of(node.children(), line),
    })
}

/// The hint in an inline component, `:name[fallback]{params}`, on the caret's line; the innermost one, when one is
/// in another's fallback.
fn inline(place: &Place) -> Option<Found> {
    let (text, at) = (&place.text, place.at);
    let mut found = None;
    for colon in 0..text.len() {
        let after_word = colon
            .checked_sub(1)
            .is_some_and(|before| text[before] == ':' || text[before].is_alphanumeric());
        if text[colon] != ':' || after_word || text.get(colon + 1) == Some(&':') {
            continue;
        }
        let Some(len) = name_len(text, colon + 1) else {
            continue;
        };
        let (start, end) = (colon + 1, colon + 1 + len);
        if text.get(end) != Some(&'[') {
            continue;
        }
        let name: String = text[start..end].iter().collect();
        if (start..=end).contains(&at) {
            found = Some(component(place, &name, start, end));
            continue;
        }
        let Some(close) = matching_bracket(text, end) else {
            continue;
        };
        let brace = close + 1;
        if text.get(brace) == Some(&'{') && at > brace {
            let list = params::braced(text, brace);
            found = params_of(place, text, at, &name, &list, brace + 1).or(found);
        }
    }
    found
}