nova-doc 0.3.0

The Nova language's documentation generator (internal to the nova toolchain)
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
//! Signatures as written (spec §8; plan decisions 10 and 11).

use nova_ast::item::WhereBound;
use nova_ast::Type;
use nova_diagnostics::{lines, FileId, Span, Spanned};
use nova_lexer::{Comment, Token};

use crate::highlight::class;
use crate::markdown::DocText;
use crate::{escape, span};

/// One file's text, with its tokens and its comments, lexed once.
pub(crate) struct FileText<'a> {
    pub text: &'a str,
    pub tokens: Vec<Spanned<Token>>,
    pub comments: Vec<Comment>,
}

impl<'a> FileText<'a> {
    pub fn new(text: &'a str, file: FileId) -> FileText<'a> {
        let (tokens, comments, _) = nova_lexer::lex_with_comments(text, file);
        FileText {
            text,
            tokens,
            comments,
        }
    }

    /// Where the first token `want` accepts starts, at or after byte
    /// `from`.
    pub fn next(&self, from: usize, want: fn(&Token) -> bool) -> Option<usize> {
        self.tokens
            .iter()
            .find(|t| t.span.start as usize >= from && want(&t.value))
            .map(|t| t.span.start as usize)
    }

    /// Where the `(…)` that opens at the first `(` at or after byte `from`
    /// ends.
    pub fn group_end(&self, from: usize) -> Option<usize> {
        let mut depth = 0usize;
        for t in self.tokens.iter().filter(|t| t.span.start as usize >= from) {
            match t.value {
                Token::LParen => depth += 1,
                Token::RParen if depth == 1 => return Some(t.span.end as usize),
                Token::RParen if depth > 1 => depth -= 1,
                _ => {}
            }
        }
        None
    }

    /// The module's docs (spec §9.2): its `//!` lines before its first
    /// token, each without `//!` and one space, and where each starts in
    /// the file (3.5b spec §7). A comment's span ends before its line
    /// break, and before a CRLF's `\r`.
    pub fn module_docs(&self) -> DocText {
        let first = self
            .tokens
            .iter()
            .find(|t| t.value != Token::Eof)
            .map_or(self.text.len(), |t| t.span.start as usize);
        let mut d = DocText::default();
        for c in self
            .comments
            .iter()
            .filter(|c| c.span.end as usize <= first)
        {
            let Some(line) = self.text.get(c.span.start as usize..c.span.end as usize) else {
                continue;
            };
            let Some(rest) = line.strip_prefix("//!") else {
                continue;
            };
            let body = rest.strip_prefix(' ').unwrap_or(rest);
            d.push(
                c.span.file,
                c.span.start + (line.len() - body.len()) as u32,
                body,
            );
        }
        d
    }
}

/// Where a declaration's text starts (spec §8): its keyword, with an
/// `async` and a `pub` before it; or, with no keyword, as for a field or a
/// variant, its name, with a `pub` before it.
pub(crate) fn decl_start(text: &str, name_start: usize) -> usize {
    let at = lines::keyword_start(text, name_start).unwrap_or(name_start);
    let before = text[..at].trim_end_matches([' ', '\t']);
    match before.strip_suffix("pub") {
        Some(rest) if !rest.ends_with(|c: char| c.is_ascii_alphanumeric() || c == '_') => {
            rest.len()
        }
        _ => at,
    }
}

/// Where a function's signature ends (plan decision 10): the `)` closing
/// its parameters, its return type, or its last `where` bound, whichever
/// ends last.
pub(crate) fn fn_end(
    ft: &FileText,
    name: Span,
    ret: Option<&Spanned<Type>>,
    wheres: &[WhereBound],
) -> usize {
    let mut end = ft.group_end(name.end as usize).unwrap_or(name.end as usize);
    if let Some(r) = ret {
        end = end.max(r.span.end as usize);
    }
    for w in wheres {
        end = end.max(w.ty.span.end as usize);
        for b in &w.bounds {
            end = end.max(b.span.end as usize);
        }
    }
    end
}

/// The HTML of the declaration `ft.text[start..end]` (spec §8): comments
/// dropped, its lines less the indentation of the line it starts on,
/// keywords and literals coloured, `own`, its name, marked, and each other
/// name `link` gives a URL to made a link.
pub(crate) fn render(
    ft: &FileText,
    start: usize,
    end: usize,
    own: Option<Span>,
    link: &dyn Fn(Span) -> Option<String>,
) -> String {
    let text = ft.text;
    let line = lines::line_start(text, start);
    let base = text[line..].len() - text[line..].trim_start_matches([' ', '\t']).len();
    let mut out = String::new();
    let mut last: Option<usize> = None;
    for t in &ft.tokens {
        let (s, e) = (t.span.start as usize, t.span.end as usize);
        if s < start || e > end || matches!(t.value, Token::Eof | Token::DocComment(_)) {
            continue;
        }
        if let Some(prev) = last {
            out.push_str(&gap(&text[prev..s], base));
        }
        let word = &text[s..e];
        match &t.value {
            Token::Ident(_) if own == Some(t.span) => span(&mut out, "name", word),
            Token::Ident(_) => match link(t.span) {
                Some(url) => {
                    out.push_str("<a href=\"");
                    out.push_str(&escape(&url));
                    out.push_str("\">");
                    out.push_str(&escape(word));
                    out.push_str("</a>");
                }
                None => out.push_str(&escape(word)),
            },
            other => match class(other) {
                Some(c) => span(&mut out, c, word),
                None => out.push_str(&escape(word)),
            },
        }
        last = Some(e);
    }
    out
}

/// What lies between two tokens, which is only spaces and comments, as it
/// shows (plan decision 11): nothing, one space, or a line break and the
/// next line's indentation less `base` characters. A block comment counts
/// as one space, except one that begins a line, which goes with the spaces
/// after it, so the line keeps its own indentation.
pub(crate) fn gap(between: &str, base: usize) -> String {
    let mut plain = String::new();
    let mut rest = between;
    while let Some(c) = rest.chars().next() {
        if let Some(after) = rest.strip_prefix("//") {
            rest = &after[after.find('\n').unwrap_or(after.len())..];
        } else if let Some(after) = rest.strip_prefix("/*") {
            rest = &after[after.find("*/").map_or(after.len(), |i| i + 2)..];
            let line = plain.rfind('\n').map(|i| &plain[i + 1..]);
            if line.is_some_and(|l| l.chars().all(|c| matches!(c, ' ' | '\t'))) {
                rest = rest.trim_start_matches([' ', '\t']);
            } else {
                plain.push(' ');
            }
        } else {
            plain.push(c);
            rest = &rest[c.len_utf8()..];
        }
    }
    match plain.rfind('\n') {
        None if plain.is_empty() => String::new(),
        None => " ".to_string(),
        Some(i) => {
            let indent = &plain[i + 1..];
            let drop = indent
                .chars()
                .take(base)
                .take_while(|c| matches!(c, ' ' | '\t'))
                .count();
            format!("\n{}", &indent[drop..])
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use nova_ast::item::TypeDef;
    use nova_ast::Item;

    fn parse(text: &str) -> nova_ast::File {
        let (tokens, errors) = nova_lexer::lex(text, FileId::DUMMY);
        assert!(errors.is_empty(), "{errors:?}");
        let (file, errors) = nova_parser::parse(&tokens, FileId::DUMMY);
        assert!(errors.is_empty(), "{errors:?}");
        file.expect("the file parses")
    }

    #[test]
    fn the_gap_between_two_tokens() {
        assert_eq!(gap("", 0), "");
        assert_eq!(gap("   ", 0), " ");
        assert_eq!(gap(" /* c */ ", 0), " ");
        assert_eq!(gap("/*c*/", 0), " ");
        assert_eq!(gap(" // why\n        ", 4), "\n    ");
        assert_eq!(gap("\r\n\r\n    ", 4), "\n");
        // A block comment that begins a line takes the spaces after it.
        assert_eq!(gap("\n        /* the left */ ", 4), "\n    ");
        assert_eq!(gap("\n  /* a */\t/* b */ ", 0), "\n  ");
        assert_eq!(gap(" // why\n  /* c */\n    ", 2), "\n  ");
    }

    #[test]
    fn a_function_stops_before_its_body_and_keeps_pub_async_and_where() {
        let text = "/// Doc.\npub async fn go<T>(x: T) -> T where T: Show { x }\n";
        let file = parse(text);
        let Item::Function(f) = &file.items[0].value else {
            panic!("not a function")
        };
        let ft = FileText::new(text, FileId::DUMMY);
        let start = decl_start(text, f.name.span.start as usize);
        let end = fn_end(&ft, f.name.span, f.return_ty.as_ref(), &f.where_clause);
        assert_eq!(
            render(&ft, start, end, Some(f.name.span), &|_| None),
            "<span class=\"kw\">pub</span> <span class=\"kw\">async</span> \
             <span class=\"kw\">fn</span> <span class=\"name\">go</span>&lt;T&gt;(x: T) -&gt; T \
             <span class=\"kw\">where</span> T: Show"
        );
    }

    #[test]
    fn a_broken_signature_keeps_its_lines_less_their_indentation() {
        // Review Focus 1.
        let text = "impl P {\n    pub fn far(\n        self,\n        // the distance\n        to: Int,\n    ) -> Int {\n        to\n    }\n}\n";
        let file = parse(text);
        let Item::Impl(imp) = &file.items[0].value else {
            panic!("not an impl")
        };
        let f = &imp.functions[0];
        let ft = FileText::new(text, FileId::DUMMY);
        let start = decl_start(text, f.name.span.start as usize);
        let end = fn_end(&ft, f.name.span, f.return_ty.as_ref(), &f.where_clause);
        assert_eq!(
            render(&ft, start, end, Some(f.name.span), &|_| None),
            "<span class=\"kw\">pub</span> <span class=\"kw\">fn</span> \
             <span class=\"name\">far</span>(\n    <span class=\"kw\">self</span>,\n    \
             to: Int,\n) -&gt; Int"
        );
    }

    #[test]
    fn a_block_comment_that_begins_a_line_keeps_its_indentation() {
        let text = "impl P {\n    pub fn far(\n        self,\n        /* the left */ to: Int,\n    ) -> Int {\n        to\n    }\n}\n";
        let file = parse(text);
        let Item::Impl(imp) = &file.items[0].value else {
            panic!("not an impl")
        };
        let f = &imp.functions[0];
        let ft = FileText::new(text, FileId::DUMMY);
        let start = decl_start(text, f.name.span.start as usize);
        let end = fn_end(&ft, f.name.span, f.return_ty.as_ref(), &f.where_clause);
        assert_eq!(
            render(&ft, start, end, Some(f.name.span), &|_| None),
            "<span class=\"kw\">pub</span> <span class=\"kw\">fn</span> \
             <span class=\"name\">far</span>(\n    <span class=\"kw\">self</span>,\n    \
             to: Int,\n) -&gt; Int"
        );
    }

    #[test]
    fn names_link_and_the_items_own_name_does_not() {
        let text = "pub fn area(r: Rect, n: Int) -> Float { 0.0 }\n";
        let ft = FileText::new(text, FileId::DUMMY);
        let at = text.find("Rect").unwrap() as u32;
        let rect = Span::new(at, at + 4, FileId::DUMMY);
        let own = Span::new(7, 11, FileId::DUMMY);
        let end = text.find(" {").unwrap();
        let link = |s: Span| (s == rect).then(|| "../g/index.html#record.Rect".to_string());
        assert_eq!(
            render(&ft, 0, end, Some(own), &link),
            "<span class=\"kw\">pub</span> <span class=\"kw\">fn</span> \
             <span class=\"name\">area</span>(r: <a href=\"../g/index.html#record.Rect\">Rect</a>, \
             n: Int) -&gt; Float"
        );
    }

    #[test]
    fn a_record_a_sum_type_a_variant_a_trait_and_a_constant_end_where_they_should() {
        let text = "pub record P<T> { x: T }\npub type S = | A | B(Int)\npub trait Show: Eq { fn show(self) -> String }\npub const N: Int = 1 + 2\n";
        let file = parse(text);
        let ft = FileText::new(text, FileId::DUMMY);
        let plain = |start: usize, end: usize| render(&ft, start, end, None, &|_| None);
        let brace = |from: u32| ft.next(from as usize, |t| *t == Token::LBrace).unwrap();
        let Item::Record(r) = &file.items[0].value else {
            panic!()
        };
        assert_eq!(
            plain(
                decl_start(text, r.name.span.start as usize),
                brace(r.name.span.end)
            ),
            "<span class=\"kw\">pub</span> <span class=\"kw\">record</span> P&lt;T&gt;"
        );
        let Item::Type(t) = &file.items[1].value else {
            panic!()
        };
        let eq = ft
            .next(t.name.span.end as usize, |t| *t == Token::Eq)
            .unwrap();
        assert_eq!(
            plain(decl_start(text, t.name.span.start as usize), eq),
            "<span class=\"kw\">pub</span> <span class=\"kw\">type</span> S"
        );
        let TypeDef::Sum(variants) = &t.def else {
            panic!()
        };
        let b = &variants[1];
        let end = ft.group_end(b.name.span.end as usize).unwrap();
        assert_eq!(plain(b.name.span.start as usize, end), "B(Int)");
        let Item::Trait(tr) = &file.items[2].value else {
            panic!()
        };
        assert_eq!(
            plain(
                decl_start(text, tr.name.span.start as usize),
                brace(tr.name.span.end)
            ),
            "<span class=\"kw\">pub</span> <span class=\"kw\">trait</span> Show: Eq"
        );
        let Item::Const(c) = &file.items[3].value else {
            panic!()
        };
        assert_eq!(
            plain(
                decl_start(text, c.name.span.start as usize),
                c.value.span.end as usize
            ),
            "<span class=\"kw\">pub</span> <span class=\"kw\">const</span> N: Int = \
             <span class=\"lit\">1</span> + <span class=\"lit\">2</span>"
        );
    }

    #[test]
    fn a_field_starts_at_its_pub() {
        let text = "record P {\n    pub x: Int,\n    y: Int,\n}\n";
        assert_eq!(
            decl_start(text, text.find("x:").unwrap()),
            text.find("pub x").unwrap()
        );
        assert_eq!(
            decl_start(text, text.find("y:").unwrap()),
            text.find("y:").unwrap()
        );
    }

    #[test]
    fn module_docs_are_the_bang_lines_before_the_first_token() {
        let ft = FileText::new(
            "// A note.\n//! The core.\n//!\n//! More.\nfn f() {}\n//! Not docs.\n",
            FileId::DUMMY,
        );
        assert_eq!(ft.module_docs().text, "The core.\n\nMore.");
        assert_eq!(
            FileText::new("fn f() {}\n", FileId::DUMMY)
                .module_docs()
                .text,
            ""
        );
        assert_eq!(
            FileText::new("//! Only docs.\n", FileId::DUMMY)
                .module_docs()
                .text,
            "Only docs."
        );
    }

    #[test]
    fn module_docs_map_back_to_their_source() {
        // 3.5b spec §7: `//!` lines need not be adjacent, so each keeps its
        // own place; and the same with CRLF.
        for src in [
            "//! One.\n// A note.\n//!Two.\n/* block */\n//!   three\n//!\nfn f() {}\n",
            "//! One.\r\n// A note.\r\n//!Two.\r\n/* block */\r\n//!   three\r\n//!\r\nfn f() {}\r\n",
        ] {
            let d = FileText::new(src, FileId::DUMMY).module_docs();
            assert_eq!(d.text, "One.\nTwo.\n  three\n", "{src:?}");
            assert_eq!(d.file, Some(FileId::DUMMY));
            for word in ["One.", "Two.", "three", "hree"] {
                assert_eq!(
                    d.source(d.text.find(word).unwrap()),
                    Some(src.find(word).unwrap() as u32),
                    "{word:?} in {src:?}"
                );
            }
            // The bare `//!` is the end of its `//!`.
            let bare = src.rfind("//!").unwrap() as u32;
            assert_eq!(d.source(d.text.len()), Some(bare + 3));
        }
        assert_eq!(
            FileText::new("fn f() {}\n", FileId::DUMMY)
                .module_docs()
                .file,
            None
        );
    }
}