]> git.lizzy.rs Git - rust.git/blob - src/tools/rust-analyzer/crates/vfs-notify/src/lib.rs
Auto merge of #99529 - Milo123459:stage-1-test, r=jyn514
[rust.git] / src / tools / rust-analyzer / crates / vfs-notify / src / lib.rs
1 //! An implementation of `loader::Handle`, based on `walkdir` and `notify`.
2 //!
3 //! The file watching bits here are untested and quite probably buggy. For this
4 //! reason, by default we don't watch files and rely on editor's file watching
5 //! capabilities.
6 //!
7 //! Hopefully, one day a reliable file watching/walking crate appears on
8 //! crates.io, and we can reduce this to trivial glue code.
9
10 #![warn(rust_2018_idioms, unused_lifetimes, semicolon_in_expressions_from_macros)]
11
12 use std::fs;
13
14 use crossbeam_channel::{never, select, unbounded, Receiver, Sender};
15 use notify::{RecommendedWatcher, RecursiveMode, Watcher};
16 use paths::{AbsPath, AbsPathBuf};
17 use vfs::loader;
18 use walkdir::WalkDir;
19
20 #[derive(Debug)]
21 pub struct NotifyHandle {
22     // Relative order of fields below is significant.
23     sender: Sender<Message>,
24     _thread: jod_thread::JoinHandle,
25 }
26
27 #[derive(Debug)]
28 enum Message {
29     Config(loader::Config),
30     Invalidate(AbsPathBuf),
31 }
32
33 impl loader::Handle for NotifyHandle {
34     fn spawn(sender: loader::Sender) -> NotifyHandle {
35         let actor = NotifyActor::new(sender);
36         let (sender, receiver) = unbounded::<Message>();
37         let thread = jod_thread::Builder::new()
38             .name("VfsLoader".to_owned())
39             .spawn(move || actor.run(receiver))
40             .expect("failed to spawn thread");
41         NotifyHandle { sender, _thread: thread }
42     }
43     fn set_config(&mut self, config: loader::Config) {
44         self.sender.send(Message::Config(config)).unwrap();
45     }
46     fn invalidate(&mut self, path: AbsPathBuf) {
47         self.sender.send(Message::Invalidate(path)).unwrap();
48     }
49     fn load_sync(&mut self, path: &AbsPath) -> Option<Vec<u8>> {
50         read(path)
51     }
52 }
53
54 type NotifyEvent = notify::Result<notify::Event>;
55
56 struct NotifyActor {
57     sender: loader::Sender,
58     watched_entries: Vec<loader::Entry>,
59     // Drop order is significant.
60     watcher: Option<(RecommendedWatcher, Receiver<NotifyEvent>)>,
61 }
62
63 #[derive(Debug)]
64 enum Event {
65     Message(Message),
66     NotifyEvent(NotifyEvent),
67 }
68
69 impl NotifyActor {
70     fn new(sender: loader::Sender) -> NotifyActor {
71         NotifyActor { sender, watched_entries: Vec::new(), watcher: None }
72     }
73     fn next_event(&self, receiver: &Receiver<Message>) -> Option<Event> {
74         let watcher_receiver = self.watcher.as_ref().map(|(_, receiver)| receiver);
75         select! {
76             recv(receiver) -> it => it.ok().map(Event::Message),
77             recv(watcher_receiver.unwrap_or(&never())) -> it => Some(Event::NotifyEvent(it.unwrap())),
78         }
79     }
80     fn run(mut self, inbox: Receiver<Message>) {
81         while let Some(event) = self.next_event(&inbox) {
82             tracing::debug!("vfs-notify event: {:?}", event);
83             match event {
84                 Event::Message(msg) => match msg {
85                     Message::Config(config) => {
86                         self.watcher = None;
87                         if !config.watch.is_empty() {
88                             let (watcher_sender, watcher_receiver) = unbounded();
89                             let watcher = log_notify_error(RecommendedWatcher::new(move |event| {
90                                 watcher_sender.send(event).unwrap();
91                             }));
92                             self.watcher = watcher.map(|it| (it, watcher_receiver));
93                         }
94
95                         let config_version = config.version;
96
97                         let n_total = config.load.len();
98                         self.send(loader::Message::Progress { n_total, n_done: 0, config_version });
99
100                         self.watched_entries.clear();
101
102                         for (i, entry) in config.load.into_iter().enumerate() {
103                             let watch = config.watch.contains(&i);
104                             if watch {
105                                 self.watched_entries.push(entry.clone());
106                             }
107                             let files = self.load_entry(entry, watch);
108                             self.send(loader::Message::Loaded { files });
109                             self.send(loader::Message::Progress {
110                                 n_total,
111                                 n_done: i + 1,
112                                 config_version,
113                             });
114                         }
115                     }
116                     Message::Invalidate(path) => {
117                         let contents = read(path.as_path());
118                         let files = vec![(path, contents)];
119                         self.send(loader::Message::Loaded { files });
120                     }
121                 },
122                 Event::NotifyEvent(event) => {
123                     if let Some(event) = log_notify_error(event) {
124                         let files = event
125                             .paths
126                             .into_iter()
127                             .map(|path| AbsPathBuf::try_from(path).unwrap())
128                             .filter_map(|path| {
129                                 let meta = fs::metadata(&path).ok()?;
130                                 if meta.file_type().is_dir()
131                                     && self
132                                         .watched_entries
133                                         .iter()
134                                         .any(|entry| entry.contains_dir(&path))
135                                 {
136                                     self.watch(path);
137                                     return None;
138                                 }
139
140                                 if !meta.file_type().is_file() {
141                                     return None;
142                                 }
143                                 if !self
144                                     .watched_entries
145                                     .iter()
146                                     .any(|entry| entry.contains_file(&path))
147                                 {
148                                     return None;
149                                 }
150
151                                 let contents = read(&path);
152                                 Some((path, contents))
153                             })
154                             .collect();
155                         self.send(loader::Message::Loaded { files });
156                     }
157                 }
158             }
159         }
160     }
161     fn load_entry(
162         &mut self,
163         entry: loader::Entry,
164         watch: bool,
165     ) -> Vec<(AbsPathBuf, Option<Vec<u8>>)> {
166         match entry {
167             loader::Entry::Files(files) => files
168                 .into_iter()
169                 .map(|file| {
170                     if watch {
171                         self.watch(file.clone());
172                     }
173                     let contents = read(file.as_path());
174                     (file, contents)
175                 })
176                 .collect::<Vec<_>>(),
177             loader::Entry::Directories(dirs) => {
178                 let mut res = Vec::new();
179
180                 for root in &dirs.include {
181                     let walkdir =
182                         WalkDir::new(root).follow_links(true).into_iter().filter_entry(|entry| {
183                             if !entry.file_type().is_dir() {
184                                 return true;
185                             }
186                             let path = AbsPath::assert(entry.path());
187                             root == path
188                                 || dirs.exclude.iter().chain(&dirs.include).all(|it| it != path)
189                         });
190
191                     let files = walkdir.filter_map(|it| it.ok()).filter_map(|entry| {
192                         let is_dir = entry.file_type().is_dir();
193                         let is_file = entry.file_type().is_file();
194                         let abs_path = AbsPathBuf::assert(entry.into_path());
195                         if is_dir && watch {
196                             self.watch(abs_path.clone());
197                         }
198                         if !is_file {
199                             return None;
200                         }
201                         let ext = abs_path.extension().unwrap_or_default();
202                         if dirs.extensions.iter().all(|it| it.as_str() != ext) {
203                             return None;
204                         }
205                         Some(abs_path)
206                     });
207
208                     res.extend(files.map(|file| {
209                         let contents = read(file.as_path());
210                         (file, contents)
211                     }));
212                 }
213                 res
214             }
215         }
216     }
217
218     fn watch(&mut self, path: AbsPathBuf) {
219         if let Some((watcher, _)) = &mut self.watcher {
220             log_notify_error(watcher.watch(path.as_ref(), RecursiveMode::NonRecursive));
221         }
222     }
223     fn send(&mut self, msg: loader::Message) {
224         (self.sender)(msg);
225     }
226 }
227
228 fn read(path: &AbsPath) -> Option<Vec<u8>> {
229     std::fs::read(path).ok()
230 }
231
232 fn log_notify_error<T>(res: notify::Result<T>) -> Option<T> {
233     res.map_err(|err| tracing::warn!("notify error: {}", err)).ok()
234 }