summaryrefslogtreecommitdiff
path: root/support/texlab/src/workspace/children_expand.rs
blob: 8fecf2d50329791e176fb04d3996a36c802d4908 (plain)
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
use std::{path::PathBuf, sync::Arc};

use anyhow::Result;
use notify::RecursiveMode;
use rayon::iter::{IntoParallelIterator, ParallelIterator};

use crate::{
    component_db::COMPONENT_DATABASE, Document, DocumentLanguage, OpenHandler, Uri, Workspace,
    WorkspaceSource, WorkspaceSubset,
};

pub struct ChildrenExpander<W> {
    workspace: Arc<W>,
}

impl<W> ChildrenExpander<W>
where
    W: Workspace + Send + Sync + 'static,
{
    pub fn new(workspace: Arc<W>) -> Self {
        workspace.register_open_handler(Arc::new(move |workspace, document| {
            Self::expand(workspace.as_ref(), document.as_ref());
        }));
        Self { workspace }
    }

    fn expand(workspace: &dyn Workspace, document: &Document) {
        if let Some(data) = document.data.as_latex() {
            let extras = &data.extras;
            let mut all_targets = vec![&extras.implicit_links.aux, &extras.implicit_links.log];
            for link in &extras.explicit_links {
                if link
                    .as_component_name()
                    .and_then(|name| COMPONENT_DATABASE.find(&name))
                    .is_none()
                {
                    all_targets.push(&link.targets);
                }
            }

            all_targets.into_par_iter().for_each(|targets| {
                for path in targets
                    .iter()
                    .filter(|uri| uri.scheme() == "file" && uri.fragment().is_none())
                    .filter_map(|uri| uri.to_file_path().ok())
                {
                    if workspace.load(path).is_ok() {
                        break;
                    }
                }
            });
        }
    }
}

impl<W: Workspace> Workspace for ChildrenExpander<W> {
    fn open(
        &self,
        uri: Arc<Uri>,
        text: String,
        language: DocumentLanguage,
        source: WorkspaceSource,
    ) -> Arc<Document> {
        self.workspace.open(uri, text, language, source)
    }

    fn register_open_handler(&self, handler: OpenHandler) {
        self.workspace.register_open_handler(handler)
    }

    fn documents(&self) -> Vec<Arc<Document>> {
        self.workspace.documents()
    }

    fn has(&self, uri: &Uri) -> bool {
        self.workspace.has(uri)
    }

    fn get(&self, uri: &Uri) -> Option<Arc<Document>> {
        self.workspace.get(uri)
    }

    fn close(&self, uri: &Uri) {
        self.workspace.close(uri)
    }

    fn is_open(&self, uri: &Uri) -> bool {
        self.workspace.is_open(uri)
    }

    fn subset(&self, uri: Arc<Uri>) -> Option<WorkspaceSubset> {
        self.workspace.subset(uri)
    }

    fn watch(&self, path: PathBuf, mode: RecursiveMode) -> Result<()> {
        self.workspace.watch(path, mode)
    }
}