summaryrefslogtreecommitdiff
path: root/support/texlab/src/workspace/watch.rs
blob: 655872f6032a27f98e6e0d6718c6f9eafe902388 (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
99
100
101
102
103
104
105
106
107
use std::{
    path::PathBuf,
    sync::{Arc, Mutex},
};

use anyhow::Result;
use log::warn;
use notify::{RecommendedWatcher, RecursiveMode, Watcher};
use rustc_hash::FxHashSet;

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

pub struct DocumentWatcher<W> {
    workspace: Arc<W>,
    watcher: Mutex<RecommendedWatcher>,
    watched_paths: Mutex<FxHashSet<PathBuf>>,
}

impl<W> DocumentWatcher<W>
where
    W: Workspace + Send + Sync + 'static,
{
    pub fn new(workspace: Arc<W>) -> Result<Self> {
        let watcher = Self::create_watcher(Arc::clone(&workspace))?;
        Ok(Self {
            workspace,
            watcher: Mutex::new(watcher),
            watched_paths: Mutex::default(),
        })
    }

    fn create_watcher(workspace: Arc<W>) -> Result<RecommendedWatcher> {
        let watcher = Watcher::new_immediate(move |event: notify::Result<notify::Event>| {
            if let Ok(event) = event {
                if event.kind.is_modify() {
                    for path in event.paths {
                        let _ = workspace.reload(path);
                    }
                }
            }
        })?;
        Ok(watcher)
    }
}

impl<W: Workspace> Workspace for DocumentWatcher<W> {
    fn open(
        &self,
        uri: Arc<Uri>,
        text: String,
        language: DocumentLanguage,
        source: WorkspaceSource,
    ) -> Arc<Document> {
        let document = self.workspace.open(uri, text, language, source);
        if document.uri.scheme() == "file" {
            if let Ok(mut path) = document.uri.to_file_path() {
                path.pop();
                let mut watched_paths = self.watched_paths.lock().unwrap();
                if !watched_paths.contains(&path) {
                    if let Err(why) = self
                        .watcher
                        .lock()
                        .unwrap()
                        .watch(&path, RecursiveMode::NonRecursive)
                    {
                        warn!(
                            "Failed to watch folder of document \"{}\": {}",
                            document.uri, why
                        );
                    }
                    watched_paths.insert(path);
                }
            }
        }
        document
    }

    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)
    }
}