1use super::{
2 fs::{self, Fs},
3 ignore::IgnoreStack,
4 DiagnosticSummary,
5};
6use ::ignore::gitignore::{Gitignore, GitignoreBuilder};
7use anyhow::{anyhow, Context, Result};
8use client::{proto, Client, PeerId, TypedEnvelope, UserStore};
9use clock::ReplicaId;
10use collections::{hash_map, HashMap, HashSet};
11use futures::{Stream, StreamExt};
12use fuzzy::CharBag;
13use gpui::{
14 executor, AppContext, AsyncAppContext, Entity, ModelContext, ModelHandle, MutableAppContext,
15 Task, UpgradeModelHandle, WeakModelHandle,
16};
17use language::{
18 range_from_lsp, Buffer, Diagnostic, DiagnosticEntry, DiagnosticSeverity, File as _, Language,
19 LanguageRegistry, Operation, PointUtf16, Rope,
20};
21use lazy_static::lazy_static;
22use lsp::LanguageServer;
23use parking_lot::Mutex;
24use postage::{
25 prelude::{Sink as _, Stream as _},
26 watch,
27};
28use serde::Deserialize;
29use smol::channel::{self, Sender};
30use std::{
31 any::Any,
32 cmp::{self, Ordering},
33 convert::{TryFrom, TryInto},
34 ffi::{OsStr, OsString},
35 fmt,
36 future::Future,
37 ops::Deref,
38 path::{Path, PathBuf},
39 sync::{
40 atomic::{AtomicUsize, Ordering::SeqCst},
41 Arc,
42 },
43 time::{Duration, SystemTime},
44};
45use sum_tree::{Bias, TreeMap};
46use sum_tree::{Edit, SeekTarget, SumTree};
47use util::{post_inc, ResultExt, TryFutureExt};
48
49lazy_static! {
50 static ref GITIGNORE: &'static OsStr = OsStr::new(".gitignore");
51}
52
53#[derive(Clone, Debug)]
54enum ScanState {
55 Idle,
56 Scanning,
57 Err(Arc<anyhow::Error>),
58}
59
60#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash, PartialOrd, Ord)]
61pub struct WorktreeId(usize);
62
63pub enum Worktree {
64 Local(LocalWorktree),
65 Remote(RemoteWorktree),
66}
67
68#[derive(Clone, Debug, Eq, PartialEq)]
69pub enum Event {
70 DiskBasedDiagnosticsUpdating,
71 DiskBasedDiagnosticsUpdated,
72 DiagnosticsUpdated(Arc<Path>),
73}
74
75impl Entity for Worktree {
76 type Event = Event;
77
78 fn app_will_quit(
79 &mut self,
80 _: &mut MutableAppContext,
81 ) -> Option<std::pin::Pin<Box<dyn 'static + Future<Output = ()>>>> {
82 use futures::FutureExt;
83
84 if let Self::Local(worktree) = self {
85 let shutdown_futures = worktree
86 .language_servers
87 .drain()
88 .filter_map(|(_, server)| server.shutdown())
89 .collect::<Vec<_>>();
90 Some(
91 async move {
92 futures::future::join_all(shutdown_futures).await;
93 }
94 .boxed(),
95 )
96 } else {
97 None
98 }
99 }
100}
101
102impl Worktree {
103 pub async fn open_local(
104 client: Arc<Client>,
105 user_store: ModelHandle<UserStore>,
106 path: impl Into<Arc<Path>>,
107 fs: Arc<dyn Fs>,
108 languages: Arc<LanguageRegistry>,
109 cx: &mut AsyncAppContext,
110 ) -> Result<ModelHandle<Self>> {
111 let (tree, scan_states_tx) =
112 LocalWorktree::new(client, user_store, path, fs.clone(), languages, cx).await?;
113 tree.update(cx, |tree, cx| {
114 let tree = tree.as_local_mut().unwrap();
115 let abs_path = tree.snapshot.abs_path.clone();
116 let background_snapshot = tree.background_snapshot.clone();
117 let background = cx.background().clone();
118 tree._background_scanner_task = Some(cx.background().spawn(async move {
119 let events = fs.watch(&abs_path, Duration::from_millis(100)).await;
120 let scanner =
121 BackgroundScanner::new(background_snapshot, scan_states_tx, fs, background);
122 scanner.run(events).await;
123 }));
124 });
125 Ok(tree)
126 }
127
128 pub async fn remote(
129 project_remote_id: u64,
130 replica_id: ReplicaId,
131 worktree: proto::Worktree,
132 client: Arc<Client>,
133 user_store: ModelHandle<UserStore>,
134 languages: Arc<LanguageRegistry>,
135 cx: &mut AsyncAppContext,
136 ) -> Result<ModelHandle<Self>> {
137 let remote_id = worktree.id;
138 let root_char_bag: CharBag = worktree
139 .root_name
140 .chars()
141 .map(|c| c.to_ascii_lowercase())
142 .collect();
143 let root_name = worktree.root_name.clone();
144 let (entries_by_path, entries_by_id, diagnostic_summaries) = cx
145 .background()
146 .spawn(async move {
147 let mut entries_by_path_edits = Vec::new();
148 let mut entries_by_id_edits = Vec::new();
149 for entry in worktree.entries {
150 match Entry::try_from((&root_char_bag, entry)) {
151 Ok(entry) => {
152 entries_by_id_edits.push(Edit::Insert(PathEntry {
153 id: entry.id,
154 path: entry.path.clone(),
155 is_ignored: entry.is_ignored,
156 scan_id: 0,
157 }));
158 entries_by_path_edits.push(Edit::Insert(entry));
159 }
160 Err(err) => log::warn!("error for remote worktree entry {:?}", err),
161 }
162 }
163
164 let mut entries_by_path = SumTree::new();
165 let mut entries_by_id = SumTree::new();
166 entries_by_path.edit(entries_by_path_edits, &());
167 entries_by_id.edit(entries_by_id_edits, &());
168
169 let diagnostic_summaries = TreeMap::from_ordered_entries(
170 worktree.diagnostic_summaries.into_iter().map(|summary| {
171 (
172 PathKey(PathBuf::from(summary.path).into()),
173 DiagnosticSummary {
174 error_count: summary.error_count as usize,
175 warning_count: summary.warning_count as usize,
176 info_count: summary.info_count as usize,
177 hint_count: summary.hint_count as usize,
178 },
179 )
180 }),
181 );
182
183 (entries_by_path, entries_by_id, diagnostic_summaries)
184 })
185 .await;
186
187 let worktree = cx.update(|cx| {
188 cx.add_model(|cx: &mut ModelContext<Worktree>| {
189 let snapshot = Snapshot {
190 id: WorktreeId(remote_id as usize),
191 scan_id: 0,
192 abs_path: Path::new("").into(),
193 root_name,
194 root_char_bag,
195 ignores: Default::default(),
196 entries_by_path,
197 entries_by_id,
198 removed_entry_ids: Default::default(),
199 next_entry_id: Default::default(),
200 };
201
202 let (updates_tx, mut updates_rx) = postage::mpsc::channel(64);
203 let (mut snapshot_tx, snapshot_rx) = watch::channel_with(snapshot.clone());
204
205 cx.background()
206 .spawn(async move {
207 while let Some(update) = updates_rx.recv().await {
208 let mut snapshot = snapshot_tx.borrow().clone();
209 if let Err(error) = snapshot.apply_update(update) {
210 log::error!("error applying worktree update: {}", error);
211 }
212 *snapshot_tx.borrow_mut() = snapshot;
213 }
214 })
215 .detach();
216
217 {
218 let mut snapshot_rx = snapshot_rx.clone();
219 cx.spawn_weak(|this, mut cx| async move {
220 while let Some(_) = snapshot_rx.recv().await {
221 if let Some(this) = cx.read(|cx| this.upgrade(cx)) {
222 this.update(&mut cx, |this, cx| this.poll_snapshot(cx));
223 } else {
224 break;
225 }
226 }
227 })
228 .detach();
229 }
230
231 Worktree::Remote(RemoteWorktree {
232 project_id: project_remote_id,
233 replica_id,
234 snapshot,
235 snapshot_rx,
236 updates_tx,
237 client: client.clone(),
238 loading_buffers: Default::default(),
239 open_buffers: Default::default(),
240 queued_operations: Default::default(),
241 languages,
242 user_store,
243 diagnostic_summaries,
244 })
245 })
246 });
247
248 Ok(worktree)
249 }
250
251 pub fn as_local(&self) -> Option<&LocalWorktree> {
252 if let Worktree::Local(worktree) = self {
253 Some(worktree)
254 } else {
255 None
256 }
257 }
258
259 pub fn as_remote(&self) -> Option<&RemoteWorktree> {
260 if let Worktree::Remote(worktree) = self {
261 Some(worktree)
262 } else {
263 None
264 }
265 }
266
267 pub fn as_local_mut(&mut self) -> Option<&mut LocalWorktree> {
268 if let Worktree::Local(worktree) = self {
269 Some(worktree)
270 } else {
271 None
272 }
273 }
274
275 pub fn as_remote_mut(&mut self) -> Option<&mut RemoteWorktree> {
276 if let Worktree::Remote(worktree) = self {
277 Some(worktree)
278 } else {
279 None
280 }
281 }
282
283 pub fn snapshot(&self) -> Snapshot {
284 match self {
285 Worktree::Local(worktree) => worktree.snapshot(),
286 Worktree::Remote(worktree) => worktree.snapshot(),
287 }
288 }
289
290 pub fn replica_id(&self) -> ReplicaId {
291 match self {
292 Worktree::Local(_) => 0,
293 Worktree::Remote(worktree) => worktree.replica_id,
294 }
295 }
296
297 pub fn remove_collaborator(
298 &mut self,
299 peer_id: PeerId,
300 replica_id: ReplicaId,
301 cx: &mut ModelContext<Self>,
302 ) {
303 match self {
304 Worktree::Local(worktree) => worktree.remove_collaborator(peer_id, replica_id, cx),
305 Worktree::Remote(worktree) => worktree.remove_collaborator(replica_id, cx),
306 }
307 }
308
309 pub fn languages(&self) -> &Arc<LanguageRegistry> {
310 match self {
311 Worktree::Local(worktree) => &worktree.language_registry,
312 Worktree::Remote(worktree) => &worktree.languages,
313 }
314 }
315
316 pub fn user_store(&self) -> &ModelHandle<UserStore> {
317 match self {
318 Worktree::Local(worktree) => &worktree.user_store,
319 Worktree::Remote(worktree) => &worktree.user_store,
320 }
321 }
322
323 pub fn handle_open_buffer(
324 &mut self,
325 envelope: TypedEnvelope<proto::OpenBuffer>,
326 rpc: Arc<Client>,
327 cx: &mut ModelContext<Self>,
328 ) -> anyhow::Result<()> {
329 let receipt = envelope.receipt();
330
331 let response = self
332 .as_local_mut()
333 .unwrap()
334 .open_remote_buffer(envelope, cx);
335
336 cx.background()
337 .spawn(
338 async move {
339 rpc.respond(receipt, response.await?).await?;
340 Ok(())
341 }
342 .log_err(),
343 )
344 .detach();
345
346 Ok(())
347 }
348
349 pub fn handle_close_buffer(
350 &mut self,
351 envelope: TypedEnvelope<proto::CloseBuffer>,
352 _: Arc<Client>,
353 cx: &mut ModelContext<Self>,
354 ) -> anyhow::Result<()> {
355 self.as_local_mut()
356 .unwrap()
357 .close_remote_buffer(envelope, cx)
358 }
359
360 pub fn diagnostic_summaries<'a>(
361 &'a self,
362 ) -> impl Iterator<Item = (Arc<Path>, DiagnosticSummary)> + 'a {
363 match self {
364 Worktree::Local(worktree) => &worktree.diagnostic_summaries,
365 Worktree::Remote(worktree) => &worktree.diagnostic_summaries,
366 }
367 .iter()
368 .map(|(path, summary)| (path.0.clone(), summary.clone()))
369 }
370
371 pub fn loading_buffers<'a>(&'a mut self) -> &'a mut LoadingBuffers {
372 match self {
373 Worktree::Local(worktree) => &mut worktree.loading_buffers,
374 Worktree::Remote(worktree) => &mut worktree.loading_buffers,
375 }
376 }
377
378 pub fn open_buffer(
379 &mut self,
380 path: impl AsRef<Path>,
381 cx: &mut ModelContext<Self>,
382 ) -> Task<Result<ModelHandle<Buffer>>> {
383 let path = path.as_ref();
384
385 // If there is already a buffer for the given path, then return it.
386 let existing_buffer = match self {
387 Worktree::Local(worktree) => worktree.get_open_buffer(path, cx),
388 Worktree::Remote(worktree) => worktree.get_open_buffer(path, cx),
389 };
390 if let Some(existing_buffer) = existing_buffer {
391 return cx.spawn(move |_, _| async move { Ok(existing_buffer) });
392 }
393
394 let path: Arc<Path> = Arc::from(path);
395 let mut loading_watch = match self.loading_buffers().entry(path.clone()) {
396 // If the given path is already being loaded, then wait for that existing
397 // task to complete and return the same buffer.
398 hash_map::Entry::Occupied(e) => e.get().clone(),
399
400 // Otherwise, record the fact that this path is now being loaded.
401 hash_map::Entry::Vacant(entry) => {
402 let (mut tx, rx) = postage::watch::channel();
403 entry.insert(rx.clone());
404
405 let load_buffer = match self {
406 Worktree::Local(worktree) => worktree.open_buffer(&path, cx),
407 Worktree::Remote(worktree) => worktree.open_buffer(&path, cx),
408 };
409 cx.spawn(move |this, mut cx| async move {
410 let result = load_buffer.await;
411
412 // After the buffer loads, record the fact that it is no longer
413 // loading.
414 this.update(&mut cx, |this, _| this.loading_buffers().remove(&path));
415 *tx.borrow_mut() = Some(result.map_err(|e| Arc::new(e)));
416 })
417 .detach();
418 rx
419 }
420 };
421
422 cx.spawn(|_, _| async move {
423 loop {
424 if let Some(result) = loading_watch.borrow().as_ref() {
425 return result.clone().map_err(|e| anyhow!("{}", e));
426 }
427 loading_watch.recv().await;
428 }
429 })
430 }
431
432 #[cfg(feature = "test-support")]
433 pub fn has_open_buffer(&self, path: impl AsRef<Path>, cx: &AppContext) -> bool {
434 let mut open_buffers: Box<dyn Iterator<Item = _>> = match self {
435 Worktree::Local(worktree) => Box::new(worktree.open_buffers.values()),
436 Worktree::Remote(worktree) => {
437 Box::new(worktree.open_buffers.values().filter_map(|buf| {
438 if let RemoteBuffer::Loaded(buf) = buf {
439 Some(buf)
440 } else {
441 None
442 }
443 }))
444 }
445 };
446
447 let path = path.as_ref();
448 open_buffers
449 .find(|buffer| {
450 if let Some(file) = buffer.upgrade(cx).and_then(|buffer| buffer.read(cx).file()) {
451 file.path().as_ref() == path
452 } else {
453 false
454 }
455 })
456 .is_some()
457 }
458
459 pub fn handle_update_buffer(
460 &mut self,
461 envelope: TypedEnvelope<proto::UpdateBuffer>,
462 cx: &mut ModelContext<Self>,
463 ) -> Result<()> {
464 let payload = envelope.payload.clone();
465 let buffer_id = payload.buffer_id as usize;
466 let ops = payload
467 .operations
468 .into_iter()
469 .map(|op| language::proto::deserialize_operation(op))
470 .collect::<Result<Vec<_>, _>>()?;
471
472 match self {
473 Worktree::Local(worktree) => {
474 let buffer = worktree
475 .open_buffers
476 .get(&buffer_id)
477 .and_then(|buf| buf.upgrade(cx))
478 .ok_or_else(|| {
479 anyhow!("invalid buffer {} in update buffer message", buffer_id)
480 })?;
481 buffer.update(cx, |buffer, cx| buffer.apply_ops(ops, cx))?;
482 }
483 Worktree::Remote(worktree) => match worktree.open_buffers.get_mut(&buffer_id) {
484 Some(RemoteBuffer::Operations(pending_ops)) => pending_ops.extend(ops),
485 Some(RemoteBuffer::Loaded(buffer)) => {
486 if let Some(buffer) = buffer.upgrade(cx) {
487 buffer.update(cx, |buffer, cx| buffer.apply_ops(ops, cx))?;
488 } else {
489 worktree
490 .open_buffers
491 .insert(buffer_id, RemoteBuffer::Operations(ops));
492 }
493 }
494 None => {
495 worktree
496 .open_buffers
497 .insert(buffer_id, RemoteBuffer::Operations(ops));
498 }
499 },
500 }
501
502 Ok(())
503 }
504
505 pub fn handle_save_buffer(
506 &mut self,
507 envelope: TypedEnvelope<proto::SaveBuffer>,
508 rpc: Arc<Client>,
509 cx: &mut ModelContext<Self>,
510 ) -> Result<()> {
511 let sender_id = envelope.original_sender_id()?;
512 let this = self.as_local().unwrap();
513 let project_id = this
514 .share
515 .as_ref()
516 .ok_or_else(|| anyhow!("can't save buffer while disconnected"))?
517 .project_id;
518
519 let buffer = this
520 .shared_buffers
521 .get(&sender_id)
522 .and_then(|shared_buffers| shared_buffers.get(&envelope.payload.buffer_id).cloned())
523 .ok_or_else(|| anyhow!("unknown buffer id {}", envelope.payload.buffer_id))?;
524
525 let receipt = envelope.receipt();
526 let worktree_id = envelope.payload.worktree_id;
527 let buffer_id = envelope.payload.buffer_id;
528 let save = cx.spawn(|_, mut cx| async move {
529 buffer.update(&mut cx, |buffer, cx| buffer.save(cx))?.await
530 });
531
532 cx.background()
533 .spawn(
534 async move {
535 let (version, mtime) = save.await?;
536
537 rpc.respond(
538 receipt,
539 proto::BufferSaved {
540 project_id,
541 worktree_id,
542 buffer_id,
543 version: (&version).into(),
544 mtime: Some(mtime.into()),
545 },
546 )
547 .await?;
548
549 Ok(())
550 }
551 .log_err(),
552 )
553 .detach();
554
555 Ok(())
556 }
557
558 pub fn handle_buffer_saved(
559 &mut self,
560 envelope: TypedEnvelope<proto::BufferSaved>,
561 cx: &mut ModelContext<Self>,
562 ) -> Result<()> {
563 let payload = envelope.payload.clone();
564 let worktree = self.as_remote_mut().unwrap();
565 if let Some(buffer) = worktree
566 .open_buffers
567 .get(&(payload.buffer_id as usize))
568 .and_then(|buf| buf.upgrade(cx))
569 {
570 buffer.update(cx, |buffer, cx| {
571 let version = payload.version.try_into()?;
572 let mtime = payload
573 .mtime
574 .ok_or_else(|| anyhow!("missing mtime"))?
575 .into();
576 buffer.did_save(version, mtime, None, cx);
577 Result::<_, anyhow::Error>::Ok(())
578 })?;
579 }
580 Ok(())
581 }
582
583 pub fn handle_format_buffer(
584 &mut self,
585 envelope: TypedEnvelope<proto::FormatBuffer>,
586 rpc: Arc<Client>,
587 cx: &mut ModelContext<Self>,
588 ) -> Result<()> {
589 let sender_id = envelope.original_sender_id()?;
590 let this = self.as_local().unwrap();
591 let buffer = this
592 .shared_buffers
593 .get(&sender_id)
594 .and_then(|shared_buffers| shared_buffers.get(&envelope.payload.buffer_id).cloned())
595 .ok_or_else(|| anyhow!("unknown buffer id {}", envelope.payload.buffer_id))?;
596
597 let receipt = envelope.receipt();
598 cx.spawn(|_, mut cx| async move {
599 let format = buffer.update(&mut cx, |buffer, cx| buffer.format(cx)).await;
600 // We spawn here in order to enqueue the sending of `Ack` *after* transmission of edits
601 // associated with formatting.
602 cx.spawn(|_| async move {
603 match format {
604 Ok(()) => rpc.respond(receipt, proto::Ack {}).await?,
605 Err(error) => {
606 rpc.respond_with_error(
607 receipt,
608 proto::Error {
609 message: error.to_string(),
610 },
611 )
612 .await?
613 }
614 }
615 Ok::<_, anyhow::Error>(())
616 })
617 .await
618 .log_err();
619 })
620 .detach();
621
622 Ok(())
623 }
624
625 fn poll_snapshot(&mut self, cx: &mut ModelContext<Self>) {
626 match self {
627 Self::Local(worktree) => {
628 let is_fake_fs = worktree.fs.is_fake();
629 worktree.snapshot = worktree.background_snapshot.lock().clone();
630 if worktree.is_scanning() {
631 if worktree.poll_task.is_none() {
632 worktree.poll_task = Some(cx.spawn(|this, mut cx| async move {
633 if is_fake_fs {
634 smol::future::yield_now().await;
635 } else {
636 smol::Timer::after(Duration::from_millis(100)).await;
637 }
638 this.update(&mut cx, |this, cx| {
639 this.as_local_mut().unwrap().poll_task = None;
640 this.poll_snapshot(cx);
641 })
642 }));
643 }
644 } else {
645 worktree.poll_task.take();
646 self.update_open_buffers(cx);
647 }
648 }
649 Self::Remote(worktree) => {
650 worktree.snapshot = worktree.snapshot_rx.borrow().clone();
651 self.update_open_buffers(cx);
652 }
653 };
654
655 cx.notify();
656 }
657
658 fn update_open_buffers(&mut self, cx: &mut ModelContext<Self>) {
659 let open_buffers: Box<dyn Iterator<Item = _>> = match &self {
660 Self::Local(worktree) => Box::new(worktree.open_buffers.iter()),
661 Self::Remote(worktree) => {
662 Box::new(worktree.open_buffers.iter().filter_map(|(id, buf)| {
663 if let RemoteBuffer::Loaded(buf) = buf {
664 Some((id, buf))
665 } else {
666 None
667 }
668 }))
669 }
670 };
671
672 let local = self.as_local().is_some();
673 let worktree_path = self.abs_path.clone();
674 let worktree_handle = cx.handle();
675 let mut buffers_to_delete = Vec::new();
676 for (buffer_id, buffer) in open_buffers {
677 if let Some(buffer) = buffer.upgrade(cx) {
678 buffer.update(cx, |buffer, cx| {
679 if let Some(old_file) = File::from_dyn(buffer.file()) {
680 let new_file = if let Some(entry) = old_file
681 .entry_id
682 .and_then(|entry_id| self.entry_for_id(entry_id))
683 {
684 File {
685 is_local: local,
686 worktree_path: worktree_path.clone(),
687 entry_id: Some(entry.id),
688 mtime: entry.mtime,
689 path: entry.path.clone(),
690 worktree: worktree_handle.clone(),
691 }
692 } else if let Some(entry) = self.entry_for_path(old_file.path().as_ref()) {
693 File {
694 is_local: local,
695 worktree_path: worktree_path.clone(),
696 entry_id: Some(entry.id),
697 mtime: entry.mtime,
698 path: entry.path.clone(),
699 worktree: worktree_handle.clone(),
700 }
701 } else {
702 File {
703 is_local: local,
704 worktree_path: worktree_path.clone(),
705 entry_id: None,
706 path: old_file.path().clone(),
707 mtime: old_file.mtime(),
708 worktree: worktree_handle.clone(),
709 }
710 };
711
712 if let Some(task) = buffer.file_updated(Box::new(new_file), cx) {
713 task.detach();
714 }
715 }
716 });
717 } else {
718 buffers_to_delete.push(*buffer_id);
719 }
720 }
721
722 for buffer_id in buffers_to_delete {
723 match self {
724 Self::Local(worktree) => {
725 worktree.open_buffers.remove(&buffer_id);
726 }
727 Self::Remote(worktree) => {
728 worktree.open_buffers.remove(&buffer_id);
729 }
730 }
731 }
732 }
733
734 pub fn update_diagnostics(
735 &mut self,
736 params: lsp::PublishDiagnosticsParams,
737 disk_based_sources: &HashSet<String>,
738 cx: &mut ModelContext<Worktree>,
739 ) -> Result<()> {
740 let this = self.as_local_mut().ok_or_else(|| anyhow!("not local"))?;
741 let abs_path = params
742 .uri
743 .to_file_path()
744 .map_err(|_| anyhow!("URI is not a file"))?;
745 let worktree_path = Arc::from(
746 abs_path
747 .strip_prefix(&this.abs_path)
748 .context("path is not within worktree")?,
749 );
750
751 let mut next_group_id = 0;
752 let mut diagnostics = Vec::default();
753 let mut primary_diagnostic_group_ids = HashMap::default();
754 let mut sources_by_group_id = HashMap::default();
755 let mut supporting_diagnostic_severities = HashMap::default();
756 for diagnostic in ¶ms.diagnostics {
757 let source = diagnostic.source.as_ref();
758 let code = diagnostic.code.as_ref().map(|code| match code {
759 lsp::NumberOrString::Number(code) => code.to_string(),
760 lsp::NumberOrString::String(code) => code.clone(),
761 });
762 let range = range_from_lsp(diagnostic.range);
763 let is_supporting = diagnostic
764 .related_information
765 .as_ref()
766 .map_or(false, |infos| {
767 infos.iter().any(|info| {
768 primary_diagnostic_group_ids.contains_key(&(
769 source,
770 code.clone(),
771 range_from_lsp(info.location.range),
772 ))
773 })
774 });
775
776 if is_supporting {
777 if let Some(severity) = diagnostic.severity {
778 supporting_diagnostic_severities
779 .insert((source, code.clone(), range), severity);
780 }
781 } else {
782 let group_id = post_inc(&mut next_group_id);
783 let is_disk_based =
784 source.map_or(false, |source| disk_based_sources.contains(source));
785
786 sources_by_group_id.insert(group_id, source);
787 primary_diagnostic_group_ids
788 .insert((source, code.clone(), range.clone()), group_id);
789
790 diagnostics.push(DiagnosticEntry {
791 range,
792 diagnostic: Diagnostic {
793 code: code.clone(),
794 severity: diagnostic.severity.unwrap_or(DiagnosticSeverity::ERROR),
795 message: diagnostic.message.clone(),
796 group_id,
797 is_primary: true,
798 is_valid: true,
799 is_disk_based,
800 },
801 });
802 if let Some(infos) = &diagnostic.related_information {
803 for info in infos {
804 if info.location.uri == params.uri {
805 let range = range_from_lsp(info.location.range);
806 diagnostics.push(DiagnosticEntry {
807 range,
808 diagnostic: Diagnostic {
809 code: code.clone(),
810 severity: DiagnosticSeverity::INFORMATION,
811 message: info.message.clone(),
812 group_id,
813 is_primary: false,
814 is_valid: true,
815 is_disk_based,
816 },
817 });
818 }
819 }
820 }
821 }
822 }
823
824 for entry in &mut diagnostics {
825 let diagnostic = &mut entry.diagnostic;
826 if !diagnostic.is_primary {
827 let source = *sources_by_group_id.get(&diagnostic.group_id).unwrap();
828 if let Some(&severity) = supporting_diagnostic_severities.get(&(
829 source,
830 diagnostic.code.clone(),
831 entry.range.clone(),
832 )) {
833 diagnostic.severity = severity;
834 }
835 }
836 }
837
838 self.update_diagnostic_entries(worktree_path, params.version, diagnostics, cx)?;
839 Ok(())
840 }
841
842 pub fn update_diagnostic_entries(
843 &mut self,
844 worktree_path: Arc<Path>,
845 version: Option<i32>,
846 diagnostics: Vec<DiagnosticEntry<PointUtf16>>,
847 cx: &mut ModelContext<Self>,
848 ) -> Result<()> {
849 let this = self.as_local_mut().unwrap();
850 for buffer in this.open_buffers.values() {
851 if let Some(buffer) = buffer.upgrade(cx) {
852 if buffer
853 .read(cx)
854 .file()
855 .map_or(false, |file| *file.path() == worktree_path)
856 {
857 let (remote_id, operation) = buffer.update(cx, |buffer, cx| {
858 (
859 buffer.remote_id(),
860 buffer.update_diagnostics(version, diagnostics.clone(), cx),
861 )
862 });
863 self.send_buffer_update(remote_id, operation?, cx);
864 break;
865 }
866 }
867 }
868
869 let this = self.as_local_mut().unwrap();
870 let summary = DiagnosticSummary::new(&diagnostics);
871 this.diagnostic_summaries
872 .insert(PathKey(worktree_path.clone()), summary.clone());
873 this.diagnostics.insert(worktree_path.clone(), diagnostics);
874
875 cx.emit(Event::DiagnosticsUpdated(worktree_path.clone()));
876
877 if let Some(share) = this.share.as_ref() {
878 cx.foreground()
879 .spawn({
880 let client = this.client.clone();
881 let project_id = share.project_id;
882 let worktree_id = this.id().to_proto();
883 let path = worktree_path.to_string_lossy().to_string();
884 async move {
885 client
886 .send(proto::UpdateDiagnosticSummary {
887 project_id,
888 worktree_id,
889 summary: Some(proto::DiagnosticSummary {
890 path,
891 error_count: summary.error_count as u32,
892 warning_count: summary.warning_count as u32,
893 info_count: summary.info_count as u32,
894 hint_count: summary.hint_count as u32,
895 }),
896 })
897 .await
898 .log_err()
899 }
900 })
901 .detach();
902 }
903
904 Ok(())
905 }
906
907 fn send_buffer_update(
908 &mut self,
909 buffer_id: u64,
910 operation: Operation,
911 cx: &mut ModelContext<Self>,
912 ) {
913 if let Some((project_id, worktree_id, rpc)) = match self {
914 Worktree::Local(worktree) => worktree
915 .share
916 .as_ref()
917 .map(|share| (share.project_id, worktree.id(), worktree.client.clone())),
918 Worktree::Remote(worktree) => Some((
919 worktree.project_id,
920 worktree.snapshot.id(),
921 worktree.client.clone(),
922 )),
923 } {
924 cx.spawn(|worktree, mut cx| async move {
925 if let Err(error) = rpc
926 .request(proto::UpdateBuffer {
927 project_id,
928 worktree_id: worktree_id.0 as u64,
929 buffer_id,
930 operations: vec![language::proto::serialize_operation(&operation)],
931 })
932 .await
933 {
934 worktree.update(&mut cx, |worktree, _| {
935 log::error!("error sending buffer operation: {}", error);
936 match worktree {
937 Worktree::Local(t) => &mut t.queued_operations,
938 Worktree::Remote(t) => &mut t.queued_operations,
939 }
940 .push((buffer_id, operation));
941 });
942 }
943 })
944 .detach();
945 }
946 }
947}
948
949impl WorktreeId {
950 pub fn from_usize(handle_id: usize) -> Self {
951 Self(handle_id)
952 }
953
954 pub(crate) fn from_proto(id: u64) -> Self {
955 Self(id as usize)
956 }
957
958 pub fn to_proto(&self) -> u64 {
959 self.0 as u64
960 }
961
962 pub fn to_usize(&self) -> usize {
963 self.0
964 }
965}
966
967impl fmt::Display for WorktreeId {
968 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
969 self.0.fmt(f)
970 }
971}
972
973#[derive(Clone)]
974pub struct Snapshot {
975 id: WorktreeId,
976 scan_id: usize,
977 abs_path: Arc<Path>,
978 root_name: String,
979 root_char_bag: CharBag,
980 ignores: HashMap<Arc<Path>, (Arc<Gitignore>, usize)>,
981 entries_by_path: SumTree<Entry>,
982 entries_by_id: SumTree<PathEntry>,
983 removed_entry_ids: HashMap<u64, usize>,
984 next_entry_id: Arc<AtomicUsize>,
985}
986
987pub struct LocalWorktree {
988 snapshot: Snapshot,
989 config: WorktreeConfig,
990 background_snapshot: Arc<Mutex<Snapshot>>,
991 last_scan_state_rx: watch::Receiver<ScanState>,
992 _background_scanner_task: Option<Task<()>>,
993 poll_task: Option<Task<()>>,
994 share: Option<ShareState>,
995 loading_buffers: LoadingBuffers,
996 open_buffers: HashMap<usize, WeakModelHandle<Buffer>>,
997 shared_buffers: HashMap<PeerId, HashMap<u64, ModelHandle<Buffer>>>,
998 diagnostics: HashMap<Arc<Path>, Vec<DiagnosticEntry<PointUtf16>>>,
999 diagnostic_summaries: TreeMap<PathKey, DiagnosticSummary>,
1000 queued_operations: Vec<(u64, Operation)>,
1001 language_registry: Arc<LanguageRegistry>,
1002 client: Arc<Client>,
1003 user_store: ModelHandle<UserStore>,
1004 fs: Arc<dyn Fs>,
1005 languages: Vec<Arc<Language>>,
1006 language_servers: HashMap<String, Arc<LanguageServer>>,
1007}
1008
1009struct ShareState {
1010 project_id: u64,
1011 snapshots_tx: Sender<Snapshot>,
1012}
1013
1014pub struct RemoteWorktree {
1015 project_id: u64,
1016 snapshot: Snapshot,
1017 snapshot_rx: watch::Receiver<Snapshot>,
1018 client: Arc<Client>,
1019 updates_tx: postage::mpsc::Sender<proto::UpdateWorktree>,
1020 replica_id: ReplicaId,
1021 loading_buffers: LoadingBuffers,
1022 open_buffers: HashMap<usize, RemoteBuffer>,
1023 languages: Arc<LanguageRegistry>,
1024 user_store: ModelHandle<UserStore>,
1025 queued_operations: Vec<(u64, Operation)>,
1026 diagnostic_summaries: TreeMap<PathKey, DiagnosticSummary>,
1027}
1028
1029type LoadingBuffers = HashMap<
1030 Arc<Path>,
1031 postage::watch::Receiver<Option<Result<ModelHandle<Buffer>, Arc<anyhow::Error>>>>,
1032>;
1033
1034#[derive(Default, Deserialize)]
1035struct WorktreeConfig {
1036 collaborators: Vec<String>,
1037}
1038
1039impl LocalWorktree {
1040 async fn new(
1041 client: Arc<Client>,
1042 user_store: ModelHandle<UserStore>,
1043 path: impl Into<Arc<Path>>,
1044 fs: Arc<dyn Fs>,
1045 languages: Arc<LanguageRegistry>,
1046 cx: &mut AsyncAppContext,
1047 ) -> Result<(ModelHandle<Worktree>, Sender<ScanState>)> {
1048 let abs_path = path.into();
1049 let path: Arc<Path> = Arc::from(Path::new(""));
1050 let next_entry_id = AtomicUsize::new(0);
1051
1052 // After determining whether the root entry is a file or a directory, populate the
1053 // snapshot's "root name", which will be used for the purpose of fuzzy matching.
1054 let root_name = abs_path
1055 .file_name()
1056 .map_or(String::new(), |f| f.to_string_lossy().to_string());
1057 let root_char_bag = root_name.chars().map(|c| c.to_ascii_lowercase()).collect();
1058 let metadata = fs.metadata(&abs_path).await?;
1059
1060 let mut config = WorktreeConfig::default();
1061 if let Ok(zed_toml) = fs.load(&abs_path.join(".zed.toml")).await {
1062 if let Ok(parsed) = toml::from_str(&zed_toml) {
1063 config = parsed;
1064 }
1065 }
1066
1067 let (scan_states_tx, scan_states_rx) = smol::channel::unbounded();
1068 let (mut last_scan_state_tx, last_scan_state_rx) = watch::channel_with(ScanState::Scanning);
1069 let tree = cx.add_model(move |cx: &mut ModelContext<Worktree>| {
1070 let mut snapshot = Snapshot {
1071 id: WorktreeId::from_usize(cx.model_id()),
1072 scan_id: 0,
1073 abs_path,
1074 root_name: root_name.clone(),
1075 root_char_bag,
1076 ignores: Default::default(),
1077 entries_by_path: Default::default(),
1078 entries_by_id: Default::default(),
1079 removed_entry_ids: Default::default(),
1080 next_entry_id: Arc::new(next_entry_id),
1081 };
1082 if let Some(metadata) = metadata {
1083 snapshot.insert_entry(
1084 Entry::new(
1085 path.into(),
1086 &metadata,
1087 &snapshot.next_entry_id,
1088 snapshot.root_char_bag,
1089 ),
1090 fs.as_ref(),
1091 );
1092 }
1093
1094 let tree = Self {
1095 snapshot: snapshot.clone(),
1096 config,
1097 background_snapshot: Arc::new(Mutex::new(snapshot)),
1098 last_scan_state_rx,
1099 _background_scanner_task: None,
1100 share: None,
1101 poll_task: None,
1102 loading_buffers: Default::default(),
1103 open_buffers: Default::default(),
1104 shared_buffers: Default::default(),
1105 diagnostics: Default::default(),
1106 diagnostic_summaries: Default::default(),
1107 queued_operations: Default::default(),
1108 language_registry: languages,
1109 client,
1110 user_store,
1111 fs,
1112 languages: Default::default(),
1113 language_servers: Default::default(),
1114 };
1115
1116 cx.spawn_weak(|this, mut cx| async move {
1117 while let Ok(scan_state) = scan_states_rx.recv().await {
1118 if let Some(handle) = cx.read(|cx| this.upgrade(cx)) {
1119 let to_send = handle.update(&mut cx, |this, cx| {
1120 last_scan_state_tx.blocking_send(scan_state).ok();
1121 this.poll_snapshot(cx);
1122 let tree = this.as_local_mut().unwrap();
1123 if !tree.is_scanning() {
1124 if let Some(share) = tree.share.as_ref() {
1125 return Some((tree.snapshot(), share.snapshots_tx.clone()));
1126 }
1127 }
1128 None
1129 });
1130
1131 if let Some((snapshot, snapshots_to_send_tx)) = to_send {
1132 if let Err(err) = snapshots_to_send_tx.send(snapshot).await {
1133 log::error!("error submitting snapshot to send {}", err);
1134 }
1135 }
1136 } else {
1137 break;
1138 }
1139 }
1140 })
1141 .detach();
1142
1143 Worktree::Local(tree)
1144 });
1145
1146 Ok((tree, scan_states_tx))
1147 }
1148
1149 pub fn authorized_logins(&self) -> Vec<String> {
1150 self.config.collaborators.clone()
1151 }
1152
1153 pub fn language_registry(&self) -> &LanguageRegistry {
1154 &self.language_registry
1155 }
1156
1157 pub fn languages(&self) -> &[Arc<Language>] {
1158 &self.languages
1159 }
1160
1161 pub fn register_language(
1162 &mut self,
1163 language: &Arc<Language>,
1164 cx: &mut ModelContext<Worktree>,
1165 ) -> Option<Arc<LanguageServer>> {
1166 if !self.languages.iter().any(|l| Arc::ptr_eq(l, language)) {
1167 self.languages.push(language.clone());
1168 }
1169
1170 if let Some(server) = self.language_servers.get(language.name()) {
1171 return Some(server.clone());
1172 }
1173
1174 if let Some(language_server) = language
1175 .start_server(self.abs_path(), cx)
1176 .log_err()
1177 .flatten()
1178 {
1179 enum DiagnosticProgress {
1180 Updating,
1181 Updated,
1182 }
1183
1184 let disk_based_sources = language
1185 .disk_based_diagnostic_sources()
1186 .cloned()
1187 .unwrap_or_default();
1188 let disk_based_diagnostics_progress_token =
1189 language.disk_based_diagnostics_progress_token().cloned();
1190 let (diagnostics_tx, diagnostics_rx) = smol::channel::unbounded();
1191 let (disk_based_diagnostics_done_tx, disk_based_diagnostics_done_rx) =
1192 smol::channel::unbounded();
1193 language_server
1194 .on_notification::<lsp::notification::PublishDiagnostics, _>(move |params| {
1195 smol::block_on(diagnostics_tx.send(params)).ok();
1196 })
1197 .detach();
1198 cx.spawn_weak(|this, mut cx| {
1199 let has_disk_based_diagnostic_progress_token =
1200 disk_based_diagnostics_progress_token.is_some();
1201 let disk_based_diagnostics_done_tx = disk_based_diagnostics_done_tx.clone();
1202 async move {
1203 while let Ok(diagnostics) = diagnostics_rx.recv().await {
1204 if let Some(handle) = cx.read(|cx| this.upgrade(cx)) {
1205 handle.update(&mut cx, |this, cx| {
1206 if !has_disk_based_diagnostic_progress_token {
1207 smol::block_on(
1208 disk_based_diagnostics_done_tx
1209 .send(DiagnosticProgress::Updating),
1210 )
1211 .ok();
1212 }
1213 this.update_diagnostics(diagnostics, &disk_based_sources, cx)
1214 .log_err();
1215 if !has_disk_based_diagnostic_progress_token {
1216 smol::block_on(
1217 disk_based_diagnostics_done_tx
1218 .send(DiagnosticProgress::Updated),
1219 )
1220 .ok();
1221 }
1222 })
1223 } else {
1224 break;
1225 }
1226 }
1227 }
1228 })
1229 .detach();
1230
1231 let mut pending_disk_based_diagnostics: i32 = 0;
1232 language_server
1233 .on_notification::<lsp::notification::Progress, _>(move |params| {
1234 let token = match params.token {
1235 lsp::NumberOrString::Number(_) => None,
1236 lsp::NumberOrString::String(token) => Some(token),
1237 };
1238
1239 if token == disk_based_diagnostics_progress_token {
1240 match params.value {
1241 lsp::ProgressParamsValue::WorkDone(progress) => match progress {
1242 lsp::WorkDoneProgress::Begin(_) => {
1243 if pending_disk_based_diagnostics == 0 {
1244 smol::block_on(
1245 disk_based_diagnostics_done_tx
1246 .send(DiagnosticProgress::Updating),
1247 )
1248 .ok();
1249 }
1250 pending_disk_based_diagnostics += 1;
1251 }
1252 lsp::WorkDoneProgress::End(_) => {
1253 pending_disk_based_diagnostics -= 1;
1254 if pending_disk_based_diagnostics == 0 {
1255 smol::block_on(
1256 disk_based_diagnostics_done_tx
1257 .send(DiagnosticProgress::Updated),
1258 )
1259 .ok();
1260 }
1261 }
1262 _ => {}
1263 },
1264 }
1265 }
1266 })
1267 .detach();
1268 let rpc = self.client.clone();
1269 cx.spawn_weak(|this, mut cx| async move {
1270 while let Ok(progress) = disk_based_diagnostics_done_rx.recv().await {
1271 if let Some(handle) = cx.read(|cx| this.upgrade(cx)) {
1272 match progress {
1273 DiagnosticProgress::Updating => {
1274 let message = handle.update(&mut cx, |this, cx| {
1275 cx.emit(Event::DiskBasedDiagnosticsUpdating);
1276 let this = this.as_local().unwrap();
1277 this.share.as_ref().map(|share| {
1278 proto::DiskBasedDiagnosticsUpdating {
1279 project_id: share.project_id,
1280 worktree_id: this.id().to_proto(),
1281 }
1282 })
1283 });
1284
1285 if let Some(message) = message {
1286 rpc.send(message).await.log_err();
1287 }
1288 }
1289 DiagnosticProgress::Updated => {
1290 let message = handle.update(&mut cx, |this, cx| {
1291 cx.emit(Event::DiskBasedDiagnosticsUpdated);
1292 let this = this.as_local().unwrap();
1293 this.share.as_ref().map(|share| {
1294 proto::DiskBasedDiagnosticsUpdated {
1295 project_id: share.project_id,
1296 worktree_id: this.id().to_proto(),
1297 }
1298 })
1299 });
1300
1301 if let Some(message) = message {
1302 rpc.send(message).await.log_err();
1303 }
1304 }
1305 }
1306 } else {
1307 break;
1308 }
1309 }
1310 })
1311 .detach();
1312
1313 self.language_servers
1314 .insert(language.name().to_string(), language_server.clone());
1315 Some(language_server.clone())
1316 } else {
1317 None
1318 }
1319 }
1320
1321 fn get_open_buffer(
1322 &mut self,
1323 path: &Path,
1324 cx: &mut ModelContext<Worktree>,
1325 ) -> Option<ModelHandle<Buffer>> {
1326 let handle = cx.handle();
1327 let mut result = None;
1328 self.open_buffers.retain(|_buffer_id, buffer| {
1329 if let Some(buffer) = buffer.upgrade(cx) {
1330 if let Some(file) = File::from_dyn(buffer.read(cx).file()) {
1331 if file.worktree == handle && file.path().as_ref() == path {
1332 result = Some(buffer);
1333 }
1334 }
1335 true
1336 } else {
1337 false
1338 }
1339 });
1340 result
1341 }
1342
1343 fn open_buffer(
1344 &mut self,
1345 path: &Path,
1346 cx: &mut ModelContext<Worktree>,
1347 ) -> Task<Result<ModelHandle<Buffer>>> {
1348 let path = Arc::from(path);
1349 cx.spawn(move |this, mut cx| async move {
1350 let (file, contents) = this
1351 .update(&mut cx, |t, cx| t.as_local().unwrap().load(&path, cx))
1352 .await?;
1353
1354 let (diagnostics, language, language_server) = this.update(&mut cx, |this, cx| {
1355 let this = this.as_local_mut().unwrap();
1356 let diagnostics = this.diagnostics.get(&path).cloned();
1357 let language = this
1358 .language_registry
1359 .select_language(file.full_path())
1360 .cloned();
1361 let server = language
1362 .as_ref()
1363 .and_then(|language| this.register_language(language, cx));
1364 (diagnostics, language, server)
1365 });
1366
1367 let mut buffer_operations = Vec::new();
1368 let buffer = cx.add_model(|cx| {
1369 let mut buffer = Buffer::from_file(0, contents, Box::new(file), cx);
1370 buffer.set_language(language, language_server, cx);
1371 if let Some(diagnostics) = diagnostics {
1372 let op = buffer.update_diagnostics(None, diagnostics, cx).unwrap();
1373 buffer_operations.push(op);
1374 }
1375 buffer
1376 });
1377
1378 this.update(&mut cx, |this, cx| {
1379 for op in buffer_operations {
1380 this.send_buffer_update(buffer.read(cx).remote_id(), op, cx);
1381 }
1382 let this = this.as_local_mut().unwrap();
1383 this.open_buffers.insert(buffer.id(), buffer.downgrade());
1384 });
1385
1386 Ok(buffer)
1387 })
1388 }
1389
1390 pub fn open_remote_buffer(
1391 &mut self,
1392 envelope: TypedEnvelope<proto::OpenBuffer>,
1393 cx: &mut ModelContext<Worktree>,
1394 ) -> Task<Result<proto::OpenBufferResponse>> {
1395 cx.spawn(|this, mut cx| async move {
1396 let peer_id = envelope.original_sender_id();
1397 let path = Path::new(&envelope.payload.path);
1398 let buffer = this
1399 .update(&mut cx, |this, cx| this.open_buffer(path, cx))
1400 .await?;
1401 this.update(&mut cx, |this, cx| {
1402 this.as_local_mut()
1403 .unwrap()
1404 .shared_buffers
1405 .entry(peer_id?)
1406 .or_default()
1407 .insert(buffer.id() as u64, buffer.clone());
1408
1409 Ok(proto::OpenBufferResponse {
1410 buffer: Some(buffer.update(cx.as_mut(), |buffer, _| buffer.to_proto())),
1411 })
1412 })
1413 })
1414 }
1415
1416 pub fn close_remote_buffer(
1417 &mut self,
1418 envelope: TypedEnvelope<proto::CloseBuffer>,
1419 cx: &mut ModelContext<Worktree>,
1420 ) -> Result<()> {
1421 if let Some(shared_buffers) = self.shared_buffers.get_mut(&envelope.original_sender_id()?) {
1422 shared_buffers.remove(&envelope.payload.buffer_id);
1423 cx.notify();
1424 }
1425
1426 Ok(())
1427 }
1428
1429 pub fn remove_collaborator(
1430 &mut self,
1431 peer_id: PeerId,
1432 replica_id: ReplicaId,
1433 cx: &mut ModelContext<Worktree>,
1434 ) {
1435 self.shared_buffers.remove(&peer_id);
1436 for (_, buffer) in &self.open_buffers {
1437 if let Some(buffer) = buffer.upgrade(cx) {
1438 buffer.update(cx, |buffer, cx| buffer.remove_peer(replica_id, cx));
1439 }
1440 }
1441 cx.notify();
1442 }
1443
1444 pub fn scan_complete(&self) -> impl Future<Output = ()> {
1445 let mut scan_state_rx = self.last_scan_state_rx.clone();
1446 async move {
1447 let mut scan_state = Some(scan_state_rx.borrow().clone());
1448 while let Some(ScanState::Scanning) = scan_state {
1449 scan_state = scan_state_rx.recv().await;
1450 }
1451 }
1452 }
1453
1454 fn is_scanning(&self) -> bool {
1455 if let ScanState::Scanning = *self.last_scan_state_rx.borrow() {
1456 true
1457 } else {
1458 false
1459 }
1460 }
1461
1462 pub fn snapshot(&self) -> Snapshot {
1463 self.snapshot.clone()
1464 }
1465
1466 pub fn abs_path(&self) -> &Arc<Path> {
1467 &self.snapshot.abs_path
1468 }
1469
1470 pub fn contains_abs_path(&self, path: &Path) -> bool {
1471 path.starts_with(&self.snapshot.abs_path)
1472 }
1473
1474 fn absolutize(&self, path: &Path) -> PathBuf {
1475 if path.file_name().is_some() {
1476 self.snapshot.abs_path.join(path)
1477 } else {
1478 self.snapshot.abs_path.to_path_buf()
1479 }
1480 }
1481
1482 fn load(&self, path: &Path, cx: &mut ModelContext<Worktree>) -> Task<Result<(File, String)>> {
1483 let handle = cx.handle();
1484 let path = Arc::from(path);
1485 let worktree_path = self.abs_path.clone();
1486 let abs_path = self.absolutize(&path);
1487 let background_snapshot = self.background_snapshot.clone();
1488 let fs = self.fs.clone();
1489 cx.spawn(|this, mut cx| async move {
1490 let text = fs.load(&abs_path).await?;
1491 // Eagerly populate the snapshot with an updated entry for the loaded file
1492 let entry = refresh_entry(fs.as_ref(), &background_snapshot, path, &abs_path).await?;
1493 this.update(&mut cx, |this, cx| this.poll_snapshot(cx));
1494 Ok((
1495 File {
1496 entry_id: Some(entry.id),
1497 worktree: handle,
1498 worktree_path,
1499 path: entry.path,
1500 mtime: entry.mtime,
1501 is_local: true,
1502 },
1503 text,
1504 ))
1505 })
1506 }
1507
1508 pub fn save_buffer_as(
1509 &self,
1510 buffer: ModelHandle<Buffer>,
1511 path: impl Into<Arc<Path>>,
1512 text: Rope,
1513 cx: &mut ModelContext<Worktree>,
1514 ) -> Task<Result<File>> {
1515 let save = self.save(path, text, cx);
1516 cx.spawn(|this, mut cx| async move {
1517 let entry = save.await?;
1518 this.update(&mut cx, |this, cx| {
1519 let this = this.as_local_mut().unwrap();
1520 this.open_buffers.insert(buffer.id(), buffer.downgrade());
1521 Ok(File {
1522 entry_id: Some(entry.id),
1523 worktree: cx.handle(),
1524 worktree_path: this.abs_path.clone(),
1525 path: entry.path,
1526 mtime: entry.mtime,
1527 is_local: true,
1528 })
1529 })
1530 })
1531 }
1532
1533 fn save(
1534 &self,
1535 path: impl Into<Arc<Path>>,
1536 text: Rope,
1537 cx: &mut ModelContext<Worktree>,
1538 ) -> Task<Result<Entry>> {
1539 let path = path.into();
1540 let abs_path = self.absolutize(&path);
1541 let background_snapshot = self.background_snapshot.clone();
1542 let fs = self.fs.clone();
1543 let save = cx.background().spawn(async move {
1544 fs.save(&abs_path, &text).await?;
1545 refresh_entry(fs.as_ref(), &background_snapshot, path.clone(), &abs_path).await
1546 });
1547
1548 cx.spawn(|this, mut cx| async move {
1549 let entry = save.await?;
1550 this.update(&mut cx, |this, cx| this.poll_snapshot(cx));
1551 Ok(entry)
1552 })
1553 }
1554
1555 pub fn share(
1556 &mut self,
1557 project_id: u64,
1558 cx: &mut ModelContext<Worktree>,
1559 ) -> Task<anyhow::Result<()>> {
1560 if self.share.is_some() {
1561 return Task::ready(Ok(()));
1562 }
1563
1564 let snapshot = self.snapshot();
1565 let rpc = self.client.clone();
1566 let worktree_id = cx.model_id() as u64;
1567 let (snapshots_to_send_tx, snapshots_to_send_rx) = smol::channel::unbounded::<Snapshot>();
1568 self.share = Some(ShareState {
1569 project_id,
1570 snapshots_tx: snapshots_to_send_tx,
1571 });
1572
1573 cx.background()
1574 .spawn({
1575 let rpc = rpc.clone();
1576 let snapshot = snapshot.clone();
1577 async move {
1578 let mut prev_snapshot = snapshot;
1579 while let Ok(snapshot) = snapshots_to_send_rx.recv().await {
1580 let message =
1581 snapshot.build_update(&prev_snapshot, project_id, worktree_id, false);
1582 match rpc.send(message).await {
1583 Ok(()) => prev_snapshot = snapshot,
1584 Err(err) => log::error!("error sending snapshot diff {}", err),
1585 }
1586 }
1587 }
1588 })
1589 .detach();
1590
1591 let diagnostic_summaries = self.diagnostic_summaries.clone();
1592 let share_message = cx.background().spawn(async move {
1593 proto::ShareWorktree {
1594 project_id,
1595 worktree: Some(snapshot.to_proto(&diagnostic_summaries)),
1596 }
1597 });
1598
1599 cx.foreground().spawn(async move {
1600 rpc.request(share_message.await).await?;
1601 Ok(())
1602 })
1603 }
1604}
1605
1606fn build_gitignore(abs_path: &Path, fs: &dyn Fs) -> Result<Gitignore> {
1607 let contents = smol::block_on(fs.load(&abs_path))?;
1608 let parent = abs_path.parent().unwrap_or(Path::new("/"));
1609 let mut builder = GitignoreBuilder::new(parent);
1610 for line in contents.lines() {
1611 builder.add_line(Some(abs_path.into()), line)?;
1612 }
1613 Ok(builder.build()?)
1614}
1615
1616impl Deref for Worktree {
1617 type Target = Snapshot;
1618
1619 fn deref(&self) -> &Self::Target {
1620 match self {
1621 Worktree::Local(worktree) => &worktree.snapshot,
1622 Worktree::Remote(worktree) => &worktree.snapshot,
1623 }
1624 }
1625}
1626
1627impl Deref for LocalWorktree {
1628 type Target = Snapshot;
1629
1630 fn deref(&self) -> &Self::Target {
1631 &self.snapshot
1632 }
1633}
1634
1635impl Deref for RemoteWorktree {
1636 type Target = Snapshot;
1637
1638 fn deref(&self) -> &Self::Target {
1639 &self.snapshot
1640 }
1641}
1642
1643impl fmt::Debug for LocalWorktree {
1644 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1645 self.snapshot.fmt(f)
1646 }
1647}
1648
1649impl RemoteWorktree {
1650 fn get_open_buffer(
1651 &mut self,
1652 path: &Path,
1653 cx: &mut ModelContext<Worktree>,
1654 ) -> Option<ModelHandle<Buffer>> {
1655 let handle = cx.handle();
1656 let mut existing_buffer = None;
1657 self.open_buffers.retain(|_buffer_id, buffer| {
1658 if let Some(buffer) = buffer.upgrade(cx.as_ref()) {
1659 if let Some(file) = File::from_dyn(buffer.read(cx).file()) {
1660 if file.worktree == handle && file.path().as_ref() == path {
1661 existing_buffer = Some(buffer);
1662 }
1663 }
1664 true
1665 } else {
1666 false
1667 }
1668 });
1669 existing_buffer
1670 }
1671
1672 fn open_buffer(
1673 &mut self,
1674 path: &Path,
1675 cx: &mut ModelContext<Worktree>,
1676 ) -> Task<Result<ModelHandle<Buffer>>> {
1677 let rpc = self.client.clone();
1678 let replica_id = self.replica_id;
1679 let project_id = self.project_id;
1680 let remote_worktree_id = self.id();
1681 let root_path = self.snapshot.abs_path.clone();
1682 let path: Arc<Path> = Arc::from(path);
1683 let path_string = path.to_string_lossy().to_string();
1684 cx.spawn_weak(move |this, mut cx| async move {
1685 let entry = this
1686 .upgrade(&cx)
1687 .ok_or_else(|| anyhow!("worktree was closed"))?
1688 .read_with(&cx, |tree, _| tree.entry_for_path(&path).cloned())
1689 .ok_or_else(|| anyhow!("file does not exist"))?;
1690 let response = rpc
1691 .request(proto::OpenBuffer {
1692 project_id,
1693 worktree_id: remote_worktree_id.to_proto(),
1694 path: path_string,
1695 })
1696 .await?;
1697
1698 let this = this
1699 .upgrade(&cx)
1700 .ok_or_else(|| anyhow!("worktree was closed"))?;
1701 let file = File {
1702 entry_id: Some(entry.id),
1703 worktree: this.clone(),
1704 worktree_path: root_path,
1705 path: entry.path,
1706 mtime: entry.mtime,
1707 is_local: false,
1708 };
1709 let language = this.read_with(&cx, |this, _| {
1710 use language::File;
1711 this.languages().select_language(file.full_path()).cloned()
1712 });
1713 let remote_buffer = response.buffer.ok_or_else(|| anyhow!("empty buffer"))?;
1714 let buffer_id = remote_buffer.id as usize;
1715 let buffer = cx.add_model(|cx| {
1716 Buffer::from_proto(replica_id, remote_buffer, Some(Box::new(file)), cx)
1717 .unwrap()
1718 .with_language(language, None, cx)
1719 });
1720 this.update(&mut cx, move |this, cx| {
1721 let this = this.as_remote_mut().unwrap();
1722 if let Some(RemoteBuffer::Operations(pending_ops)) = this
1723 .open_buffers
1724 .insert(buffer_id, RemoteBuffer::Loaded(buffer.downgrade()))
1725 {
1726 buffer.update(cx, |buf, cx| buf.apply_ops(pending_ops, cx))?;
1727 }
1728 Result::<_, anyhow::Error>::Ok(buffer)
1729 })
1730 })
1731 }
1732
1733 pub fn close_all_buffers(&mut self, cx: &mut MutableAppContext) {
1734 for (_, buffer) in self.open_buffers.drain() {
1735 if let RemoteBuffer::Loaded(buffer) = buffer {
1736 if let Some(buffer) = buffer.upgrade(cx) {
1737 buffer.update(cx, |buffer, cx| buffer.close(cx))
1738 }
1739 }
1740 }
1741 }
1742
1743 fn snapshot(&self) -> Snapshot {
1744 self.snapshot.clone()
1745 }
1746
1747 pub fn update_from_remote(
1748 &mut self,
1749 envelope: TypedEnvelope<proto::UpdateWorktree>,
1750 cx: &mut ModelContext<Worktree>,
1751 ) -> Result<()> {
1752 let mut tx = self.updates_tx.clone();
1753 let payload = envelope.payload.clone();
1754 cx.background()
1755 .spawn(async move {
1756 tx.send(payload).await.expect("receiver runs to completion");
1757 })
1758 .detach();
1759
1760 Ok(())
1761 }
1762
1763 pub fn update_diagnostic_summary(
1764 &mut self,
1765 envelope: TypedEnvelope<proto::UpdateDiagnosticSummary>,
1766 cx: &mut ModelContext<Worktree>,
1767 ) {
1768 if let Some(summary) = envelope.payload.summary {
1769 let path: Arc<Path> = Path::new(&summary.path).into();
1770 self.diagnostic_summaries.insert(
1771 PathKey(path.clone()),
1772 DiagnosticSummary {
1773 error_count: summary.error_count as usize,
1774 warning_count: summary.warning_count as usize,
1775 info_count: summary.info_count as usize,
1776 hint_count: summary.hint_count as usize,
1777 },
1778 );
1779 cx.emit(Event::DiagnosticsUpdated(path));
1780 }
1781 }
1782
1783 pub fn disk_based_diagnostics_updating(&self, cx: &mut ModelContext<Worktree>) {
1784 cx.emit(Event::DiskBasedDiagnosticsUpdating);
1785 }
1786
1787 pub fn disk_based_diagnostics_updated(&self, cx: &mut ModelContext<Worktree>) {
1788 cx.emit(Event::DiskBasedDiagnosticsUpdated);
1789 }
1790
1791 pub fn remove_collaborator(&mut self, replica_id: ReplicaId, cx: &mut ModelContext<Worktree>) {
1792 for (_, buffer) in &self.open_buffers {
1793 if let Some(buffer) = buffer.upgrade(cx) {
1794 buffer.update(cx, |buffer, cx| buffer.remove_peer(replica_id, cx));
1795 }
1796 }
1797 cx.notify();
1798 }
1799}
1800
1801enum RemoteBuffer {
1802 Operations(Vec<Operation>),
1803 Loaded(WeakModelHandle<Buffer>),
1804}
1805
1806impl RemoteBuffer {
1807 fn upgrade(&self, cx: &impl UpgradeModelHandle) -> Option<ModelHandle<Buffer>> {
1808 match self {
1809 Self::Operations(_) => None,
1810 Self::Loaded(buffer) => buffer.upgrade(cx),
1811 }
1812 }
1813}
1814
1815impl Snapshot {
1816 pub fn id(&self) -> WorktreeId {
1817 self.id
1818 }
1819
1820 pub fn to_proto(
1821 &self,
1822 diagnostic_summaries: &TreeMap<PathKey, DiagnosticSummary>,
1823 ) -> proto::Worktree {
1824 let root_name = self.root_name.clone();
1825 proto::Worktree {
1826 id: self.id.0 as u64,
1827 root_name,
1828 entries: self
1829 .entries_by_path
1830 .iter()
1831 .filter(|e| !e.is_ignored)
1832 .map(Into::into)
1833 .collect(),
1834 diagnostic_summaries: diagnostic_summaries
1835 .iter()
1836 .map(|(path, summary)| summary.to_proto(path.0.clone()))
1837 .collect(),
1838 }
1839 }
1840
1841 pub fn build_update(
1842 &self,
1843 other: &Self,
1844 project_id: u64,
1845 worktree_id: u64,
1846 include_ignored: bool,
1847 ) -> proto::UpdateWorktree {
1848 let mut updated_entries = Vec::new();
1849 let mut removed_entries = Vec::new();
1850 let mut self_entries = self
1851 .entries_by_id
1852 .cursor::<()>()
1853 .filter(|e| include_ignored || !e.is_ignored)
1854 .peekable();
1855 let mut other_entries = other
1856 .entries_by_id
1857 .cursor::<()>()
1858 .filter(|e| include_ignored || !e.is_ignored)
1859 .peekable();
1860 loop {
1861 match (self_entries.peek(), other_entries.peek()) {
1862 (Some(self_entry), Some(other_entry)) => {
1863 match Ord::cmp(&self_entry.id, &other_entry.id) {
1864 Ordering::Less => {
1865 let entry = self.entry_for_id(self_entry.id).unwrap().into();
1866 updated_entries.push(entry);
1867 self_entries.next();
1868 }
1869 Ordering::Equal => {
1870 if self_entry.scan_id != other_entry.scan_id {
1871 let entry = self.entry_for_id(self_entry.id).unwrap().into();
1872 updated_entries.push(entry);
1873 }
1874
1875 self_entries.next();
1876 other_entries.next();
1877 }
1878 Ordering::Greater => {
1879 removed_entries.push(other_entry.id as u64);
1880 other_entries.next();
1881 }
1882 }
1883 }
1884 (Some(self_entry), None) => {
1885 let entry = self.entry_for_id(self_entry.id).unwrap().into();
1886 updated_entries.push(entry);
1887 self_entries.next();
1888 }
1889 (None, Some(other_entry)) => {
1890 removed_entries.push(other_entry.id as u64);
1891 other_entries.next();
1892 }
1893 (None, None) => break,
1894 }
1895 }
1896
1897 proto::UpdateWorktree {
1898 project_id,
1899 worktree_id,
1900 root_name: self.root_name().to_string(),
1901 updated_entries,
1902 removed_entries,
1903 }
1904 }
1905
1906 fn apply_update(&mut self, update: proto::UpdateWorktree) -> Result<()> {
1907 self.scan_id += 1;
1908 let scan_id = self.scan_id;
1909
1910 let mut entries_by_path_edits = Vec::new();
1911 let mut entries_by_id_edits = Vec::new();
1912 for entry_id in update.removed_entries {
1913 let entry_id = entry_id as usize;
1914 let entry = self
1915 .entry_for_id(entry_id)
1916 .ok_or_else(|| anyhow!("unknown entry"))?;
1917 entries_by_path_edits.push(Edit::Remove(PathKey(entry.path.clone())));
1918 entries_by_id_edits.push(Edit::Remove(entry.id));
1919 }
1920
1921 for entry in update.updated_entries {
1922 let entry = Entry::try_from((&self.root_char_bag, entry))?;
1923 if let Some(PathEntry { path, .. }) = self.entries_by_id.get(&entry.id, &()) {
1924 entries_by_path_edits.push(Edit::Remove(PathKey(path.clone())));
1925 }
1926 entries_by_id_edits.push(Edit::Insert(PathEntry {
1927 id: entry.id,
1928 path: entry.path.clone(),
1929 is_ignored: entry.is_ignored,
1930 scan_id,
1931 }));
1932 entries_by_path_edits.push(Edit::Insert(entry));
1933 }
1934
1935 self.entries_by_path.edit(entries_by_path_edits, &());
1936 self.entries_by_id.edit(entries_by_id_edits, &());
1937
1938 Ok(())
1939 }
1940
1941 pub fn file_count(&self) -> usize {
1942 self.entries_by_path.summary().file_count
1943 }
1944
1945 pub fn visible_file_count(&self) -> usize {
1946 self.entries_by_path.summary().visible_file_count
1947 }
1948
1949 fn traverse_from_offset(
1950 &self,
1951 include_dirs: bool,
1952 include_ignored: bool,
1953 start_offset: usize,
1954 ) -> Traversal {
1955 let mut cursor = self.entries_by_path.cursor();
1956 cursor.seek(
1957 &TraversalTarget::Count {
1958 count: start_offset,
1959 include_dirs,
1960 include_ignored,
1961 },
1962 Bias::Right,
1963 &(),
1964 );
1965 Traversal {
1966 cursor,
1967 include_dirs,
1968 include_ignored,
1969 }
1970 }
1971
1972 fn traverse_from_path(
1973 &self,
1974 include_dirs: bool,
1975 include_ignored: bool,
1976 path: &Path,
1977 ) -> Traversal {
1978 let mut cursor = self.entries_by_path.cursor();
1979 cursor.seek(&TraversalTarget::Path(path), Bias::Left, &());
1980 Traversal {
1981 cursor,
1982 include_dirs,
1983 include_ignored,
1984 }
1985 }
1986
1987 pub fn files(&self, include_ignored: bool, start: usize) -> Traversal {
1988 self.traverse_from_offset(false, include_ignored, start)
1989 }
1990
1991 pub fn entries(&self, include_ignored: bool) -> Traversal {
1992 self.traverse_from_offset(true, include_ignored, 0)
1993 }
1994
1995 pub fn paths(&self) -> impl Iterator<Item = &Arc<Path>> {
1996 let empty_path = Path::new("");
1997 self.entries_by_path
1998 .cursor::<()>()
1999 .filter(move |entry| entry.path.as_ref() != empty_path)
2000 .map(|entry| &entry.path)
2001 }
2002
2003 fn child_entries<'a>(&'a self, parent_path: &'a Path) -> ChildEntriesIter<'a> {
2004 let mut cursor = self.entries_by_path.cursor();
2005 cursor.seek(&TraversalTarget::Path(parent_path), Bias::Right, &());
2006 let traversal = Traversal {
2007 cursor,
2008 include_dirs: true,
2009 include_ignored: true,
2010 };
2011 ChildEntriesIter {
2012 traversal,
2013 parent_path,
2014 }
2015 }
2016
2017 pub fn root_entry(&self) -> Option<&Entry> {
2018 self.entry_for_path("")
2019 }
2020
2021 pub fn root_name(&self) -> &str {
2022 &self.root_name
2023 }
2024
2025 pub fn entry_for_path(&self, path: impl AsRef<Path>) -> Option<&Entry> {
2026 let path = path.as_ref();
2027 self.traverse_from_path(true, true, path)
2028 .entry()
2029 .and_then(|entry| {
2030 if entry.path.as_ref() == path {
2031 Some(entry)
2032 } else {
2033 None
2034 }
2035 })
2036 }
2037
2038 pub fn entry_for_id(&self, id: usize) -> Option<&Entry> {
2039 let entry = self.entries_by_id.get(&id, &())?;
2040 self.entry_for_path(&entry.path)
2041 }
2042
2043 pub fn inode_for_path(&self, path: impl AsRef<Path>) -> Option<u64> {
2044 self.entry_for_path(path.as_ref()).map(|e| e.inode)
2045 }
2046
2047 fn insert_entry(&mut self, mut entry: Entry, fs: &dyn Fs) -> Entry {
2048 if !entry.is_dir() && entry.path.file_name() == Some(&GITIGNORE) {
2049 let abs_path = self.abs_path.join(&entry.path);
2050 match build_gitignore(&abs_path, fs) {
2051 Ok(ignore) => {
2052 let ignore_dir_path = entry.path.parent().unwrap();
2053 self.ignores
2054 .insert(ignore_dir_path.into(), (Arc::new(ignore), self.scan_id));
2055 }
2056 Err(error) => {
2057 log::error!(
2058 "error loading .gitignore file {:?} - {:?}",
2059 &entry.path,
2060 error
2061 );
2062 }
2063 }
2064 }
2065
2066 self.reuse_entry_id(&mut entry);
2067 self.entries_by_path.insert_or_replace(entry.clone(), &());
2068 self.entries_by_id.insert_or_replace(
2069 PathEntry {
2070 id: entry.id,
2071 path: entry.path.clone(),
2072 is_ignored: entry.is_ignored,
2073 scan_id: self.scan_id,
2074 },
2075 &(),
2076 );
2077 entry
2078 }
2079
2080 fn populate_dir(
2081 &mut self,
2082 parent_path: Arc<Path>,
2083 entries: impl IntoIterator<Item = Entry>,
2084 ignore: Option<Arc<Gitignore>>,
2085 ) {
2086 let mut parent_entry = self
2087 .entries_by_path
2088 .get(&PathKey(parent_path.clone()), &())
2089 .unwrap()
2090 .clone();
2091 if let Some(ignore) = ignore {
2092 self.ignores.insert(parent_path, (ignore, self.scan_id));
2093 }
2094 if matches!(parent_entry.kind, EntryKind::PendingDir) {
2095 parent_entry.kind = EntryKind::Dir;
2096 } else {
2097 unreachable!();
2098 }
2099
2100 let mut entries_by_path_edits = vec![Edit::Insert(parent_entry)];
2101 let mut entries_by_id_edits = Vec::new();
2102
2103 for mut entry in entries {
2104 self.reuse_entry_id(&mut entry);
2105 entries_by_id_edits.push(Edit::Insert(PathEntry {
2106 id: entry.id,
2107 path: entry.path.clone(),
2108 is_ignored: entry.is_ignored,
2109 scan_id: self.scan_id,
2110 }));
2111 entries_by_path_edits.push(Edit::Insert(entry));
2112 }
2113
2114 self.entries_by_path.edit(entries_by_path_edits, &());
2115 self.entries_by_id.edit(entries_by_id_edits, &());
2116 }
2117
2118 fn reuse_entry_id(&mut self, entry: &mut Entry) {
2119 if let Some(removed_entry_id) = self.removed_entry_ids.remove(&entry.inode) {
2120 entry.id = removed_entry_id;
2121 } else if let Some(existing_entry) = self.entry_for_path(&entry.path) {
2122 entry.id = existing_entry.id;
2123 }
2124 }
2125
2126 fn remove_path(&mut self, path: &Path) {
2127 let mut new_entries;
2128 let removed_entries;
2129 {
2130 let mut cursor = self.entries_by_path.cursor::<TraversalProgress>();
2131 new_entries = cursor.slice(&TraversalTarget::Path(path), Bias::Left, &());
2132 removed_entries = cursor.slice(&TraversalTarget::PathSuccessor(path), Bias::Left, &());
2133 new_entries.push_tree(cursor.suffix(&()), &());
2134 }
2135 self.entries_by_path = new_entries;
2136
2137 let mut entries_by_id_edits = Vec::new();
2138 for entry in removed_entries.cursor::<()>() {
2139 let removed_entry_id = self
2140 .removed_entry_ids
2141 .entry(entry.inode)
2142 .or_insert(entry.id);
2143 *removed_entry_id = cmp::max(*removed_entry_id, entry.id);
2144 entries_by_id_edits.push(Edit::Remove(entry.id));
2145 }
2146 self.entries_by_id.edit(entries_by_id_edits, &());
2147
2148 if path.file_name() == Some(&GITIGNORE) {
2149 if let Some((_, scan_id)) = self.ignores.get_mut(path.parent().unwrap()) {
2150 *scan_id = self.scan_id;
2151 }
2152 }
2153 }
2154
2155 fn ignore_stack_for_path(&self, path: &Path, is_dir: bool) -> Arc<IgnoreStack> {
2156 let mut new_ignores = Vec::new();
2157 for ancestor in path.ancestors().skip(1) {
2158 if let Some((ignore, _)) = self.ignores.get(ancestor) {
2159 new_ignores.push((ancestor, Some(ignore.clone())));
2160 } else {
2161 new_ignores.push((ancestor, None));
2162 }
2163 }
2164
2165 let mut ignore_stack = IgnoreStack::none();
2166 for (parent_path, ignore) in new_ignores.into_iter().rev() {
2167 if ignore_stack.is_path_ignored(&parent_path, true) {
2168 ignore_stack = IgnoreStack::all();
2169 break;
2170 } else if let Some(ignore) = ignore {
2171 ignore_stack = ignore_stack.append(Arc::from(parent_path), ignore);
2172 }
2173 }
2174
2175 if ignore_stack.is_path_ignored(path, is_dir) {
2176 ignore_stack = IgnoreStack::all();
2177 }
2178
2179 ignore_stack
2180 }
2181}
2182
2183impl fmt::Debug for Snapshot {
2184 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2185 for entry in self.entries_by_path.cursor::<()>() {
2186 for _ in entry.path.ancestors().skip(1) {
2187 write!(f, " ")?;
2188 }
2189 writeln!(f, "{:?} (inode: {})", entry.path, entry.inode)?;
2190 }
2191 Ok(())
2192 }
2193}
2194
2195#[derive(Clone, PartialEq)]
2196pub struct File {
2197 entry_id: Option<usize>,
2198 worktree: ModelHandle<Worktree>,
2199 worktree_path: Arc<Path>,
2200 pub path: Arc<Path>,
2201 pub mtime: SystemTime,
2202 is_local: bool,
2203}
2204
2205impl language::File for File {
2206 fn mtime(&self) -> SystemTime {
2207 self.mtime
2208 }
2209
2210 fn path(&self) -> &Arc<Path> {
2211 &self.path
2212 }
2213
2214 fn abs_path(&self) -> Option<PathBuf> {
2215 if self.is_local {
2216 Some(self.worktree_path.join(&self.path))
2217 } else {
2218 None
2219 }
2220 }
2221
2222 fn full_path(&self) -> PathBuf {
2223 let mut full_path = PathBuf::new();
2224 if let Some(worktree_name) = self.worktree_path.file_name() {
2225 full_path.push(worktree_name);
2226 }
2227 full_path.push(&self.path);
2228 full_path
2229 }
2230
2231 /// Returns the last component of this handle's absolute path. If this handle refers to the root
2232 /// of its worktree, then this method will return the name of the worktree itself.
2233 fn file_name<'a>(&'a self) -> Option<OsString> {
2234 self.path
2235 .file_name()
2236 .or_else(|| self.worktree_path.file_name())
2237 .map(Into::into)
2238 }
2239
2240 fn is_deleted(&self) -> bool {
2241 self.entry_id.is_none()
2242 }
2243
2244 fn save(
2245 &self,
2246 buffer_id: u64,
2247 text: Rope,
2248 version: clock::Global,
2249 cx: &mut MutableAppContext,
2250 ) -> Task<Result<(clock::Global, SystemTime)>> {
2251 let worktree_id = self.worktree.read(cx).id().to_proto();
2252 self.worktree.update(cx, |worktree, cx| match worktree {
2253 Worktree::Local(worktree) => {
2254 let rpc = worktree.client.clone();
2255 let project_id = worktree.share.as_ref().map(|share| share.project_id);
2256 let save = worktree.save(self.path.clone(), text, cx);
2257 cx.background().spawn(async move {
2258 let entry = save.await?;
2259 if let Some(project_id) = project_id {
2260 rpc.send(proto::BufferSaved {
2261 project_id,
2262 worktree_id,
2263 buffer_id,
2264 version: (&version).into(),
2265 mtime: Some(entry.mtime.into()),
2266 })
2267 .await?;
2268 }
2269 Ok((version, entry.mtime))
2270 })
2271 }
2272 Worktree::Remote(worktree) => {
2273 let rpc = worktree.client.clone();
2274 let project_id = worktree.project_id;
2275 cx.foreground().spawn(async move {
2276 let response = rpc
2277 .request(proto::SaveBuffer {
2278 project_id,
2279 worktree_id,
2280 buffer_id,
2281 })
2282 .await?;
2283 let version = response.version.try_into()?;
2284 let mtime = response
2285 .mtime
2286 .ok_or_else(|| anyhow!("missing mtime"))?
2287 .into();
2288 Ok((version, mtime))
2289 })
2290 }
2291 })
2292 }
2293
2294 fn load_local(&self, cx: &AppContext) -> Option<Task<Result<String>>> {
2295 let worktree = self.worktree.read(cx).as_local()?;
2296 let abs_path = worktree.absolutize(&self.path);
2297 let fs = worktree.fs.clone();
2298 Some(
2299 cx.background()
2300 .spawn(async move { fs.load(&abs_path).await }),
2301 )
2302 }
2303
2304 fn format_remote(
2305 &self,
2306 buffer_id: u64,
2307 cx: &mut MutableAppContext,
2308 ) -> Option<Task<Result<()>>> {
2309 let worktree = self.worktree.read(cx);
2310 let worktree_id = worktree.id().to_proto();
2311 let worktree = worktree.as_remote()?;
2312 let rpc = worktree.client.clone();
2313 let project_id = worktree.project_id;
2314 Some(cx.foreground().spawn(async move {
2315 rpc.request(proto::FormatBuffer {
2316 project_id,
2317 worktree_id,
2318 buffer_id,
2319 })
2320 .await?;
2321 Ok(())
2322 }))
2323 }
2324
2325 fn buffer_updated(&self, buffer_id: u64, operation: Operation, cx: &mut MutableAppContext) {
2326 self.worktree.update(cx, |worktree, cx| {
2327 worktree.send_buffer_update(buffer_id, operation, cx);
2328 });
2329 }
2330
2331 fn buffer_removed(&self, buffer_id: u64, cx: &mut MutableAppContext) {
2332 self.worktree.update(cx, |worktree, cx| {
2333 if let Worktree::Remote(worktree) = worktree {
2334 let project_id = worktree.project_id;
2335 let worktree_id = worktree.id().to_proto();
2336 let rpc = worktree.client.clone();
2337 cx.background()
2338 .spawn(async move {
2339 if let Err(error) = rpc
2340 .send(proto::CloseBuffer {
2341 project_id,
2342 worktree_id,
2343 buffer_id,
2344 })
2345 .await
2346 {
2347 log::error!("error closing remote buffer: {}", error);
2348 }
2349 })
2350 .detach();
2351 }
2352 });
2353 }
2354
2355 fn as_any(&self) -> &dyn Any {
2356 self
2357 }
2358}
2359
2360impl File {
2361 pub fn from_dyn(file: Option<&dyn language::File>) -> Option<&Self> {
2362 file.and_then(|f| f.as_any().downcast_ref())
2363 }
2364
2365 pub fn worktree_id(&self, cx: &AppContext) -> WorktreeId {
2366 self.worktree.read(cx).id()
2367 }
2368}
2369
2370#[derive(Clone, Debug)]
2371pub struct Entry {
2372 pub id: usize,
2373 pub kind: EntryKind,
2374 pub path: Arc<Path>,
2375 pub inode: u64,
2376 pub mtime: SystemTime,
2377 pub is_symlink: bool,
2378 pub is_ignored: bool,
2379}
2380
2381#[derive(Clone, Debug)]
2382pub enum EntryKind {
2383 PendingDir,
2384 Dir,
2385 File(CharBag),
2386}
2387
2388impl Entry {
2389 fn new(
2390 path: Arc<Path>,
2391 metadata: &fs::Metadata,
2392 next_entry_id: &AtomicUsize,
2393 root_char_bag: CharBag,
2394 ) -> Self {
2395 Self {
2396 id: next_entry_id.fetch_add(1, SeqCst),
2397 kind: if metadata.is_dir {
2398 EntryKind::PendingDir
2399 } else {
2400 EntryKind::File(char_bag_for_path(root_char_bag, &path))
2401 },
2402 path,
2403 inode: metadata.inode,
2404 mtime: metadata.mtime,
2405 is_symlink: metadata.is_symlink,
2406 is_ignored: false,
2407 }
2408 }
2409
2410 pub fn is_dir(&self) -> bool {
2411 matches!(self.kind, EntryKind::Dir | EntryKind::PendingDir)
2412 }
2413
2414 pub fn is_file(&self) -> bool {
2415 matches!(self.kind, EntryKind::File(_))
2416 }
2417}
2418
2419impl sum_tree::Item for Entry {
2420 type Summary = EntrySummary;
2421
2422 fn summary(&self) -> Self::Summary {
2423 let visible_count = if self.is_ignored { 0 } else { 1 };
2424 let file_count;
2425 let visible_file_count;
2426 if self.is_file() {
2427 file_count = 1;
2428 visible_file_count = visible_count;
2429 } else {
2430 file_count = 0;
2431 visible_file_count = 0;
2432 }
2433
2434 EntrySummary {
2435 max_path: self.path.clone(),
2436 count: 1,
2437 visible_count,
2438 file_count,
2439 visible_file_count,
2440 }
2441 }
2442}
2443
2444impl sum_tree::KeyedItem for Entry {
2445 type Key = PathKey;
2446
2447 fn key(&self) -> Self::Key {
2448 PathKey(self.path.clone())
2449 }
2450}
2451
2452#[derive(Clone, Debug)]
2453pub struct EntrySummary {
2454 max_path: Arc<Path>,
2455 count: usize,
2456 visible_count: usize,
2457 file_count: usize,
2458 visible_file_count: usize,
2459}
2460
2461impl Default for EntrySummary {
2462 fn default() -> Self {
2463 Self {
2464 max_path: Arc::from(Path::new("")),
2465 count: 0,
2466 visible_count: 0,
2467 file_count: 0,
2468 visible_file_count: 0,
2469 }
2470 }
2471}
2472
2473impl sum_tree::Summary for EntrySummary {
2474 type Context = ();
2475
2476 fn add_summary(&mut self, rhs: &Self, _: &()) {
2477 self.max_path = rhs.max_path.clone();
2478 self.visible_count += rhs.visible_count;
2479 self.file_count += rhs.file_count;
2480 self.visible_file_count += rhs.visible_file_count;
2481 }
2482}
2483
2484#[derive(Clone, Debug)]
2485struct PathEntry {
2486 id: usize,
2487 path: Arc<Path>,
2488 is_ignored: bool,
2489 scan_id: usize,
2490}
2491
2492impl sum_tree::Item for PathEntry {
2493 type Summary = PathEntrySummary;
2494
2495 fn summary(&self) -> Self::Summary {
2496 PathEntrySummary { max_id: self.id }
2497 }
2498}
2499
2500impl sum_tree::KeyedItem for PathEntry {
2501 type Key = usize;
2502
2503 fn key(&self) -> Self::Key {
2504 self.id
2505 }
2506}
2507
2508#[derive(Clone, Debug, Default)]
2509struct PathEntrySummary {
2510 max_id: usize,
2511}
2512
2513impl sum_tree::Summary for PathEntrySummary {
2514 type Context = ();
2515
2516 fn add_summary(&mut self, summary: &Self, _: &Self::Context) {
2517 self.max_id = summary.max_id;
2518 }
2519}
2520
2521impl<'a> sum_tree::Dimension<'a, PathEntrySummary> for usize {
2522 fn add_summary(&mut self, summary: &'a PathEntrySummary, _: &()) {
2523 *self = summary.max_id;
2524 }
2525}
2526
2527#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
2528pub struct PathKey(Arc<Path>);
2529
2530impl Default for PathKey {
2531 fn default() -> Self {
2532 Self(Path::new("").into())
2533 }
2534}
2535
2536impl<'a> sum_tree::Dimension<'a, EntrySummary> for PathKey {
2537 fn add_summary(&mut self, summary: &'a EntrySummary, _: &()) {
2538 self.0 = summary.max_path.clone();
2539 }
2540}
2541
2542struct BackgroundScanner {
2543 fs: Arc<dyn Fs>,
2544 snapshot: Arc<Mutex<Snapshot>>,
2545 notify: Sender<ScanState>,
2546 executor: Arc<executor::Background>,
2547}
2548
2549impl BackgroundScanner {
2550 fn new(
2551 snapshot: Arc<Mutex<Snapshot>>,
2552 notify: Sender<ScanState>,
2553 fs: Arc<dyn Fs>,
2554 executor: Arc<executor::Background>,
2555 ) -> Self {
2556 Self {
2557 fs,
2558 snapshot,
2559 notify,
2560 executor,
2561 }
2562 }
2563
2564 fn abs_path(&self) -> Arc<Path> {
2565 self.snapshot.lock().abs_path.clone()
2566 }
2567
2568 fn snapshot(&self) -> Snapshot {
2569 self.snapshot.lock().clone()
2570 }
2571
2572 async fn run(mut self, events_rx: impl Stream<Item = Vec<fsevent::Event>>) {
2573 if self.notify.send(ScanState::Scanning).await.is_err() {
2574 return;
2575 }
2576
2577 if let Err(err) = self.scan_dirs().await {
2578 if self
2579 .notify
2580 .send(ScanState::Err(Arc::new(err)))
2581 .await
2582 .is_err()
2583 {
2584 return;
2585 }
2586 }
2587
2588 if self.notify.send(ScanState::Idle).await.is_err() {
2589 return;
2590 }
2591
2592 futures::pin_mut!(events_rx);
2593 while let Some(events) = events_rx.next().await {
2594 if self.notify.send(ScanState::Scanning).await.is_err() {
2595 break;
2596 }
2597
2598 if !self.process_events(events).await {
2599 break;
2600 }
2601
2602 if self.notify.send(ScanState::Idle).await.is_err() {
2603 break;
2604 }
2605 }
2606 }
2607
2608 async fn scan_dirs(&mut self) -> Result<()> {
2609 let root_char_bag;
2610 let next_entry_id;
2611 let is_dir;
2612 {
2613 let snapshot = self.snapshot.lock();
2614 root_char_bag = snapshot.root_char_bag;
2615 next_entry_id = snapshot.next_entry_id.clone();
2616 is_dir = snapshot.root_entry().map_or(false, |e| e.is_dir())
2617 };
2618
2619 if is_dir {
2620 let path: Arc<Path> = Arc::from(Path::new(""));
2621 let abs_path = self.abs_path();
2622 let (tx, rx) = channel::unbounded();
2623 tx.send(ScanJob {
2624 abs_path: abs_path.to_path_buf(),
2625 path,
2626 ignore_stack: IgnoreStack::none(),
2627 scan_queue: tx.clone(),
2628 })
2629 .await
2630 .unwrap();
2631 drop(tx);
2632
2633 self.executor
2634 .scoped(|scope| {
2635 for _ in 0..self.executor.num_cpus() {
2636 scope.spawn(async {
2637 while let Ok(job) = rx.recv().await {
2638 if let Err(err) = self
2639 .scan_dir(root_char_bag, next_entry_id.clone(), &job)
2640 .await
2641 {
2642 log::error!("error scanning {:?}: {}", job.abs_path, err);
2643 }
2644 }
2645 });
2646 }
2647 })
2648 .await;
2649 }
2650
2651 Ok(())
2652 }
2653
2654 async fn scan_dir(
2655 &self,
2656 root_char_bag: CharBag,
2657 next_entry_id: Arc<AtomicUsize>,
2658 job: &ScanJob,
2659 ) -> Result<()> {
2660 let mut new_entries: Vec<Entry> = Vec::new();
2661 let mut new_jobs: Vec<ScanJob> = Vec::new();
2662 let mut ignore_stack = job.ignore_stack.clone();
2663 let mut new_ignore = None;
2664
2665 let mut child_paths = self.fs.read_dir(&job.abs_path).await?;
2666 while let Some(child_abs_path) = child_paths.next().await {
2667 let child_abs_path = match child_abs_path {
2668 Ok(child_abs_path) => child_abs_path,
2669 Err(error) => {
2670 log::error!("error processing entry {:?}", error);
2671 continue;
2672 }
2673 };
2674 let child_name = child_abs_path.file_name().unwrap();
2675 let child_path: Arc<Path> = job.path.join(child_name).into();
2676 let child_metadata = match self.fs.metadata(&child_abs_path).await? {
2677 Some(metadata) => metadata,
2678 None => continue,
2679 };
2680
2681 // If we find a .gitignore, add it to the stack of ignores used to determine which paths are ignored
2682 if child_name == *GITIGNORE {
2683 match build_gitignore(&child_abs_path, self.fs.as_ref()) {
2684 Ok(ignore) => {
2685 let ignore = Arc::new(ignore);
2686 ignore_stack = ignore_stack.append(job.path.clone(), ignore.clone());
2687 new_ignore = Some(ignore);
2688 }
2689 Err(error) => {
2690 log::error!(
2691 "error loading .gitignore file {:?} - {:?}",
2692 child_name,
2693 error
2694 );
2695 }
2696 }
2697
2698 // Update ignore status of any child entries we've already processed to reflect the
2699 // ignore file in the current directory. Because `.gitignore` starts with a `.`,
2700 // there should rarely be too numerous. Update the ignore stack associated with any
2701 // new jobs as well.
2702 let mut new_jobs = new_jobs.iter_mut();
2703 for entry in &mut new_entries {
2704 entry.is_ignored = ignore_stack.is_path_ignored(&entry.path, entry.is_dir());
2705 if entry.is_dir() {
2706 new_jobs.next().unwrap().ignore_stack = if entry.is_ignored {
2707 IgnoreStack::all()
2708 } else {
2709 ignore_stack.clone()
2710 };
2711 }
2712 }
2713 }
2714
2715 let mut child_entry = Entry::new(
2716 child_path.clone(),
2717 &child_metadata,
2718 &next_entry_id,
2719 root_char_bag,
2720 );
2721
2722 if child_metadata.is_dir {
2723 let is_ignored = ignore_stack.is_path_ignored(&child_path, true);
2724 child_entry.is_ignored = is_ignored;
2725 new_entries.push(child_entry);
2726 new_jobs.push(ScanJob {
2727 abs_path: child_abs_path,
2728 path: child_path,
2729 ignore_stack: if is_ignored {
2730 IgnoreStack::all()
2731 } else {
2732 ignore_stack.clone()
2733 },
2734 scan_queue: job.scan_queue.clone(),
2735 });
2736 } else {
2737 child_entry.is_ignored = ignore_stack.is_path_ignored(&child_path, false);
2738 new_entries.push(child_entry);
2739 };
2740 }
2741
2742 self.snapshot
2743 .lock()
2744 .populate_dir(job.path.clone(), new_entries, new_ignore);
2745 for new_job in new_jobs {
2746 job.scan_queue.send(new_job).await.unwrap();
2747 }
2748
2749 Ok(())
2750 }
2751
2752 async fn process_events(&mut self, mut events: Vec<fsevent::Event>) -> bool {
2753 let mut snapshot = self.snapshot();
2754 snapshot.scan_id += 1;
2755
2756 let root_abs_path = if let Ok(abs_path) = self.fs.canonicalize(&snapshot.abs_path).await {
2757 abs_path
2758 } else {
2759 return false;
2760 };
2761 let root_char_bag = snapshot.root_char_bag;
2762 let next_entry_id = snapshot.next_entry_id.clone();
2763
2764 events.sort_unstable_by(|a, b| a.path.cmp(&b.path));
2765 events.dedup_by(|a, b| a.path.starts_with(&b.path));
2766
2767 for event in &events {
2768 match event.path.strip_prefix(&root_abs_path) {
2769 Ok(path) => snapshot.remove_path(&path),
2770 Err(_) => {
2771 log::error!(
2772 "unexpected event {:?} for root path {:?}",
2773 event.path,
2774 root_abs_path
2775 );
2776 continue;
2777 }
2778 }
2779 }
2780
2781 let (scan_queue_tx, scan_queue_rx) = channel::unbounded();
2782 for event in events {
2783 let path: Arc<Path> = match event.path.strip_prefix(&root_abs_path) {
2784 Ok(path) => Arc::from(path.to_path_buf()),
2785 Err(_) => {
2786 log::error!(
2787 "unexpected event {:?} for root path {:?}",
2788 event.path,
2789 root_abs_path
2790 );
2791 continue;
2792 }
2793 };
2794
2795 match self.fs.metadata(&event.path).await {
2796 Ok(Some(metadata)) => {
2797 let ignore_stack = snapshot.ignore_stack_for_path(&path, metadata.is_dir);
2798 let mut fs_entry = Entry::new(
2799 path.clone(),
2800 &metadata,
2801 snapshot.next_entry_id.as_ref(),
2802 snapshot.root_char_bag,
2803 );
2804 fs_entry.is_ignored = ignore_stack.is_all();
2805 snapshot.insert_entry(fs_entry, self.fs.as_ref());
2806 if metadata.is_dir {
2807 scan_queue_tx
2808 .send(ScanJob {
2809 abs_path: event.path,
2810 path,
2811 ignore_stack,
2812 scan_queue: scan_queue_tx.clone(),
2813 })
2814 .await
2815 .unwrap();
2816 }
2817 }
2818 Ok(None) => {}
2819 Err(err) => {
2820 // TODO - create a special 'error' entry in the entries tree to mark this
2821 log::error!("error reading file on event {:?}", err);
2822 }
2823 }
2824 }
2825
2826 *self.snapshot.lock() = snapshot;
2827
2828 // Scan any directories that were created as part of this event batch.
2829 drop(scan_queue_tx);
2830 self.executor
2831 .scoped(|scope| {
2832 for _ in 0..self.executor.num_cpus() {
2833 scope.spawn(async {
2834 while let Ok(job) = scan_queue_rx.recv().await {
2835 if let Err(err) = self
2836 .scan_dir(root_char_bag, next_entry_id.clone(), &job)
2837 .await
2838 {
2839 log::error!("error scanning {:?}: {}", job.abs_path, err);
2840 }
2841 }
2842 });
2843 }
2844 })
2845 .await;
2846
2847 // Attempt to detect renames only over a single batch of file-system events.
2848 self.snapshot.lock().removed_entry_ids.clear();
2849
2850 self.update_ignore_statuses().await;
2851 true
2852 }
2853
2854 async fn update_ignore_statuses(&self) {
2855 let mut snapshot = self.snapshot();
2856
2857 let mut ignores_to_update = Vec::new();
2858 let mut ignores_to_delete = Vec::new();
2859 for (parent_path, (_, scan_id)) in &snapshot.ignores {
2860 if *scan_id == snapshot.scan_id && snapshot.entry_for_path(parent_path).is_some() {
2861 ignores_to_update.push(parent_path.clone());
2862 }
2863
2864 let ignore_path = parent_path.join(&*GITIGNORE);
2865 if snapshot.entry_for_path(ignore_path).is_none() {
2866 ignores_to_delete.push(parent_path.clone());
2867 }
2868 }
2869
2870 for parent_path in ignores_to_delete {
2871 snapshot.ignores.remove(&parent_path);
2872 self.snapshot.lock().ignores.remove(&parent_path);
2873 }
2874
2875 let (ignore_queue_tx, ignore_queue_rx) = channel::unbounded();
2876 ignores_to_update.sort_unstable();
2877 let mut ignores_to_update = ignores_to_update.into_iter().peekable();
2878 while let Some(parent_path) = ignores_to_update.next() {
2879 while ignores_to_update
2880 .peek()
2881 .map_or(false, |p| p.starts_with(&parent_path))
2882 {
2883 ignores_to_update.next().unwrap();
2884 }
2885
2886 let ignore_stack = snapshot.ignore_stack_for_path(&parent_path, true);
2887 ignore_queue_tx
2888 .send(UpdateIgnoreStatusJob {
2889 path: parent_path,
2890 ignore_stack,
2891 ignore_queue: ignore_queue_tx.clone(),
2892 })
2893 .await
2894 .unwrap();
2895 }
2896 drop(ignore_queue_tx);
2897
2898 self.executor
2899 .scoped(|scope| {
2900 for _ in 0..self.executor.num_cpus() {
2901 scope.spawn(async {
2902 while let Ok(job) = ignore_queue_rx.recv().await {
2903 self.update_ignore_status(job, &snapshot).await;
2904 }
2905 });
2906 }
2907 })
2908 .await;
2909 }
2910
2911 async fn update_ignore_status(&self, job: UpdateIgnoreStatusJob, snapshot: &Snapshot) {
2912 let mut ignore_stack = job.ignore_stack;
2913 if let Some((ignore, _)) = snapshot.ignores.get(&job.path) {
2914 ignore_stack = ignore_stack.append(job.path.clone(), ignore.clone());
2915 }
2916
2917 let mut entries_by_id_edits = Vec::new();
2918 let mut entries_by_path_edits = Vec::new();
2919 for mut entry in snapshot.child_entries(&job.path).cloned() {
2920 let was_ignored = entry.is_ignored;
2921 entry.is_ignored = ignore_stack.is_path_ignored(&entry.path, entry.is_dir());
2922 if entry.is_dir() {
2923 let child_ignore_stack = if entry.is_ignored {
2924 IgnoreStack::all()
2925 } else {
2926 ignore_stack.clone()
2927 };
2928 job.ignore_queue
2929 .send(UpdateIgnoreStatusJob {
2930 path: entry.path.clone(),
2931 ignore_stack: child_ignore_stack,
2932 ignore_queue: job.ignore_queue.clone(),
2933 })
2934 .await
2935 .unwrap();
2936 }
2937
2938 if entry.is_ignored != was_ignored {
2939 let mut path_entry = snapshot.entries_by_id.get(&entry.id, &()).unwrap().clone();
2940 path_entry.scan_id = snapshot.scan_id;
2941 path_entry.is_ignored = entry.is_ignored;
2942 entries_by_id_edits.push(Edit::Insert(path_entry));
2943 entries_by_path_edits.push(Edit::Insert(entry));
2944 }
2945 }
2946
2947 let mut snapshot = self.snapshot.lock();
2948 snapshot.entries_by_path.edit(entries_by_path_edits, &());
2949 snapshot.entries_by_id.edit(entries_by_id_edits, &());
2950 }
2951}
2952
2953async fn refresh_entry(
2954 fs: &dyn Fs,
2955 snapshot: &Mutex<Snapshot>,
2956 path: Arc<Path>,
2957 abs_path: &Path,
2958) -> Result<Entry> {
2959 let root_char_bag;
2960 let next_entry_id;
2961 {
2962 let snapshot = snapshot.lock();
2963 root_char_bag = snapshot.root_char_bag;
2964 next_entry_id = snapshot.next_entry_id.clone();
2965 }
2966 let entry = Entry::new(
2967 path,
2968 &fs.metadata(abs_path)
2969 .await?
2970 .ok_or_else(|| anyhow!("could not read saved file metadata"))?,
2971 &next_entry_id,
2972 root_char_bag,
2973 );
2974 Ok(snapshot.lock().insert_entry(entry, fs))
2975}
2976
2977fn char_bag_for_path(root_char_bag: CharBag, path: &Path) -> CharBag {
2978 let mut result = root_char_bag;
2979 result.extend(
2980 path.to_string_lossy()
2981 .chars()
2982 .map(|c| c.to_ascii_lowercase()),
2983 );
2984 result
2985}
2986
2987struct ScanJob {
2988 abs_path: PathBuf,
2989 path: Arc<Path>,
2990 ignore_stack: Arc<IgnoreStack>,
2991 scan_queue: Sender<ScanJob>,
2992}
2993
2994struct UpdateIgnoreStatusJob {
2995 path: Arc<Path>,
2996 ignore_stack: Arc<IgnoreStack>,
2997 ignore_queue: Sender<UpdateIgnoreStatusJob>,
2998}
2999
3000pub trait WorktreeHandle {
3001 #[cfg(test)]
3002 fn flush_fs_events<'a>(
3003 &self,
3004 cx: &'a gpui::TestAppContext,
3005 ) -> futures::future::LocalBoxFuture<'a, ()>;
3006}
3007
3008impl WorktreeHandle for ModelHandle<Worktree> {
3009 // When the worktree's FS event stream sometimes delivers "redundant" events for FS changes that
3010 // occurred before the worktree was constructed. These events can cause the worktree to perfrom
3011 // extra directory scans, and emit extra scan-state notifications.
3012 //
3013 // This function mutates the worktree's directory and waits for those mutations to be picked up,
3014 // to ensure that all redundant FS events have already been processed.
3015 #[cfg(test)]
3016 fn flush_fs_events<'a>(
3017 &self,
3018 cx: &'a gpui::TestAppContext,
3019 ) -> futures::future::LocalBoxFuture<'a, ()> {
3020 use smol::future::FutureExt;
3021
3022 let filename = "fs-event-sentinel";
3023 let root_path = cx.read(|cx| self.read(cx).abs_path.clone());
3024 let tree = self.clone();
3025 async move {
3026 std::fs::write(root_path.join(filename), "").unwrap();
3027 tree.condition(&cx, |tree, _| tree.entry_for_path(filename).is_some())
3028 .await;
3029
3030 std::fs::remove_file(root_path.join(filename)).unwrap();
3031 tree.condition(&cx, |tree, _| tree.entry_for_path(filename).is_none())
3032 .await;
3033
3034 cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
3035 .await;
3036 }
3037 .boxed_local()
3038 }
3039}
3040
3041#[derive(Clone, Debug)]
3042struct TraversalProgress<'a> {
3043 max_path: &'a Path,
3044 count: usize,
3045 visible_count: usize,
3046 file_count: usize,
3047 visible_file_count: usize,
3048}
3049
3050impl<'a> TraversalProgress<'a> {
3051 fn count(&self, include_dirs: bool, include_ignored: bool) -> usize {
3052 match (include_ignored, include_dirs) {
3053 (true, true) => self.count,
3054 (true, false) => self.file_count,
3055 (false, true) => self.visible_count,
3056 (false, false) => self.visible_file_count,
3057 }
3058 }
3059}
3060
3061impl<'a> sum_tree::Dimension<'a, EntrySummary> for TraversalProgress<'a> {
3062 fn add_summary(&mut self, summary: &'a EntrySummary, _: &()) {
3063 self.max_path = summary.max_path.as_ref();
3064 self.count += summary.count;
3065 self.visible_count += summary.visible_count;
3066 self.file_count += summary.file_count;
3067 self.visible_file_count += summary.visible_file_count;
3068 }
3069}
3070
3071impl<'a> Default for TraversalProgress<'a> {
3072 fn default() -> Self {
3073 Self {
3074 max_path: Path::new(""),
3075 count: 0,
3076 visible_count: 0,
3077 file_count: 0,
3078 visible_file_count: 0,
3079 }
3080 }
3081}
3082
3083pub struct Traversal<'a> {
3084 cursor: sum_tree::Cursor<'a, Entry, TraversalProgress<'a>>,
3085 include_ignored: bool,
3086 include_dirs: bool,
3087}
3088
3089impl<'a> Traversal<'a> {
3090 pub fn advance(&mut self) -> bool {
3091 self.advance_to_offset(self.offset() + 1)
3092 }
3093
3094 pub fn advance_to_offset(&mut self, offset: usize) -> bool {
3095 self.cursor.seek_forward(
3096 &TraversalTarget::Count {
3097 count: offset,
3098 include_dirs: self.include_dirs,
3099 include_ignored: self.include_ignored,
3100 },
3101 Bias::Right,
3102 &(),
3103 )
3104 }
3105
3106 pub fn advance_to_sibling(&mut self) -> bool {
3107 while let Some(entry) = self.cursor.item() {
3108 self.cursor.seek_forward(
3109 &TraversalTarget::PathSuccessor(&entry.path),
3110 Bias::Left,
3111 &(),
3112 );
3113 if let Some(entry) = self.cursor.item() {
3114 if (self.include_dirs || !entry.is_dir())
3115 && (self.include_ignored || !entry.is_ignored)
3116 {
3117 return true;
3118 }
3119 }
3120 }
3121 false
3122 }
3123
3124 pub fn entry(&self) -> Option<&'a Entry> {
3125 self.cursor.item()
3126 }
3127
3128 pub fn offset(&self) -> usize {
3129 self.cursor
3130 .start()
3131 .count(self.include_dirs, self.include_ignored)
3132 }
3133}
3134
3135impl<'a> Iterator for Traversal<'a> {
3136 type Item = &'a Entry;
3137
3138 fn next(&mut self) -> Option<Self::Item> {
3139 if let Some(item) = self.entry() {
3140 self.advance();
3141 Some(item)
3142 } else {
3143 None
3144 }
3145 }
3146}
3147
3148#[derive(Debug)]
3149enum TraversalTarget<'a> {
3150 Path(&'a Path),
3151 PathSuccessor(&'a Path),
3152 Count {
3153 count: usize,
3154 include_ignored: bool,
3155 include_dirs: bool,
3156 },
3157}
3158
3159impl<'a, 'b> SeekTarget<'a, EntrySummary, TraversalProgress<'a>> for TraversalTarget<'b> {
3160 fn cmp(&self, cursor_location: &TraversalProgress<'a>, _: &()) -> Ordering {
3161 match self {
3162 TraversalTarget::Path(path) => path.cmp(&cursor_location.max_path),
3163 TraversalTarget::PathSuccessor(path) => {
3164 if !cursor_location.max_path.starts_with(path) {
3165 Ordering::Equal
3166 } else {
3167 Ordering::Greater
3168 }
3169 }
3170 TraversalTarget::Count {
3171 count,
3172 include_dirs,
3173 include_ignored,
3174 } => Ord::cmp(
3175 count,
3176 &cursor_location.count(*include_dirs, *include_ignored),
3177 ),
3178 }
3179 }
3180}
3181
3182struct ChildEntriesIter<'a> {
3183 parent_path: &'a Path,
3184 traversal: Traversal<'a>,
3185}
3186
3187impl<'a> Iterator for ChildEntriesIter<'a> {
3188 type Item = &'a Entry;
3189
3190 fn next(&mut self) -> Option<Self::Item> {
3191 if let Some(item) = self.traversal.entry() {
3192 if item.path.starts_with(&self.parent_path) {
3193 self.traversal.advance_to_sibling();
3194 return Some(item);
3195 }
3196 }
3197 None
3198 }
3199}
3200
3201impl<'a> From<&'a Entry> for proto::Entry {
3202 fn from(entry: &'a Entry) -> Self {
3203 Self {
3204 id: entry.id as u64,
3205 is_dir: entry.is_dir(),
3206 path: entry.path.to_string_lossy().to_string(),
3207 inode: entry.inode,
3208 mtime: Some(entry.mtime.into()),
3209 is_symlink: entry.is_symlink,
3210 is_ignored: entry.is_ignored,
3211 }
3212 }
3213}
3214
3215impl<'a> TryFrom<(&'a CharBag, proto::Entry)> for Entry {
3216 type Error = anyhow::Error;
3217
3218 fn try_from((root_char_bag, entry): (&'a CharBag, proto::Entry)) -> Result<Self> {
3219 if let Some(mtime) = entry.mtime {
3220 let kind = if entry.is_dir {
3221 EntryKind::Dir
3222 } else {
3223 let mut char_bag = root_char_bag.clone();
3224 char_bag.extend(entry.path.chars().map(|c| c.to_ascii_lowercase()));
3225 EntryKind::File(char_bag)
3226 };
3227 let path: Arc<Path> = Arc::from(Path::new(&entry.path));
3228 Ok(Entry {
3229 id: entry.id as usize,
3230 kind,
3231 path: path.clone(),
3232 inode: entry.inode,
3233 mtime: mtime.into(),
3234 is_symlink: entry.is_symlink,
3235 is_ignored: entry.is_ignored,
3236 })
3237 } else {
3238 Err(anyhow!(
3239 "missing mtime in remote worktree entry {:?}",
3240 entry.path
3241 ))
3242 }
3243 }
3244}
3245
3246#[cfg(test)]
3247mod tests {
3248 use super::*;
3249 use crate::fs::FakeFs;
3250 use anyhow::Result;
3251 use client::test::{FakeHttpClient, FakeServer};
3252 use fs::RealFs;
3253 use gpui::test::subscribe;
3254 use language::{tree_sitter_rust, DiagnosticEntry, LanguageServerConfig};
3255 use language::{Diagnostic, LanguageConfig};
3256 use lsp::Url;
3257 use rand::prelude::*;
3258 use serde_json::json;
3259 use std::{cell::RefCell, rc::Rc};
3260 use std::{
3261 env,
3262 fmt::Write,
3263 time::{SystemTime, UNIX_EPOCH},
3264 };
3265 use text::Point;
3266 use unindent::Unindent as _;
3267 use util::test::temp_tree;
3268
3269 #[gpui::test]
3270 async fn test_traversal(mut cx: gpui::TestAppContext) {
3271 let fs = FakeFs::new();
3272 fs.insert_tree(
3273 "/root",
3274 json!({
3275 ".gitignore": "a/b\n",
3276 "a": {
3277 "b": "",
3278 "c": "",
3279 }
3280 }),
3281 )
3282 .await;
3283
3284 let http_client = FakeHttpClient::with_404_response();
3285 let client = Client::new(http_client.clone());
3286 let user_store = cx.add_model(|cx| UserStore::new(client.clone(), http_client, cx));
3287
3288 let tree = Worktree::open_local(
3289 client,
3290 user_store,
3291 Arc::from(Path::new("/root")),
3292 Arc::new(fs),
3293 Default::default(),
3294 &mut cx.to_async(),
3295 )
3296 .await
3297 .unwrap();
3298 cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
3299 .await;
3300
3301 tree.read_with(&cx, |tree, _| {
3302 assert_eq!(
3303 tree.entries(false)
3304 .map(|entry| entry.path.as_ref())
3305 .collect::<Vec<_>>(),
3306 vec![
3307 Path::new(""),
3308 Path::new(".gitignore"),
3309 Path::new("a"),
3310 Path::new("a/c"),
3311 ]
3312 );
3313 })
3314 }
3315
3316 #[gpui::test]
3317 async fn test_save_file(mut cx: gpui::TestAppContext) {
3318 let dir = temp_tree(json!({
3319 "file1": "the old contents",
3320 }));
3321
3322 let http_client = FakeHttpClient::with_404_response();
3323 let client = Client::new(http_client.clone());
3324 let user_store = cx.add_model(|cx| UserStore::new(client.clone(), http_client, cx));
3325
3326 let tree = Worktree::open_local(
3327 client,
3328 user_store,
3329 dir.path(),
3330 Arc::new(RealFs),
3331 Default::default(),
3332 &mut cx.to_async(),
3333 )
3334 .await
3335 .unwrap();
3336 let buffer = tree
3337 .update(&mut cx, |tree, cx| tree.open_buffer("file1", cx))
3338 .await
3339 .unwrap();
3340 let save = buffer.update(&mut cx, |buffer, cx| {
3341 buffer.edit(Some(0..0), "a line of text.\n".repeat(10 * 1024), cx);
3342 buffer.save(cx).unwrap()
3343 });
3344 save.await.unwrap();
3345
3346 let new_text = std::fs::read_to_string(dir.path().join("file1")).unwrap();
3347 assert_eq!(new_text, buffer.read_with(&cx, |buffer, _| buffer.text()));
3348 }
3349
3350 #[gpui::test]
3351 async fn test_save_in_single_file_worktree(mut cx: gpui::TestAppContext) {
3352 let dir = temp_tree(json!({
3353 "file1": "the old contents",
3354 }));
3355 let file_path = dir.path().join("file1");
3356
3357 let http_client = FakeHttpClient::with_404_response();
3358 let client = Client::new(http_client.clone());
3359 let user_store = cx.add_model(|cx| UserStore::new(client.clone(), http_client, cx));
3360
3361 let tree = Worktree::open_local(
3362 client,
3363 user_store,
3364 file_path.clone(),
3365 Arc::new(RealFs),
3366 Default::default(),
3367 &mut cx.to_async(),
3368 )
3369 .await
3370 .unwrap();
3371 cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
3372 .await;
3373 cx.read(|cx| assert_eq!(tree.read(cx).file_count(), 1));
3374
3375 let buffer = tree
3376 .update(&mut cx, |tree, cx| tree.open_buffer("", cx))
3377 .await
3378 .unwrap();
3379 let save = buffer.update(&mut cx, |buffer, cx| {
3380 buffer.edit(Some(0..0), "a line of text.\n".repeat(10 * 1024), cx);
3381 buffer.save(cx).unwrap()
3382 });
3383 save.await.unwrap();
3384
3385 let new_text = std::fs::read_to_string(file_path).unwrap();
3386 assert_eq!(new_text, buffer.read_with(&cx, |buffer, _| buffer.text()));
3387 }
3388
3389 #[gpui::test]
3390 async fn test_rescan_and_remote_updates(mut cx: gpui::TestAppContext) {
3391 let dir = temp_tree(json!({
3392 "a": {
3393 "file1": "",
3394 "file2": "",
3395 "file3": "",
3396 },
3397 "b": {
3398 "c": {
3399 "file4": "",
3400 "file5": "",
3401 }
3402 }
3403 }));
3404
3405 let user_id = 5;
3406 let http_client = FakeHttpClient::with_404_response();
3407 let mut client = Client::new(http_client.clone());
3408 let server = FakeServer::for_client(user_id, &mut client, &cx).await;
3409 let user_store = server.build_user_store(client.clone(), &mut cx).await;
3410 let tree = Worktree::open_local(
3411 client,
3412 user_store.clone(),
3413 dir.path(),
3414 Arc::new(RealFs),
3415 Default::default(),
3416 &mut cx.to_async(),
3417 )
3418 .await
3419 .unwrap();
3420
3421 let buffer_for_path = |path: &'static str, cx: &mut gpui::TestAppContext| {
3422 let buffer = tree.update(cx, |tree, cx| tree.open_buffer(path, cx));
3423 async move { buffer.await.unwrap() }
3424 };
3425 let id_for_path = |path: &'static str, cx: &gpui::TestAppContext| {
3426 tree.read_with(cx, |tree, _| {
3427 tree.entry_for_path(path)
3428 .expect(&format!("no entry for path {}", path))
3429 .id
3430 })
3431 };
3432
3433 let buffer2 = buffer_for_path("a/file2", &mut cx).await;
3434 let buffer3 = buffer_for_path("a/file3", &mut cx).await;
3435 let buffer4 = buffer_for_path("b/c/file4", &mut cx).await;
3436 let buffer5 = buffer_for_path("b/c/file5", &mut cx).await;
3437
3438 let file2_id = id_for_path("a/file2", &cx);
3439 let file3_id = id_for_path("a/file3", &cx);
3440 let file4_id = id_for_path("b/c/file4", &cx);
3441
3442 // Wait for the initial scan.
3443 cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
3444 .await;
3445
3446 // Create a remote copy of this worktree.
3447 let initial_snapshot = tree.read_with(&cx, |tree, _| tree.snapshot());
3448 let remote = Worktree::remote(
3449 1,
3450 1,
3451 initial_snapshot.to_proto(&Default::default()),
3452 Client::new(http_client.clone()),
3453 user_store,
3454 Default::default(),
3455 &mut cx.to_async(),
3456 )
3457 .await
3458 .unwrap();
3459
3460 cx.read(|cx| {
3461 assert!(!buffer2.read(cx).is_dirty());
3462 assert!(!buffer3.read(cx).is_dirty());
3463 assert!(!buffer4.read(cx).is_dirty());
3464 assert!(!buffer5.read(cx).is_dirty());
3465 });
3466
3467 // Rename and delete files and directories.
3468 tree.flush_fs_events(&cx).await;
3469 std::fs::rename(dir.path().join("a/file3"), dir.path().join("b/c/file3")).unwrap();
3470 std::fs::remove_file(dir.path().join("b/c/file5")).unwrap();
3471 std::fs::rename(dir.path().join("b/c"), dir.path().join("d")).unwrap();
3472 std::fs::rename(dir.path().join("a/file2"), dir.path().join("a/file2.new")).unwrap();
3473 tree.flush_fs_events(&cx).await;
3474
3475 let expected_paths = vec![
3476 "a",
3477 "a/file1",
3478 "a/file2.new",
3479 "b",
3480 "d",
3481 "d/file3",
3482 "d/file4",
3483 ];
3484
3485 cx.read(|app| {
3486 assert_eq!(
3487 tree.read(app)
3488 .paths()
3489 .map(|p| p.to_str().unwrap())
3490 .collect::<Vec<_>>(),
3491 expected_paths
3492 );
3493
3494 assert_eq!(id_for_path("a/file2.new", &cx), file2_id);
3495 assert_eq!(id_for_path("d/file3", &cx), file3_id);
3496 assert_eq!(id_for_path("d/file4", &cx), file4_id);
3497
3498 assert_eq!(
3499 buffer2.read(app).file().unwrap().path().as_ref(),
3500 Path::new("a/file2.new")
3501 );
3502 assert_eq!(
3503 buffer3.read(app).file().unwrap().path().as_ref(),
3504 Path::new("d/file3")
3505 );
3506 assert_eq!(
3507 buffer4.read(app).file().unwrap().path().as_ref(),
3508 Path::new("d/file4")
3509 );
3510 assert_eq!(
3511 buffer5.read(app).file().unwrap().path().as_ref(),
3512 Path::new("b/c/file5")
3513 );
3514
3515 assert!(!buffer2.read(app).file().unwrap().is_deleted());
3516 assert!(!buffer3.read(app).file().unwrap().is_deleted());
3517 assert!(!buffer4.read(app).file().unwrap().is_deleted());
3518 assert!(buffer5.read(app).file().unwrap().is_deleted());
3519 });
3520
3521 // Update the remote worktree. Check that it becomes consistent with the
3522 // local worktree.
3523 remote.update(&mut cx, |remote, cx| {
3524 let update_message =
3525 tree.read(cx)
3526 .snapshot()
3527 .build_update(&initial_snapshot, 1, 1, true);
3528 remote
3529 .as_remote_mut()
3530 .unwrap()
3531 .snapshot
3532 .apply_update(update_message)
3533 .unwrap();
3534
3535 assert_eq!(
3536 remote
3537 .paths()
3538 .map(|p| p.to_str().unwrap())
3539 .collect::<Vec<_>>(),
3540 expected_paths
3541 );
3542 });
3543 }
3544
3545 #[gpui::test]
3546 async fn test_rescan_with_gitignore(mut cx: gpui::TestAppContext) {
3547 let dir = temp_tree(json!({
3548 ".git": {},
3549 ".gitignore": "ignored-dir\n",
3550 "tracked-dir": {
3551 "tracked-file1": "tracked contents",
3552 },
3553 "ignored-dir": {
3554 "ignored-file1": "ignored contents",
3555 }
3556 }));
3557
3558 let http_client = FakeHttpClient::with_404_response();
3559 let client = Client::new(http_client.clone());
3560 let user_store = cx.add_model(|cx| UserStore::new(client.clone(), http_client, cx));
3561
3562 let tree = Worktree::open_local(
3563 client,
3564 user_store,
3565 dir.path(),
3566 Arc::new(RealFs),
3567 Default::default(),
3568 &mut cx.to_async(),
3569 )
3570 .await
3571 .unwrap();
3572 cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
3573 .await;
3574 tree.flush_fs_events(&cx).await;
3575 cx.read(|cx| {
3576 let tree = tree.read(cx);
3577 let tracked = tree.entry_for_path("tracked-dir/tracked-file1").unwrap();
3578 let ignored = tree.entry_for_path("ignored-dir/ignored-file1").unwrap();
3579 assert_eq!(tracked.is_ignored, false);
3580 assert_eq!(ignored.is_ignored, true);
3581 });
3582
3583 std::fs::write(dir.path().join("tracked-dir/tracked-file2"), "").unwrap();
3584 std::fs::write(dir.path().join("ignored-dir/ignored-file2"), "").unwrap();
3585 tree.flush_fs_events(&cx).await;
3586 cx.read(|cx| {
3587 let tree = tree.read(cx);
3588 let dot_git = tree.entry_for_path(".git").unwrap();
3589 let tracked = tree.entry_for_path("tracked-dir/tracked-file2").unwrap();
3590 let ignored = tree.entry_for_path("ignored-dir/ignored-file2").unwrap();
3591 assert_eq!(tracked.is_ignored, false);
3592 assert_eq!(ignored.is_ignored, true);
3593 assert_eq!(dot_git.is_ignored, true);
3594 });
3595 }
3596
3597 #[gpui::test]
3598 async fn test_buffer_deduping(mut cx: gpui::TestAppContext) {
3599 let user_id = 100;
3600 let http_client = FakeHttpClient::with_404_response();
3601 let mut client = Client::new(http_client);
3602 let server = FakeServer::for_client(user_id, &mut client, &cx).await;
3603 let user_store = server.build_user_store(client.clone(), &mut cx).await;
3604
3605 let fs = Arc::new(FakeFs::new());
3606 fs.insert_tree(
3607 "/the-dir",
3608 json!({
3609 "a.txt": "a-contents",
3610 "b.txt": "b-contents",
3611 }),
3612 )
3613 .await;
3614
3615 let worktree = Worktree::open_local(
3616 client.clone(),
3617 user_store,
3618 "/the-dir".as_ref(),
3619 fs,
3620 Default::default(),
3621 &mut cx.to_async(),
3622 )
3623 .await
3624 .unwrap();
3625
3626 // Spawn multiple tasks to open paths, repeating some paths.
3627 let (buffer_a_1, buffer_b, buffer_a_2) = worktree.update(&mut cx, |worktree, cx| {
3628 (
3629 worktree.open_buffer("a.txt", cx),
3630 worktree.open_buffer("b.txt", cx),
3631 worktree.open_buffer("a.txt", cx),
3632 )
3633 });
3634
3635 let buffer_a_1 = buffer_a_1.await.unwrap();
3636 let buffer_a_2 = buffer_a_2.await.unwrap();
3637 let buffer_b = buffer_b.await.unwrap();
3638 assert_eq!(buffer_a_1.read_with(&cx, |b, _| b.text()), "a-contents");
3639 assert_eq!(buffer_b.read_with(&cx, |b, _| b.text()), "b-contents");
3640
3641 // There is only one buffer per path.
3642 let buffer_a_id = buffer_a_1.id();
3643 assert_eq!(buffer_a_2.id(), buffer_a_id);
3644
3645 // Open the same path again while it is still open.
3646 drop(buffer_a_1);
3647 let buffer_a_3 = worktree
3648 .update(&mut cx, |worktree, cx| worktree.open_buffer("a.txt", cx))
3649 .await
3650 .unwrap();
3651
3652 // There's still only one buffer per path.
3653 assert_eq!(buffer_a_3.id(), buffer_a_id);
3654 }
3655
3656 #[gpui::test]
3657 async fn test_buffer_is_dirty(mut cx: gpui::TestAppContext) {
3658 use std::fs;
3659
3660 let dir = temp_tree(json!({
3661 "file1": "abc",
3662 "file2": "def",
3663 "file3": "ghi",
3664 }));
3665 let http_client = FakeHttpClient::with_404_response();
3666 let client = Client::new(http_client.clone());
3667 let user_store = cx.add_model(|cx| UserStore::new(client.clone(), http_client, cx));
3668
3669 let tree = Worktree::open_local(
3670 client,
3671 user_store,
3672 dir.path(),
3673 Arc::new(RealFs),
3674 Default::default(),
3675 &mut cx.to_async(),
3676 )
3677 .await
3678 .unwrap();
3679 tree.flush_fs_events(&cx).await;
3680 cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
3681 .await;
3682
3683 let buffer1 = tree
3684 .update(&mut cx, |tree, cx| tree.open_buffer("file1", cx))
3685 .await
3686 .unwrap();
3687 let events = Rc::new(RefCell::new(Vec::new()));
3688
3689 // initially, the buffer isn't dirty.
3690 buffer1.update(&mut cx, |buffer, cx| {
3691 cx.subscribe(&buffer1, {
3692 let events = events.clone();
3693 move |_, _, event, _| events.borrow_mut().push(event.clone())
3694 })
3695 .detach();
3696
3697 assert!(!buffer.is_dirty());
3698 assert!(events.borrow().is_empty());
3699
3700 buffer.edit(vec![1..2], "", cx);
3701 });
3702
3703 // after the first edit, the buffer is dirty, and emits a dirtied event.
3704 buffer1.update(&mut cx, |buffer, cx| {
3705 assert!(buffer.text() == "ac");
3706 assert!(buffer.is_dirty());
3707 assert_eq!(
3708 *events.borrow(),
3709 &[language::Event::Edited, language::Event::Dirtied]
3710 );
3711 events.borrow_mut().clear();
3712 buffer.did_save(buffer.version(), buffer.file().unwrap().mtime(), None, cx);
3713 });
3714
3715 // after saving, the buffer is not dirty, and emits a saved event.
3716 buffer1.update(&mut cx, |buffer, cx| {
3717 assert!(!buffer.is_dirty());
3718 assert_eq!(*events.borrow(), &[language::Event::Saved]);
3719 events.borrow_mut().clear();
3720
3721 buffer.edit(vec![1..1], "B", cx);
3722 buffer.edit(vec![2..2], "D", cx);
3723 });
3724
3725 // after editing again, the buffer is dirty, and emits another dirty event.
3726 buffer1.update(&mut cx, |buffer, cx| {
3727 assert!(buffer.text() == "aBDc");
3728 assert!(buffer.is_dirty());
3729 assert_eq!(
3730 *events.borrow(),
3731 &[
3732 language::Event::Edited,
3733 language::Event::Dirtied,
3734 language::Event::Edited,
3735 ],
3736 );
3737 events.borrow_mut().clear();
3738
3739 // TODO - currently, after restoring the buffer to its
3740 // previously-saved state, the is still considered dirty.
3741 buffer.edit([1..3], "", cx);
3742 assert!(buffer.text() == "ac");
3743 assert!(buffer.is_dirty());
3744 });
3745
3746 assert_eq!(*events.borrow(), &[language::Event::Edited]);
3747
3748 // When a file is deleted, the buffer is considered dirty.
3749 let events = Rc::new(RefCell::new(Vec::new()));
3750 let buffer2 = tree
3751 .update(&mut cx, |tree, cx| tree.open_buffer("file2", cx))
3752 .await
3753 .unwrap();
3754 buffer2.update(&mut cx, |_, cx| {
3755 cx.subscribe(&buffer2, {
3756 let events = events.clone();
3757 move |_, _, event, _| events.borrow_mut().push(event.clone())
3758 })
3759 .detach();
3760 });
3761
3762 fs::remove_file(dir.path().join("file2")).unwrap();
3763 buffer2.condition(&cx, |b, _| b.is_dirty()).await;
3764 assert_eq!(
3765 *events.borrow(),
3766 &[language::Event::Dirtied, language::Event::FileHandleChanged]
3767 );
3768
3769 // When a file is already dirty when deleted, we don't emit a Dirtied event.
3770 let events = Rc::new(RefCell::new(Vec::new()));
3771 let buffer3 = tree
3772 .update(&mut cx, |tree, cx| tree.open_buffer("file3", cx))
3773 .await
3774 .unwrap();
3775 buffer3.update(&mut cx, |_, cx| {
3776 cx.subscribe(&buffer3, {
3777 let events = events.clone();
3778 move |_, _, event, _| events.borrow_mut().push(event.clone())
3779 })
3780 .detach();
3781 });
3782
3783 tree.flush_fs_events(&cx).await;
3784 buffer3.update(&mut cx, |buffer, cx| {
3785 buffer.edit(Some(0..0), "x", cx);
3786 });
3787 events.borrow_mut().clear();
3788 fs::remove_file(dir.path().join("file3")).unwrap();
3789 buffer3
3790 .condition(&cx, |_, _| !events.borrow().is_empty())
3791 .await;
3792 assert_eq!(*events.borrow(), &[language::Event::FileHandleChanged]);
3793 cx.read(|cx| assert!(buffer3.read(cx).is_dirty()));
3794 }
3795
3796 #[gpui::test]
3797 async fn test_buffer_file_changes_on_disk(mut cx: gpui::TestAppContext) {
3798 use std::fs;
3799
3800 let initial_contents = "aaa\nbbbbb\nc\n";
3801 let dir = temp_tree(json!({ "the-file": initial_contents }));
3802 let http_client = FakeHttpClient::with_404_response();
3803 let client = Client::new(http_client.clone());
3804 let user_store = cx.add_model(|cx| UserStore::new(client.clone(), http_client, cx));
3805
3806 let tree = Worktree::open_local(
3807 client,
3808 user_store,
3809 dir.path(),
3810 Arc::new(RealFs),
3811 Default::default(),
3812 &mut cx.to_async(),
3813 )
3814 .await
3815 .unwrap();
3816 cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
3817 .await;
3818
3819 let abs_path = dir.path().join("the-file");
3820 let buffer = tree
3821 .update(&mut cx, |tree, cx| {
3822 tree.open_buffer(Path::new("the-file"), cx)
3823 })
3824 .await
3825 .unwrap();
3826
3827 // TODO
3828 // Add a cursor on each row.
3829 // let selection_set_id = buffer.update(&mut cx, |buffer, cx| {
3830 // assert!(!buffer.is_dirty());
3831 // buffer.add_selection_set(
3832 // &(0..3)
3833 // .map(|row| Selection {
3834 // id: row as usize,
3835 // start: Point::new(row, 1),
3836 // end: Point::new(row, 1),
3837 // reversed: false,
3838 // goal: SelectionGoal::None,
3839 // })
3840 // .collect::<Vec<_>>(),
3841 // cx,
3842 // )
3843 // });
3844
3845 // Change the file on disk, adding two new lines of text, and removing
3846 // one line.
3847 buffer.read_with(&cx, |buffer, _| {
3848 assert!(!buffer.is_dirty());
3849 assert!(!buffer.has_conflict());
3850 });
3851 let new_contents = "AAAA\naaa\nBB\nbbbbb\n";
3852 fs::write(&abs_path, new_contents).unwrap();
3853
3854 // Because the buffer was not modified, it is reloaded from disk. Its
3855 // contents are edited according to the diff between the old and new
3856 // file contents.
3857 buffer
3858 .condition(&cx, |buffer, _| buffer.text() == new_contents)
3859 .await;
3860
3861 buffer.update(&mut cx, |buffer, _| {
3862 assert_eq!(buffer.text(), new_contents);
3863 assert!(!buffer.is_dirty());
3864 assert!(!buffer.has_conflict());
3865
3866 // TODO
3867 // let cursor_positions = buffer
3868 // .selection_set(selection_set_id)
3869 // .unwrap()
3870 // .selections::<Point>(&*buffer)
3871 // .map(|selection| {
3872 // assert_eq!(selection.start, selection.end);
3873 // selection.start
3874 // })
3875 // .collect::<Vec<_>>();
3876 // assert_eq!(
3877 // cursor_positions,
3878 // [Point::new(1, 1), Point::new(3, 1), Point::new(4, 0)]
3879 // );
3880 });
3881
3882 // Modify the buffer
3883 buffer.update(&mut cx, |buffer, cx| {
3884 buffer.edit(vec![0..0], " ", cx);
3885 assert!(buffer.is_dirty());
3886 assert!(!buffer.has_conflict());
3887 });
3888
3889 // Change the file on disk again, adding blank lines to the beginning.
3890 fs::write(&abs_path, "\n\n\nAAAA\naaa\nBB\nbbbbb\n").unwrap();
3891
3892 // Because the buffer is modified, it doesn't reload from disk, but is
3893 // marked as having a conflict.
3894 buffer
3895 .condition(&cx, |buffer, _| buffer.has_conflict())
3896 .await;
3897 }
3898
3899 #[gpui::test]
3900 async fn test_language_server_diagnostics(mut cx: gpui::TestAppContext) {
3901 let (language_server_config, mut fake_server) =
3902 LanguageServerConfig::fake(cx.background()).await;
3903 let progress_token = language_server_config
3904 .disk_based_diagnostics_progress_token
3905 .clone()
3906 .unwrap();
3907 let mut languages = LanguageRegistry::new();
3908 languages.add(Arc::new(Language::new(
3909 LanguageConfig {
3910 name: "Rust".to_string(),
3911 path_suffixes: vec!["rs".to_string()],
3912 language_server: Some(language_server_config),
3913 ..Default::default()
3914 },
3915 Some(tree_sitter_rust::language()),
3916 )));
3917
3918 let dir = temp_tree(json!({
3919 "a.rs": "fn a() { A }",
3920 "b.rs": "const y: i32 = 1",
3921 }));
3922
3923 let http_client = FakeHttpClient::with_404_response();
3924 let client = Client::new(http_client.clone());
3925 let user_store = cx.add_model(|cx| UserStore::new(client.clone(), http_client, cx));
3926
3927 let tree = Worktree::open_local(
3928 client,
3929 user_store,
3930 dir.path(),
3931 Arc::new(RealFs),
3932 Arc::new(languages),
3933 &mut cx.to_async(),
3934 )
3935 .await
3936 .unwrap();
3937 cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
3938 .await;
3939
3940 // Cause worktree to start the fake language server
3941 let _buffer = tree
3942 .update(&mut cx, |tree, cx| tree.open_buffer("b.rs", cx))
3943 .await
3944 .unwrap();
3945
3946 let mut events = subscribe(&tree, &mut cx);
3947
3948 fake_server.start_progress(&progress_token).await;
3949 assert_eq!(
3950 events.next().await.unwrap(),
3951 Event::DiskBasedDiagnosticsUpdating
3952 );
3953
3954 fake_server.start_progress(&progress_token).await;
3955 fake_server.end_progress(&progress_token).await;
3956 fake_server.start_progress(&progress_token).await;
3957
3958 fake_server
3959 .notify::<lsp::notification::PublishDiagnostics>(lsp::PublishDiagnosticsParams {
3960 uri: Url::from_file_path(dir.path().join("a.rs")).unwrap(),
3961 version: None,
3962 diagnostics: vec![lsp::Diagnostic {
3963 range: lsp::Range::new(lsp::Position::new(0, 9), lsp::Position::new(0, 10)),
3964 severity: Some(lsp::DiagnosticSeverity::ERROR),
3965 message: "undefined variable 'A'".to_string(),
3966 ..Default::default()
3967 }],
3968 })
3969 .await;
3970 assert_eq!(
3971 events.next().await.unwrap(),
3972 Event::DiagnosticsUpdated(Arc::from(Path::new("a.rs")))
3973 );
3974
3975 fake_server.end_progress(&progress_token).await;
3976 fake_server.end_progress(&progress_token).await;
3977 assert_eq!(
3978 events.next().await.unwrap(),
3979 Event::DiskBasedDiagnosticsUpdated
3980 );
3981
3982 let buffer = tree
3983 .update(&mut cx, |tree, cx| tree.open_buffer("a.rs", cx))
3984 .await
3985 .unwrap();
3986
3987 buffer.read_with(&cx, |buffer, _| {
3988 let snapshot = buffer.snapshot();
3989 let diagnostics = snapshot
3990 .diagnostics_in_range::<_, Point>(0..buffer.len())
3991 .collect::<Vec<_>>();
3992 assert_eq!(
3993 diagnostics,
3994 &[DiagnosticEntry {
3995 range: Point::new(0, 9)..Point::new(0, 10),
3996 diagnostic: Diagnostic {
3997 severity: lsp::DiagnosticSeverity::ERROR,
3998 message: "undefined variable 'A'".to_string(),
3999 group_id: 0,
4000 is_primary: true,
4001 ..Default::default()
4002 }
4003 }]
4004 )
4005 });
4006 }
4007
4008 #[gpui::test]
4009 async fn test_grouped_diagnostics(mut cx: gpui::TestAppContext) {
4010 let fs = Arc::new(FakeFs::new());
4011 let http_client = FakeHttpClient::with_404_response();
4012 let client = Client::new(http_client.clone());
4013 let user_store = cx.add_model(|cx| UserStore::new(client.clone(), http_client, cx));
4014
4015 fs.insert_tree(
4016 "/the-dir",
4017 json!({
4018 "a.rs": "
4019 fn foo(mut v: Vec<usize>) {
4020 for x in &v {
4021 v.push(1);
4022 }
4023 }
4024 "
4025 .unindent(),
4026 }),
4027 )
4028 .await;
4029
4030 let worktree = Worktree::open_local(
4031 client.clone(),
4032 user_store,
4033 "/the-dir".as_ref(),
4034 fs,
4035 Default::default(),
4036 &mut cx.to_async(),
4037 )
4038 .await
4039 .unwrap();
4040
4041 let buffer = worktree
4042 .update(&mut cx, |tree, cx| tree.open_buffer("a.rs", cx))
4043 .await
4044 .unwrap();
4045
4046 let buffer_uri = Url::from_file_path("/the-dir/a.rs").unwrap();
4047 let message = lsp::PublishDiagnosticsParams {
4048 uri: buffer_uri.clone(),
4049 diagnostics: vec![
4050 lsp::Diagnostic {
4051 range: lsp::Range::new(lsp::Position::new(1, 8), lsp::Position::new(1, 9)),
4052 severity: Some(DiagnosticSeverity::WARNING),
4053 message: "error 1".to_string(),
4054 related_information: Some(vec![lsp::DiagnosticRelatedInformation {
4055 location: lsp::Location {
4056 uri: buffer_uri.clone(),
4057 range: lsp::Range::new(
4058 lsp::Position::new(1, 8),
4059 lsp::Position::new(1, 9),
4060 ),
4061 },
4062 message: "error 1 hint 1".to_string(),
4063 }]),
4064 ..Default::default()
4065 },
4066 lsp::Diagnostic {
4067 range: lsp::Range::new(lsp::Position::new(1, 8), lsp::Position::new(1, 9)),
4068 severity: Some(DiagnosticSeverity::HINT),
4069 message: "error 1 hint 1".to_string(),
4070 related_information: Some(vec![lsp::DiagnosticRelatedInformation {
4071 location: lsp::Location {
4072 uri: buffer_uri.clone(),
4073 range: lsp::Range::new(
4074 lsp::Position::new(1, 8),
4075 lsp::Position::new(1, 9),
4076 ),
4077 },
4078 message: "original diagnostic".to_string(),
4079 }]),
4080 ..Default::default()
4081 },
4082 lsp::Diagnostic {
4083 range: lsp::Range::new(lsp::Position::new(2, 8), lsp::Position::new(2, 17)),
4084 severity: Some(DiagnosticSeverity::ERROR),
4085 message: "error 2".to_string(),
4086 related_information: Some(vec![
4087 lsp::DiagnosticRelatedInformation {
4088 location: lsp::Location {
4089 uri: buffer_uri.clone(),
4090 range: lsp::Range::new(
4091 lsp::Position::new(1, 13),
4092 lsp::Position::new(1, 15),
4093 ),
4094 },
4095 message: "error 2 hint 1".to_string(),
4096 },
4097 lsp::DiagnosticRelatedInformation {
4098 location: lsp::Location {
4099 uri: buffer_uri.clone(),
4100 range: lsp::Range::new(
4101 lsp::Position::new(1, 13),
4102 lsp::Position::new(1, 15),
4103 ),
4104 },
4105 message: "error 2 hint 2".to_string(),
4106 },
4107 ]),
4108 ..Default::default()
4109 },
4110 lsp::Diagnostic {
4111 range: lsp::Range::new(lsp::Position::new(1, 13), lsp::Position::new(1, 15)),
4112 severity: Some(DiagnosticSeverity::HINT),
4113 message: "error 2 hint 1".to_string(),
4114 related_information: Some(vec![lsp::DiagnosticRelatedInformation {
4115 location: lsp::Location {
4116 uri: buffer_uri.clone(),
4117 range: lsp::Range::new(
4118 lsp::Position::new(2, 8),
4119 lsp::Position::new(2, 17),
4120 ),
4121 },
4122 message: "original diagnostic".to_string(),
4123 }]),
4124 ..Default::default()
4125 },
4126 lsp::Diagnostic {
4127 range: lsp::Range::new(lsp::Position::new(1, 13), lsp::Position::new(1, 15)),
4128 severity: Some(DiagnosticSeverity::HINT),
4129 message: "error 2 hint 2".to_string(),
4130 related_information: Some(vec![lsp::DiagnosticRelatedInformation {
4131 location: lsp::Location {
4132 uri: buffer_uri.clone(),
4133 range: lsp::Range::new(
4134 lsp::Position::new(2, 8),
4135 lsp::Position::new(2, 17),
4136 ),
4137 },
4138 message: "original diagnostic".to_string(),
4139 }]),
4140 ..Default::default()
4141 },
4142 ],
4143 version: None,
4144 };
4145
4146 worktree
4147 .update(&mut cx, |tree, cx| {
4148 tree.update_diagnostics(message, &Default::default(), cx)
4149 })
4150 .unwrap();
4151 let buffer = buffer.read_with(&cx, |buffer, _| buffer.snapshot());
4152
4153 assert_eq!(
4154 buffer
4155 .diagnostics_in_range::<_, Point>(0..buffer.len())
4156 .collect::<Vec<_>>(),
4157 &[
4158 DiagnosticEntry {
4159 range: Point::new(1, 8)..Point::new(1, 9),
4160 diagnostic: Diagnostic {
4161 severity: DiagnosticSeverity::WARNING,
4162 message: "error 1".to_string(),
4163 group_id: 0,
4164 is_primary: true,
4165 ..Default::default()
4166 }
4167 },
4168 DiagnosticEntry {
4169 range: Point::new(1, 8)..Point::new(1, 9),
4170 diagnostic: Diagnostic {
4171 severity: DiagnosticSeverity::HINT,
4172 message: "error 1 hint 1".to_string(),
4173 group_id: 0,
4174 is_primary: false,
4175 ..Default::default()
4176 }
4177 },
4178 DiagnosticEntry {
4179 range: Point::new(1, 13)..Point::new(1, 15),
4180 diagnostic: Diagnostic {
4181 severity: DiagnosticSeverity::HINT,
4182 message: "error 2 hint 1".to_string(),
4183 group_id: 1,
4184 is_primary: false,
4185 ..Default::default()
4186 }
4187 },
4188 DiagnosticEntry {
4189 range: Point::new(1, 13)..Point::new(1, 15),
4190 diagnostic: Diagnostic {
4191 severity: DiagnosticSeverity::HINT,
4192 message: "error 2 hint 2".to_string(),
4193 group_id: 1,
4194 is_primary: false,
4195 ..Default::default()
4196 }
4197 },
4198 DiagnosticEntry {
4199 range: Point::new(2, 8)..Point::new(2, 17),
4200 diagnostic: Diagnostic {
4201 severity: DiagnosticSeverity::ERROR,
4202 message: "error 2".to_string(),
4203 group_id: 1,
4204 is_primary: true,
4205 ..Default::default()
4206 }
4207 }
4208 ]
4209 );
4210
4211 assert_eq!(
4212 buffer.diagnostic_group::<Point>(0).collect::<Vec<_>>(),
4213 &[
4214 DiagnosticEntry {
4215 range: Point::new(1, 8)..Point::new(1, 9),
4216 diagnostic: Diagnostic {
4217 severity: DiagnosticSeverity::WARNING,
4218 message: "error 1".to_string(),
4219 group_id: 0,
4220 is_primary: true,
4221 ..Default::default()
4222 }
4223 },
4224 DiagnosticEntry {
4225 range: Point::new(1, 8)..Point::new(1, 9),
4226 diagnostic: Diagnostic {
4227 severity: DiagnosticSeverity::HINT,
4228 message: "error 1 hint 1".to_string(),
4229 group_id: 0,
4230 is_primary: false,
4231 ..Default::default()
4232 }
4233 },
4234 ]
4235 );
4236 assert_eq!(
4237 buffer.diagnostic_group::<Point>(1).collect::<Vec<_>>(),
4238 &[
4239 DiagnosticEntry {
4240 range: Point::new(1, 13)..Point::new(1, 15),
4241 diagnostic: Diagnostic {
4242 severity: DiagnosticSeverity::HINT,
4243 message: "error 2 hint 1".to_string(),
4244 group_id: 1,
4245 is_primary: false,
4246 ..Default::default()
4247 }
4248 },
4249 DiagnosticEntry {
4250 range: Point::new(1, 13)..Point::new(1, 15),
4251 diagnostic: Diagnostic {
4252 severity: DiagnosticSeverity::HINT,
4253 message: "error 2 hint 2".to_string(),
4254 group_id: 1,
4255 is_primary: false,
4256 ..Default::default()
4257 }
4258 },
4259 DiagnosticEntry {
4260 range: Point::new(2, 8)..Point::new(2, 17),
4261 diagnostic: Diagnostic {
4262 severity: DiagnosticSeverity::ERROR,
4263 message: "error 2".to_string(),
4264 group_id: 1,
4265 is_primary: true,
4266 ..Default::default()
4267 }
4268 }
4269 ]
4270 );
4271 }
4272
4273 #[gpui::test(iterations = 100)]
4274 fn test_random(mut rng: StdRng) {
4275 let operations = env::var("OPERATIONS")
4276 .map(|o| o.parse().unwrap())
4277 .unwrap_or(40);
4278 let initial_entries = env::var("INITIAL_ENTRIES")
4279 .map(|o| o.parse().unwrap())
4280 .unwrap_or(20);
4281
4282 let root_dir = tempdir::TempDir::new("worktree-test").unwrap();
4283 for _ in 0..initial_entries {
4284 randomly_mutate_tree(root_dir.path(), 1.0, &mut rng).unwrap();
4285 }
4286 log::info!("Generated initial tree");
4287
4288 let (notify_tx, _notify_rx) = smol::channel::unbounded();
4289 let fs = Arc::new(RealFs);
4290 let next_entry_id = Arc::new(AtomicUsize::new(0));
4291 let mut initial_snapshot = Snapshot {
4292 id: WorktreeId::from_usize(0),
4293 scan_id: 0,
4294 abs_path: root_dir.path().into(),
4295 entries_by_path: Default::default(),
4296 entries_by_id: Default::default(),
4297 removed_entry_ids: Default::default(),
4298 ignores: Default::default(),
4299 root_name: Default::default(),
4300 root_char_bag: Default::default(),
4301 next_entry_id: next_entry_id.clone(),
4302 };
4303 initial_snapshot.insert_entry(
4304 Entry::new(
4305 Path::new("").into(),
4306 &smol::block_on(fs.metadata(root_dir.path()))
4307 .unwrap()
4308 .unwrap(),
4309 &next_entry_id,
4310 Default::default(),
4311 ),
4312 fs.as_ref(),
4313 );
4314 let mut scanner = BackgroundScanner::new(
4315 Arc::new(Mutex::new(initial_snapshot.clone())),
4316 notify_tx,
4317 fs.clone(),
4318 Arc::new(gpui::executor::Background::new()),
4319 );
4320 smol::block_on(scanner.scan_dirs()).unwrap();
4321 scanner.snapshot().check_invariants();
4322
4323 let mut events = Vec::new();
4324 let mut snapshots = Vec::new();
4325 let mut mutations_len = operations;
4326 while mutations_len > 1 {
4327 if !events.is_empty() && rng.gen_bool(0.4) {
4328 let len = rng.gen_range(0..=events.len());
4329 let to_deliver = events.drain(0..len).collect::<Vec<_>>();
4330 log::info!("Delivering events: {:#?}", to_deliver);
4331 smol::block_on(scanner.process_events(to_deliver));
4332 scanner.snapshot().check_invariants();
4333 } else {
4334 events.extend(randomly_mutate_tree(root_dir.path(), 0.6, &mut rng).unwrap());
4335 mutations_len -= 1;
4336 }
4337
4338 if rng.gen_bool(0.2) {
4339 snapshots.push(scanner.snapshot());
4340 }
4341 }
4342 log::info!("Quiescing: {:#?}", events);
4343 smol::block_on(scanner.process_events(events));
4344 scanner.snapshot().check_invariants();
4345
4346 let (notify_tx, _notify_rx) = smol::channel::unbounded();
4347 let mut new_scanner = BackgroundScanner::new(
4348 Arc::new(Mutex::new(initial_snapshot)),
4349 notify_tx,
4350 scanner.fs.clone(),
4351 scanner.executor.clone(),
4352 );
4353 smol::block_on(new_scanner.scan_dirs()).unwrap();
4354 assert_eq!(
4355 scanner.snapshot().to_vec(true),
4356 new_scanner.snapshot().to_vec(true)
4357 );
4358
4359 for mut prev_snapshot in snapshots {
4360 let include_ignored = rng.gen::<bool>();
4361 if !include_ignored {
4362 let mut entries_by_path_edits = Vec::new();
4363 let mut entries_by_id_edits = Vec::new();
4364 for entry in prev_snapshot
4365 .entries_by_id
4366 .cursor::<()>()
4367 .filter(|e| e.is_ignored)
4368 {
4369 entries_by_path_edits.push(Edit::Remove(PathKey(entry.path.clone())));
4370 entries_by_id_edits.push(Edit::Remove(entry.id));
4371 }
4372
4373 prev_snapshot
4374 .entries_by_path
4375 .edit(entries_by_path_edits, &());
4376 prev_snapshot.entries_by_id.edit(entries_by_id_edits, &());
4377 }
4378
4379 let update = scanner
4380 .snapshot()
4381 .build_update(&prev_snapshot, 0, 0, include_ignored);
4382 prev_snapshot.apply_update(update).unwrap();
4383 assert_eq!(
4384 prev_snapshot.to_vec(true),
4385 scanner.snapshot().to_vec(include_ignored)
4386 );
4387 }
4388 }
4389
4390 fn randomly_mutate_tree(
4391 root_path: &Path,
4392 insertion_probability: f64,
4393 rng: &mut impl Rng,
4394 ) -> Result<Vec<fsevent::Event>> {
4395 let root_path = root_path.canonicalize().unwrap();
4396 let (dirs, files) = read_dir_recursive(root_path.clone());
4397
4398 let mut events = Vec::new();
4399 let mut record_event = |path: PathBuf| {
4400 events.push(fsevent::Event {
4401 event_id: SystemTime::now()
4402 .duration_since(UNIX_EPOCH)
4403 .unwrap()
4404 .as_secs(),
4405 flags: fsevent::StreamFlags::empty(),
4406 path,
4407 });
4408 };
4409
4410 if (files.is_empty() && dirs.len() == 1) || rng.gen_bool(insertion_probability) {
4411 let path = dirs.choose(rng).unwrap();
4412 let new_path = path.join(gen_name(rng));
4413
4414 if rng.gen() {
4415 log::info!("Creating dir {:?}", new_path.strip_prefix(root_path)?);
4416 std::fs::create_dir(&new_path)?;
4417 } else {
4418 log::info!("Creating file {:?}", new_path.strip_prefix(root_path)?);
4419 std::fs::write(&new_path, "")?;
4420 }
4421 record_event(new_path);
4422 } else if rng.gen_bool(0.05) {
4423 let ignore_dir_path = dirs.choose(rng).unwrap();
4424 let ignore_path = ignore_dir_path.join(&*GITIGNORE);
4425
4426 let (subdirs, subfiles) = read_dir_recursive(ignore_dir_path.clone());
4427 let files_to_ignore = {
4428 let len = rng.gen_range(0..=subfiles.len());
4429 subfiles.choose_multiple(rng, len)
4430 };
4431 let dirs_to_ignore = {
4432 let len = rng.gen_range(0..subdirs.len());
4433 subdirs.choose_multiple(rng, len)
4434 };
4435
4436 let mut ignore_contents = String::new();
4437 for path_to_ignore in files_to_ignore.chain(dirs_to_ignore) {
4438 write!(
4439 ignore_contents,
4440 "{}\n",
4441 path_to_ignore
4442 .strip_prefix(&ignore_dir_path)?
4443 .to_str()
4444 .unwrap()
4445 )
4446 .unwrap();
4447 }
4448 log::info!(
4449 "Creating {:?} with contents:\n{}",
4450 ignore_path.strip_prefix(&root_path)?,
4451 ignore_contents
4452 );
4453 std::fs::write(&ignore_path, ignore_contents).unwrap();
4454 record_event(ignore_path);
4455 } else {
4456 let old_path = {
4457 let file_path = files.choose(rng);
4458 let dir_path = dirs[1..].choose(rng);
4459 file_path.into_iter().chain(dir_path).choose(rng).unwrap()
4460 };
4461
4462 let is_rename = rng.gen();
4463 if is_rename {
4464 let new_path_parent = dirs
4465 .iter()
4466 .filter(|d| !d.starts_with(old_path))
4467 .choose(rng)
4468 .unwrap();
4469
4470 let overwrite_existing_dir =
4471 !old_path.starts_with(&new_path_parent) && rng.gen_bool(0.3);
4472 let new_path = if overwrite_existing_dir {
4473 std::fs::remove_dir_all(&new_path_parent).ok();
4474 new_path_parent.to_path_buf()
4475 } else {
4476 new_path_parent.join(gen_name(rng))
4477 };
4478
4479 log::info!(
4480 "Renaming {:?} to {}{:?}",
4481 old_path.strip_prefix(&root_path)?,
4482 if overwrite_existing_dir {
4483 "overwrite "
4484 } else {
4485 ""
4486 },
4487 new_path.strip_prefix(&root_path)?
4488 );
4489 std::fs::rename(&old_path, &new_path)?;
4490 record_event(old_path.clone());
4491 record_event(new_path);
4492 } else if old_path.is_dir() {
4493 let (dirs, files) = read_dir_recursive(old_path.clone());
4494
4495 log::info!("Deleting dir {:?}", old_path.strip_prefix(&root_path)?);
4496 std::fs::remove_dir_all(&old_path).unwrap();
4497 for file in files {
4498 record_event(file);
4499 }
4500 for dir in dirs {
4501 record_event(dir);
4502 }
4503 } else {
4504 log::info!("Deleting file {:?}", old_path.strip_prefix(&root_path)?);
4505 std::fs::remove_file(old_path).unwrap();
4506 record_event(old_path.clone());
4507 }
4508 }
4509
4510 Ok(events)
4511 }
4512
4513 fn read_dir_recursive(path: PathBuf) -> (Vec<PathBuf>, Vec<PathBuf>) {
4514 let child_entries = std::fs::read_dir(&path).unwrap();
4515 let mut dirs = vec![path];
4516 let mut files = Vec::new();
4517 for child_entry in child_entries {
4518 let child_path = child_entry.unwrap().path();
4519 if child_path.is_dir() {
4520 let (child_dirs, child_files) = read_dir_recursive(child_path);
4521 dirs.extend(child_dirs);
4522 files.extend(child_files);
4523 } else {
4524 files.push(child_path);
4525 }
4526 }
4527 (dirs, files)
4528 }
4529
4530 fn gen_name(rng: &mut impl Rng) -> String {
4531 (0..6)
4532 .map(|_| rng.sample(rand::distributions::Alphanumeric))
4533 .map(char::from)
4534 .collect()
4535 }
4536
4537 impl Snapshot {
4538 fn check_invariants(&self) {
4539 let mut files = self.files(true, 0);
4540 let mut visible_files = self.files(false, 0);
4541 for entry in self.entries_by_path.cursor::<()>() {
4542 if entry.is_file() {
4543 assert_eq!(files.next().unwrap().inode, entry.inode);
4544 if !entry.is_ignored {
4545 assert_eq!(visible_files.next().unwrap().inode, entry.inode);
4546 }
4547 }
4548 }
4549 assert!(files.next().is_none());
4550 assert!(visible_files.next().is_none());
4551
4552 let mut bfs_paths = Vec::new();
4553 let mut stack = vec![Path::new("")];
4554 while let Some(path) = stack.pop() {
4555 bfs_paths.push(path);
4556 let ix = stack.len();
4557 for child_entry in self.child_entries(path) {
4558 stack.insert(ix, &child_entry.path);
4559 }
4560 }
4561
4562 let dfs_paths = self
4563 .entries_by_path
4564 .cursor::<()>()
4565 .map(|e| e.path.as_ref())
4566 .collect::<Vec<_>>();
4567 assert_eq!(bfs_paths, dfs_paths);
4568
4569 for (ignore_parent_path, _) in &self.ignores {
4570 assert!(self.entry_for_path(ignore_parent_path).is_some());
4571 assert!(self
4572 .entry_for_path(ignore_parent_path.join(&*GITIGNORE))
4573 .is_some());
4574 }
4575 }
4576
4577 fn to_vec(&self, include_ignored: bool) -> Vec<(&Path, u64, bool)> {
4578 let mut paths = Vec::new();
4579 for entry in self.entries_by_path.cursor::<()>() {
4580 if include_ignored || !entry.is_ignored {
4581 paths.push((entry.path.as_ref(), entry.inode, entry.is_ignored));
4582 }
4583 }
4584 paths.sort_by(|a, b| a.0.cmp(&b.0));
4585 paths
4586 }
4587 }
4588}