typst_analyzer_analysis/completion/
markup.rs

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
/*!
# Markup

Paragraph break Blank line parbreak
Strong emphasis *strong* strong
Emphasis _emphasis_ emph
Raw text `print(1)` raw
Link https://typst.app/ link
Label <intro> label
Reference @intro ref
Bullet list - item list
Numbered list + item enum
Term list / Term: description terms
Line break \ linebreak
Smart quote 'single' or "double" smartquote
Code expression #rect(width: 1cm) Scripting
Comment /* block */, // line Below
TODO: Heading = Heading heading
TODO: Math $x^2$ Math
TODO: Symbol shorthand ~, --- Symbols
TODO: Character escape Tweet at us \#ad Below
*/

use std::collections::HashMap;

use tower_lsp::lsp_types::{CompletionItem, CompletionItemKind, InsertTextFormat, MarkupContent};

use crate::{get_images, typ_logger};

use super::generic::TypCmpItem;

pub fn items() -> Vec<CompletionItem> {
    let mut items = Vec::new();
    items.append(&mut headers());
    items
}

pub fn constructors() -> Vec<CompletionItem> {
    let mut items = Vec::new();
    // vec of tuples with the constructor name, the label details and insert text
    let constructor: Vec<(&str, &str, String)> = vec![
        ("Paragraph break", "parbreak", "parbreak()\n".to_owned()),
        ("Strong emphasis", "strong", "*${1:Text}*".to_owned()),
        ("Strong emphasis", "bold", "*${1:Text}*".to_owned()),
        ("Emphasis", "emph", "_${1:Text}_".to_owned()),
        ("Raw text", "raw", "`${1:Text}`".to_owned()),
        ("Link", "link", "https://${1:URL}/".to_owned()),
        ("Label", "label", "<${1:Text}>".to_owned()),
        ("Reference", "ref", "@${1:Text}".to_owned()),
        ("Bullet list", "bullet-list", "- ${1:Text}".to_owned()),
        ("Numbered list", "numbered-list", "+ ${1:Text}".to_owned()),
        (
            "Term list",
            "terms",
            "/ ${1:Term}: ${2:Description}".to_owned(),
        ),
        ("Line break", "linebreak", "\\".to_owned()),
        ("Smart quote", "smartquote", "'${1:Text}'".to_owned()),
        ("Code expression", "Scripting", "#${1:Code}".to_owned()),
        ("Comment", "line-comment", "// ${1:Text}".to_owned()),
        ("Comment", "block-comment", "/*\n ${1:Text} \n*/".to_owned()),
    ];
    for ctx in constructor {
        let item = TypCmpItem {
            label: ctx.1.to_owned(),
            label_details: "markup",
            kind: CompletionItemKind::CONSTRUCTOR,
            documentation: ctx.0,
            insert_text: ctx.2.to_owned(),
        };
        items.push(item);
    }
    TypCmpItem::get_cmp(items)
}

fn get_headers() -> HashMap<String, String> {
    let mut cmp_item = HashMap::new();
    let mut item = "=".to_owned();

    // Use a fresh `label` for each iteration
    for num in 1..7 {
        let label = format!("h{}", num); // Create the label as "h<num>"
        cmp_item.insert(label, item.clone());
        item.push('='); // Add an additional '#' to the item for the next iteration
    }
    cmp_item
}

#[test]
fn header_test() {
    let mut expected = HashMap::new();
    expected.insert("h1".to_owned(), "=".to_owned());
    expected.insert("h2".to_owned(), "==".to_owned());
    expected.insert("h3".to_owned(), "===".to_owned());
    expected.insert("h4".to_owned(), "====".to_owned());
    expected.insert("h5".to_owned(), "=====".to_owned());
    expected.insert("h6".to_owned(), "======".to_owned());

    let result = get_headers();

    assert_eq!(
        result, expected,
        "The generated HashMap does not match the expected output."
    );
}

fn headers() -> Vec<CompletionItem> {
    let header = get_headers();
    let mut header_items = Vec::new();
    for (key, val) in header {
        let num: usize = key[1..].parse().unwrap_or(0);
        header_items.push(CompletionItem {
            label: key.to_owned(),
            label_details: Some(tower_lsp::lsp_types::CompletionItemLabelDetails {
                detail: Some("Header".to_owned()),
                description: None,
            }),
            kind: Some(CompletionItemKind::CONSTRUCTOR),
            documentation: Some(tower_lsp::lsp_types::Documentation::MarkupContent(
                MarkupContent {
                    kind: tower_lsp::lsp_types::MarkupKind::Markdown,
                    value: format!("level {} heading", num),
                },
            )),
            insert_text: Some(format!("{} ", val)),
            insert_text_format: Some(InsertTextFormat::PLAIN_TEXT),
            ..Default::default()
        });
    }
    header_items
}

pub fn typ_image_cmp() -> Result<Vec<CompletionItem>, anyhow::Error> {
    let mut items = Vec::new();
    let images = get_images()?;
        typ_logger!("image: {:#?}", images);
    for item in images {
        let image = item.to_string_lossy().to_string();
        let item = TypCmpItem {
            label: "image".to_owned(),
            label_details: "markup",
            kind: CompletionItemKind::FILE,
            documentation: "Image",
            insert_text: format!("#image(\"{}\", width: 100%)", image),
        };
        typ_logger!("image: {}", image);
        items.push(item);
    }
    Ok(TypCmpItem::get_cmp(items))
}