tinymist_project/
entry.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
use std::sync::Arc;

use serde::{Deserialize, Serialize};
use tinymist_l10n::DebugL10n;
use tinymist_std::error::prelude::*;
use tinymist_std::hash::FxDashMap;
use tinymist_std::ImmutPath;
use tinymist_world::EntryState;
use typst::syntax::VirtualPath;

/// The kind of project resolution.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub enum ProjectResolutionKind {
    /// Manage typst documents like what we did in Markdown. Each single file is
    /// an individual document and no project resolution is needed.
    /// This is the default behavior.
    #[default]
    SingleFile,
    /// Manage typst documents like what we did in Rust. For each workspace,
    /// tinymist tracks your preview and compilation history, and stores the
    /// information in a lock file. Tinymist will automatically selects the main
    /// file to use according to the lock file. This also allows other tools
    /// push preview and export tasks to language server by updating the
    /// lock file.
    LockDatabase,
}

/// Entry resolver
#[derive(Debug, Default, Clone)]
pub struct EntryResolver {
    /// The kind of project resolution.
    pub project_resolution: ProjectResolutionKind,
    /// Specifies the root path of the project manually.
    pub root_path: Option<ImmutPath>,
    /// The workspace roots from initialization.
    pub roots: Vec<ImmutPath>,
    /// Default entry path from the configuration.
    pub entry: Option<ImmutPath>,
    /// The path to the typst.toml files.
    pub typst_toml_cache: Arc<FxDashMap<ImmutPath, Option<ImmutPath>>>,
}

impl EntryResolver {
    /// Resolves the root directory for the entry file.
    pub fn root(&self, entry: Option<&ImmutPath>) -> Option<ImmutPath> {
        if let Some(root) = &self.root_path {
            return Some(root.clone());
        }

        if let Some(entry) = entry {
            for root in self.roots.iter() {
                if entry.starts_with(root) {
                    return Some(root.clone());
                }
            }

            if !self.roots.is_empty() {
                log::warn!("entry is not in any set root directory");
            }

            let typst_toml_cache = &self.typst_toml_cache;

            match typst_toml_cache.get(entry).map(|r| r.clone()) {
                // In the case that the file is out of workspace, it is believed to not edited
                // frequently. When we check the package root of such files and didn't find it
                // previously, we quickly return None to avoid heavy IO frequently.
                //
                // todo: we avoid heavy io for the case when no root is set, but people should
                // restart the server to refresh the cache
                Some(None) => return None,
                Some(Some(cached)) => {
                    let cached = cached.clone();
                    if cached.join("typst.toml").exists() {
                        return Some(cached.clone());
                    }
                    typst_toml_cache.remove(entry);
                }
                None => {}
            };

            // cache miss, check the file system
            // todo: heavy io here?
            for ancestor in entry.ancestors() {
                let typst_toml = ancestor.join("typst.toml");
                if typst_toml.exists() {
                    let ancestor: ImmutPath = ancestor.into();
                    typst_toml_cache.insert(entry.clone(), Some(ancestor.clone()));
                    return Some(ancestor);
                }
            }
            typst_toml_cache.insert(entry.clone(), None);

            if let Some(parent) = entry.parent() {
                return Some(parent.into());
            }
        }

        if !self.roots.is_empty() {
            return Some(self.roots[0].clone());
        }

        None
    }

    /// Resolves the entry state.
    pub fn resolve(&self, entry: Option<ImmutPath>) -> EntryState {
        let root_dir = self.root(entry.as_ref());
        self.resolve_with_root(root_dir, entry)
    }

    /// Resolves the entry state.
    pub fn resolve_with_root(
        &self,
        root_dir: Option<ImmutPath>,
        entry: Option<ImmutPath>,
    ) -> EntryState {
        // todo: formalize untitled path
        // let is_untitled = entry.as_ref().is_some_and(|p| p.starts_with("/untitled"));
        // let root_dir = self.determine_root(if is_untitled { None } else {
        // entry.as_ref() });

        let entry = match (entry, root_dir) {
            // (Some(entry), Some(root)) if is_untitled => Some(EntryState::new_rooted(
            //     root,
            //     Some(FileId::new(None, VirtualPath::new(entry))),
            // )),
            (Some(entry), Some(root)) => match entry.strip_prefix(&root) {
                Ok(stripped) => Some(EntryState::new_rooted(
                    root,
                    Some(VirtualPath::new(stripped)),
                )),
                Err(err) => {
                    log::info!("Entry is not in root directory: err {err:?}: entry: {entry:?}, root: {root:?}");
                    EntryState::new_rooted_by_parent(entry)
                }
            },
            (Some(entry), None) => EntryState::new_rooted_by_parent(entry),
            (None, Some(root)) => Some(EntryState::new_workspace(root)),
            (None, None) => None,
        };

        entry.unwrap_or_else(|| match self.root(None) {
            Some(root) => EntryState::new_workspace(root),
            None => EntryState::new_detached(),
        })
    }

