diff options
Diffstat (limited to 'crates/ra_vfs/src/lib.rs')
-rw-r--r-- | crates/ra_vfs/src/lib.rs | 81 |
1 files changed, 32 insertions, 49 deletions
diff --git a/crates/ra_vfs/src/lib.rs b/crates/ra_vfs/src/lib.rs index 1ca94dcd6..889ed788d 100644 --- a/crates/ra_vfs/src/lib.rs +++ b/crates/ra_vfs/src/lib.rs | |||
@@ -60,7 +60,7 @@ impl RootFilter { | |||
60 | } | 60 | } |
61 | } | 61 | } |
62 | 62 | ||
63 | fn has_rs_extension(p: &Path) -> bool { | 63 | pub(crate) fn has_rs_extension(p: &Path) -> bool { |
64 | p.extension() == Some(OsStr::new("rs")) | 64 | p.extension() == Some(OsStr::new("rs")) |
65 | } | 65 | } |
66 | 66 | ||
@@ -98,7 +98,7 @@ impl Vfs { | |||
98 | pub fn new(mut roots: Vec<PathBuf>) -> (Vfs, Vec<VfsRoot>) { | 98 | pub fn new(mut roots: Vec<PathBuf>) -> (Vfs, Vec<VfsRoot>) { |
99 | let (worker, worker_handle) = io::start(); | 99 | let (worker, worker_handle) = io::start(); |
100 | 100 | ||
101 | let watcher = Watcher::start().unwrap(); // TODO return Result? | 101 | let watcher = Watcher::start(worker.inp.clone()).unwrap(); // TODO return Result? |
102 | 102 | ||
103 | let mut res = Vfs { | 103 | let mut res = Vfs { |
104 | roots: Arena::default(), | 104 | roots: Arena::default(), |
@@ -127,7 +127,7 @@ impl Vfs { | |||
127 | nested.iter().all(|it| it != entry.path()) | 127 | nested.iter().all(|it| it != entry.path()) |
128 | } | 128 | } |
129 | }; | 129 | }; |
130 | let task = io::Task { | 130 | let task = io::Task::AddRoot { |
131 | root, | 131 | root, |
132 | path: path.clone(), | 132 | path: path.clone(), |
133 | filter: Box::new(filter), | 133 | filter: Box::new(filter), |
@@ -188,58 +188,43 @@ impl Vfs { | |||
188 | &self.worker.out | 188 | &self.worker.out |
189 | } | 189 | } |
190 | 190 | ||
191 | pub fn change_receiver(&self) -> &Receiver<WatcherChange> { | ||
192 | &self.watcher.change_receiver() | ||
193 | } | ||
194 | |||
195 | pub fn handle_task(&mut self, task: io::TaskResult) { | 191 | pub fn handle_task(&mut self, task: io::TaskResult) { |
196 | let mut files = Vec::new(); | 192 | match task { |
197 | // While we were scanning the root in the backgound, a file might have | 193 | io::TaskResult::AddRoot(task) => { |
198 | // been open in the editor, so we need to account for that. | 194 | let mut files = Vec::new(); |
199 | let exising = self.root2files[&task.root] | 195 | // While we were scanning the root in the backgound, a file might have |
200 | .iter() | 196 | // been open in the editor, so we need to account for that. |
201 | .map(|&file| (self.files[file].path.clone(), file)) | 197 | let exising = self.root2files[&task.root] |
202 | .collect::<FxHashMap<_, _>>(); | 198 | .iter() |
203 | for (path, text) in task.files { | 199 | .map(|&file| (self.files[file].path.clone(), file)) |
204 | if let Some(&file) = exising.get(&path) { | 200 | .collect::<FxHashMap<_, _>>(); |
205 | let text = Arc::clone(&self.files[file].text); | 201 | for (path, text) in task.files { |
206 | files.push((file, path, text)); | 202 | if let Some(&file) = exising.get(&path) { |
207 | continue; | 203 | let text = Arc::clone(&self.files[file].text); |
208 | } | 204 | files.push((file, path, text)); |
209 | let text = Arc::new(text); | 205 | continue; |
210 | let file = self.add_file(task.root, path.clone(), Arc::clone(&text)); | 206 | } |
211 | files.push((file, path, text)); | 207 | let text = Arc::new(text); |
212 | } | 208 | let file = self.add_file(task.root, path.clone(), Arc::clone(&text)); |
213 | 209 | files.push((file, path, text)); | |
214 | let change = VfsChange::AddRoot { | 210 | } |
215 | root: task.root, | ||
216 | files, | ||
217 | }; | ||
218 | self.pending_changes.push(change); | ||
219 | } | ||
220 | 211 | ||
221 | pub fn handle_change(&mut self, change: WatcherChange) { | 212 | let change = VfsChange::AddRoot { |
222 | match change { | 213 | root: task.root, |
223 | WatcherChange::Create(path) => { | 214 | files, |
224 | self.add_file_overlay(&path, None); | 215 | }; |
225 | } | 216 | self.pending_changes.push(change); |
226 | WatcherChange::Remove(path) => { | ||
227 | self.remove_file_overlay(&path); | ||
228 | } | ||
229 | WatcherChange::Rename(src, dst) => { | ||
230 | self.remove_file_overlay(&src); | ||
231 | self.add_file_overlay(&dst, None); | ||
232 | } | 217 | } |
233 | WatcherChange::Write(path) => { | 218 | io::TaskResult::WatcherChange(change) => { |
234 | self.change_file_overlay(&path, None); | 219 | // TODO |
220 | unimplemented!() | ||
235 | } | 221 | } |
236 | } | 222 | } |
237 | } | 223 | } |
238 | 224 | ||
239 | pub fn add_file_overlay(&mut self, path: &Path, text: Option<String>) -> Option<VfsFile> { | 225 | pub fn add_file_overlay(&mut self, path: &Path, text: String) -> Option<VfsFile> { |
240 | let mut res = None; | 226 | let mut res = None; |
241 | if let Some((root, rel_path, file)) = self.find_root(path) { | 227 | if let Some((root, rel_path, file)) = self.find_root(path) { |
242 | let text = text.unwrap_or_else(|| fs::read_to_string(&path).unwrap_or_default()); | ||
243 | let text = Arc::new(text); | 228 | let text = Arc::new(text); |
244 | let change = if let Some(file) = file { | 229 | let change = if let Some(file) = file { |
245 | res = Some(file); | 230 | res = Some(file); |
@@ -260,10 +245,8 @@ impl Vfs { | |||
260 | res | 245 | res |
261 | } | 246 | } |
262 | 247 | ||
263 | pub fn change_file_overlay(&mut self, path: &Path, new_text: Option<String>) { | 248 | pub fn change_file_overlay(&mut self, path: &Path, new_text: String) { |
264 | if let Some((_root, _path, file)) = self.find_root(path) { | 249 | if let Some((_root, _path, file)) = self.find_root(path) { |
265 | let new_text = | ||
266 | new_text.unwrap_or_else(|| fs::read_to_string(&path).unwrap_or_default()); | ||
267 | let file = file.expect("can't change a file which wasn't added"); | 250 | let file = file.expect("can't change a file which wasn't added"); |
268 | let text = Arc::new(new_text); | 251 | let text = Arc::new(new_text); |
269 | self.change_file(file, Arc::clone(&text)); | 252 | self.change_file(file, Arc::clone(&text)); |