cairo-lang-doc 2.18.0

A collection of documentation processing utilities for the Cairo programming language.
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
use std::fmt;
use std::ops::Range;

use cairo_lang_debug::DebugWithDb;
use cairo_lang_filesystem::span::{TextOffset, TextSpan, TextWidth};
use itertools::Itertools;
use pulldown_cmark::{
    Alignment, BrokenLink, CodeBlockKind, Event, HeadingLevel, LinkType, Options,
    Parser as MarkdownParser, Tag, TagEnd,
};

use crate::db::DocGroup;

#[derive(PartialEq, Eq, Hash, Debug, Clone)]
pub struct MarkdownLink {
    /// The span of the whole link, including the label, the destination URL and the delimiters.
    pub link_span: TextSpan,
    /// Where the link leads to. Not present when the label could not be resolved.
    pub dest_span: Option<TextSpan>,
    /// The underlying content of `dest_span`, if present.
    pub dest_text: Option<String>,
}

/// Token representing a link to another item inside the documentation.
#[derive(Debug, PartialEq, Clone, Eq, salsa::Update)]
pub struct CommentLinkToken {
    /// A link part that's inside "[]" brackets.
    pub label: String,
    /// A link part that's inside "()" brackets, right after the label.
    pub path: Option<String>,
    /// The link.
    pub md_link: MarkdownLink,
}

/// Generic type for a comment token. It's either plain content or a link.
/// Notice that the Content token type can store much more than just one word.
#[derive(Debug, PartialEq, Clone, Eq, salsa::Update)]
pub enum DocumentationCommentToken {
    /// Token with plain documentation content.
    Content(String),
    /// Link token.
    Link(CommentLinkToken),
}

impl DocumentationCommentToken {
    /// Checks if string representation of [`DocumentationCommentToken`] ends with newline.
    pub fn ends_with_newline(self) -> bool {
        match self {
            DocumentationCommentToken::Content(content) => content.ends_with('\n'),
            DocumentationCommentToken::Link(link_token) => link_token.label.ends_with('\n'),
        }
    }
}

/// Helper struct for formatting possibly nested Markdown lists.
struct DocCommentListItem {
    /// Ordered list item separator.
    delimiter: Option<u64>,
    /// Flag for an ordered list.
    is_ordered_list: bool,
}

struct PendingLink {
    label: String,
    path: Option<String>,
    link_start: usize,
    link_type: LinkType,
    destination: String,
    label_range: Option<Range<usize>>,
}

