tinymist_query/syntax/
index.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
use std::str::FromStr;

use rustc_hash::FxHashSet;
use tinymist_world::package::PackageSpec;

use crate::{adt::interner::Interned, prelude::*};

#[derive(Default)]
pub struct IndexInfo {
    pub(crate) paths: FxHashSet<Interned<str>>,
    pub(crate) packages: FxHashSet<PackageSpec>,
    pub(crate) identifiers: FxHashSet<Interned<str>>,
}

#[comemo::memoize]
pub fn get_index_info(src: &Source) -> Arc<IndexInfo> {
    let root = src.root();
    let mut worker = IndexWorker {
        info: IndexInfo::default(),
    };
    worker.visit(root);
    Arc::new(worker.info)
}

struct IndexWorker {
    info: IndexInfo,
}

impl IndexWorker {
    fn visit(&mut self, node: &SyntaxNode) {
        match node.cast::<ast::Expr>() {
            Some(ast::Expr::Str(path_str)) => {
                if path_str.to_untyped().text().len() > 65536 {
                    // skip long strings
                    return;
                }
                let path_str = path_str.get();

                if path_str.starts_with('@') {
                    let pkg_spec = PackageSpec::from_str(&path_str).ok();
                    if let Some(pkg_spec) = pkg_spec {
                        self.info.identifiers.insert(pkg_spec.name.clone().into());
                        self.info.packages.insert(pkg_spec);
                    }
                    return;
                }
                let path = Path::new(path_str.as_str());
                let name = path.file_name().unwrap_or_default().to_str();
                if let Some(name) = name {
                    self.info.paths.insert(name.into());
                }
            }
            Some(ast::Expr::MathIdent(ident)) => {
                self.info.identifiers.insert(ident.get().into());
            }
            Some(ast::Expr::Ident(ident)) => {
                self.info.identifiers.insert(ident.get().into());
            }
            _ => {}
        }

        for child in node.children() {
            self.visit(child);
        }
    }
}