    /// Resolves the directory to store the lock file.
    pub fn resolve_lock(&self, entry: &EntryState) -> Option<ImmutPath> {
        match self.project_resolution {
            ProjectResolutionKind::LockDatabase if entry.is_in_package() => {
                log::info!("ProjectResolver: no lock for package: {entry:?}");
                None
            }
            ProjectResolutionKind::LockDatabase => {
                let root = entry.workspace_root();
                log::info!("ProjectResolver: lock for {entry:?} at {root:?}");

                root
            }
            ProjectResolutionKind::SingleFile => None,
        }
    }

    /// Resolves the default entry path.
    pub fn resolve_default(&self) -> Option<ImmutPath> {
        let entry = self.entry.as_ref();
        // todo: pre-compute this when updating config
        if let Some(entry) = entry {
            if entry.is_relative() {
                let root = self.root(None)?;
                return Some(root.join(entry).as_path().into());
            }
        }
        entry.cloned()
    }

    /// Validates the configuration.
    pub fn validate(&self) -> Result<()> {
        if let Some(root) = &self.root_path {
            if !root.is_absolute() {
                tinymist_l10n::bail!(
                    "tinymist-project.validate-error.root-path-not-absolute",
                    "rootPath or typstExtraArgs.root must be an absolute path: {root:?}",
                    root = root.debug_l10n()
                );
            }
        }

        Ok(())
    }
}

#[cfg(test)]
#[cfg(any(windows, unix, target_os = "macos"))]
mod entry_tests {
    use tinymist_world::vfs::WorkspaceResolver;

    use super::*;
    use std::path::Path;

    #[test]
    fn test_entry_resolution() {
        let root_path = Path::new(if cfg!(windows) { "C:\\root" } else { "/root" });

        let entry = EntryResolver {
            root_path: Some(ImmutPath::from(root_path)),
            ..Default::default()
        };

        let entry = entry.resolve(if cfg!(windows) {
            Some(Path::new("C:\\root\\main.typ").into())
        } else {
            Some(Path::new("/root/main.typ").into())
        });

        assert_eq!(entry.root(), Some(ImmutPath::from(root_path)));
        assert_eq!(
            entry.main(),
            Some(WorkspaceResolver::workspace_file(
                entry.root().as_ref(),
                VirtualPath::new("main.typ")
            ))
        );
    }

    #[test]
    fn test_entry_resolution_multi_root() {
        let root_path = Path::new(if cfg!(windows) { "C:\\root" } else { "/root" });
        let root2_path = Path::new(if cfg!(windows) { "C:\\root2" } else { "/root2" });

        let entry = EntryResolver {
            root_path: Some(ImmutPath::from(root_path)),
            roots: vec![ImmutPath::from(root_path), ImmutPath::from(root2_path)],
            ..Default::default()
        };

        {
            let entry = entry.resolve(if cfg!(windows) {
                Some(Path::new("C:\\root\\main.typ").into())
            } else {
                Some(Path::new("/root/main.typ").into())
            });

            assert_eq!(entry.root(), Some(ImmutPath::from(root_path)));
            assert_eq!(
                entry.main(),
                Some(WorkspaceResolver::workspace_file(
                    entry.root().as_ref(),
                    VirtualPath::new("main.typ")
                ))
            );
        }

        {
            let entry = entry.resolve(if cfg!(windows) {
                Some(Path::new("C:\\root2\\main.typ").into())
            } else {
                Some(Path::new("/root2/main.typ").into())
            });

            assert_eq!(entry.root(), Some(ImmutPath::from(root2_path)));
            assert_eq!(
                entry.main(),
                Some(WorkspaceResolver::workspace_file(
                    entry.root().as_ref(),
                    VirtualPath::new("main.typ")
                ))
            );
        }
    }

    #[test]
    fn test_entry_resolution_default_multi_root() {
        let root_path = Path::new(if cfg!(windows) { "C:\\root" } else { "/root" });
        let root2_path = Path::new(if cfg!(windows) { "C:\\root2" } else { "/root2" });

        let mut entry = EntryResolver {
            root_path: Some(ImmutPath::from(root_path)),
            roots: vec![ImmutPath::from(root_path), ImmutPath::from(root2_path)],
            ..Default::default()
        };

        {
            entry.entry = if cfg!(windows) {
                Some(Path::new("C:\\root\\main.typ").into())
            } else {
                Some(Path::new("/root/main.typ").into())
            };

            let default_entry = entry.resolve_default();

            assert_eq!(default_entry, entry.entry);
        }

        {
            entry.entry = Some(Path::new("main.typ").into());

            let default_entry = entry.resolve_default();

            assert_eq!(
                default_entry,
                if cfg!(windows) {
                    Some(Path::new("C:\\root\\main.typ").into())
                } else {
                    Some(Path::new("/root/main.typ").into())
                }
            );
        }
    }
}