/// Parses documentation comment content into a vector of [DocumentationCommentToken]s, keeping
/// the order in which they were present in the content.
///
/// We look for 3 link patterns (ignore the backslash):
/// "\[label\](path)", "\[path\]" or "\[`path`\]".
pub fn parse_documentation_comment(documentation_comment: &str) -> Vec<DocumentationCommentToken> {
    let mut tokens = Vec::new();
    let mut current_link: Option<PendingLink> = None;
    let mut is_indented_code_block = false;
    let mut replacer = |broken_link: BrokenLink<'_>| {
        if matches!(broken_link.link_type, LinkType::ShortcutUnknown | LinkType::Shortcut) {
            return Some((broken_link.reference.to_string().into(), "".into()));
        }
        None
    };

    let mut options = Options::empty();
    options.insert(Options::ENABLE_TABLES);
    let parser = MarkdownParser::new_with_broken_link_callback(
        documentation_comment,
        options,
        Some(&mut replacer),
    );

    let mut list_nesting: Vec<DocCommentListItem> = Vec::new();
    let write_list_item_prefix =
        |list_nesting: &mut Vec<DocCommentListItem>,
         tokens: &mut Vec<DocumentationCommentToken>| {
            if !list_nesting.is_empty() {
                let indent = "  ".repeat(list_nesting.len() - 1);
                let list_nesting = list_nesting.last_mut().unwrap();

                let item_delimiter = if list_nesting.is_ordered_list {
                    let delimiter = list_nesting.delimiter.unwrap_or(0);
                    list_nesting.delimiter = Some(delimiter + 1);
                    format!("{indent}{delimiter}.",)
                } else {
                    format!("{indent}-")
                };
                tokens
                    .push(DocumentationCommentToken::Content(format!("{indent}{item_delimiter} ")));
            }
        };
    let mut prefix_list_item = false;
    let mut last_two_events = [None, None];
    let mut table_alignment: Vec<Alignment> = Vec::new();

    for (event, range) in parser.into_offset_iter() {
        match &event {
            Event::Text(text) => {
                if prefix_list_item {
                    write_list_item_prefix(&mut list_nesting, &mut tokens);
                    prefix_list_item = false;
                }
                if let Some(link) = current_link.as_mut() {
                    link.label.push_str(text.as_ref());
                    link.label_range = Some(range.clone());
                } else {
                    let text = {
                        if is_indented_code_block {
                            format!("    {text}")
                        } else {
                            text.to_string()
                        }
                    };
                    tokens.push(DocumentationCommentToken::Content(text));
                }
            }
            Event::Code(code) => {
                if prefix_list_item {
                    write_list_item_prefix(&mut list_nesting, &mut tokens);
                    prefix_list_item = false;
                }
                let complete_code = format!("`{code}`");
                if let Some(link) = current_link.as_mut() {
                    link.label.push_str(&complete_code);
                    link.label_range = Some(range.clone());
                } else {
                    tokens.push(DocumentationCommentToken::Content(complete_code));
                }
            }
            Event::Start(tag_start) => match tag_start {
                Tag::Heading { level, .. } => {
                    if let Some(last_token) = tokens.last_mut()
                        && !last_token.clone().ends_with_newline()
                    {
                        tokens.push(DocumentationCommentToken::Content("\n".to_string()));
                    }
                    tokens.push(DocumentationCommentToken::Content(format!(
                        "{} ",
                        heading_level_to_markdown(*level)
                    )));
                }
                Tag::List(list_type) => {
                    if !list_nesting.is_empty() {
                        tokens.push(DocumentationCommentToken::Content("\n".to_string()));
                    }
                    list_nesting.push(DocCommentListItem {
                        delimiter: *list_type,
                        is_ordered_list: list_type.is_some(),
                    });
                }
                Tag::CodeBlock(kind) => match kind {
                    CodeBlockKind::Fenced(language) => {
                        if language.trim().is_empty() {
                            tokens.push(DocumentationCommentToken::Content(String::from(
                                "```cairo\n",
                            )));
                        } else {
                            tokens.push(DocumentationCommentToken::Content(format!(
                                "```{language}\n"
                            )));
                        }
                    }
                    CodeBlockKind::Indented => {
                        tokens.push(DocumentationCommentToken::Content("\n".to_string()));
                        is_indented_code_block = true;
                    }
                },
                Tag::Link { link_type, dest_url, .. } => {
                    let path = match *link_type {
                        LinkType::ShortcutUnknown | LinkType::Shortcut => None,
                        _ => Some(dest_url.clone().into_string()),
                    };
                    current_link = Some(PendingLink {
                        label: String::new(),
                        path,
                        link_start: range.start,
                        link_type: *link_type,
                        destination: dest_url.clone().into_string(),
                        label_range: None,
                    });
                }
                Tag::Paragraph | Tag::TableRow => {
                    tokens.push(DocumentationCommentToken::Content("\n".to_string()));
                }
                Tag::Item => {
                    prefix_list_item = true;
                }
                Tag::Table(alignment) => {
                    table_alignment = alignment.clone();
                    tokens.push(DocumentationCommentToken::Content("\n".to_string()));
                }
                Tag::TableCell => {
                    tokens.push(DocumentationCommentToken::Content("|".to_string()));
                }
                Tag::Strong => {
                    tokens.push(DocumentationCommentToken::Content("**".to_string()));
                }
                Tag::Emphasis => {
                    tokens.push(DocumentationCommentToken::Content("_".to_string()));
                }
                _ => {}
            },
            Event::End(tag_end) => match tag_end {
                TagEnd::Heading(_) | TagEnd::Table => {
                    tokens.push(DocumentationCommentToken::Content("\n".to_string()));
                }
                TagEnd::List(_) => {
                    list_nesting.pop();
                }
                TagEnd::Item
                    if !matches!(last_two_events[0], Some(Event::End(_)))
                        | !matches!(last_two_events[1], Some(Event::End(_))) =>
                {
                    tokens.push(DocumentationCommentToken::Content("\n".to_string()));
                }
                TagEnd::TableHead => {
                    tokens.push(DocumentationCommentToken::Content(format!(
                        "|\n|{}|",
                        table_alignment
                            .iter()
                            .map(|a| {
                                let (left, right) = get_alignment_markers(a);
                                format!("{left}---{right}")
                            })
                            .join("|")
                    )));
                    table_alignment.clear();
                }
                TagEnd::CodeBlock => {
                    if !is_indented_code_block {
                        tokens.push(DocumentationCommentToken::Content("```\n".to_string()));
                    }
                    is_indented_code_block = false;
                }
                TagEnd::Link => {
                    if let Some(link) = current_link {
                        let link_span = span_from_relative_range(
                            documentation_comment,
                            link.link_start..range.end,
                        );
                        let (dest_span, dest_text) = link
                            .label_range
                            .as_ref()
                            .and_then(|label_range| {
                                location_from_link_fields(
                                    link.link_type,
                                    &link.destination,
                                    label_range,
                                )
                            })
                            .map(|(dest_range, dest_text)| {
                                (
                                    Some(span_from_relative_range(
                                        documentation_comment,
                                        dest_range,
                                    )),
                                    Some(dest_text),
                                )
                            })
                            .unwrap_or((None, None));
                        let md_link = MarkdownLink { link_span, dest_span, dest_text };
                        tokens.push(DocumentationCommentToken::Link(CommentLinkToken {
                            label: link.label,
                            path: link.path,
                            md_link,
                        }));
                    }
                    current_link = None;
                }
                TagEnd::TableRow => {
                    tokens.push(DocumentationCommentToken::Content("|".to_string()));
                }
                TagEnd::Strong => {
                    tokens.push(DocumentationCommentToken::Content("**".to_string()));
                }
                TagEnd::Emphasis => {
                    tokens.push(DocumentationCommentToken::Content("_".to_string()));
                }
                TagEnd::Paragraph => {
                    tokens.push(DocumentationCommentToken::Content("\n".to_string()));
                }
                _ => {}
            },
            Event::SoftBreak => {
                tokens.push(DocumentationCommentToken::Content("\n".to_string()));
            }
            Event::Rule => {
                tokens.push(DocumentationCommentToken::Content("___\n".to_string()));
            }
            _ => {}
        }
        last_two_events = [last_two_events[1].clone(), Some(event)];
    }

    if let Some(DocumentationCommentToken::Content(token)) = tokens.first()
        && token == "\n"
    {
        tokens.remove(0);
    }
    if let Some(DocumentationCommentToken::Content(token)) = tokens.last_mut() {
        *token = token.trim_end().to_string();
        if token.is_empty() {
            tokens.pop();
        }
    }

    tokens
}

