tinymist_query/analysis/completion/
import.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
//! Completion for import items.

use super::*;
impl CompletionPair<'_, '_, '_> {
    /// Complete imports.
    pub fn complete_imports(&mut self) -> bool {
        // On the colon marker of an import list:
        // "#import "path.typ":|"
        if_chain! {
            if matches!(self.cursor.leaf.kind(), SyntaxKind::Colon);
            if let Some(parent) = self.cursor.leaf.clone().parent();
            if let Some(ast::Expr::Import(import)) = parent.get().cast();
            if !matches!(import.imports(), Some(ast::Imports::Wildcard));
            if let Some(source) = parent.children().find(|child| child.is::<ast::Expr>());
            then {
                let items = match import.imports() {
                    Some(ast::Imports::Items(items)) => items,
                    _ => Default::default(),
                };

                self.cursor.from = self.cursor.cursor;

                self.import_item_completions(items, vec![], &source);
                if items.iter().next().is_some() {
                    self.worker.enrich("", ", ");
                }
                return true;
            }
        }

        // Behind an import list:
        // "#import "path.typ": |",
        // "#import "path.typ": a, b, |".
        if_chain! {
            if let Some(prev) = self.cursor.leaf.prev_sibling();
            if let Some(ast::Expr::Import(import)) = prev.get().cast();
            if !self.cursor.text[prev.offset()..self.cursor.cursor].contains('\n');
            if let Some(ast::Imports::Items(items)) = import.imports();
            if let Some(source) = prev.children().find(|child| child.is::<ast::Expr>());
            then {
                self.  cursor.from = self.cursor.cursor;
                self.import_item_completions(items, vec![], &source);
                return true;
            }
        }

        // Behind a comma in an import list:
        // "#import "path.typ": this,|".
        if_chain! {
            if matches!(self.cursor.leaf.kind(), SyntaxKind::Comma);
            if let Some(parent) = self.cursor.leaf.clone().parent();
            if parent.kind() == SyntaxKind::ImportItems;
            if let Some(grand) = parent.parent();
            if let Some(ast::Expr::Import(import)) = grand.get().cast();
            if let Some(ast::Imports::Items(items)) = import.imports();
            if let Some(source) = grand.children().find(|child| child.is::<ast::Expr>());
            then {
                self.import_item_completions(items, vec![], &source);
                self.worker.enrich(" ", "");
                return true;
            }
        }

        // Behind a half-started identifier in an import list:
        // "#import "path.typ": th|".
        if_chain! {
            if matches!(self.cursor.leaf.kind(), SyntaxKind::Ident | SyntaxKind::Dot);
            if let Some(path_ctx) = self.cursor.leaf.clone().parent();
            if path_ctx.kind() == SyntaxKind::ImportItemPath;
            if let Some(parent) = path_ctx.parent();
            if parent.kind() == SyntaxKind::ImportItems;
            if let Some(grand) = parent.parent();
            if let Some(ast::Expr::Import(import)) = grand.get().cast();
            if let Some(ast::Imports::Items(items)) = import.imports();
            if let Some(source) = grand.children().find(|child| child.is::<ast::Expr>());
            then {
                if self.cursor.leaf.kind() == SyntaxKind::Ident {
                    self.cursor.from = self.cursor.leaf.offset();
                }
                let path = path_ctx.cast::<ast::ImportItemPath>().map(|path| path.iter().take_while(|ident| ident.span() != self.cursor.leaf.span()).collect());
                self.import_item_completions( items, path.unwrap_or_default(), &source);
                return true;
            }
        }

        false
    }

    /// Add completions for all exports of a module.
    pub fn import_item_completions(
        &mut self,
        existing: ast::ImportItems,
        comps: Vec<ast::Ident>,
        source: &LinkedNode,
    ) {
        // Select the source by `comps`
        let value = self.worker.ctx.module_by_syntax(source);
        let value = comps.iter().fold(value.as_ref(), |value, comp| {
            value?.scope()?.get(comp)?.read().into()
        });
        let Some(scope) = value.and_then(|v| v.scope()) else {
            return;
        };

        // Check imported items in the scope
        let seen = existing
            .iter()
            .flat_map(|item| {
                let item_comps = item.path().iter().collect::<Vec<_>>();
                if item_comps.len() == comps.len() + 1
                    && item_comps
                        .iter()
                        .zip(comps.as_slice())
                        .all(|(l, r)| l.as_str() == r.as_str())
                {
                    // item_comps.len() >= 1
                    item_comps.last().cloned()
                } else {
                    None
                }
            })
            .collect::<Vec<_>>();

        if existing.iter().next().is_none() {
            self.snippet_completion("*", "*", "Import everything.");
        }

        for (name, bind) in scope.iter() {
            if seen.iter().all(|item| item.as_str() != name) {
                self.value_completion(Some(name.clone()), bind.read(), false, None);
            }
        }
    }
}