tinymist_query/syntax/
module.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
use std::sync::Once;

use regex::RegexSet;

use crate::prelude::*;

/// The dependency information of a module (file).
#[derive(Debug, Clone)]
pub struct ModuleDependency {
    /// The dependencies of this module.
    pub dependencies: EcoVec<TypstFileId>,
    /// The dependents of this module.
    pub dependents: EcoVec<TypstFileId>,
}

/// Construct the module dependencies of the given context.
///
/// It will scan all the files in the context, using
/// [`LocalContext::source_files`], and find the dependencies and dependents
/// of each file.
pub fn construct_module_dependencies(
    ctx: &mut LocalContext,
) -> HashMap<TypstFileId, ModuleDependency> {
    let mut dependencies = HashMap::new();
    let mut dependents = HashMap::new();

    for file_id in ctx.source_files().clone() {
        let source = match ctx.shared.source_by_id(file_id) {
            Ok(source) => source,
            Err(err) => {
                static WARN_ONCE: Once = Once::new();
                WARN_ONCE.call_once(|| {
                    log::warn!("construct_module_dependencies: {err:?}");
                });
                continue;
            }
        };

        let file_id = source.id();
        let ei = ctx.shared.expr_stage(&source);

        dependencies
            .entry(file_id)
            .or_insert_with(|| ModuleDependency {
                dependencies: ei.imports.keys().cloned().collect(),
                dependents: EcoVec::default(),
            });
        for (dep, _) in ei.imports.clone() {
            dependents
                .entry(dep)
                .or_insert_with(EcoVec::new)
                .push(file_id);
        }
    }

    for (file_id, dependents) in dependents {
        if let Some(dep) = dependencies.get_mut(&file_id) {
            dep.dependents = dependents;
        }
    }

    dependencies
}

fn is_hidden(entry: &walkdir::DirEntry) -> bool {
    entry
        .file_name()
        .to_str()
        .map(|s| s.starts_with('.'))
        .unwrap_or(false)
}

/// Scan the files in the workspace and return the file ids.
///
/// Note: this function will touch the physical file system.
pub(crate) fn scan_workspace_files<T>(
    root: &Path,
    ext: &RegexSet,
    f: impl Fn(&Path) -> T,
) -> Vec<T> {
    let mut res = vec![];
    let mut it = walkdir::WalkDir::new(root).follow_links(false).into_iter();
    loop {
        let de = match it.next() {
            None => break,
            Some(Err(_err)) => continue,
            Some(Ok(entry)) => entry,
        };
        if is_hidden(&de) {
            if de.file_type().is_dir() {
                it.skip_current_dir();
            }
            continue;
        }

        /// this is a temporary solution to ignore some common build directories
        static IGNORE_REGEX: LazyLock<RegexSet> = LazyLock::new(|| {
            RegexSet::new([
                r#"^build$"#,
                r#"^target$"#,
                r#"^node_modules$"#,
                r#"^out$"#,
                r#"^dist$"#,
            ])
            .unwrap()
        });
        if de
            .path()
            .file_name()
            .and_then(|s| s.to_str())
            .is_some_and(|s| IGNORE_REGEX.is_match(s))
        {
            if de.file_type().is_dir() {
                it.skip_current_dir();
            }
            continue;
        }

        if !de.file_type().is_file() {
            continue;
        }
        if !de
            .path()
            .extension()
            .and_then(|err| err.to_str())
            .is_some_and(|err| ext.is_match(err))
        {
            continue;
        }

        let path = de.path();
        let relative_path = match path.strip_prefix(root) {
            Ok(path) => path,
            Err(err) => {
                log::warn!("failed to strip prefix, path: {path:?}, root: {root:?}: {err}");
                continue;
            }
        };

        res.push(f(relative_path));

        // two times of max number of typst file ids
        if res.len() >= (u16::MAX as usize) {
            break;
        }
    }

    res
}