impl fmt::Display for CommentLinkToken {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.path.clone() {
            Some(path) => write!(f, "[{}]({})", self.label, path),
            None => write!(f, "[{}]", self.label),
        }
    }
}

impl fmt::Display for DocumentationCommentToken {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            DocumentationCommentToken::Content(content) => {
                write!(f, "{content}")
            }
            DocumentationCommentToken::Link(link_token) => {
                write!(f, "{link_token}")
            }
        }
    }
}

impl<'db> DebugWithDb<'db> for CommentLinkToken {
    type Db = dyn DocGroup;
    fn fmt(&self, f: &mut fmt::Formatter<'_>, _db: &Self::Db) -> fmt::Result {
        f.debug_struct("CommentLinkToken")
            .field("label", &self.label)
            .field("path", &self.path)
            .field("md_link", &self.md_link)
            .finish()
    }
}

/// Converts a byte range within the string into a `TextSpan` relative to the string start.
fn span_from_relative_range(content: &str, range: Range<usize>) -> TextSpan {
    let start = TextOffset::START.add_width(TextWidth::at(content, range.start));
    let end = TextOffset::START.add_width(TextWidth::at(content, range.end));
    TextSpan::new(start, end)
}

/// Extracts a location link span and normalized destination text for the given link fields.
fn location_from_link_fields(
    link_type: LinkType,
    destination: &str,
    label_range: &Range<usize>,
) -> Option<(Range<usize>, String)> {
    let (destination_normalized, backticked) = normalize_location_text(destination)?;

    match link_type {
        LinkType::Inline => {
            let range = find_inline_destination_range(label_range.end, destination);
            Some((range, destination_normalized))
        }
        LinkType::Collapsed
        | LinkType::CollapsedUnknown
        | LinkType::Shortcut
        | LinkType::ShortcutUnknown => Some((label_range.clone(), destination_normalized)),
        _ => None,
    }
    .map(|(range, text)| (trim_backtick_range(range.clone(), backticked), text))
}

