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