typst_analyzer/completion/
mod.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
#![allow(dead_code)]
use std::collections::HashMap;

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

// use self::markup;

pub mod handle;
pub(crate) mod markup;

// FIX: update
fn table() -> HashMap<String, String> {
    let mut tables = HashMap::new();

    // Generate tables from 1x1 to 7x7 (NxM)
    for n in 1..=7 {
        // Number of rows
        for m in 1..=7 {
            // Number of columns
            let key = format!("table{}x{}", n, m); // Key format: tableNxM

            // Generate table header: Column1 | Column2 | ... | ColumnM
            let header = (1..=m)
                .map(|i| format!("Column{}", i))
                .collect::<Vec<_>>()
                .join(" | ");

            // Generate separator line: --------------- | --------------- | ... | ---------------
            let separator = (0..m)
                .map(|_| "---------------")
                .collect::<Vec<_>>()
                .join(" | ");

            // Generate rows: Item1.1 | Item2.1 | ... | ItemN.1 for N rows
            let mut rows = Vec::new();
            for i in 1..=n {
                let row = (1..=m)
                    .map(|j| format!("Item{}.{}", i, j))
                    .collect::<Vec<_>>()
                    .join(" | ");
                rows.push(row);
            }

            // Join the header, separator, and rows to form the table in markdown format
            let value = format!(
                "| {} |\n| {} |\n| {} |",
                header,
                separator,
                rows.join(" |\n| ")
            );

            tables.insert(key, value);
        }
    }
    tables
}

// FIX: update
fn provide_tables() -> Vec<CompletionItem> {
    let table = table();
    let mut table_items = Vec::new();
    for (key, val) in table {
        let num: usize = key[1..].parse().unwrap_or(0);
        table_items.push(CompletionItem {
            label: key.to_owned(),
            label_details: Some(tower_lsp::lsp_types::CompletionItemLabelDetails {
                detail: Some("Table".to_owned()),
                description: None,
            }),
            kind: Some(CompletionItemKind::CONSTANT),
            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()
        });
    }
    table_items
}

// FIX: update
fn snippets() -> Vec<CompletionItem> {
    vec![CompletionItem {
        label: "maths".to_owned(),
        label_details: Some(tower_lsp::lsp_types::CompletionItemLabelDetails {
            detail: Some("details from label_details".to_owned()),
            description: None,
        }),
        kind: Some(CompletionItemKind::CONSTANT),
        detail: Some(
            "# Details from CompletionItem
                "
            .to_owned(),
        ),
        documentation: Some(tower_lsp::lsp_types::Documentation::MarkupContent(
            MarkupContent {
                kind: tower_lsp::lsp_types::MarkupKind::Markdown,
                value: "documentation for the function".to_owned(),
            },
        )),
        insert_text: Some("this will be placed".to_owned()),
        insert_text_format: Some(InsertTextFormat::PLAIN_TEXT),
        ..Default::default()
    }]
}

// FIX: update
fn provide_markdown_links() -> Vec<CompletionItem> {
    vec![CompletionItem {
        label: "link".to_owned(),
        label_details: Some(tower_lsp::lsp_types::CompletionItemLabelDetails {
            detail: Some("Markdown Link".to_owned()),
            description: None,
        }),
        kind: Some(CompletionItemKind::VARIABLE),
        documentation: Some(Documentation::MarkupContent(MarkupContent {
            kind: MarkupKind::Markdown,
            value: "Insert Markdown link: [LINK TEXT](URL)".to_string(),
        })),
        insert_text: Some(format!("[{}]({})", "${1:Link Text}", "${2:URL}")),
        insert_text_format: Some(InsertTextFormat::SNIPPET),
        ..Default::default()
    }]
}
// FIX: update
fn provide_images() -> Vec<CompletionItem> {
    vec![CompletionItem {
        label: "image".to_owned(),
        label_details: Some(tower_lsp::lsp_types::CompletionItemLabelDetails {
            detail: Some("Image".to_owned()),
            description: None,
        }),
        kind: Some(CompletionItemKind::FUNCTION),
        documentation: Some(Documentation::MarkupContent(MarkupContent {
            kind: MarkupKind::Markdown,
            value: "Insert Image: ![alt text](path/to/image.png)".to_string(),
        })),
        insert_text: Some(format!("![{}]({})", "${1:Alt Text}", "${2:PATH}")),
        insert_text_format: Some(InsertTextFormat::SNIPPET),
        ..Default::default()
    }]
}