/// Returns true when the string looks like a location path (letters, digits, '_' or ':').
fn is_location_string(value: &str) -> bool {
    !value.is_empty() && value.chars().all(|c| c.is_ascii_alphanumeric() || c == '_' || c == ':')
}

/// Normalizes the link destination and reports whether it was backticked.
fn normalize_location_text(value: &str) -> Option<(String, bool)> {
    let (value, backticked) = strip_backticks(value);
    is_location_string(value).then(|| (value.to_string(), backticked))
}

/// Strips backticks around a string if present and reports whether a pair was removed.
fn strip_backticks(value: &str) -> (&str, bool) {
    let value = value.trim();
    if let Some(stripped) = value.strip_prefix('`').and_then(|rest| rest.strip_suffix('`')) {
        (stripped, true)
    } else {
        (value, false)
    }
}

/// Trims the range by one on each end when a backticked span is expected.
fn trim_backtick_range(range: Range<usize>, backticked: bool) -> Range<usize> {
    if backticked { (range.start + 1)..(range.end - 1) } else { range }
}

/// Computes the range for an inline destination that follows a label.
fn find_inline_destination_range(label_last_end: usize, destination: &str) -> Range<usize> {
    let destination_start = label_last_end + 2;
    let destination_end = destination_start + destination.len();
    destination_start..destination_end
}

/// Maps `HeadingLevel` to the correct markdown marker.
fn heading_level_to_markdown(heading_level: HeadingLevel) -> String {
    let heading_char: String = String::from("#");
    match heading_level {
        HeadingLevel::H1 => heading_char,
        HeadingLevel::H2 => heading_char.repeat(2),
        HeadingLevel::H3 => heading_char.repeat(3),
        HeadingLevel::H4 => heading_char.repeat(4),
        HeadingLevel::H5 => heading_char.repeat(5),
        HeadingLevel::H6 => heading_char.repeat(6),
    }
}

/// Maps [`Alignment`] to the correct markdown markers.
fn get_alignment_markers(alignment: &Alignment) -> (String, String) {
    let (left, right) = match alignment {
        Alignment::None => ("", ""),
        Alignment::Left => (":", ""),
        Alignment::Right => ("", ":"),
        Alignment::Center => (":", ":"),
    };
    (left.to_string(), right.to_string())
}