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