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