1mod ignore;
2mod lsp_command;
3pub mod search;
4pub mod worktree;
5
6#[cfg(test)]
7mod project_tests;
8
9use anyhow::{anyhow, Context, Result};
10use client::{proto, Client, TypedEnvelope, UserStore};
11use clock::ReplicaId;
12use collections::{hash_map, BTreeMap, HashMap, HashSet};
13use futures::{
14 channel::{mpsc, oneshot},
15 future::Shared,
16 AsyncWriteExt, Future, FutureExt, StreamExt, TryFutureExt,
17};
18use gpui::{
19 AnyModelHandle, AppContext, AsyncAppContext, Entity, ModelContext, ModelHandle,
20 MutableAppContext, Task, UpgradeModelHandle, WeakModelHandle,
21};
22use language::{
23 point_to_lsp,
24 proto::{
25 deserialize_anchor, deserialize_line_ending, deserialize_version, serialize_anchor,
26 serialize_version,
27 },
28 range_from_lsp, range_to_lsp, Anchor, Bias, Buffer, CachedLspAdapter, CharKind, CodeAction,
29 CodeLabel, Completion, Diagnostic, DiagnosticEntry, DiagnosticSet, Event as BufferEvent,
30 File as _, Language, LanguageRegistry, LanguageServerName, LocalFile, OffsetRangeExt,
31 Operation, Patch, PointUtf16, TextBufferSnapshot, ToOffset, ToPointUtf16, Transaction,
32 Unclipped,
33};
34use lsp::{
35 DiagnosticSeverity, DiagnosticTag, DocumentHighlightKind, LanguageServer, LanguageString,
36 MarkedString,
37};
38use lsp_command::*;
39use parking_lot::Mutex;
40use postage::watch;
41use rand::prelude::*;
42use search::SearchQuery;
43use serde::Serialize;
44use settings::{FormatOnSave, Formatter, Settings};
45use sha2::{Digest, Sha256};
46use similar::{ChangeTag, TextDiff};
47use std::{
48 cell::RefCell,
49 cmp::{self, Ordering},
50 convert::TryInto,
51 hash::Hash,
52 mem,
53 num::NonZeroU32,
54 ops::Range,
55 path::{Component, Path, PathBuf},
56 rc::Rc,
57 str,
58 sync::{
59 atomic::{AtomicUsize, Ordering::SeqCst},
60 Arc,
61 },
62 time::Instant,
63};
64use terminal::{Terminal, TerminalBuilder};
65use thiserror::Error;
66use util::{defer, post_inc, ResultExt, TryFutureExt as _};
67
68pub use fs::*;
69pub use worktree::*;
70
71pub trait Item: Entity {
72 fn entry_id(&self, cx: &AppContext) -> Option<ProjectEntryId>;
73}
74
75// Language server state is stored across 3 collections:
76// language_servers =>
77// a mapping from unique server id to LanguageServerState which can either be a task for a
78// server in the process of starting, or a running server with adapter and language server arcs
79// language_server_ids => a mapping from worktreeId and server name to the unique server id
80// language_server_statuses => a mapping from unique server id to the current server status
81//
82// Multiple worktrees can map to the same language server for example when you jump to the definition
83// of a file in the standard library. So language_server_ids is used to look up which server is active
84// for a given worktree and language server name
85//
86// When starting a language server, first the id map is checked to make sure a server isn't already available
87// for that worktree. If there is one, it finishes early. Otherwise, a new id is allocated and and
88// the Starting variant of LanguageServerState is stored in the language_servers map.
89pub struct Project {
90 worktrees: Vec<WorktreeHandle>,
91 active_entry: Option<ProjectEntryId>,
92 languages: Arc<LanguageRegistry>,
93 language_servers: HashMap<usize, LanguageServerState>,
94 language_server_ids: HashMap<(WorktreeId, LanguageServerName), usize>,
95 language_server_statuses: BTreeMap<usize, LanguageServerStatus>,
96 language_server_settings: Arc<Mutex<serde_json::Value>>,
97 last_workspace_edits_by_language_server: HashMap<usize, ProjectTransaction>,
98 next_language_server_id: usize,
99 client: Arc<client::Client>,
100 next_entry_id: Arc<AtomicUsize>,
101 next_diagnostic_group_id: usize,
102 user_store: ModelHandle<UserStore>,
103 fs: Arc<dyn Fs>,
104 client_state: Option<ProjectClientState>,
105 collaborators: HashMap<proto::PeerId, Collaborator>,
106 client_subscriptions: Vec<client::Subscription>,
107 _subscriptions: Vec<gpui::Subscription>,
108 opened_buffer: (watch::Sender<()>, watch::Receiver<()>),
109 shared_buffers: HashMap<proto::PeerId, HashSet<u64>>,
110 #[allow(clippy::type_complexity)]
111 loading_buffers: HashMap<
112 ProjectPath,
113 postage::watch::Receiver<Option<Result<ModelHandle<Buffer>, Arc<anyhow::Error>>>>,
114 >,
115 #[allow(clippy::type_complexity)]
116 loading_local_worktrees:
117 HashMap<Arc<Path>, Shared<Task<Result<ModelHandle<Worktree>, Arc<anyhow::Error>>>>>,
118 opened_buffers: HashMap<u64, OpenBuffer>,
119 incomplete_buffers: HashMap<u64, ModelHandle<Buffer>>,
120 buffer_snapshots: HashMap<u64, Vec<(i32, TextBufferSnapshot)>>,
121 buffers_being_formatted: HashSet<usize>,
122 nonce: u128,
123 _maintain_buffer_languages: Task<()>,
124}
125
126#[derive(Error, Debug)]
127pub enum JoinProjectError {
128 #[error("host declined join request")]
129 HostDeclined,
130 #[error("host closed the project")]
131 HostClosedProject,
132 #[error("host went offline")]
133 HostWentOffline,
134 #[error("{0}")]
135 Other(#[from] anyhow::Error),
136}
137
138enum OpenBuffer {
139 Strong(ModelHandle<Buffer>),
140 Weak(WeakModelHandle<Buffer>),
141 Operations(Vec<Operation>),
142}
143
144enum WorktreeHandle {
145 Strong(ModelHandle<Worktree>),
146 Weak(WeakModelHandle<Worktree>),
147}
148
149enum ProjectClientState {
150 Local {
151 remote_id: u64,
152 metadata_changed: mpsc::UnboundedSender<oneshot::Sender<()>>,
153 _maintain_metadata: Task<()>,
154 },
155 Remote {
156 sharing_has_stopped: bool,
157 remote_id: u64,
158 replica_id: ReplicaId,
159 },
160}
161
162#[derive(Clone, Debug)]
163pub struct Collaborator {
164 pub peer_id: proto::PeerId,
165 pub replica_id: ReplicaId,
166}
167
168#[derive(Clone, Debug, PartialEq, Eq)]
169pub enum Event {
170 ActiveEntryChanged(Option<ProjectEntryId>),
171 WorktreeAdded,
172 WorktreeRemoved(WorktreeId),
173 DiskBasedDiagnosticsStarted {
174 language_server_id: usize,
175 },
176 DiskBasedDiagnosticsFinished {
177 language_server_id: usize,
178 },
179 DiagnosticsUpdated {
180 path: ProjectPath,
181 language_server_id: usize,
182 },
183 RemoteIdChanged(Option<u64>),
184 DisconnectedFromHost,
185 CollaboratorLeft(proto::PeerId),
186}
187
188pub enum LanguageServerState {
189 Starting(Task<Option<Arc<LanguageServer>>>),
190 Running {
191 language: Arc<Language>,
192 adapter: Arc<CachedLspAdapter>,
193 server: Arc<LanguageServer>,
194 },
195}
196
197#[derive(Serialize)]
198pub struct LanguageServerStatus {
199 pub name: String,
200 pub pending_work: BTreeMap<String, LanguageServerProgress>,
201 pub has_pending_diagnostic_updates: bool,
202 progress_tokens: HashSet<String>,
203}
204
205#[derive(Clone, Debug, Serialize)]
206pub struct LanguageServerProgress {
207 pub message: Option<String>,
208 pub percentage: Option<usize>,
209 #[serde(skip_serializing)]
210 pub last_update_at: Instant,
211}
212
213#[derive(Clone, Debug, Eq, PartialEq, Hash, PartialOrd, Ord)]
214pub struct ProjectPath {
215 pub worktree_id: WorktreeId,
216 pub path: Arc<Path>,
217}
218
219#[derive(Copy, Clone, Debug, Default, PartialEq, Serialize)]
220pub struct DiagnosticSummary {
221 pub language_server_id: usize,
222 pub error_count: usize,
223 pub warning_count: usize,
224}
225
226#[derive(Debug, Clone)]
227pub struct Location {
228 pub buffer: ModelHandle<Buffer>,
229 pub range: Range<language::Anchor>,
230}
231
232#[derive(Debug, Clone)]
233pub struct LocationLink {
234 pub origin: Option<Location>,
235 pub target: Location,
236}
237
238#[derive(Debug)]
239pub struct DocumentHighlight {
240 pub range: Range<language::Anchor>,
241 pub kind: DocumentHighlightKind,
242}
243
244#[derive(Clone, Debug)]
245pub struct Symbol {
246 pub language_server_name: LanguageServerName,
247 pub source_worktree_id: WorktreeId,
248 pub path: ProjectPath,
249 pub label: CodeLabel,
250 pub name: String,
251 pub kind: lsp::SymbolKind,
252 pub range: Range<Unclipped<PointUtf16>>,
253 pub signature: [u8; 32],
254}
255
256#[derive(Clone, Debug, PartialEq)]
257pub struct HoverBlock {
258 pub text: String,
259 pub language: Option<String>,
260}
261
262impl HoverBlock {
263 fn try_new(marked_string: MarkedString) -> Option<Self> {
264 let result = match marked_string {
265 MarkedString::LanguageString(LanguageString { language, value }) => HoverBlock {
266 text: value,
267 language: Some(language),
268 },
269 MarkedString::String(text) => HoverBlock {
270 text,
271 language: None,
272 },
273 };
274 if result.text.is_empty() {
275 None
276 } else {
277 Some(result)
278 }
279 }
280}
281
282#[derive(Debug)]
283pub struct Hover {
284 pub contents: Vec<HoverBlock>,
285 pub range: Option<Range<language::Anchor>>,
286}
287
288#[derive(Default)]
289pub struct ProjectTransaction(pub HashMap<ModelHandle<Buffer>, language::Transaction>);
290
291impl DiagnosticSummary {
292 fn new<'a, T: 'a>(
293 language_server_id: usize,
294 diagnostics: impl IntoIterator<Item = &'a DiagnosticEntry<T>>,
295 ) -> Self {
296 let mut this = Self {
297 language_server_id,
298 error_count: 0,
299 warning_count: 0,
300 };
301
302 for entry in diagnostics {
303 if entry.diagnostic.is_primary {
304 match entry.diagnostic.severity {
305 DiagnosticSeverity::ERROR => this.error_count += 1,
306 DiagnosticSeverity::WARNING => this.warning_count += 1,
307 _ => {}
308 }
309 }
310 }
311
312 this
313 }
314
315 pub fn is_empty(&self) -> bool {
316 self.error_count == 0 && self.warning_count == 0
317 }
318
319 pub fn to_proto(&self, path: &Path) -> proto::DiagnosticSummary {
320 proto::DiagnosticSummary {
321 path: path.to_string_lossy().to_string(),
322 language_server_id: self.language_server_id as u64,
323 error_count: self.error_count as u32,
324 warning_count: self.warning_count as u32,
325 }
326 }
327}
328
329#[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
330pub struct ProjectEntryId(usize);
331
332impl ProjectEntryId {
333 pub const MAX: Self = Self(usize::MAX);
334
335 pub fn new(counter: &AtomicUsize) -> Self {
336 Self(counter.fetch_add(1, SeqCst))
337 }
338
339 pub fn from_proto(id: u64) -> Self {
340 Self(id as usize)
341 }
342
343 pub fn to_proto(&self) -> u64 {
344 self.0 as u64
345 }
346
347 pub fn to_usize(&self) -> usize {
348 self.0
349 }
350}
351
352#[derive(Debug, Clone, Copy, PartialEq, Eq)]
353pub enum FormatTrigger {
354 Save,
355 Manual,
356}
357
358impl FormatTrigger {
359 fn from_proto(value: i32) -> FormatTrigger {
360 match value {
361 0 => FormatTrigger::Save,
362 1 => FormatTrigger::Manual,
363 _ => FormatTrigger::Save,
364 }
365 }
366}
367
368impl Project {
369 pub fn init(client: &Arc<Client>) {
370 client.add_model_message_handler(Self::handle_add_collaborator);
371 client.add_model_message_handler(Self::handle_buffer_reloaded);
372 client.add_model_message_handler(Self::handle_buffer_saved);
373 client.add_model_message_handler(Self::handle_start_language_server);
374 client.add_model_message_handler(Self::handle_update_language_server);
375 client.add_model_message_handler(Self::handle_remove_collaborator);
376 client.add_model_message_handler(Self::handle_update_project);
377 client.add_model_message_handler(Self::handle_unshare_project);
378 client.add_model_message_handler(Self::handle_create_buffer_for_peer);
379 client.add_model_message_handler(Self::handle_update_buffer_file);
380 client.add_model_message_handler(Self::handle_update_buffer);
381 client.add_model_message_handler(Self::handle_update_diagnostic_summary);
382 client.add_model_message_handler(Self::handle_update_worktree);
383 client.add_model_request_handler(Self::handle_create_project_entry);
384 client.add_model_request_handler(Self::handle_rename_project_entry);
385 client.add_model_request_handler(Self::handle_copy_project_entry);
386 client.add_model_request_handler(Self::handle_delete_project_entry);
387 client.add_model_request_handler(Self::handle_apply_additional_edits_for_completion);
388 client.add_model_request_handler(Self::handle_apply_code_action);
389 client.add_model_request_handler(Self::handle_reload_buffers);
390 client.add_model_request_handler(Self::handle_format_buffers);
391 client.add_model_request_handler(Self::handle_get_code_actions);
392 client.add_model_request_handler(Self::handle_get_completions);
393 client.add_model_request_handler(Self::handle_lsp_command::<GetHover>);
394 client.add_model_request_handler(Self::handle_lsp_command::<GetDefinition>);
395 client.add_model_request_handler(Self::handle_lsp_command::<GetTypeDefinition>);
396 client.add_model_request_handler(Self::handle_lsp_command::<GetDocumentHighlights>);
397 client.add_model_request_handler(Self::handle_lsp_command::<GetReferences>);
398 client.add_model_request_handler(Self::handle_lsp_command::<PrepareRename>);
399 client.add_model_request_handler(Self::handle_lsp_command::<PerformRename>);
400 client.add_model_request_handler(Self::handle_search_project);
401 client.add_model_request_handler(Self::handle_get_project_symbols);
402 client.add_model_request_handler(Self::handle_open_buffer_for_symbol);
403 client.add_model_request_handler(Self::handle_open_buffer_by_id);
404 client.add_model_request_handler(Self::handle_open_buffer_by_path);
405 client.add_model_request_handler(Self::handle_save_buffer);
406 client.add_model_message_handler(Self::handle_update_diff_base);
407 }
408
409 pub fn local(
410 client: Arc<Client>,
411 user_store: ModelHandle<UserStore>,
412 languages: Arc<LanguageRegistry>,
413 fs: Arc<dyn Fs>,
414 cx: &mut MutableAppContext,
415 ) -> ModelHandle<Self> {
416 cx.add_model(|cx: &mut ModelContext<Self>| Self {
417 worktrees: Default::default(),
418 collaborators: Default::default(),
419 opened_buffers: Default::default(),
420 shared_buffers: Default::default(),
421 incomplete_buffers: Default::default(),
422 loading_buffers: Default::default(),
423 loading_local_worktrees: Default::default(),
424 buffer_snapshots: Default::default(),
425 client_state: None,
426 opened_buffer: watch::channel(),
427 client_subscriptions: Vec::new(),
428 _subscriptions: vec![cx.observe_global::<Settings, _>(Self::on_settings_changed)],
429 _maintain_buffer_languages: Self::maintain_buffer_languages(&languages, cx),
430 active_entry: None,
431 languages,
432 client,
433 user_store,
434 fs,
435 next_entry_id: Default::default(),
436 next_diagnostic_group_id: Default::default(),
437 language_servers: Default::default(),
438 language_server_ids: Default::default(),
439 language_server_statuses: Default::default(),
440 last_workspace_edits_by_language_server: Default::default(),
441 language_server_settings: Default::default(),
442 buffers_being_formatted: Default::default(),
443 next_language_server_id: 0,
444 nonce: StdRng::from_entropy().gen(),
445 })
446 }
447
448 pub async fn remote(
449 remote_id: u64,
450 client: Arc<Client>,
451 user_store: ModelHandle<UserStore>,
452 languages: Arc<LanguageRegistry>,
453 fs: Arc<dyn Fs>,
454 mut cx: AsyncAppContext,
455 ) -> Result<ModelHandle<Self>, JoinProjectError> {
456 client.authenticate_and_connect(true, &cx).await?;
457
458 let subscription = client.subscribe_to_entity(remote_id);
459 let response = client
460 .request(proto::JoinProject {
461 project_id: remote_id,
462 })
463 .await?;
464 let this = cx.add_model(|cx| {
465 let replica_id = response.replica_id as ReplicaId;
466
467 let mut worktrees = Vec::new();
468 for worktree in response.worktrees {
469 let worktree = cx.update(|cx| {
470 Worktree::remote(remote_id, replica_id, worktree, client.clone(), cx)
471 });
472 worktrees.push(worktree);
473 }
474
475 let mut this = Self {
476 worktrees: Vec::new(),
477 loading_buffers: Default::default(),
478 opened_buffer: watch::channel(),
479 shared_buffers: Default::default(),
480 incomplete_buffers: Default::default(),
481 loading_local_worktrees: Default::default(),
482 active_entry: None,
483 collaborators: Default::default(),
484 _maintain_buffer_languages: Self::maintain_buffer_languages(&languages, cx),
485 languages,
486 user_store: user_store.clone(),
487 fs,
488 next_entry_id: Default::default(),
489 next_diagnostic_group_id: Default::default(),
490 client_subscriptions: Default::default(),
491 _subscriptions: Default::default(),
492 client: client.clone(),
493 client_state: Some(ProjectClientState::Remote {
494 sharing_has_stopped: false,
495 remote_id,
496 replica_id,
497 }),
498 language_servers: Default::default(),
499 language_server_ids: Default::default(),
500 language_server_settings: Default::default(),
501 language_server_statuses: response
502 .language_servers
503 .into_iter()
504 .map(|server| {
505 (
506 server.id as usize,
507 LanguageServerStatus {
508 name: server.name,
509 pending_work: Default::default(),
510 has_pending_diagnostic_updates: false,
511 progress_tokens: Default::default(),
512 },
513 )
514 })
515 .collect(),
516 last_workspace_edits_by_language_server: Default::default(),
517 next_language_server_id: 0,
518 opened_buffers: Default::default(),
519 buffers_being_formatted: Default::default(),
520 buffer_snapshots: Default::default(),
521 nonce: StdRng::from_entropy().gen(),
522 };
523 for worktree in worktrees {
524 let _ = this.add_worktree(&worktree, cx);
525 }
526 this
527 });
528 let subscription = subscription.set_model(&this, &mut cx);
529
530 let user_ids = response
531 .collaborators
532 .iter()
533 .map(|peer| peer.user_id)
534 .collect();
535 user_store
536 .update(&mut cx, |user_store, cx| user_store.get_users(user_ids, cx))
537 .await?;
538
539 this.update(&mut cx, |this, cx| {
540 this.set_collaborators_from_proto(response.collaborators, cx)?;
541 this.client_subscriptions.push(subscription);
542 anyhow::Ok(())
543 })?;
544
545 Ok(this)
546 }
547
548 #[cfg(any(test, feature = "test-support"))]
549 pub async fn test(
550 fs: Arc<dyn Fs>,
551 root_paths: impl IntoIterator<Item = &Path>,
552 cx: &mut gpui::TestAppContext,
553 ) -> ModelHandle<Project> {
554 if !cx.read(|cx| cx.has_global::<Settings>()) {
555 cx.update(|cx| {
556 cx.set_global(Settings::test(cx));
557 cx.set_global(HomeDir(Path::new("/tmp/").to_path_buf()))
558 });
559 }
560
561 let languages = Arc::new(LanguageRegistry::test());
562 let http_client = client::test::FakeHttpClient::with_404_response();
563 let client = cx.update(|cx| client::Client::new(http_client.clone(), cx));
564 let user_store = cx.add_model(|cx| UserStore::new(client.clone(), http_client, cx));
565 let project = cx.update(|cx| Project::local(client, user_store, languages, fs, cx));
566 for path in root_paths {
567 let (tree, _) = project
568 .update(cx, |project, cx| {
569 project.find_or_create_local_worktree(path, true, cx)
570 })
571 .await
572 .unwrap();
573 tree.read_with(cx, |tree, _| tree.as_local().unwrap().scan_complete())
574 .await;
575 }
576 project
577 }
578
579 fn on_settings_changed(&mut self, cx: &mut ModelContext<Self>) {
580 let settings = cx.global::<Settings>();
581
582 let mut language_servers_to_start = Vec::new();
583 for buffer in self.opened_buffers.values() {
584 if let Some(buffer) = buffer.upgrade(cx) {
585 let buffer = buffer.read(cx);
586 if let Some((file, language)) = File::from_dyn(buffer.file()).zip(buffer.language())
587 {
588 if settings.enable_language_server(Some(&language.name())) {
589 let worktree = file.worktree.read(cx);
590 language_servers_to_start.push((
591 worktree.id(),
592 worktree.as_local().unwrap().abs_path().clone(),
593 language.clone(),
594 ));
595 }
596 }
597 }
598 }
599
600 let mut language_servers_to_stop = Vec::new();
601 for language in self.languages.to_vec() {
602 if let Some(lsp_adapter) = language.lsp_adapter() {
603 if !settings.enable_language_server(Some(&language.name())) {
604 let lsp_name = &lsp_adapter.name;
605 for (worktree_id, started_lsp_name) in self.language_server_ids.keys() {
606 if lsp_name == started_lsp_name {
607 language_servers_to_stop.push((*worktree_id, started_lsp_name.clone()));
608 }
609 }
610 }
611 }
612 }
613
614 // Stop all newly-disabled language servers.
615 for (worktree_id, adapter_name) in language_servers_to_stop {
616 self.stop_language_server(worktree_id, adapter_name, cx)
617 .detach();
618 }
619
620 // Start all the newly-enabled language servers.
621 for (worktree_id, worktree_path, language) in language_servers_to_start {
622 self.start_language_server(worktree_id, worktree_path, language, cx);
623 }
624
625 cx.notify();
626 }
627
628 pub fn buffer_for_id(&self, remote_id: u64, cx: &AppContext) -> Option<ModelHandle<Buffer>> {
629 self.opened_buffers
630 .get(&remote_id)
631 .and_then(|buffer| buffer.upgrade(cx))
632 }
633
634 pub fn languages(&self) -> &Arc<LanguageRegistry> {
635 &self.languages
636 }
637
638 pub fn client(&self) -> Arc<Client> {
639 self.client.clone()
640 }
641
642 pub fn user_store(&self) -> ModelHandle<UserStore> {
643 self.user_store.clone()
644 }
645
646 #[cfg(any(test, feature = "test-support"))]
647 pub fn check_invariants(&self, cx: &AppContext) {
648 if self.is_local() {
649 let mut worktree_root_paths = HashMap::default();
650 for worktree in self.worktrees(cx) {
651 let worktree = worktree.read(cx);
652 let abs_path = worktree.as_local().unwrap().abs_path().clone();
653 let prev_worktree_id = worktree_root_paths.insert(abs_path.clone(), worktree.id());
654 assert_eq!(
655 prev_worktree_id,
656 None,
657 "abs path {:?} for worktree {:?} is not unique ({:?} was already registered with the same path)",
658 abs_path,
659 worktree.id(),
660 prev_worktree_id
661 )
662 }
663 } else {
664 let replica_id = self.replica_id();
665 for buffer in self.opened_buffers.values() {
666 if let Some(buffer) = buffer.upgrade(cx) {
667 let buffer = buffer.read(cx);
668 assert_eq!(
669 buffer.deferred_ops_len(),
670 0,
671 "replica {}, buffer {} has deferred operations",
672 replica_id,
673 buffer.remote_id()
674 );
675 }
676 }
677 }
678 }
679
680 #[cfg(any(test, feature = "test-support"))]
681 pub fn has_open_buffer(&self, path: impl Into<ProjectPath>, cx: &AppContext) -> bool {
682 let path = path.into();
683 if let Some(worktree) = self.worktree_for_id(path.worktree_id, cx) {
684 self.opened_buffers.iter().any(|(_, buffer)| {
685 if let Some(buffer) = buffer.upgrade(cx) {
686 if let Some(file) = File::from_dyn(buffer.read(cx).file()) {
687 if file.worktree == worktree && file.path() == &path.path {
688 return true;
689 }
690 }
691 }
692 false
693 })
694 } else {
695 false
696 }
697 }
698
699 pub fn fs(&self) -> &Arc<dyn Fs> {
700 &self.fs
701 }
702
703 pub fn remote_id(&self) -> Option<u64> {
704 match self.client_state.as_ref()? {
705 ProjectClientState::Local { remote_id, .. }
706 | ProjectClientState::Remote { remote_id, .. } => Some(*remote_id),
707 }
708 }
709
710 pub fn replica_id(&self) -> ReplicaId {
711 match &self.client_state {
712 Some(ProjectClientState::Remote { replica_id, .. }) => *replica_id,
713 _ => 0,
714 }
715 }
716
717 fn metadata_changed(&mut self, cx: &mut ModelContext<Self>) -> impl Future<Output = ()> {
718 let (tx, rx) = oneshot::channel();
719 if let Some(ProjectClientState::Local {
720 metadata_changed, ..
721 }) = &mut self.client_state
722 {
723 let _ = metadata_changed.unbounded_send(tx);
724 }
725 cx.notify();
726
727 async move {
728 // If the project is shared, this will resolve when the `_maintain_metadata` task has
729 // a chance to update the metadata. Otherwise, it will resolve right away because `tx`
730 // will get dropped.
731 let _ = rx.await;
732 }
733 }
734
735 pub fn collaborators(&self) -> &HashMap<proto::PeerId, Collaborator> {
736 &self.collaborators
737 }
738
739 /// Collect all worktrees, including ones that don't appear in the project panel
740 pub fn worktrees<'a>(
741 &'a self,
742 cx: &'a AppContext,
743 ) -> impl 'a + DoubleEndedIterator<Item = ModelHandle<Worktree>> {
744 self.worktrees
745 .iter()
746 .filter_map(move |worktree| worktree.upgrade(cx))
747 }
748
749 /// Collect all user-visible worktrees, the ones that appear in the project panel
750 pub fn visible_worktrees<'a>(
751 &'a self,
752 cx: &'a AppContext,
753 ) -> impl 'a + DoubleEndedIterator<Item = ModelHandle<Worktree>> {
754 self.worktrees.iter().filter_map(|worktree| {
755 worktree.upgrade(cx).and_then(|worktree| {
756 if worktree.read(cx).is_visible() {
757 Some(worktree)
758 } else {
759 None
760 }
761 })
762 })
763 }
764
765 pub fn worktree_root_names<'a>(&'a self, cx: &'a AppContext) -> impl Iterator<Item = &'a str> {
766 self.visible_worktrees(cx)
767 .map(|tree| tree.read(cx).root_name())
768 }
769
770 pub fn worktree_for_id(
771 &self,
772 id: WorktreeId,
773 cx: &AppContext,
774 ) -> Option<ModelHandle<Worktree>> {
775 self.worktrees(cx)
776 .find(|worktree| worktree.read(cx).id() == id)
777 }
778
779 pub fn worktree_for_entry(
780 &self,
781 entry_id: ProjectEntryId,
782 cx: &AppContext,
783 ) -> Option<ModelHandle<Worktree>> {
784 self.worktrees(cx)
785 .find(|worktree| worktree.read(cx).contains_entry(entry_id))
786 }
787
788 pub fn worktree_id_for_entry(
789 &self,
790 entry_id: ProjectEntryId,
791 cx: &AppContext,
792 ) -> Option<WorktreeId> {
793 self.worktree_for_entry(entry_id, cx)
794 .map(|worktree| worktree.read(cx).id())
795 }
796
797 pub fn contains_paths(&self, paths: &[PathBuf], cx: &AppContext) -> bool {
798 paths.iter().all(|path| self.contains_path(path, cx))
799 }
800
801 pub fn contains_path(&self, path: &Path, cx: &AppContext) -> bool {
802 for worktree in self.worktrees(cx) {
803 let worktree = worktree.read(cx).as_local();
804 if worktree.map_or(false, |w| w.contains_abs_path(path)) {
805 return true;
806 }
807 }
808 false
809 }
810
811 pub fn create_entry(
812 &mut self,
813 project_path: impl Into<ProjectPath>,
814 is_directory: bool,
815 cx: &mut ModelContext<Self>,
816 ) -> Option<Task<Result<Entry>>> {
817 let project_path = project_path.into();
818 let worktree = self.worktree_for_id(project_path.worktree_id, cx)?;
819 if self.is_local() {
820 Some(worktree.update(cx, |worktree, cx| {
821 worktree
822 .as_local_mut()
823 .unwrap()
824 .create_entry(project_path.path, is_directory, cx)
825 }))
826 } else {
827 let client = self.client.clone();
828 let project_id = self.remote_id().unwrap();
829 Some(cx.spawn_weak(|_, mut cx| async move {
830 let response = client
831 .request(proto::CreateProjectEntry {
832 worktree_id: project_path.worktree_id.to_proto(),
833 project_id,
834 path: project_path.path.to_string_lossy().into(),
835 is_directory,
836 })
837 .await?;
838 let entry = response
839 .entry
840 .ok_or_else(|| anyhow!("missing entry in response"))?;
841 worktree
842 .update(&mut cx, |worktree, cx| {
843 worktree.as_remote_mut().unwrap().insert_entry(
844 entry,
845 response.worktree_scan_id as usize,
846 cx,
847 )
848 })
849 .await
850 }))
851 }
852 }
853
854 pub fn copy_entry(
855 &mut self,
856 entry_id: ProjectEntryId,
857 new_path: impl Into<Arc<Path>>,
858 cx: &mut ModelContext<Self>,
859 ) -> Option<Task<Result<Entry>>> {
860 let worktree = self.worktree_for_entry(entry_id, cx)?;
861 let new_path = new_path.into();
862 if self.is_local() {
863 worktree.update(cx, |worktree, cx| {
864 worktree
865 .as_local_mut()
866 .unwrap()
867 .copy_entry(entry_id, new_path, cx)
868 })
869 } else {
870 let client = self.client.clone();
871 let project_id = self.remote_id().unwrap();
872
873 Some(cx.spawn_weak(|_, mut cx| async move {
874 let response = client
875 .request(proto::CopyProjectEntry {
876 project_id,
877 entry_id: entry_id.to_proto(),
878 new_path: new_path.to_string_lossy().into(),
879 })
880 .await?;
881 let entry = response
882 .entry
883 .ok_or_else(|| anyhow!("missing entry in response"))?;
884 worktree
885 .update(&mut cx, |worktree, cx| {
886 worktree.as_remote_mut().unwrap().insert_entry(
887 entry,
888 response.worktree_scan_id as usize,
889 cx,
890 )
891 })
892 .await
893 }))
894 }
895 }
896
897 pub fn rename_entry(
898 &mut self,
899 entry_id: ProjectEntryId,
900 new_path: impl Into<Arc<Path>>,
901 cx: &mut ModelContext<Self>,
902 ) -> Option<Task<Result<Entry>>> {
903 let worktree = self.worktree_for_entry(entry_id, cx)?;
904 let new_path = new_path.into();
905 if self.is_local() {
906 worktree.update(cx, |worktree, cx| {
907 worktree
908 .as_local_mut()
909 .unwrap()
910 .rename_entry(entry_id, new_path, cx)
911 })
912 } else {
913 let client = self.client.clone();
914 let project_id = self.remote_id().unwrap();
915
916 Some(cx.spawn_weak(|_, mut cx| async move {
917 let response = client
918 .request(proto::RenameProjectEntry {
919 project_id,
920 entry_id: entry_id.to_proto(),
921 new_path: new_path.to_string_lossy().into(),
922 })
923 .await?;
924 let entry = response
925 .entry
926 .ok_or_else(|| anyhow!("missing entry in response"))?;
927 worktree
928 .update(&mut cx, |worktree, cx| {
929 worktree.as_remote_mut().unwrap().insert_entry(
930 entry,
931 response.worktree_scan_id as usize,
932 cx,
933 )
934 })
935 .await
936 }))
937 }
938 }
939
940 pub fn delete_entry(
941 &mut self,
942 entry_id: ProjectEntryId,
943 cx: &mut ModelContext<Self>,
944 ) -> Option<Task<Result<()>>> {
945 let worktree = self.worktree_for_entry(entry_id, cx)?;
946 if self.is_local() {
947 worktree.update(cx, |worktree, cx| {
948 worktree.as_local_mut().unwrap().delete_entry(entry_id, cx)
949 })
950 } else {
951 let client = self.client.clone();
952 let project_id = self.remote_id().unwrap();
953 Some(cx.spawn_weak(|_, mut cx| async move {
954 let response = client
955 .request(proto::DeleteProjectEntry {
956 project_id,
957 entry_id: entry_id.to_proto(),
958 })
959 .await?;
960 worktree
961 .update(&mut cx, move |worktree, cx| {
962 worktree.as_remote_mut().unwrap().delete_entry(
963 entry_id,
964 response.worktree_scan_id as usize,
965 cx,
966 )
967 })
968 .await
969 }))
970 }
971 }
972
973 pub fn shared(&mut self, project_id: u64, cx: &mut ModelContext<Self>) -> Task<Result<()>> {
974 if self.client_state.is_some() {
975 return Task::ready(Err(anyhow!("project was already shared")));
976 }
977
978 let mut worktree_share_tasks = Vec::new();
979
980 for open_buffer in self.opened_buffers.values_mut() {
981 match open_buffer {
982 OpenBuffer::Strong(_) => {}
983 OpenBuffer::Weak(buffer) => {
984 if let Some(buffer) = buffer.upgrade(cx) {
985 *open_buffer = OpenBuffer::Strong(buffer);
986 }
987 }
988 OpenBuffer::Operations(_) => unreachable!(),
989 }
990 }
991
992 for worktree_handle in self.worktrees.iter_mut() {
993 match worktree_handle {
994 WorktreeHandle::Strong(_) => {}
995 WorktreeHandle::Weak(worktree) => {
996 if let Some(worktree) = worktree.upgrade(cx) {
997 *worktree_handle = WorktreeHandle::Strong(worktree);
998 }
999 }
1000 }
1001 }
1002
1003 for (server_id, status) in &self.language_server_statuses {
1004 self.client
1005 .send(proto::StartLanguageServer {
1006 project_id,
1007 server: Some(proto::LanguageServer {
1008 id: *server_id as u64,
1009 name: status.name.clone(),
1010 }),
1011 })
1012 .log_err();
1013 }
1014
1015 for worktree in self.worktrees(cx).collect::<Vec<_>>() {
1016 worktree.update(cx, |worktree, cx| {
1017 let worktree = worktree.as_local_mut().unwrap();
1018 worktree_share_tasks.push(worktree.share(project_id, cx));
1019 });
1020 }
1021
1022 self.client_subscriptions.push(
1023 self.client
1024 .subscribe_to_entity(project_id)
1025 .set_model(&cx.handle(), &mut cx.to_async()),
1026 );
1027 let _ = self.metadata_changed(cx);
1028 cx.emit(Event::RemoteIdChanged(Some(project_id)));
1029 cx.notify();
1030
1031 let (metadata_changed_tx, mut metadata_changed_rx) = mpsc::unbounded();
1032 self.client_state = Some(ProjectClientState::Local {
1033 remote_id: project_id,
1034 metadata_changed: metadata_changed_tx,
1035 _maintain_metadata: cx.spawn_weak(move |this, cx| async move {
1036 let mut txs = Vec::new();
1037 while let Some(tx) = metadata_changed_rx.next().await {
1038 txs.push(tx);
1039 while let Ok(Some(next_tx)) = metadata_changed_rx.try_next() {
1040 txs.push(next_tx);
1041 }
1042
1043 let Some(this) = this.upgrade(&cx) else { break };
1044 this.read_with(&cx, |this, cx| {
1045 this.client.request(proto::UpdateProject {
1046 project_id,
1047 worktrees: this.worktree_metadata_protos(cx),
1048 })
1049 })
1050 .await
1051 .log_err();
1052
1053 for tx in txs.drain(..) {
1054 let _ = tx.send(());
1055 }
1056 }
1057 }),
1058 });
1059
1060 cx.foreground().spawn(async move {
1061 futures::future::try_join_all(worktree_share_tasks).await?;
1062 Ok(())
1063 })
1064 }
1065
1066 pub fn reshared(
1067 &mut self,
1068 message: proto::ResharedProject,
1069 cx: &mut ModelContext<Self>,
1070 ) -> Result<()> {
1071 self.set_collaborators_from_proto(message.collaborators, cx)?;
1072 for worktree in self.worktrees.iter() {
1073 if let Some(worktree) = worktree.upgrade(&cx) {
1074 worktree.update(cx, |worktree, _| {
1075 if let Some(worktree) = worktree.as_local_mut() {
1076 worktree.reshare()
1077 } else {
1078 Ok(())
1079 }
1080 })?;
1081 }
1082 }
1083 Ok(())
1084 }
1085
1086 pub fn rejoined(
1087 &mut self,
1088 message: proto::RejoinedProject,
1089 cx: &mut ModelContext<Self>,
1090 ) -> Result<()> {
1091 self.set_collaborators_from_proto(message.collaborators, cx)?;
1092 Ok(())
1093 }
1094
1095 pub fn worktree_metadata_protos(&self, cx: &AppContext) -> Vec<proto::WorktreeMetadata> {
1096 self.worktrees(cx)
1097 .map(|worktree| {
1098 let worktree = worktree.read(cx);
1099 proto::WorktreeMetadata {
1100 id: worktree.id().to_proto(),
1101 root_name: worktree.root_name().into(),
1102 visible: worktree.is_visible(),
1103 abs_path: worktree.abs_path().to_string_lossy().into(),
1104 }
1105 })
1106 .collect()
1107 }
1108
1109 pub fn unshare(&mut self, cx: &mut ModelContext<Self>) -> Result<()> {
1110 if self.is_remote() {
1111 return Err(anyhow!("attempted to unshare a remote project"));
1112 }
1113
1114 if let Some(ProjectClientState::Local { remote_id, .. }) = self.client_state.take() {
1115 self.collaborators.clear();
1116 self.shared_buffers.clear();
1117 self.client_subscriptions.clear();
1118
1119 for worktree_handle in self.worktrees.iter_mut() {
1120 if let WorktreeHandle::Strong(worktree) = worktree_handle {
1121 let is_visible = worktree.update(cx, |worktree, _| {
1122 worktree.as_local_mut().unwrap().unshare();
1123 worktree.is_visible()
1124 });
1125 if !is_visible {
1126 *worktree_handle = WorktreeHandle::Weak(worktree.downgrade());
1127 }
1128 }
1129 }
1130
1131 for open_buffer in self.opened_buffers.values_mut() {
1132 if let OpenBuffer::Strong(buffer) = open_buffer {
1133 *open_buffer = OpenBuffer::Weak(buffer.downgrade());
1134 }
1135 }
1136
1137 let _ = self.metadata_changed(cx);
1138 cx.notify();
1139 self.client.send(proto::UnshareProject {
1140 project_id: remote_id,
1141 })?;
1142
1143 Ok(())
1144 } else {
1145 Err(anyhow!("attempted to unshare an unshared project"))
1146 }
1147 }
1148
1149 pub fn disconnected_from_host(&mut self, cx: &mut ModelContext<Self>) {
1150 if let Some(ProjectClientState::Remote {
1151 sharing_has_stopped,
1152 ..
1153 }) = &mut self.client_state
1154 {
1155 *sharing_has_stopped = true;
1156 self.collaborators.clear();
1157 for worktree in &self.worktrees {
1158 if let Some(worktree) = worktree.upgrade(cx) {
1159 worktree.update(cx, |worktree, _| {
1160 if let Some(worktree) = worktree.as_remote_mut() {
1161 worktree.disconnected_from_host();
1162 }
1163 });
1164 }
1165 }
1166 cx.emit(Event::DisconnectedFromHost);
1167 cx.notify();
1168
1169 // Wake up all futures currently waiting on a buffer to get opened,
1170 // to give them a chance to fail now that we've disconnected.
1171 *self.opened_buffer.0.borrow_mut() = ();
1172 }
1173 }
1174
1175 pub fn is_read_only(&self) -> bool {
1176 match &self.client_state {
1177 Some(ProjectClientState::Remote {
1178 sharing_has_stopped,
1179 ..
1180 }) => *sharing_has_stopped,
1181 _ => false,
1182 }
1183 }
1184
1185 pub fn is_local(&self) -> bool {
1186 match &self.client_state {
1187 Some(ProjectClientState::Remote { .. }) => false,
1188 _ => true,
1189 }
1190 }
1191
1192 pub fn is_remote(&self) -> bool {
1193 !self.is_local()
1194 }
1195
1196 pub fn create_terminal(
1197 &mut self,
1198 working_directory: Option<PathBuf>,
1199 window_id: usize,
1200 cx: &mut ModelContext<Self>,
1201 ) -> Result<ModelHandle<Terminal>> {
1202 if self.is_remote() {
1203 return Err(anyhow!(
1204 "creating terminals as a guest is not supported yet"
1205 ));
1206 } else {
1207 let settings = cx.global::<Settings>();
1208 let shell = settings.terminal_shell();
1209 let envs = settings.terminal_env();
1210 let scroll = settings.terminal_scroll();
1211
1212 TerminalBuilder::new(
1213 working_directory.clone(),
1214 shell,
1215 envs,
1216 settings.terminal_overrides.blinking.clone(),
1217 scroll,
1218 window_id,
1219 )
1220 .map(|builder| cx.add_model(|cx| builder.subscribe(cx)))
1221 }
1222 }
1223
1224 pub fn create_buffer(
1225 &mut self,
1226 text: &str,
1227 language: Option<Arc<Language>>,
1228 cx: &mut ModelContext<Self>,
1229 ) -> Result<ModelHandle<Buffer>> {
1230 if self.is_remote() {
1231 return Err(anyhow!("creating buffers as a guest is not supported yet"));
1232 }
1233
1234 let buffer = cx.add_model(|cx| {
1235 Buffer::new(self.replica_id(), text, cx)
1236 .with_language(language.unwrap_or_else(|| language::PLAIN_TEXT.clone()), cx)
1237 });
1238 self.register_buffer(&buffer, cx)?;
1239 Ok(buffer)
1240 }
1241
1242 pub fn open_path(
1243 &mut self,
1244 path: impl Into<ProjectPath>,
1245 cx: &mut ModelContext<Self>,
1246 ) -> Task<Result<(ProjectEntryId, AnyModelHandle)>> {
1247 let task = self.open_buffer(path, cx);
1248 cx.spawn_weak(|_, cx| async move {
1249 let buffer = task.await?;
1250 let project_entry_id = buffer
1251 .read_with(&cx, |buffer, cx| {
1252 File::from_dyn(buffer.file()).and_then(|file| file.project_entry_id(cx))
1253 })
1254 .ok_or_else(|| anyhow!("no project entry"))?;
1255 Ok((project_entry_id, buffer.into()))
1256 })
1257 }
1258
1259 pub fn open_local_buffer(
1260 &mut self,
1261 abs_path: impl AsRef<Path>,
1262 cx: &mut ModelContext<Self>,
1263 ) -> Task<Result<ModelHandle<Buffer>>> {
1264 if let Some((worktree, relative_path)) = self.find_local_worktree(abs_path.as_ref(), cx) {
1265 self.open_buffer((worktree.read(cx).id(), relative_path), cx)
1266 } else {
1267 Task::ready(Err(anyhow!("no such path")))
1268 }
1269 }
1270
1271 pub fn open_buffer(
1272 &mut self,
1273 path: impl Into<ProjectPath>,
1274 cx: &mut ModelContext<Self>,
1275 ) -> Task<Result<ModelHandle<Buffer>>> {
1276 let project_path = path.into();
1277 let worktree = if let Some(worktree) = self.worktree_for_id(project_path.worktree_id, cx) {
1278 worktree
1279 } else {
1280 return Task::ready(Err(anyhow!("no such worktree")));
1281 };
1282
1283 // If there is already a buffer for the given path, then return it.
1284 let existing_buffer = self.get_open_buffer(&project_path, cx);
1285 if let Some(existing_buffer) = existing_buffer {
1286 return Task::ready(Ok(existing_buffer));
1287 }
1288
1289 let mut loading_watch = match self.loading_buffers.entry(project_path.clone()) {
1290 // If the given path is already being loaded, then wait for that existing
1291 // task to complete and return the same buffer.
1292 hash_map::Entry::Occupied(e) => e.get().clone(),
1293
1294 // Otherwise, record the fact that this path is now being loaded.
1295 hash_map::Entry::Vacant(entry) => {
1296 let (mut tx, rx) = postage::watch::channel();
1297 entry.insert(rx.clone());
1298
1299 let load_buffer = if worktree.read(cx).is_local() {
1300 self.open_local_buffer_internal(&project_path.path, &worktree, cx)
1301 } else {
1302 self.open_remote_buffer_internal(&project_path.path, &worktree, cx)
1303 };
1304
1305 cx.spawn(move |this, mut cx| async move {
1306 let load_result = load_buffer.await;
1307 *tx.borrow_mut() = Some(this.update(&mut cx, |this, _| {
1308 // Record the fact that the buffer is no longer loading.
1309 this.loading_buffers.remove(&project_path);
1310 let buffer = load_result.map_err(Arc::new)?;
1311 Ok(buffer)
1312 }));
1313 })
1314 .detach();
1315 rx
1316 }
1317 };
1318
1319 cx.foreground().spawn(async move {
1320 loop {
1321 if let Some(result) = loading_watch.borrow().as_ref() {
1322 match result {
1323 Ok(buffer) => return Ok(buffer.clone()),
1324 Err(error) => return Err(anyhow!("{}", error)),
1325 }
1326 }
1327 loading_watch.next().await;
1328 }
1329 })
1330 }
1331
1332 fn open_local_buffer_internal(
1333 &mut self,
1334 path: &Arc<Path>,
1335 worktree: &ModelHandle<Worktree>,
1336 cx: &mut ModelContext<Self>,
1337 ) -> Task<Result<ModelHandle<Buffer>>> {
1338 let load_buffer = worktree.update(cx, |worktree, cx| {
1339 let worktree = worktree.as_local_mut().unwrap();
1340 worktree.load_buffer(path, cx)
1341 });
1342 cx.spawn(|this, mut cx| async move {
1343 let buffer = load_buffer.await?;
1344 this.update(&mut cx, |this, cx| this.register_buffer(&buffer, cx))?;
1345 Ok(buffer)
1346 })
1347 }
1348
1349 fn open_remote_buffer_internal(
1350 &mut self,
1351 path: &Arc<Path>,
1352 worktree: &ModelHandle<Worktree>,
1353 cx: &mut ModelContext<Self>,
1354 ) -> Task<Result<ModelHandle<Buffer>>> {
1355 let rpc = self.client.clone();
1356 let project_id = self.remote_id().unwrap();
1357 let remote_worktree_id = worktree.read(cx).id();
1358 let path = path.clone();
1359 let path_string = path.to_string_lossy().to_string();
1360 cx.spawn(|this, mut cx| async move {
1361 let response = rpc
1362 .request(proto::OpenBufferByPath {
1363 project_id,
1364 worktree_id: remote_worktree_id.to_proto(),
1365 path: path_string,
1366 })
1367 .await?;
1368 this.update(&mut cx, |this, cx| {
1369 this.wait_for_buffer(response.buffer_id, cx)
1370 })
1371 .await
1372 })
1373 }
1374
1375 /// LanguageServerName is owned, because it is inserted into a map
1376 fn open_local_buffer_via_lsp(
1377 &mut self,
1378 abs_path: lsp::Url,
1379 language_server_id: usize,
1380 language_server_name: LanguageServerName,
1381 cx: &mut ModelContext<Self>,
1382 ) -> Task<Result<ModelHandle<Buffer>>> {
1383 cx.spawn(|this, mut cx| async move {
1384 let abs_path = abs_path
1385 .to_file_path()
1386 .map_err(|_| anyhow!("can't convert URI to path"))?;
1387 let (worktree, relative_path) = if let Some(result) =
1388 this.read_with(&cx, |this, cx| this.find_local_worktree(&abs_path, cx))
1389 {
1390 result
1391 } else {
1392 let worktree = this
1393 .update(&mut cx, |this, cx| {
1394 this.create_local_worktree(&abs_path, false, cx)
1395 })
1396 .await?;
1397 this.update(&mut cx, |this, cx| {
1398 this.language_server_ids.insert(
1399 (worktree.read(cx).id(), language_server_name),
1400 language_server_id,
1401 );
1402 });
1403 (worktree, PathBuf::new())
1404 };
1405
1406 let project_path = ProjectPath {
1407 worktree_id: worktree.read_with(&cx, |worktree, _| worktree.id()),
1408 path: relative_path.into(),
1409 };
1410 this.update(&mut cx, |this, cx| this.open_buffer(project_path, cx))
1411 .await
1412 })
1413 }
1414
1415 pub fn open_buffer_by_id(
1416 &mut self,
1417 id: u64,
1418 cx: &mut ModelContext<Self>,
1419 ) -> Task<Result<ModelHandle<Buffer>>> {
1420 if let Some(buffer) = self.buffer_for_id(id, cx) {
1421 Task::ready(Ok(buffer))
1422 } else if self.is_local() {
1423 Task::ready(Err(anyhow!("buffer {} does not exist", id)))
1424 } else if let Some(project_id) = self.remote_id() {
1425 let request = self
1426 .client
1427 .request(proto::OpenBufferById { project_id, id });
1428 cx.spawn(|this, mut cx| async move {
1429 let buffer_id = request.await?.buffer_id;
1430 this.update(&mut cx, |this, cx| this.wait_for_buffer(buffer_id, cx))
1431 .await
1432 })
1433 } else {
1434 Task::ready(Err(anyhow!("cannot open buffer while disconnected")))
1435 }
1436 }
1437
1438 pub fn save_buffer_as(
1439 &mut self,
1440 buffer: ModelHandle<Buffer>,
1441 abs_path: PathBuf,
1442 cx: &mut ModelContext<Project>,
1443 ) -> Task<Result<()>> {
1444 let worktree_task = self.find_or_create_local_worktree(&abs_path, true, cx);
1445 let old_path =
1446 File::from_dyn(buffer.read(cx).file()).and_then(|f| Some(f.as_local()?.abs_path(cx)));
1447 cx.spawn(|this, mut cx| async move {
1448 if let Some(old_path) = old_path {
1449 this.update(&mut cx, |this, cx| {
1450 this.unregister_buffer_from_language_server(&buffer, old_path, cx);
1451 });
1452 }
1453 let (worktree, path) = worktree_task.await?;
1454 worktree
1455 .update(&mut cx, |worktree, cx| {
1456 worktree
1457 .as_local_mut()
1458 .unwrap()
1459 .save_buffer_as(buffer.clone(), path, cx)
1460 })
1461 .await?;
1462 this.update(&mut cx, |this, cx| {
1463 this.assign_language_to_buffer(&buffer, cx);
1464 this.register_buffer_with_language_server(&buffer, cx);
1465 });
1466 Ok(())
1467 })
1468 }
1469
1470 pub fn get_open_buffer(
1471 &mut self,
1472 path: &ProjectPath,
1473 cx: &mut ModelContext<Self>,
1474 ) -> Option<ModelHandle<Buffer>> {
1475 let worktree = self.worktree_for_id(path.worktree_id, cx)?;
1476 self.opened_buffers.values().find_map(|buffer| {
1477 let buffer = buffer.upgrade(cx)?;
1478 let file = File::from_dyn(buffer.read(cx).file())?;
1479 if file.worktree == worktree && file.path() == &path.path {
1480 Some(buffer)
1481 } else {
1482 None
1483 }
1484 })
1485 }
1486
1487 fn register_buffer(
1488 &mut self,
1489 buffer: &ModelHandle<Buffer>,
1490 cx: &mut ModelContext<Self>,
1491 ) -> Result<()> {
1492 let remote_id = buffer.read(cx).remote_id();
1493 let open_buffer = if self.is_remote() || self.is_shared() {
1494 OpenBuffer::Strong(buffer.clone())
1495 } else {
1496 OpenBuffer::Weak(buffer.downgrade())
1497 };
1498
1499 match self.opened_buffers.insert(remote_id, open_buffer) {
1500 None => {}
1501 Some(OpenBuffer::Operations(operations)) => {
1502 buffer.update(cx, |buffer, cx| buffer.apply_ops(operations, cx))?
1503 }
1504 Some(OpenBuffer::Weak(existing_handle)) => {
1505 if existing_handle.upgrade(cx).is_some() {
1506 Err(anyhow!(
1507 "already registered buffer with remote id {}",
1508 remote_id
1509 ))?
1510 }
1511 }
1512 Some(OpenBuffer::Strong(_)) => Err(anyhow!(
1513 "already registered buffer with remote id {}",
1514 remote_id
1515 ))?,
1516 }
1517 cx.subscribe(buffer, |this, buffer, event, cx| {
1518 this.on_buffer_event(buffer, event, cx);
1519 })
1520 .detach();
1521
1522 self.assign_language_to_buffer(buffer, cx);
1523 self.register_buffer_with_language_server(buffer, cx);
1524 cx.observe_release(buffer, |this, buffer, cx| {
1525 if let Some(file) = File::from_dyn(buffer.file()) {
1526 if file.is_local() {
1527 let uri = lsp::Url::from_file_path(file.abs_path(cx)).unwrap();
1528 if let Some((_, server)) = this.language_server_for_buffer(buffer, cx) {
1529 server
1530 .notify::<lsp::notification::DidCloseTextDocument>(
1531 lsp::DidCloseTextDocumentParams {
1532 text_document: lsp::TextDocumentIdentifier::new(uri),
1533 },
1534 )
1535 .log_err();
1536 }
1537 }
1538 }
1539 })
1540 .detach();
1541
1542 *self.opened_buffer.0.borrow_mut() = ();
1543 Ok(())
1544 }
1545
1546 fn register_buffer_with_language_server(
1547 &mut self,
1548 buffer_handle: &ModelHandle<Buffer>,
1549 cx: &mut ModelContext<Self>,
1550 ) {
1551 let buffer = buffer_handle.read(cx);
1552 let buffer_id = buffer.remote_id();
1553 if let Some(file) = File::from_dyn(buffer.file()) {
1554 if file.is_local() {
1555 let uri = lsp::Url::from_file_path(file.abs_path(cx)).unwrap();
1556 let initial_snapshot = buffer.text_snapshot();
1557
1558 let mut language_server = None;
1559 let mut language_id = None;
1560 if let Some(language) = buffer.language() {
1561 let worktree_id = file.worktree_id(cx);
1562 if let Some(adapter) = language.lsp_adapter() {
1563 language_id = adapter.language_ids.get(language.name().as_ref()).cloned();
1564 language_server = self
1565 .language_server_ids
1566 .get(&(worktree_id, adapter.name.clone()))
1567 .and_then(|id| self.language_servers.get(id))
1568 .and_then(|server_state| {
1569 if let LanguageServerState::Running { server, .. } = server_state {
1570 Some(server.clone())
1571 } else {
1572 None
1573 }
1574 });
1575 }
1576 }
1577
1578 if let Some(local_worktree) = file.worktree.read(cx).as_local() {
1579 if let Some(diagnostics) = local_worktree.diagnostics_for_path(file.path()) {
1580 self.update_buffer_diagnostics(buffer_handle, diagnostics, None, cx)
1581 .log_err();
1582 }
1583 }
1584
1585 if let Some(server) = language_server {
1586 server
1587 .notify::<lsp::notification::DidOpenTextDocument>(
1588 lsp::DidOpenTextDocumentParams {
1589 text_document: lsp::TextDocumentItem::new(
1590 uri,
1591 language_id.unwrap_or_default(),
1592 0,
1593 initial_snapshot.text(),
1594 ),
1595 },
1596 )
1597 .log_err();
1598 buffer_handle.update(cx, |buffer, cx| {
1599 buffer.set_completion_triggers(
1600 server
1601 .capabilities()
1602 .completion_provider
1603 .as_ref()
1604 .and_then(|provider| provider.trigger_characters.clone())
1605 .unwrap_or_default(),
1606 cx,
1607 )
1608 });
1609 self.buffer_snapshots
1610 .insert(buffer_id, vec![(0, initial_snapshot)]);
1611 }
1612 }
1613 }
1614 }
1615
1616 fn unregister_buffer_from_language_server(
1617 &mut self,
1618 buffer: &ModelHandle<Buffer>,
1619 old_path: PathBuf,
1620 cx: &mut ModelContext<Self>,
1621 ) {
1622 buffer.update(cx, |buffer, cx| {
1623 buffer.update_diagnostics(Default::default(), cx);
1624 self.buffer_snapshots.remove(&buffer.remote_id());
1625 if let Some((_, language_server)) = self.language_server_for_buffer(buffer, cx) {
1626 language_server
1627 .notify::<lsp::notification::DidCloseTextDocument>(
1628 lsp::DidCloseTextDocumentParams {
1629 text_document: lsp::TextDocumentIdentifier::new(
1630 lsp::Url::from_file_path(old_path).unwrap(),
1631 ),
1632 },
1633 )
1634 .log_err();
1635 }
1636 });
1637 }
1638
1639 fn on_buffer_event(
1640 &mut self,
1641 buffer: ModelHandle<Buffer>,
1642 event: &BufferEvent,
1643 cx: &mut ModelContext<Self>,
1644 ) -> Option<()> {
1645 match event {
1646 BufferEvent::Operation(operation) => {
1647 if let Some(project_id) = self.remote_id() {
1648 let request = self.client.request(proto::UpdateBuffer {
1649 project_id,
1650 buffer_id: buffer.read(cx).remote_id(),
1651 operations: vec![language::proto::serialize_operation(operation)],
1652 });
1653 cx.background().spawn(request).detach_and_log_err(cx);
1654 }
1655 }
1656 BufferEvent::Edited { .. } => {
1657 let language_server = self
1658 .language_server_for_buffer(buffer.read(cx), cx)
1659 .map(|(_, server)| server.clone())?;
1660 let buffer = buffer.read(cx);
1661 let file = File::from_dyn(buffer.file())?;
1662 let abs_path = file.as_local()?.abs_path(cx);
1663 let uri = lsp::Url::from_file_path(abs_path).unwrap();
1664 let buffer_snapshots = self.buffer_snapshots.get_mut(&buffer.remote_id())?;
1665 let (version, prev_snapshot) = buffer_snapshots.last()?;
1666 let next_snapshot = buffer.text_snapshot();
1667 let next_version = version + 1;
1668
1669 let content_changes = buffer
1670 .edits_since::<(PointUtf16, usize)>(prev_snapshot.version())
1671 .map(|edit| {
1672 let edit_start = edit.new.start.0;
1673 let edit_end = edit_start + (edit.old.end.0 - edit.old.start.0);
1674 let new_text = next_snapshot
1675 .text_for_range(edit.new.start.1..edit.new.end.1)
1676 .collect();
1677 lsp::TextDocumentContentChangeEvent {
1678 range: Some(lsp::Range::new(
1679 point_to_lsp(edit_start),
1680 point_to_lsp(edit_end),
1681 )),
1682 range_length: None,
1683 text: new_text,
1684 }
1685 })
1686 .collect();
1687
1688 buffer_snapshots.push((next_version, next_snapshot));
1689
1690 language_server
1691 .notify::<lsp::notification::DidChangeTextDocument>(
1692 lsp::DidChangeTextDocumentParams {
1693 text_document: lsp::VersionedTextDocumentIdentifier::new(
1694 uri,
1695 next_version,
1696 ),
1697 content_changes,
1698 },
1699 )
1700 .log_err();
1701 }
1702 BufferEvent::Saved => {
1703 let file = File::from_dyn(buffer.read(cx).file())?;
1704 let worktree_id = file.worktree_id(cx);
1705 let abs_path = file.as_local()?.abs_path(cx);
1706 let text_document = lsp::TextDocumentIdentifier {
1707 uri: lsp::Url::from_file_path(abs_path).unwrap(),
1708 };
1709
1710 for (_, _, server) in self.language_servers_for_worktree(worktree_id) {
1711 server
1712 .notify::<lsp::notification::DidSaveTextDocument>(
1713 lsp::DidSaveTextDocumentParams {
1714 text_document: text_document.clone(),
1715 text: None,
1716 },
1717 )
1718 .log_err();
1719 }
1720
1721 // After saving a buffer, simulate disk-based diagnostics being finished for languages
1722 // that don't support a disk-based progress token.
1723 let (lsp_adapter, language_server) =
1724 self.language_server_for_buffer(buffer.read(cx), cx)?;
1725 if lsp_adapter.disk_based_diagnostics_progress_token.is_none() {
1726 let server_id = language_server.server_id();
1727 self.disk_based_diagnostics_finished(server_id, cx);
1728 self.broadcast_language_server_update(
1729 server_id,
1730 proto::update_language_server::Variant::DiskBasedDiagnosticsUpdated(
1731 proto::LspDiskBasedDiagnosticsUpdated {},
1732 ),
1733 );
1734 }
1735 }
1736 _ => {}
1737 }
1738
1739 None
1740 }
1741
1742 fn language_servers_for_worktree(
1743 &self,
1744 worktree_id: WorktreeId,
1745 ) -> impl Iterator<Item = (&Arc<CachedLspAdapter>, &Arc<Language>, &Arc<LanguageServer>)> {
1746 self.language_server_ids
1747 .iter()
1748 .filter_map(move |((language_server_worktree_id, _), id)| {
1749 if *language_server_worktree_id == worktree_id {
1750 if let Some(LanguageServerState::Running {
1751 adapter,
1752 language,
1753 server,
1754 }) = self.language_servers.get(id)
1755 {
1756 return Some((adapter, language, server));
1757 }
1758 }
1759 None
1760 })
1761 }
1762
1763 fn maintain_buffer_languages(
1764 languages: &LanguageRegistry,
1765 cx: &mut ModelContext<Project>,
1766 ) -> Task<()> {
1767 let mut subscription = languages.subscribe();
1768 cx.spawn_weak(|project, mut cx| async move {
1769 while let Some(()) = subscription.next().await {
1770 if let Some(project) = project.upgrade(&cx) {
1771 project.update(&mut cx, |project, cx| {
1772 let mut buffers_without_language = Vec::new();
1773 for buffer in project.opened_buffers.values() {
1774 if let Some(buffer) = buffer.upgrade(cx) {
1775 if buffer.read(cx).language().is_none() {
1776 buffers_without_language.push(buffer);
1777 }
1778 }
1779 }
1780
1781 for buffer in buffers_without_language {
1782 project.assign_language_to_buffer(&buffer, cx);
1783 project.register_buffer_with_language_server(&buffer, cx);
1784 }
1785 });
1786 }
1787 }
1788 })
1789 }
1790
1791 fn assign_language_to_buffer(
1792 &mut self,
1793 buffer: &ModelHandle<Buffer>,
1794 cx: &mut ModelContext<Self>,
1795 ) -> Option<()> {
1796 // If the buffer has a language, set it and start the language server if we haven't already.
1797 let full_path = buffer.read(cx).file()?.full_path(cx);
1798 let new_language = self.languages.select_language(&full_path)?;
1799 buffer.update(cx, |buffer, cx| {
1800 if buffer.language().map_or(true, |old_language| {
1801 !Arc::ptr_eq(old_language, &new_language)
1802 }) {
1803 buffer.set_language_registry(self.languages.clone());
1804 buffer.set_language(Some(new_language.clone()), cx);
1805 }
1806 });
1807
1808 let file = File::from_dyn(buffer.read(cx).file())?;
1809 let worktree = file.worktree.read(cx).as_local()?;
1810 let worktree_id = worktree.id();
1811 let worktree_abs_path = worktree.abs_path().clone();
1812 self.start_language_server(worktree_id, worktree_abs_path, new_language, cx);
1813
1814 None
1815 }
1816
1817 fn merge_json_value_into(source: serde_json::Value, target: &mut serde_json::Value) {
1818 use serde_json::Value;
1819
1820 match (source, target) {
1821 (Value::Object(source), Value::Object(target)) => {
1822 for (key, value) in source {
1823 if let Some(target) = target.get_mut(&key) {
1824 Self::merge_json_value_into(value, target);
1825 } else {
1826 target.insert(key.clone(), value);
1827 }
1828 }
1829 }
1830
1831 (source, target) => *target = source,
1832 }
1833 }
1834
1835 fn start_language_server(
1836 &mut self,
1837 worktree_id: WorktreeId,
1838 worktree_path: Arc<Path>,
1839 language: Arc<Language>,
1840 cx: &mut ModelContext<Self>,
1841 ) {
1842 if !cx
1843 .global::<Settings>()
1844 .enable_language_server(Some(&language.name()))
1845 {
1846 return;
1847 }
1848
1849 let adapter = if let Some(adapter) = language.lsp_adapter() {
1850 adapter
1851 } else {
1852 return;
1853 };
1854 let key = (worktree_id, adapter.name.clone());
1855
1856 let mut initialization_options = adapter.initialization_options.clone();
1857
1858 let lsp = &cx.global::<Settings>().lsp.get(&adapter.name.0);
1859 let override_options = lsp.map(|s| s.initialization_options.clone()).flatten();
1860 match (&mut initialization_options, override_options) {
1861 (Some(initialization_options), Some(override_options)) => {
1862 Self::merge_json_value_into(override_options, initialization_options);
1863 }
1864
1865 (None, override_options) => initialization_options = override_options,
1866
1867 _ => {}
1868 }
1869
1870 self.language_server_ids
1871 .entry(key.clone())
1872 .or_insert_with(|| {
1873 let server_id = post_inc(&mut self.next_language_server_id);
1874 let language_server = self.languages.start_language_server(
1875 server_id,
1876 language.clone(),
1877 worktree_path,
1878 self.client.http_client(),
1879 cx,
1880 );
1881 self.language_servers.insert(
1882 server_id,
1883 LanguageServerState::Starting(cx.spawn_weak(|this, mut cx| async move {
1884 let language_server = language_server?.await.log_err()?;
1885 let language_server = language_server
1886 .initialize(initialization_options)
1887 .await
1888 .log_err()?;
1889 let this = this.upgrade(&cx)?;
1890
1891 language_server
1892 .on_notification::<lsp::notification::PublishDiagnostics, _>({
1893 let this = this.downgrade();
1894 let adapter = adapter.clone();
1895 move |mut params, cx| {
1896 let this = this;
1897 let adapter = adapter.clone();
1898 cx.spawn(|mut cx| async move {
1899 adapter.process_diagnostics(&mut params).await;
1900 if let Some(this) = this.upgrade(&cx) {
1901 this.update(&mut cx, |this, cx| {
1902 this.update_diagnostics(
1903 server_id,
1904 params,
1905 &adapter.disk_based_diagnostic_sources,
1906 cx,
1907 )
1908 .log_err();
1909 });
1910 }
1911 })
1912 .detach();
1913 }
1914 })
1915 .detach();
1916
1917 language_server
1918 .on_request::<lsp::request::WorkspaceConfiguration, _, _>({
1919 let settings = this.read_with(&cx, |this, _| {
1920 this.language_server_settings.clone()
1921 });
1922 move |params, _| {
1923 let settings = settings.lock().clone();
1924 async move {
1925 Ok(params
1926 .items
1927 .into_iter()
1928 .map(|item| {
1929 if let Some(section) = &item.section {
1930 settings
1931 .get(section)
1932 .cloned()
1933 .unwrap_or(serde_json::Value::Null)
1934 } else {
1935 settings.clone()
1936 }
1937 })
1938 .collect())
1939 }
1940 }
1941 })
1942 .detach();
1943
1944 // Even though we don't have handling for these requests, respond to them to
1945 // avoid stalling any language server like `gopls` which waits for a response
1946 // to these requests when initializing.
1947 language_server
1948 .on_request::<lsp::request::WorkDoneProgressCreate, _, _>({
1949 let this = this.downgrade();
1950 move |params, mut cx| async move {
1951 if let Some(this) = this.upgrade(&cx) {
1952 this.update(&mut cx, |this, _| {
1953 if let Some(status) =
1954 this.language_server_statuses.get_mut(&server_id)
1955 {
1956 if let lsp::NumberOrString::String(token) =
1957 params.token
1958 {
1959 status.progress_tokens.insert(token);
1960 }
1961 }
1962 });
1963 }
1964 Ok(())
1965 }
1966 })
1967 .detach();
1968 language_server
1969 .on_request::<lsp::request::RegisterCapability, _, _>(|_, _| async {
1970 Ok(())
1971 })
1972 .detach();
1973
1974 language_server
1975 .on_request::<lsp::request::ApplyWorkspaceEdit, _, _>({
1976 let this = this.downgrade();
1977 let adapter = adapter.clone();
1978 let language_server = language_server.clone();
1979 move |params, cx| {
1980 Self::on_lsp_workspace_edit(
1981 this,
1982 params,
1983 server_id,
1984 adapter.clone(),
1985 language_server.clone(),
1986 cx,
1987 )
1988 }
1989 })
1990 .detach();
1991
1992 let disk_based_diagnostics_progress_token =
1993 adapter.disk_based_diagnostics_progress_token.clone();
1994
1995 language_server
1996 .on_notification::<lsp::notification::Progress, _>({
1997 let this = this.downgrade();
1998 move |params, mut cx| {
1999 if let Some(this) = this.upgrade(&cx) {
2000 this.update(&mut cx, |this, cx| {
2001 this.on_lsp_progress(
2002 params,
2003 server_id,
2004 disk_based_diagnostics_progress_token.clone(),
2005 cx,
2006 );
2007 });
2008 }
2009 }
2010 })
2011 .detach();
2012
2013 this.update(&mut cx, |this, cx| {
2014 // If the language server for this key doesn't match the server id, don't store the
2015 // server. Which will cause it to be dropped, killing the process
2016 if this
2017 .language_server_ids
2018 .get(&key)
2019 .map(|id| id != &server_id)
2020 .unwrap_or(false)
2021 {
2022 return None;
2023 }
2024
2025 // Update language_servers collection with Running variant of LanguageServerState
2026 // indicating that the server is up and running and ready
2027 this.language_servers.insert(
2028 server_id,
2029 LanguageServerState::Running {
2030 adapter: adapter.clone(),
2031 language,
2032 server: language_server.clone(),
2033 },
2034 );
2035 this.language_server_statuses.insert(
2036 server_id,
2037 LanguageServerStatus {
2038 name: language_server.name().to_string(),
2039 pending_work: Default::default(),
2040 has_pending_diagnostic_updates: false,
2041 progress_tokens: Default::default(),
2042 },
2043 );
2044 language_server
2045 .notify::<lsp::notification::DidChangeConfiguration>(
2046 lsp::DidChangeConfigurationParams {
2047 settings: this.language_server_settings.lock().clone(),
2048 },
2049 )
2050 .ok();
2051
2052 if let Some(project_id) = this.remote_id() {
2053 this.client
2054 .send(proto::StartLanguageServer {
2055 project_id,
2056 server: Some(proto::LanguageServer {
2057 id: server_id as u64,
2058 name: language_server.name().to_string(),
2059 }),
2060 })
2061 .log_err();
2062 }
2063
2064 // Tell the language server about every open buffer in the worktree that matches the language.
2065 for buffer in this.opened_buffers.values() {
2066 if let Some(buffer_handle) = buffer.upgrade(cx) {
2067 let buffer = buffer_handle.read(cx);
2068 let file = if let Some(file) = File::from_dyn(buffer.file()) {
2069 file
2070 } else {
2071 continue;
2072 };
2073 let language = if let Some(language) = buffer.language() {
2074 language
2075 } else {
2076 continue;
2077 };
2078 if file.worktree.read(cx).id() != key.0
2079 || language.lsp_adapter().map(|a| a.name.clone())
2080 != Some(key.1.clone())
2081 {
2082 continue;
2083 }
2084
2085 let file = file.as_local()?;
2086 let versions = this
2087 .buffer_snapshots
2088 .entry(buffer.remote_id())
2089 .or_insert_with(|| vec![(0, buffer.text_snapshot())]);
2090 let (version, initial_snapshot) = versions.last().unwrap();
2091 let uri = lsp::Url::from_file_path(file.abs_path(cx)).unwrap();
2092 language_server
2093 .notify::<lsp::notification::DidOpenTextDocument>(
2094 lsp::DidOpenTextDocumentParams {
2095 text_document: lsp::TextDocumentItem::new(
2096 uri,
2097 adapter
2098 .language_ids
2099 .get(language.name().as_ref())
2100 .cloned()
2101 .unwrap_or_default(),
2102 *version,
2103 initial_snapshot.text(),
2104 ),
2105 },
2106 )
2107 .log_err()?;
2108 buffer_handle.update(cx, |buffer, cx| {
2109 buffer.set_completion_triggers(
2110 language_server
2111 .capabilities()
2112 .completion_provider
2113 .as_ref()
2114 .and_then(|provider| {
2115 provider.trigger_characters.clone()
2116 })
2117 .unwrap_or_default(),
2118 cx,
2119 )
2120 });
2121 }
2122 }
2123
2124 cx.notify();
2125 Some(language_server)
2126 })
2127 })),
2128 );
2129
2130 server_id
2131 });
2132 }
2133
2134 // Returns a list of all of the worktrees which no longer have a language server and the root path
2135 // for the stopped server
2136 fn stop_language_server(
2137 &mut self,
2138 worktree_id: WorktreeId,
2139 adapter_name: LanguageServerName,
2140 cx: &mut ModelContext<Self>,
2141 ) -> Task<(Option<PathBuf>, Vec<WorktreeId>)> {
2142 let key = (worktree_id, adapter_name);
2143 if let Some(server_id) = self.language_server_ids.remove(&key) {
2144 // Remove other entries for this language server as well
2145 let mut orphaned_worktrees = vec![worktree_id];
2146 let other_keys = self.language_server_ids.keys().cloned().collect::<Vec<_>>();
2147 for other_key in other_keys {
2148 if self.language_server_ids.get(&other_key) == Some(&server_id) {
2149 self.language_server_ids.remove(&other_key);
2150 orphaned_worktrees.push(other_key.0);
2151 }
2152 }
2153
2154 self.language_server_statuses.remove(&server_id);
2155 cx.notify();
2156
2157 let server_state = self.language_servers.remove(&server_id);
2158 cx.spawn_weak(|this, mut cx| async move {
2159 let mut root_path = None;
2160
2161 let server = match server_state {
2162 Some(LanguageServerState::Starting(started_language_server)) => {
2163 started_language_server.await
2164 }
2165 Some(LanguageServerState::Running { server, .. }) => Some(server),
2166 None => None,
2167 };
2168
2169 if let Some(server) = server {
2170 root_path = Some(server.root_path().clone());
2171 if let Some(shutdown) = server.shutdown() {
2172 shutdown.await;
2173 }
2174 }
2175
2176 if let Some(this) = this.upgrade(&cx) {
2177 this.update(&mut cx, |this, cx| {
2178 this.language_server_statuses.remove(&server_id);
2179 cx.notify();
2180 });
2181 }
2182
2183 (root_path, orphaned_worktrees)
2184 })
2185 } else {
2186 Task::ready((None, Vec::new()))
2187 }
2188 }
2189
2190 pub fn restart_language_servers_for_buffers(
2191 &mut self,
2192 buffers: impl IntoIterator<Item = ModelHandle<Buffer>>,
2193 cx: &mut ModelContext<Self>,
2194 ) -> Option<()> {
2195 let language_server_lookup_info: HashSet<(WorktreeId, Arc<Path>, PathBuf)> = buffers
2196 .into_iter()
2197 .filter_map(|buffer| {
2198 let file = File::from_dyn(buffer.read(cx).file())?;
2199 let worktree = file.worktree.read(cx).as_local()?;
2200 let worktree_id = worktree.id();
2201 let worktree_abs_path = worktree.abs_path().clone();
2202 let full_path = file.full_path(cx);
2203 Some((worktree_id, worktree_abs_path, full_path))
2204 })
2205 .collect();
2206 for (worktree_id, worktree_abs_path, full_path) in language_server_lookup_info {
2207 let language = self.languages.select_language(&full_path)?;
2208 self.restart_language_server(worktree_id, worktree_abs_path, language, cx);
2209 }
2210
2211 None
2212 }
2213
2214 fn restart_language_server(
2215 &mut self,
2216 worktree_id: WorktreeId,
2217 fallback_path: Arc<Path>,
2218 language: Arc<Language>,
2219 cx: &mut ModelContext<Self>,
2220 ) {
2221 let adapter = if let Some(adapter) = language.lsp_adapter() {
2222 adapter
2223 } else {
2224 return;
2225 };
2226
2227 let server_name = adapter.name.clone();
2228 let stop = self.stop_language_server(worktree_id, server_name.clone(), cx);
2229 cx.spawn_weak(|this, mut cx| async move {
2230 let (original_root_path, orphaned_worktrees) = stop.await;
2231 if let Some(this) = this.upgrade(&cx) {
2232 this.update(&mut cx, |this, cx| {
2233 // Attempt to restart using original server path. Fallback to passed in
2234 // path if we could not retrieve the root path
2235 let root_path = original_root_path
2236 .map(|path_buf| Arc::from(path_buf.as_path()))
2237 .unwrap_or(fallback_path);
2238
2239 this.start_language_server(worktree_id, root_path, language, cx);
2240
2241 // Lookup new server id and set it for each of the orphaned worktrees
2242 if let Some(new_server_id) = this
2243 .language_server_ids
2244 .get(&(worktree_id, server_name.clone()))
2245 .cloned()
2246 {
2247 for orphaned_worktree in orphaned_worktrees {
2248 this.language_server_ids
2249 .insert((orphaned_worktree, server_name.clone()), new_server_id);
2250 }
2251 }
2252 });
2253 }
2254 })
2255 .detach();
2256 }
2257
2258 fn on_lsp_progress(
2259 &mut self,
2260 progress: lsp::ProgressParams,
2261 server_id: usize,
2262 disk_based_diagnostics_progress_token: Option<String>,
2263 cx: &mut ModelContext<Self>,
2264 ) {
2265 let token = match progress.token {
2266 lsp::NumberOrString::String(token) => token,
2267 lsp::NumberOrString::Number(token) => {
2268 log::info!("skipping numeric progress token {}", token);
2269 return;
2270 }
2271 };
2272 let lsp::ProgressParamsValue::WorkDone(progress) = progress.value;
2273 let language_server_status =
2274 if let Some(status) = self.language_server_statuses.get_mut(&server_id) {
2275 status
2276 } else {
2277 return;
2278 };
2279
2280 if !language_server_status.progress_tokens.contains(&token) {
2281 return;
2282 }
2283
2284 let is_disk_based_diagnostics_progress = disk_based_diagnostics_progress_token
2285 .as_ref()
2286 .map_or(false, |disk_based_token| {
2287 token.starts_with(disk_based_token)
2288 });
2289
2290 match progress {
2291 lsp::WorkDoneProgress::Begin(report) => {
2292 if is_disk_based_diagnostics_progress {
2293 language_server_status.has_pending_diagnostic_updates = true;
2294 self.disk_based_diagnostics_started(server_id, cx);
2295 self.broadcast_language_server_update(
2296 server_id,
2297 proto::update_language_server::Variant::DiskBasedDiagnosticsUpdating(
2298 proto::LspDiskBasedDiagnosticsUpdating {},
2299 ),
2300 );
2301 } else {
2302 self.on_lsp_work_start(
2303 server_id,
2304 token.clone(),
2305 LanguageServerProgress {
2306 message: report.message.clone(),
2307 percentage: report.percentage.map(|p| p as usize),
2308 last_update_at: Instant::now(),
2309 },
2310 cx,
2311 );
2312 self.broadcast_language_server_update(
2313 server_id,
2314 proto::update_language_server::Variant::WorkStart(proto::LspWorkStart {
2315 token,
2316 message: report.message,
2317 percentage: report.percentage.map(|p| p as u32),
2318 }),
2319 );
2320 }
2321 }
2322 lsp::WorkDoneProgress::Report(report) => {
2323 if !is_disk_based_diagnostics_progress {
2324 self.on_lsp_work_progress(
2325 server_id,
2326 token.clone(),
2327 LanguageServerProgress {
2328 message: report.message.clone(),
2329 percentage: report.percentage.map(|p| p as usize),
2330 last_update_at: Instant::now(),
2331 },
2332 cx,
2333 );
2334 self.broadcast_language_server_update(
2335 server_id,
2336 proto::update_language_server::Variant::WorkProgress(
2337 proto::LspWorkProgress {
2338 token,
2339 message: report.message,
2340 percentage: report.percentage.map(|p| p as u32),
2341 },
2342 ),
2343 );
2344 }
2345 }
2346 lsp::WorkDoneProgress::End(_) => {
2347 language_server_status.progress_tokens.remove(&token);
2348
2349 if is_disk_based_diagnostics_progress {
2350 language_server_status.has_pending_diagnostic_updates = false;
2351 self.disk_based_diagnostics_finished(server_id, cx);
2352 self.broadcast_language_server_update(
2353 server_id,
2354 proto::update_language_server::Variant::DiskBasedDiagnosticsUpdated(
2355 proto::LspDiskBasedDiagnosticsUpdated {},
2356 ),
2357 );
2358 } else {
2359 self.on_lsp_work_end(server_id, token.clone(), cx);
2360 self.broadcast_language_server_update(
2361 server_id,
2362 proto::update_language_server::Variant::WorkEnd(proto::LspWorkEnd {
2363 token,
2364 }),
2365 );
2366 }
2367 }
2368 }
2369 }
2370
2371 fn on_lsp_work_start(
2372 &mut self,
2373 language_server_id: usize,
2374 token: String,
2375 progress: LanguageServerProgress,
2376 cx: &mut ModelContext<Self>,
2377 ) {
2378 if let Some(status) = self.language_server_statuses.get_mut(&language_server_id) {
2379 status.pending_work.insert(token, progress);
2380 cx.notify();
2381 }
2382 }
2383
2384 fn on_lsp_work_progress(
2385 &mut self,
2386 language_server_id: usize,
2387 token: String,
2388 progress: LanguageServerProgress,
2389 cx: &mut ModelContext<Self>,
2390 ) {
2391 if let Some(status) = self.language_server_statuses.get_mut(&language_server_id) {
2392 let entry = status
2393 .pending_work
2394 .entry(token)
2395 .or_insert(LanguageServerProgress {
2396 message: Default::default(),
2397 percentage: Default::default(),
2398 last_update_at: progress.last_update_at,
2399 });
2400 if progress.message.is_some() {
2401 entry.message = progress.message;
2402 }
2403 if progress.percentage.is_some() {
2404 entry.percentage = progress.percentage;
2405 }
2406 entry.last_update_at = progress.last_update_at;
2407 cx.notify();
2408 }
2409 }
2410
2411 fn on_lsp_work_end(
2412 &mut self,
2413 language_server_id: usize,
2414 token: String,
2415 cx: &mut ModelContext<Self>,
2416 ) {
2417 if let Some(status) = self.language_server_statuses.get_mut(&language_server_id) {
2418 status.pending_work.remove(&token);
2419 cx.notify();
2420 }
2421 }
2422
2423 async fn on_lsp_workspace_edit(
2424 this: WeakModelHandle<Self>,
2425 params: lsp::ApplyWorkspaceEditParams,
2426 server_id: usize,
2427 adapter: Arc<CachedLspAdapter>,
2428 language_server: Arc<LanguageServer>,
2429 mut cx: AsyncAppContext,
2430 ) -> Result<lsp::ApplyWorkspaceEditResponse> {
2431 let this = this
2432 .upgrade(&cx)
2433 .ok_or_else(|| anyhow!("project project closed"))?;
2434 let transaction = Self::deserialize_workspace_edit(
2435 this.clone(),
2436 params.edit,
2437 true,
2438 adapter.clone(),
2439 language_server.clone(),
2440 &mut cx,
2441 )
2442 .await
2443 .log_err();
2444 this.update(&mut cx, |this, _| {
2445 if let Some(transaction) = transaction {
2446 this.last_workspace_edits_by_language_server
2447 .insert(server_id, transaction);
2448 }
2449 });
2450 Ok(lsp::ApplyWorkspaceEditResponse {
2451 applied: true,
2452 failed_change: None,
2453 failure_reason: None,
2454 })
2455 }
2456
2457 fn broadcast_language_server_update(
2458 &self,
2459 language_server_id: usize,
2460 event: proto::update_language_server::Variant,
2461 ) {
2462 if let Some(project_id) = self.remote_id() {
2463 self.client
2464 .send(proto::UpdateLanguageServer {
2465 project_id,
2466 language_server_id: language_server_id as u64,
2467 variant: Some(event),
2468 })
2469 .log_err();
2470 }
2471 }
2472
2473 pub fn set_language_server_settings(&mut self, settings: serde_json::Value) {
2474 for server_state in self.language_servers.values() {
2475 if let LanguageServerState::Running { server, .. } = server_state {
2476 server
2477 .notify::<lsp::notification::DidChangeConfiguration>(
2478 lsp::DidChangeConfigurationParams {
2479 settings: settings.clone(),
2480 },
2481 )
2482 .ok();
2483 }
2484 }
2485 *self.language_server_settings.lock() = settings;
2486 }
2487
2488 pub fn language_server_statuses(
2489 &self,
2490 ) -> impl DoubleEndedIterator<Item = &LanguageServerStatus> {
2491 self.language_server_statuses.values()
2492 }
2493
2494 pub fn update_diagnostics(
2495 &mut self,
2496 language_server_id: usize,
2497 params: lsp::PublishDiagnosticsParams,
2498 disk_based_sources: &[String],
2499 cx: &mut ModelContext<Self>,
2500 ) -> Result<()> {
2501 let abs_path = params
2502 .uri
2503 .to_file_path()
2504 .map_err(|_| anyhow!("URI is not a file"))?;
2505 let mut diagnostics = Vec::default();
2506 let mut primary_diagnostic_group_ids = HashMap::default();
2507 let mut sources_by_group_id = HashMap::default();
2508 let mut supporting_diagnostics = HashMap::default();
2509 for diagnostic in ¶ms.diagnostics {
2510 let source = diagnostic.source.as_ref();
2511 let code = diagnostic.code.as_ref().map(|code| match code {
2512 lsp::NumberOrString::Number(code) => code.to_string(),
2513 lsp::NumberOrString::String(code) => code.clone(),
2514 });
2515 let range = range_from_lsp(diagnostic.range);
2516 let is_supporting = diagnostic
2517 .related_information
2518 .as_ref()
2519 .map_or(false, |infos| {
2520 infos.iter().any(|info| {
2521 primary_diagnostic_group_ids.contains_key(&(
2522 source,
2523 code.clone(),
2524 range_from_lsp(info.location.range),
2525 ))
2526 })
2527 });
2528
2529 let is_unnecessary = diagnostic.tags.as_ref().map_or(false, |tags| {
2530 tags.iter().any(|tag| *tag == DiagnosticTag::UNNECESSARY)
2531 });
2532
2533 if is_supporting {
2534 supporting_diagnostics.insert(
2535 (source, code.clone(), range),
2536 (diagnostic.severity, is_unnecessary),
2537 );
2538 } else {
2539 let group_id = post_inc(&mut self.next_diagnostic_group_id);
2540 let is_disk_based =
2541 source.map_or(false, |source| disk_based_sources.contains(source));
2542
2543 sources_by_group_id.insert(group_id, source);
2544 primary_diagnostic_group_ids
2545 .insert((source, code.clone(), range.clone()), group_id);
2546
2547 diagnostics.push(DiagnosticEntry {
2548 range,
2549 diagnostic: Diagnostic {
2550 code: code.clone(),
2551 severity: diagnostic.severity.unwrap_or(DiagnosticSeverity::ERROR),
2552 message: diagnostic.message.clone(),
2553 group_id,
2554 is_primary: true,
2555 is_valid: true,
2556 is_disk_based,
2557 is_unnecessary,
2558 },
2559 });
2560 if let Some(infos) = &diagnostic.related_information {
2561 for info in infos {
2562 if info.location.uri == params.uri && !info.message.is_empty() {
2563 let range = range_from_lsp(info.location.range);
2564 diagnostics.push(DiagnosticEntry {
2565 range,
2566 diagnostic: Diagnostic {
2567 code: code.clone(),
2568 severity: DiagnosticSeverity::INFORMATION,
2569 message: info.message.clone(),
2570 group_id,
2571 is_primary: false,
2572 is_valid: true,
2573 is_disk_based,
2574 is_unnecessary: false,
2575 },
2576 });
2577 }
2578 }
2579 }
2580 }
2581 }
2582
2583 for entry in &mut diagnostics {
2584 let diagnostic = &mut entry.diagnostic;
2585 if !diagnostic.is_primary {
2586 let source = *sources_by_group_id.get(&diagnostic.group_id).unwrap();
2587 if let Some(&(severity, is_unnecessary)) = supporting_diagnostics.get(&(
2588 source,
2589 diagnostic.code.clone(),
2590 entry.range.clone(),
2591 )) {
2592 if let Some(severity) = severity {
2593 diagnostic.severity = severity;
2594 }
2595 diagnostic.is_unnecessary = is_unnecessary;
2596 }
2597 }
2598 }
2599
2600 self.update_diagnostic_entries(
2601 language_server_id,
2602 abs_path,
2603 params.version,
2604 diagnostics,
2605 cx,
2606 )?;
2607 Ok(())
2608 }
2609
2610 pub fn update_diagnostic_entries(
2611 &mut self,
2612 language_server_id: usize,
2613 abs_path: PathBuf,
2614 version: Option<i32>,
2615 diagnostics: Vec<DiagnosticEntry<Unclipped<PointUtf16>>>,
2616 cx: &mut ModelContext<Project>,
2617 ) -> Result<(), anyhow::Error> {
2618 let (worktree, relative_path) = self
2619 .find_local_worktree(&abs_path, cx)
2620 .ok_or_else(|| anyhow!("no worktree found for diagnostics"))?;
2621
2622 let project_path = ProjectPath {
2623 worktree_id: worktree.read(cx).id(),
2624 path: relative_path.into(),
2625 };
2626 if let Some(buffer) = self.get_open_buffer(&project_path, cx) {
2627 self.update_buffer_diagnostics(&buffer, diagnostics.clone(), version, cx)?;
2628 }
2629
2630 let updated = worktree.update(cx, |worktree, cx| {
2631 worktree
2632 .as_local_mut()
2633 .ok_or_else(|| anyhow!("not a local worktree"))?
2634 .update_diagnostics(
2635 language_server_id,
2636 project_path.path.clone(),
2637 diagnostics,
2638 cx,
2639 )
2640 })?;
2641 if updated {
2642 cx.emit(Event::DiagnosticsUpdated {
2643 language_server_id,
2644 path: project_path,
2645 });
2646 }
2647 Ok(())
2648 }
2649
2650 fn update_buffer_diagnostics(
2651 &mut self,
2652 buffer: &ModelHandle<Buffer>,
2653 mut diagnostics: Vec<DiagnosticEntry<Unclipped<PointUtf16>>>,
2654 version: Option<i32>,
2655 cx: &mut ModelContext<Self>,
2656 ) -> Result<()> {
2657 fn compare_diagnostics(a: &Diagnostic, b: &Diagnostic) -> Ordering {
2658 Ordering::Equal
2659 .then_with(|| b.is_primary.cmp(&a.is_primary))
2660 .then_with(|| a.is_disk_based.cmp(&b.is_disk_based))
2661 .then_with(|| a.severity.cmp(&b.severity))
2662 .then_with(|| a.message.cmp(&b.message))
2663 }
2664
2665 let snapshot = self.buffer_snapshot_for_lsp_version(buffer, version, cx)?;
2666
2667 diagnostics.sort_unstable_by(|a, b| {
2668 Ordering::Equal
2669 .then_with(|| a.range.start.cmp(&b.range.start))
2670 .then_with(|| b.range.end.cmp(&a.range.end))
2671 .then_with(|| compare_diagnostics(&a.diagnostic, &b.diagnostic))
2672 });
2673
2674 let mut sanitized_diagnostics = Vec::new();
2675 let edits_since_save = Patch::new(
2676 snapshot
2677 .edits_since::<Unclipped<PointUtf16>>(buffer.read(cx).saved_version())
2678 .collect(),
2679 );
2680 for entry in diagnostics {
2681 let start;
2682 let end;
2683 if entry.diagnostic.is_disk_based {
2684 // Some diagnostics are based on files on disk instead of buffers'
2685 // current contents. Adjust these diagnostics' ranges to reflect
2686 // any unsaved edits.
2687 start = edits_since_save.old_to_new(entry.range.start);
2688 end = edits_since_save.old_to_new(entry.range.end);
2689 } else {
2690 start = entry.range.start;
2691 end = entry.range.end;
2692 }
2693
2694 let mut range = snapshot.clip_point_utf16(start, Bias::Left)
2695 ..snapshot.clip_point_utf16(end, Bias::Right);
2696
2697 // Expand empty ranges by one codepoint
2698 if range.start == range.end {
2699 // This will be go to the next boundary when being clipped
2700 range.end.column += 1;
2701 range.end = snapshot.clip_point_utf16(Unclipped(range.end), Bias::Right);
2702 if range.start == range.end && range.end.column > 0 {
2703 range.start.column -= 1;
2704 range.end = snapshot.clip_point_utf16(Unclipped(range.end), Bias::Left);
2705 }
2706 }
2707
2708 sanitized_diagnostics.push(DiagnosticEntry {
2709 range,
2710 diagnostic: entry.diagnostic,
2711 });
2712 }
2713 drop(edits_since_save);
2714
2715 let set = DiagnosticSet::new(sanitized_diagnostics, &snapshot);
2716 buffer.update(cx, |buffer, cx| buffer.update_diagnostics(set, cx));
2717 Ok(())
2718 }
2719
2720 pub fn reload_buffers(
2721 &self,
2722 buffers: HashSet<ModelHandle<Buffer>>,
2723 push_to_history: bool,
2724 cx: &mut ModelContext<Self>,
2725 ) -> Task<Result<ProjectTransaction>> {
2726 let mut local_buffers = Vec::new();
2727 let mut remote_buffers = None;
2728 for buffer_handle in buffers {
2729 let buffer = buffer_handle.read(cx);
2730 if buffer.is_dirty() {
2731 if let Some(file) = File::from_dyn(buffer.file()) {
2732 if file.is_local() {
2733 local_buffers.push(buffer_handle);
2734 } else {
2735 remote_buffers.get_or_insert(Vec::new()).push(buffer_handle);
2736 }
2737 }
2738 }
2739 }
2740
2741 let remote_buffers = self.remote_id().zip(remote_buffers);
2742 let client = self.client.clone();
2743
2744 cx.spawn(|this, mut cx| async move {
2745 let mut project_transaction = ProjectTransaction::default();
2746
2747 if let Some((project_id, remote_buffers)) = remote_buffers {
2748 let response = client
2749 .request(proto::ReloadBuffers {
2750 project_id,
2751 buffer_ids: remote_buffers
2752 .iter()
2753 .map(|buffer| buffer.read_with(&cx, |buffer, _| buffer.remote_id()))
2754 .collect(),
2755 })
2756 .await?
2757 .transaction
2758 .ok_or_else(|| anyhow!("missing transaction"))?;
2759 project_transaction = this
2760 .update(&mut cx, |this, cx| {
2761 this.deserialize_project_transaction(response, push_to_history, cx)
2762 })
2763 .await?;
2764 }
2765
2766 for buffer in local_buffers {
2767 let transaction = buffer
2768 .update(&mut cx, |buffer, cx| buffer.reload(cx))
2769 .await?;
2770 buffer.update(&mut cx, |buffer, cx| {
2771 if let Some(transaction) = transaction {
2772 if !push_to_history {
2773 buffer.forget_transaction(transaction.id);
2774 }
2775 project_transaction.0.insert(cx.handle(), transaction);
2776 }
2777 });
2778 }
2779
2780 Ok(project_transaction)
2781 })
2782 }
2783
2784 pub fn format(
2785 &self,
2786 buffers: HashSet<ModelHandle<Buffer>>,
2787 push_to_history: bool,
2788 trigger: FormatTrigger,
2789 cx: &mut ModelContext<Project>,
2790 ) -> Task<Result<ProjectTransaction>> {
2791 let mut local_buffers = Vec::new();
2792 let mut remote_buffers = None;
2793 for buffer_handle in buffers {
2794 let buffer = buffer_handle.read(cx);
2795 if let Some(file) = File::from_dyn(buffer.file()) {
2796 if let Some(buffer_abs_path) = file.as_local().map(|f| f.abs_path(cx)) {
2797 if let Some((_, server)) = self.language_server_for_buffer(buffer, cx) {
2798 local_buffers.push((buffer_handle, buffer_abs_path, server.clone()));
2799 }
2800 } else {
2801 remote_buffers.get_or_insert(Vec::new()).push(buffer_handle);
2802 }
2803 } else {
2804 return Task::ready(Ok(Default::default()));
2805 }
2806 }
2807
2808 let remote_buffers = self.remote_id().zip(remote_buffers);
2809 let client = self.client.clone();
2810
2811 cx.spawn(|this, mut cx| async move {
2812 let mut project_transaction = ProjectTransaction::default();
2813
2814 if let Some((project_id, remote_buffers)) = remote_buffers {
2815 let response = client
2816 .request(proto::FormatBuffers {
2817 project_id,
2818 trigger: trigger as i32,
2819 buffer_ids: remote_buffers
2820 .iter()
2821 .map(|buffer| buffer.read_with(&cx, |buffer, _| buffer.remote_id()))
2822 .collect(),
2823 })
2824 .await?
2825 .transaction
2826 .ok_or_else(|| anyhow!("missing transaction"))?;
2827 project_transaction = this
2828 .update(&mut cx, |this, cx| {
2829 this.deserialize_project_transaction(response, push_to_history, cx)
2830 })
2831 .await?;
2832 }
2833
2834 // Do not allow multiple concurrent formatting requests for the
2835 // same buffer.
2836 this.update(&mut cx, |this, _| {
2837 local_buffers
2838 .retain(|(buffer, _, _)| this.buffers_being_formatted.insert(buffer.id()));
2839 });
2840 let _cleanup = defer({
2841 let this = this.clone();
2842 let mut cx = cx.clone();
2843 let local_buffers = &local_buffers;
2844 move || {
2845 this.update(&mut cx, |this, _| {
2846 for (buffer, _, _) in local_buffers {
2847 this.buffers_being_formatted.remove(&buffer.id());
2848 }
2849 });
2850 }
2851 });
2852
2853 for (buffer, buffer_abs_path, language_server) in &local_buffers {
2854 let (format_on_save, formatter, tab_size) = buffer.read_with(&cx, |buffer, cx| {
2855 let settings = cx.global::<Settings>();
2856 let language_name = buffer.language().map(|language| language.name());
2857 (
2858 settings.format_on_save(language_name.as_deref()),
2859 settings.formatter(language_name.as_deref()),
2860 settings.tab_size(language_name.as_deref()),
2861 )
2862 });
2863
2864 let transaction = match (formatter, format_on_save) {
2865 (_, FormatOnSave::Off) if trigger == FormatTrigger::Save => continue,
2866
2867 (Formatter::LanguageServer, FormatOnSave::On | FormatOnSave::Off)
2868 | (_, FormatOnSave::LanguageServer) => Self::format_via_lsp(
2869 &this,
2870 &buffer,
2871 &buffer_abs_path,
2872 &language_server,
2873 tab_size,
2874 &mut cx,
2875 )
2876 .await
2877 .context("failed to format via language server")?,
2878
2879 (
2880 Formatter::External { command, arguments },
2881 FormatOnSave::On | FormatOnSave::Off,
2882 )
2883 | (_, FormatOnSave::External { command, arguments }) => {
2884 Self::format_via_external_command(
2885 &buffer,
2886 &buffer_abs_path,
2887 &command,
2888 &arguments,
2889 &mut cx,
2890 )
2891 .await
2892 .context(format!(
2893 "failed to format via external command {:?}",
2894 command
2895 ))?
2896 }
2897 };
2898
2899 if let Some(transaction) = transaction {
2900 if !push_to_history {
2901 buffer.update(&mut cx, |buffer, _| {
2902 buffer.forget_transaction(transaction.id)
2903 });
2904 }
2905 project_transaction.0.insert(buffer.clone(), transaction);
2906 }
2907 }
2908
2909 Ok(project_transaction)
2910 })
2911 }
2912
2913 async fn format_via_lsp(
2914 this: &ModelHandle<Self>,
2915 buffer: &ModelHandle<Buffer>,
2916 abs_path: &Path,
2917 language_server: &Arc<LanguageServer>,
2918 tab_size: NonZeroU32,
2919 cx: &mut AsyncAppContext,
2920 ) -> Result<Option<Transaction>> {
2921 let text_document =
2922 lsp::TextDocumentIdentifier::new(lsp::Url::from_file_path(abs_path).unwrap());
2923 let capabilities = &language_server.capabilities();
2924 let lsp_edits = if capabilities
2925 .document_formatting_provider
2926 .as_ref()
2927 .map_or(false, |provider| *provider != lsp::OneOf::Left(false))
2928 {
2929 language_server
2930 .request::<lsp::request::Formatting>(lsp::DocumentFormattingParams {
2931 text_document,
2932 options: lsp::FormattingOptions {
2933 tab_size: tab_size.into(),
2934 insert_spaces: true,
2935 insert_final_newline: Some(true),
2936 ..Default::default()
2937 },
2938 work_done_progress_params: Default::default(),
2939 })
2940 .await?
2941 } else if capabilities
2942 .document_range_formatting_provider
2943 .as_ref()
2944 .map_or(false, |provider| *provider != lsp::OneOf::Left(false))
2945 {
2946 let buffer_start = lsp::Position::new(0, 0);
2947 let buffer_end =
2948 buffer.read_with(cx, |buffer, _| point_to_lsp(buffer.max_point_utf16()));
2949 language_server
2950 .request::<lsp::request::RangeFormatting>(lsp::DocumentRangeFormattingParams {
2951 text_document,
2952 range: lsp::Range::new(buffer_start, buffer_end),
2953 options: lsp::FormattingOptions {
2954 tab_size: tab_size.into(),
2955 insert_spaces: true,
2956 insert_final_newline: Some(true),
2957 ..Default::default()
2958 },
2959 work_done_progress_params: Default::default(),
2960 })
2961 .await?
2962 } else {
2963 None
2964 };
2965
2966 if let Some(lsp_edits) = lsp_edits {
2967 let edits = this
2968 .update(cx, |this, cx| {
2969 this.edits_from_lsp(buffer, lsp_edits, None, cx)
2970 })
2971 .await?;
2972 buffer.update(cx, |buffer, cx| {
2973 buffer.finalize_last_transaction();
2974 buffer.start_transaction();
2975 for (range, text) in edits {
2976 buffer.edit([(range, text)], None, cx);
2977 }
2978 if buffer.end_transaction(cx).is_some() {
2979 let transaction = buffer.finalize_last_transaction().unwrap().clone();
2980 Ok(Some(transaction))
2981 } else {
2982 Ok(None)
2983 }
2984 })
2985 } else {
2986 Ok(None)
2987 }
2988 }
2989
2990 async fn format_via_external_command(
2991 buffer: &ModelHandle<Buffer>,
2992 buffer_abs_path: &Path,
2993 command: &str,
2994 arguments: &[String],
2995 cx: &mut AsyncAppContext,
2996 ) -> Result<Option<Transaction>> {
2997 let working_dir_path = buffer.read_with(cx, |buffer, cx| {
2998 let file = File::from_dyn(buffer.file())?;
2999 let worktree = file.worktree.read(cx).as_local()?;
3000 let mut worktree_path = worktree.abs_path().to_path_buf();
3001 if worktree.root_entry()?.is_file() {
3002 worktree_path.pop();
3003 }
3004 Some(worktree_path)
3005 });
3006
3007 if let Some(working_dir_path) = working_dir_path {
3008 let mut child =
3009 smol::process::Command::new(command)
3010 .args(arguments.iter().map(|arg| {
3011 arg.replace("{buffer_path}", &buffer_abs_path.to_string_lossy())
3012 }))
3013 .current_dir(&working_dir_path)
3014 .stdin(smol::process::Stdio::piped())
3015 .stdout(smol::process::Stdio::piped())
3016 .stderr(smol::process::Stdio::piped())
3017 .spawn()?;
3018 let stdin = child
3019 .stdin
3020 .as_mut()
3021 .ok_or_else(|| anyhow!("failed to acquire stdin"))?;
3022 let text = buffer.read_with(cx, |buffer, _| buffer.as_rope().clone());
3023 for chunk in text.chunks() {
3024 stdin.write_all(chunk.as_bytes()).await?;
3025 }
3026 stdin.flush().await?;
3027
3028 let output = child.output().await?;
3029 if !output.status.success() {
3030 return Err(anyhow!(
3031 "command failed with exit code {:?}:\nstdout: {}\nstderr: {}",
3032 output.status.code(),
3033 String::from_utf8_lossy(&output.stdout),
3034 String::from_utf8_lossy(&output.stderr),
3035 ));
3036 }
3037
3038 let stdout = String::from_utf8(output.stdout)?;
3039 let diff = buffer
3040 .read_with(cx, |buffer, cx| buffer.diff(stdout, cx))
3041 .await;
3042 Ok(buffer.update(cx, |buffer, cx| buffer.apply_diff(diff, cx).cloned()))
3043 } else {
3044 Ok(None)
3045 }
3046 }
3047
3048 pub fn definition<T: ToPointUtf16>(
3049 &self,
3050 buffer: &ModelHandle<Buffer>,
3051 position: T,
3052 cx: &mut ModelContext<Self>,
3053 ) -> Task<Result<Vec<LocationLink>>> {
3054 let position = position.to_point_utf16(buffer.read(cx));
3055 self.request_lsp(buffer.clone(), GetDefinition { position }, cx)
3056 }
3057
3058 pub fn type_definition<T: ToPointUtf16>(
3059 &self,
3060 buffer: &ModelHandle<Buffer>,
3061 position: T,
3062 cx: &mut ModelContext<Self>,
3063 ) -> Task<Result<Vec<LocationLink>>> {
3064 let position = position.to_point_utf16(buffer.read(cx));
3065 self.request_lsp(buffer.clone(), GetTypeDefinition { position }, cx)
3066 }
3067
3068 pub fn references<T: ToPointUtf16>(
3069 &self,
3070 buffer: &ModelHandle<Buffer>,
3071 position: T,
3072 cx: &mut ModelContext<Self>,
3073 ) -> Task<Result<Vec<Location>>> {
3074 let position = position.to_point_utf16(buffer.read(cx));
3075 self.request_lsp(buffer.clone(), GetReferences { position }, cx)
3076 }
3077
3078 pub fn document_highlights<T: ToPointUtf16>(
3079 &self,
3080 buffer: &ModelHandle<Buffer>,
3081 position: T,
3082 cx: &mut ModelContext<Self>,
3083 ) -> Task<Result<Vec<DocumentHighlight>>> {
3084 let position = position.to_point_utf16(buffer.read(cx));
3085 self.request_lsp(buffer.clone(), GetDocumentHighlights { position }, cx)
3086 }
3087
3088 pub fn symbols(&self, query: &str, cx: &mut ModelContext<Self>) -> Task<Result<Vec<Symbol>>> {
3089 if self.is_local() {
3090 let mut requests = Vec::new();
3091 for ((worktree_id, _), server_id) in self.language_server_ids.iter() {
3092 let worktree_id = *worktree_id;
3093 if let Some(worktree) = self
3094 .worktree_for_id(worktree_id, cx)
3095 .and_then(|worktree| worktree.read(cx).as_local())
3096 {
3097 if let Some(LanguageServerState::Running {
3098 adapter,
3099 language,
3100 server,
3101 }) = self.language_servers.get(server_id)
3102 {
3103 let adapter = adapter.clone();
3104 let language = language.clone();
3105 let worktree_abs_path = worktree.abs_path().clone();
3106 requests.push(
3107 server
3108 .request::<lsp::request::WorkspaceSymbol>(
3109 lsp::WorkspaceSymbolParams {
3110 query: query.to_string(),
3111 ..Default::default()
3112 },
3113 )
3114 .log_err()
3115 .map(move |response| {
3116 (
3117 adapter,
3118 language,
3119 worktree_id,
3120 worktree_abs_path,
3121 response.unwrap_or_default(),
3122 )
3123 }),
3124 );
3125 }
3126 }
3127 }
3128
3129 cx.spawn_weak(|this, cx| async move {
3130 let responses = futures::future::join_all(requests).await;
3131 let this = if let Some(this) = this.upgrade(&cx) {
3132 this
3133 } else {
3134 return Ok(Default::default());
3135 };
3136 let symbols = this.read_with(&cx, |this, cx| {
3137 let mut symbols = Vec::new();
3138 for (
3139 adapter,
3140 adapter_language,
3141 source_worktree_id,
3142 worktree_abs_path,
3143 response,
3144 ) in responses
3145 {
3146 symbols.extend(response.into_iter().flatten().filter_map(|lsp_symbol| {
3147 let abs_path = lsp_symbol.location.uri.to_file_path().ok()?;
3148 let mut worktree_id = source_worktree_id;
3149 let path;
3150 if let Some((worktree, rel_path)) =
3151 this.find_local_worktree(&abs_path, cx)
3152 {
3153 worktree_id = worktree.read(cx).id();
3154 path = rel_path;
3155 } else {
3156 path = relativize_path(&worktree_abs_path, &abs_path);
3157 }
3158
3159 let project_path = ProjectPath {
3160 worktree_id,
3161 path: path.into(),
3162 };
3163 let signature = this.symbol_signature(&project_path);
3164 let language = this
3165 .languages
3166 .select_language(&project_path.path)
3167 .unwrap_or(adapter_language.clone());
3168 let language_server_name = adapter.name.clone();
3169 Some(async move {
3170 let label = language
3171 .label_for_symbol(&lsp_symbol.name, lsp_symbol.kind)
3172 .await;
3173
3174 Symbol {
3175 language_server_name,
3176 source_worktree_id,
3177 path: project_path,
3178 label: label.unwrap_or_else(|| {
3179 CodeLabel::plain(lsp_symbol.name.clone(), None)
3180 }),
3181 kind: lsp_symbol.kind,
3182 name: lsp_symbol.name,
3183 range: range_from_lsp(lsp_symbol.location.range),
3184 signature,
3185 }
3186 })
3187 }));
3188 }
3189 symbols
3190 });
3191 Ok(futures::future::join_all(symbols).await)
3192 })
3193 } else if let Some(project_id) = self.remote_id() {
3194 let request = self.client.request(proto::GetProjectSymbols {
3195 project_id,
3196 query: query.to_string(),
3197 });
3198 cx.spawn_weak(|this, cx| async move {
3199 let response = request.await?;
3200 let mut symbols = Vec::new();
3201 if let Some(this) = this.upgrade(&cx) {
3202 let new_symbols = this.read_with(&cx, |this, _| {
3203 response
3204 .symbols
3205 .into_iter()
3206 .map(|symbol| this.deserialize_symbol(symbol))
3207 .collect::<Vec<_>>()
3208 });
3209 symbols = futures::future::join_all(new_symbols)
3210 .await
3211 .into_iter()
3212 .filter_map(|symbol| symbol.log_err())
3213 .collect::<Vec<_>>();
3214 }
3215 Ok(symbols)
3216 })
3217 } else {
3218 Task::ready(Ok(Default::default()))
3219 }
3220 }
3221
3222 pub fn open_buffer_for_symbol(
3223 &mut self,
3224 symbol: &Symbol,
3225 cx: &mut ModelContext<Self>,
3226 ) -> Task<Result<ModelHandle<Buffer>>> {
3227 if self.is_local() {
3228 let language_server_id = if let Some(id) = self.language_server_ids.get(&(
3229 symbol.source_worktree_id,
3230 symbol.language_server_name.clone(),
3231 )) {
3232 *id
3233 } else {
3234 return Task::ready(Err(anyhow!(
3235 "language server for worktree and language not found"
3236 )));
3237 };
3238
3239 let worktree_abs_path = if let Some(worktree_abs_path) = self
3240 .worktree_for_id(symbol.path.worktree_id, cx)
3241 .and_then(|worktree| worktree.read(cx).as_local())
3242 .map(|local_worktree| local_worktree.abs_path())
3243 {
3244 worktree_abs_path
3245 } else {
3246 return Task::ready(Err(anyhow!("worktree not found for symbol")));
3247 };
3248 let symbol_abs_path = worktree_abs_path.join(&symbol.path.path);
3249 let symbol_uri = if let Ok(uri) = lsp::Url::from_file_path(symbol_abs_path) {
3250 uri
3251 } else {
3252 return Task::ready(Err(anyhow!("invalid symbol path")));
3253 };
3254
3255 self.open_local_buffer_via_lsp(
3256 symbol_uri,
3257 language_server_id,
3258 symbol.language_server_name.clone(),
3259 cx,
3260 )
3261 } else if let Some(project_id) = self.remote_id() {
3262 let request = self.client.request(proto::OpenBufferForSymbol {
3263 project_id,
3264 symbol: Some(serialize_symbol(symbol)),
3265 });
3266 cx.spawn(|this, mut cx| async move {
3267 let response = request.await?;
3268 this.update(&mut cx, |this, cx| {
3269 this.wait_for_buffer(response.buffer_id, cx)
3270 })
3271 .await
3272 })
3273 } else {
3274 Task::ready(Err(anyhow!("project does not have a remote id")))
3275 }
3276 }
3277
3278 pub fn hover<T: ToPointUtf16>(
3279 &self,
3280 buffer: &ModelHandle<Buffer>,
3281 position: T,
3282 cx: &mut ModelContext<Self>,
3283 ) -> Task<Result<Option<Hover>>> {
3284 let position = position.to_point_utf16(buffer.read(cx));
3285 self.request_lsp(buffer.clone(), GetHover { position }, cx)
3286 }
3287
3288 pub fn completions<T: ToPointUtf16>(
3289 &self,
3290 source_buffer_handle: &ModelHandle<Buffer>,
3291 position: T,
3292 cx: &mut ModelContext<Self>,
3293 ) -> Task<Result<Vec<Completion>>> {
3294 let source_buffer_handle = source_buffer_handle.clone();
3295 let source_buffer = source_buffer_handle.read(cx);
3296 let buffer_id = source_buffer.remote_id();
3297 let language = source_buffer.language().cloned();
3298 let worktree;
3299 let buffer_abs_path;
3300 if let Some(file) = File::from_dyn(source_buffer.file()) {
3301 worktree = file.worktree.clone();
3302 buffer_abs_path = file.as_local().map(|f| f.abs_path(cx));
3303 } else {
3304 return Task::ready(Ok(Default::default()));
3305 };
3306
3307 let position = Unclipped(position.to_point_utf16(source_buffer));
3308 let anchor = source_buffer.anchor_after(position);
3309
3310 if worktree.read(cx).as_local().is_some() {
3311 let buffer_abs_path = buffer_abs_path.unwrap();
3312 let lang_server =
3313 if let Some((_, server)) = self.language_server_for_buffer(source_buffer, cx) {
3314 server.clone()
3315 } else {
3316 return Task::ready(Ok(Default::default()));
3317 };
3318
3319 cx.spawn(|_, cx| async move {
3320 let completions = lang_server
3321 .request::<lsp::request::Completion>(lsp::CompletionParams {
3322 text_document_position: lsp::TextDocumentPositionParams::new(
3323 lsp::TextDocumentIdentifier::new(
3324 lsp::Url::from_file_path(buffer_abs_path).unwrap(),
3325 ),
3326 point_to_lsp(position.0),
3327 ),
3328 context: Default::default(),
3329 work_done_progress_params: Default::default(),
3330 partial_result_params: Default::default(),
3331 })
3332 .await
3333 .context("lsp completion request failed")?;
3334
3335 let completions = if let Some(completions) = completions {
3336 match completions {
3337 lsp::CompletionResponse::Array(completions) => completions,
3338 lsp::CompletionResponse::List(list) => list.items,
3339 }
3340 } else {
3341 Default::default()
3342 };
3343
3344 let completions = source_buffer_handle.read_with(&cx, |this, _| {
3345 let snapshot = this.snapshot();
3346 let clipped_position = this.clip_point_utf16(position, Bias::Left);
3347 let mut range_for_token = None;
3348 completions
3349 .into_iter()
3350 .filter_map(move |mut lsp_completion| {
3351 // For now, we can only handle additional edits if they are returned
3352 // when resolving the completion, not if they are present initially.
3353 if lsp_completion
3354 .additional_text_edits
3355 .as_ref()
3356 .map_or(false, |edits| !edits.is_empty())
3357 {
3358 return None;
3359 }
3360
3361 let (old_range, mut new_text) = match lsp_completion.text_edit.as_ref()
3362 {
3363 // If the language server provides a range to overwrite, then
3364 // check that the range is valid.
3365 Some(lsp::CompletionTextEdit::Edit(edit)) => {
3366 let range = range_from_lsp(edit.range);
3367 let start = snapshot.clip_point_utf16(range.start, Bias::Left);
3368 let end = snapshot.clip_point_utf16(range.end, Bias::Left);
3369 if start != range.start.0 || end != range.end.0 {
3370 log::info!("completion out of expected range");
3371 return None;
3372 }
3373 (
3374 snapshot.anchor_before(start)..snapshot.anchor_after(end),
3375 edit.new_text.clone(),
3376 )
3377 }
3378 // If the language server does not provide a range, then infer
3379 // the range based on the syntax tree.
3380 None => {
3381 if position.0 != clipped_position {
3382 log::info!("completion out of expected range");
3383 return None;
3384 }
3385 let Range { start, end } = range_for_token
3386 .get_or_insert_with(|| {
3387 let offset = position.to_offset(&snapshot);
3388 let (range, kind) = snapshot.surrounding_word(offset);
3389 if kind == Some(CharKind::Word) {
3390 range
3391 } else {
3392 offset..offset
3393 }
3394 })
3395 .clone();
3396 let text = lsp_completion
3397 .insert_text
3398 .as_ref()
3399 .unwrap_or(&lsp_completion.label)
3400 .clone();
3401 (
3402 snapshot.anchor_before(start)..snapshot.anchor_after(end),
3403 text,
3404 )
3405 }
3406 Some(lsp::CompletionTextEdit::InsertAndReplace(_)) => {
3407 log::info!("unsupported insert/replace completion");
3408 return None;
3409 }
3410 };
3411
3412 LineEnding::normalize(&mut new_text);
3413 let language = language.clone();
3414 Some(async move {
3415 let mut label = None;
3416 if let Some(language) = language {
3417 language.process_completion(&mut lsp_completion).await;
3418 label = language.label_for_completion(&lsp_completion).await;
3419 }
3420 Completion {
3421 old_range,
3422 new_text,
3423 label: label.unwrap_or_else(|| {
3424 CodeLabel::plain(
3425 lsp_completion.label.clone(),
3426 lsp_completion.filter_text.as_deref(),
3427 )
3428 }),
3429 lsp_completion,
3430 }
3431 })
3432 })
3433 });
3434
3435 Ok(futures::future::join_all(completions).await)
3436 })
3437 } else if let Some(project_id) = self.remote_id() {
3438 let rpc = self.client.clone();
3439 let message = proto::GetCompletions {
3440 project_id,
3441 buffer_id,
3442 position: Some(language::proto::serialize_anchor(&anchor)),
3443 version: serialize_version(&source_buffer.version()),
3444 };
3445 cx.spawn_weak(|this, mut cx| async move {
3446 let response = rpc.request(message).await?;
3447
3448 if this
3449 .upgrade(&cx)
3450 .ok_or_else(|| anyhow!("project was dropped"))?
3451 .read_with(&cx, |this, _| this.is_read_only())
3452 {
3453 return Err(anyhow!(
3454 "failed to get completions: project was disconnected"
3455 ));
3456 } else {
3457 source_buffer_handle
3458 .update(&mut cx, |buffer, _| {
3459 buffer.wait_for_version(deserialize_version(response.version))
3460 })
3461 .await;
3462
3463 let completions = response.completions.into_iter().map(|completion| {
3464 language::proto::deserialize_completion(completion, language.clone())
3465 });
3466 futures::future::try_join_all(completions).await
3467 }
3468 })
3469 } else {
3470 Task::ready(Ok(Default::default()))
3471 }
3472 }
3473
3474 pub fn apply_additional_edits_for_completion(
3475 &self,
3476 buffer_handle: ModelHandle<Buffer>,
3477 completion: Completion,
3478 push_to_history: bool,
3479 cx: &mut ModelContext<Self>,
3480 ) -> Task<Result<Option<Transaction>>> {
3481 let buffer = buffer_handle.read(cx);
3482 let buffer_id = buffer.remote_id();
3483
3484 if self.is_local() {
3485 let lang_server = match self.language_server_for_buffer(buffer, cx) {
3486 Some((_, server)) => server.clone(),
3487 _ => return Task::ready(Ok(Default::default())),
3488 };
3489
3490 cx.spawn(|this, mut cx| async move {
3491 let resolved_completion = lang_server
3492 .request::<lsp::request::ResolveCompletionItem>(completion.lsp_completion)
3493 .await?;
3494
3495 if let Some(edits) = resolved_completion.additional_text_edits {
3496 let edits = this
3497 .update(&mut cx, |this, cx| {
3498 this.edits_from_lsp(&buffer_handle, edits, None, cx)
3499 })
3500 .await?;
3501
3502 buffer_handle.update(&mut cx, |buffer, cx| {
3503 buffer.finalize_last_transaction();
3504 buffer.start_transaction();
3505
3506 for (range, text) in edits {
3507 let primary = &completion.old_range;
3508 let start_within = primary.start.cmp(&range.start, buffer).is_le()
3509 && primary.end.cmp(&range.start, buffer).is_ge();
3510 let end_within = range.start.cmp(&primary.end, buffer).is_le()
3511 && range.end.cmp(&primary.end, buffer).is_ge();
3512
3513 //Skip addtional edits which overlap with the primary completion edit
3514 //https://github.com/zed-industries/zed/pull/1871
3515 if !start_within && !end_within {
3516 buffer.edit([(range, text)], None, cx);
3517 }
3518 }
3519
3520 let transaction = if buffer.end_transaction(cx).is_some() {
3521 let transaction = buffer.finalize_last_transaction().unwrap().clone();
3522 if !push_to_history {
3523 buffer.forget_transaction(transaction.id);
3524 }
3525 Some(transaction)
3526 } else {
3527 None
3528 };
3529 Ok(transaction)
3530 })
3531 } else {
3532 Ok(None)
3533 }
3534 })
3535 } else if let Some(project_id) = self.remote_id() {
3536 let client = self.client.clone();
3537 cx.spawn(|_, mut cx| async move {
3538 let response = client
3539 .request(proto::ApplyCompletionAdditionalEdits {
3540 project_id,
3541 buffer_id,
3542 completion: Some(language::proto::serialize_completion(&completion)),
3543 })
3544 .await?;
3545
3546 if let Some(transaction) = response.transaction {
3547 let transaction = language::proto::deserialize_transaction(transaction)?;
3548 buffer_handle
3549 .update(&mut cx, |buffer, _| {
3550 buffer.wait_for_edits(transaction.edit_ids.iter().copied())
3551 })
3552 .await;
3553 if push_to_history {
3554 buffer_handle.update(&mut cx, |buffer, _| {
3555 buffer.push_transaction(transaction.clone(), Instant::now());
3556 });
3557 }
3558 Ok(Some(transaction))
3559 } else {
3560 Ok(None)
3561 }
3562 })
3563 } else {
3564 Task::ready(Err(anyhow!("project does not have a remote id")))
3565 }
3566 }
3567
3568 pub fn code_actions<T: Clone + ToOffset>(
3569 &self,
3570 buffer_handle: &ModelHandle<Buffer>,
3571 range: Range<T>,
3572 cx: &mut ModelContext<Self>,
3573 ) -> Task<Result<Vec<CodeAction>>> {
3574 let buffer_handle = buffer_handle.clone();
3575 let buffer = buffer_handle.read(cx);
3576 let snapshot = buffer.snapshot();
3577 let relevant_diagnostics = snapshot
3578 .diagnostics_in_range::<usize, usize>(range.to_offset(&snapshot), false)
3579 .map(|entry| entry.to_lsp_diagnostic_stub())
3580 .collect();
3581 let buffer_id = buffer.remote_id();
3582 let worktree;
3583 let buffer_abs_path;
3584 if let Some(file) = File::from_dyn(buffer.file()) {
3585 worktree = file.worktree.clone();
3586 buffer_abs_path = file.as_local().map(|f| f.abs_path(cx));
3587 } else {
3588 return Task::ready(Ok(Default::default()));
3589 };
3590 let range = buffer.anchor_before(range.start)..buffer.anchor_before(range.end);
3591
3592 if worktree.read(cx).as_local().is_some() {
3593 let buffer_abs_path = buffer_abs_path.unwrap();
3594 let lang_server = if let Some((_, server)) = self.language_server_for_buffer(buffer, cx)
3595 {
3596 server.clone()
3597 } else {
3598 return Task::ready(Ok(Default::default()));
3599 };
3600
3601 let lsp_range = range_to_lsp(range.to_point_utf16(buffer));
3602 cx.foreground().spawn(async move {
3603 if lang_server.capabilities().code_action_provider.is_none() {
3604 return Ok(Default::default());
3605 }
3606
3607 Ok(lang_server
3608 .request::<lsp::request::CodeActionRequest>(lsp::CodeActionParams {
3609 text_document: lsp::TextDocumentIdentifier::new(
3610 lsp::Url::from_file_path(buffer_abs_path).unwrap(),
3611 ),
3612 range: lsp_range,
3613 work_done_progress_params: Default::default(),
3614 partial_result_params: Default::default(),
3615 context: lsp::CodeActionContext {
3616 diagnostics: relevant_diagnostics,
3617 only: Some(vec![
3618 lsp::CodeActionKind::EMPTY,
3619 lsp::CodeActionKind::QUICKFIX,
3620 lsp::CodeActionKind::REFACTOR,
3621 lsp::CodeActionKind::REFACTOR_EXTRACT,
3622 lsp::CodeActionKind::SOURCE,
3623 ]),
3624 },
3625 })
3626 .await?
3627 .unwrap_or_default()
3628 .into_iter()
3629 .filter_map(|entry| {
3630 if let lsp::CodeActionOrCommand::CodeAction(lsp_action) = entry {
3631 Some(CodeAction {
3632 range: range.clone(),
3633 lsp_action,
3634 })
3635 } else {
3636 None
3637 }
3638 })
3639 .collect())
3640 })
3641 } else if let Some(project_id) = self.remote_id() {
3642 let rpc = self.client.clone();
3643 let version = buffer.version();
3644 cx.spawn_weak(|this, mut cx| async move {
3645 let response = rpc
3646 .request(proto::GetCodeActions {
3647 project_id,
3648 buffer_id,
3649 start: Some(language::proto::serialize_anchor(&range.start)),
3650 end: Some(language::proto::serialize_anchor(&range.end)),
3651 version: serialize_version(&version),
3652 })
3653 .await?;
3654
3655 if this
3656 .upgrade(&cx)
3657 .ok_or_else(|| anyhow!("project was dropped"))?
3658 .read_with(&cx, |this, _| this.is_read_only())
3659 {
3660 return Err(anyhow!(
3661 "failed to get code actions: project was disconnected"
3662 ));
3663 } else {
3664 buffer_handle
3665 .update(&mut cx, |buffer, _| {
3666 buffer.wait_for_version(deserialize_version(response.version))
3667 })
3668 .await;
3669
3670 response
3671 .actions
3672 .into_iter()
3673 .map(language::proto::deserialize_code_action)
3674 .collect()
3675 }
3676 })
3677 } else {
3678 Task::ready(Ok(Default::default()))
3679 }
3680 }
3681
3682 pub fn apply_code_action(
3683 &self,
3684 buffer_handle: ModelHandle<Buffer>,
3685 mut action: CodeAction,
3686 push_to_history: bool,
3687 cx: &mut ModelContext<Self>,
3688 ) -> Task<Result<ProjectTransaction>> {
3689 if self.is_local() {
3690 let buffer = buffer_handle.read(cx);
3691 let (lsp_adapter, lang_server) =
3692 if let Some((adapter, server)) = self.language_server_for_buffer(buffer, cx) {
3693 (adapter.clone(), server.clone())
3694 } else {
3695 return Task::ready(Ok(Default::default()));
3696 };
3697 let range = action.range.to_point_utf16(buffer);
3698
3699 cx.spawn(|this, mut cx| async move {
3700 if let Some(lsp_range) = action
3701 .lsp_action
3702 .data
3703 .as_mut()
3704 .and_then(|d| d.get_mut("codeActionParams"))
3705 .and_then(|d| d.get_mut("range"))
3706 {
3707 *lsp_range = serde_json::to_value(&range_to_lsp(range)).unwrap();
3708 action.lsp_action = lang_server
3709 .request::<lsp::request::CodeActionResolveRequest>(action.lsp_action)
3710 .await?;
3711 } else {
3712 let actions = this
3713 .update(&mut cx, |this, cx| {
3714 this.code_actions(&buffer_handle, action.range, cx)
3715 })
3716 .await?;
3717 action.lsp_action = actions
3718 .into_iter()
3719 .find(|a| a.lsp_action.title == action.lsp_action.title)
3720 .ok_or_else(|| anyhow!("code action is outdated"))?
3721 .lsp_action;
3722 }
3723
3724 if let Some(edit) = action.lsp_action.edit {
3725 if edit.changes.is_some() || edit.document_changes.is_some() {
3726 return Self::deserialize_workspace_edit(
3727 this,
3728 edit,
3729 push_to_history,
3730 lsp_adapter.clone(),
3731 lang_server.clone(),
3732 &mut cx,
3733 )
3734 .await;
3735 }
3736 }
3737
3738 if let Some(command) = action.lsp_action.command {
3739 this.update(&mut cx, |this, _| {
3740 this.last_workspace_edits_by_language_server
3741 .remove(&lang_server.server_id());
3742 });
3743 lang_server
3744 .request::<lsp::request::ExecuteCommand>(lsp::ExecuteCommandParams {
3745 command: command.command,
3746 arguments: command.arguments.unwrap_or_default(),
3747 ..Default::default()
3748 })
3749 .await?;
3750 return Ok(this.update(&mut cx, |this, _| {
3751 this.last_workspace_edits_by_language_server
3752 .remove(&lang_server.server_id())
3753 .unwrap_or_default()
3754 }));
3755 }
3756
3757 Ok(ProjectTransaction::default())
3758 })
3759 } else if let Some(project_id) = self.remote_id() {
3760 let client = self.client.clone();
3761 let request = proto::ApplyCodeAction {
3762 project_id,
3763 buffer_id: buffer_handle.read(cx).remote_id(),
3764 action: Some(language::proto::serialize_code_action(&action)),
3765 };
3766 cx.spawn(|this, mut cx| async move {
3767 let response = client
3768 .request(request)
3769 .await?
3770 .transaction
3771 .ok_or_else(|| anyhow!("missing transaction"))?;
3772 this.update(&mut cx, |this, cx| {
3773 this.deserialize_project_transaction(response, push_to_history, cx)
3774 })
3775 .await
3776 })
3777 } else {
3778 Task::ready(Err(anyhow!("project does not have a remote id")))
3779 }
3780 }
3781
3782 async fn deserialize_workspace_edit(
3783 this: ModelHandle<Self>,
3784 edit: lsp::WorkspaceEdit,
3785 push_to_history: bool,
3786 lsp_adapter: Arc<CachedLspAdapter>,
3787 language_server: Arc<LanguageServer>,
3788 cx: &mut AsyncAppContext,
3789 ) -> Result<ProjectTransaction> {
3790 let fs = this.read_with(cx, |this, _| this.fs.clone());
3791 let mut operations = Vec::new();
3792 if let Some(document_changes) = edit.document_changes {
3793 match document_changes {
3794 lsp::DocumentChanges::Edits(edits) => {
3795 operations.extend(edits.into_iter().map(lsp::DocumentChangeOperation::Edit))
3796 }
3797 lsp::DocumentChanges::Operations(ops) => operations = ops,
3798 }
3799 } else if let Some(changes) = edit.changes {
3800 operations.extend(changes.into_iter().map(|(uri, edits)| {
3801 lsp::DocumentChangeOperation::Edit(lsp::TextDocumentEdit {
3802 text_document: lsp::OptionalVersionedTextDocumentIdentifier {
3803 uri,
3804 version: None,
3805 },
3806 edits: edits.into_iter().map(lsp::OneOf::Left).collect(),
3807 })
3808 }));
3809 }
3810
3811 let mut project_transaction = ProjectTransaction::default();
3812 for operation in operations {
3813 match operation {
3814 lsp::DocumentChangeOperation::Op(lsp::ResourceOp::Create(op)) => {
3815 let abs_path = op
3816 .uri
3817 .to_file_path()
3818 .map_err(|_| anyhow!("can't convert URI to path"))?;
3819
3820 if let Some(parent_path) = abs_path.parent() {
3821 fs.create_dir(parent_path).await?;
3822 }
3823 if abs_path.ends_with("/") {
3824 fs.create_dir(&abs_path).await?;
3825 } else {
3826 fs.create_file(&abs_path, op.options.map(Into::into).unwrap_or_default())
3827 .await?;
3828 }
3829 }
3830 lsp::DocumentChangeOperation::Op(lsp::ResourceOp::Rename(op)) => {
3831 let source_abs_path = op
3832 .old_uri
3833 .to_file_path()
3834 .map_err(|_| anyhow!("can't convert URI to path"))?;
3835 let target_abs_path = op
3836 .new_uri
3837 .to_file_path()
3838 .map_err(|_| anyhow!("can't convert URI to path"))?;
3839 fs.rename(
3840 &source_abs_path,
3841 &target_abs_path,
3842 op.options.map(Into::into).unwrap_or_default(),
3843 )
3844 .await?;
3845 }
3846 lsp::DocumentChangeOperation::Op(lsp::ResourceOp::Delete(op)) => {
3847 let abs_path = op
3848 .uri
3849 .to_file_path()
3850 .map_err(|_| anyhow!("can't convert URI to path"))?;
3851 let options = op.options.map(Into::into).unwrap_or_default();
3852 if abs_path.ends_with("/") {
3853 fs.remove_dir(&abs_path, options).await?;
3854 } else {
3855 fs.remove_file(&abs_path, options).await?;
3856 }
3857 }
3858 lsp::DocumentChangeOperation::Edit(op) => {
3859 let buffer_to_edit = this
3860 .update(cx, |this, cx| {
3861 this.open_local_buffer_via_lsp(
3862 op.text_document.uri,
3863 language_server.server_id(),
3864 lsp_adapter.name.clone(),
3865 cx,
3866 )
3867 })
3868 .await?;
3869
3870 let edits = this
3871 .update(cx, |this, cx| {
3872 let edits = op.edits.into_iter().map(|edit| match edit {
3873 lsp::OneOf::Left(edit) => edit,
3874 lsp::OneOf::Right(edit) => edit.text_edit,
3875 });
3876 this.edits_from_lsp(
3877 &buffer_to_edit,
3878 edits,
3879 op.text_document.version,
3880 cx,
3881 )
3882 })
3883 .await?;
3884
3885 let transaction = buffer_to_edit.update(cx, |buffer, cx| {
3886 buffer.finalize_last_transaction();
3887 buffer.start_transaction();
3888 for (range, text) in edits {
3889 buffer.edit([(range, text)], None, cx);
3890 }
3891 let transaction = if buffer.end_transaction(cx).is_some() {
3892 let transaction = buffer.finalize_last_transaction().unwrap().clone();
3893 if !push_to_history {
3894 buffer.forget_transaction(transaction.id);
3895 }
3896 Some(transaction)
3897 } else {
3898 None
3899 };
3900
3901 transaction
3902 });
3903 if let Some(transaction) = transaction {
3904 project_transaction.0.insert(buffer_to_edit, transaction);
3905 }
3906 }
3907 }
3908 }
3909
3910 Ok(project_transaction)
3911 }
3912
3913 pub fn prepare_rename<T: ToPointUtf16>(
3914 &self,
3915 buffer: ModelHandle<Buffer>,
3916 position: T,
3917 cx: &mut ModelContext<Self>,
3918 ) -> Task<Result<Option<Range<Anchor>>>> {
3919 let position = position.to_point_utf16(buffer.read(cx));
3920 self.request_lsp(buffer, PrepareRename { position }, cx)
3921 }
3922
3923 pub fn perform_rename<T: ToPointUtf16>(
3924 &self,
3925 buffer: ModelHandle<Buffer>,
3926 position: T,
3927 new_name: String,
3928 push_to_history: bool,
3929 cx: &mut ModelContext<Self>,
3930 ) -> Task<Result<ProjectTransaction>> {
3931 let position = position.to_point_utf16(buffer.read(cx));
3932 self.request_lsp(
3933 buffer,
3934 PerformRename {
3935 position,
3936 new_name,
3937 push_to_history,
3938 },
3939 cx,
3940 )
3941 }
3942
3943 #[allow(clippy::type_complexity)]
3944 pub fn search(
3945 &self,
3946 query: SearchQuery,
3947 cx: &mut ModelContext<Self>,
3948 ) -> Task<Result<HashMap<ModelHandle<Buffer>, Vec<Range<Anchor>>>>> {
3949 if self.is_local() {
3950 let snapshots = self
3951 .visible_worktrees(cx)
3952 .filter_map(|tree| {
3953 let tree = tree.read(cx).as_local()?;
3954 Some(tree.snapshot())
3955 })
3956 .collect::<Vec<_>>();
3957
3958 let background = cx.background().clone();
3959 let path_count: usize = snapshots.iter().map(|s| s.visible_file_count()).sum();
3960 if path_count == 0 {
3961 return Task::ready(Ok(Default::default()));
3962 }
3963 let workers = background.num_cpus().min(path_count);
3964 let (matching_paths_tx, mut matching_paths_rx) = smol::channel::bounded(1024);
3965 cx.background()
3966 .spawn({
3967 let fs = self.fs.clone();
3968 let background = cx.background().clone();
3969 let query = query.clone();
3970 async move {
3971 let fs = &fs;
3972 let query = &query;
3973 let matching_paths_tx = &matching_paths_tx;
3974 let paths_per_worker = (path_count + workers - 1) / workers;
3975 let snapshots = &snapshots;
3976 background
3977 .scoped(|scope| {
3978 for worker_ix in 0..workers {
3979 let worker_start_ix = worker_ix * paths_per_worker;
3980 let worker_end_ix = worker_start_ix + paths_per_worker;
3981 scope.spawn(async move {
3982 let mut snapshot_start_ix = 0;
3983 let mut abs_path = PathBuf::new();
3984 for snapshot in snapshots {
3985 let snapshot_end_ix =
3986 snapshot_start_ix + snapshot.visible_file_count();
3987 if worker_end_ix <= snapshot_start_ix {
3988 break;
3989 } else if worker_start_ix > snapshot_end_ix {
3990 snapshot_start_ix = snapshot_end_ix;
3991 continue;
3992 } else {
3993 let start_in_snapshot = worker_start_ix
3994 .saturating_sub(snapshot_start_ix);
3995 let end_in_snapshot =
3996 cmp::min(worker_end_ix, snapshot_end_ix)
3997 - snapshot_start_ix;
3998
3999 for entry in snapshot
4000 .files(false, start_in_snapshot)
4001 .take(end_in_snapshot - start_in_snapshot)
4002 {
4003 if matching_paths_tx.is_closed() {
4004 break;
4005 }
4006
4007 abs_path.clear();
4008 abs_path.push(&snapshot.abs_path());
4009 abs_path.push(&entry.path);
4010 let matches = if let Some(file) =
4011 fs.open_sync(&abs_path).await.log_err()
4012 {
4013 query.detect(file).unwrap_or(false)
4014 } else {
4015 false
4016 };
4017
4018 if matches {
4019 let project_path =
4020 (snapshot.id(), entry.path.clone());
4021 if matching_paths_tx
4022 .send(project_path)
4023 .await
4024 .is_err()
4025 {
4026 break;
4027 }
4028 }
4029 }
4030
4031 snapshot_start_ix = snapshot_end_ix;
4032 }
4033 }
4034 });
4035 }
4036 })
4037 .await;
4038 }
4039 })
4040 .detach();
4041
4042 let (buffers_tx, buffers_rx) = smol::channel::bounded(1024);
4043 let open_buffers = self
4044 .opened_buffers
4045 .values()
4046 .filter_map(|b| b.upgrade(cx))
4047 .collect::<HashSet<_>>();
4048 cx.spawn(|this, cx| async move {
4049 for buffer in &open_buffers {
4050 let snapshot = buffer.read_with(&cx, |buffer, _| buffer.snapshot());
4051 buffers_tx.send((buffer.clone(), snapshot)).await?;
4052 }
4053
4054 let open_buffers = Rc::new(RefCell::new(open_buffers));
4055 while let Some(project_path) = matching_paths_rx.next().await {
4056 if buffers_tx.is_closed() {
4057 break;
4058 }
4059
4060 let this = this.clone();
4061 let open_buffers = open_buffers.clone();
4062 let buffers_tx = buffers_tx.clone();
4063 cx.spawn(|mut cx| async move {
4064 if let Some(buffer) = this
4065 .update(&mut cx, |this, cx| this.open_buffer(project_path, cx))
4066 .await
4067 .log_err()
4068 {
4069 if open_buffers.borrow_mut().insert(buffer.clone()) {
4070 let snapshot = buffer.read_with(&cx, |buffer, _| buffer.snapshot());
4071 buffers_tx.send((buffer, snapshot)).await?;
4072 }
4073 }
4074
4075 Ok::<_, anyhow::Error>(())
4076 })
4077 .detach();
4078 }
4079
4080 Ok::<_, anyhow::Error>(())
4081 })
4082 .detach_and_log_err(cx);
4083
4084 let background = cx.background().clone();
4085 cx.background().spawn(async move {
4086 let query = &query;
4087 let mut matched_buffers = Vec::new();
4088 for _ in 0..workers {
4089 matched_buffers.push(HashMap::default());
4090 }
4091 background
4092 .scoped(|scope| {
4093 for worker_matched_buffers in matched_buffers.iter_mut() {
4094 let mut buffers_rx = buffers_rx.clone();
4095 scope.spawn(async move {
4096 while let Some((buffer, snapshot)) = buffers_rx.next().await {
4097 let buffer_matches = query
4098 .search(snapshot.as_rope())
4099 .await
4100 .iter()
4101 .map(|range| {
4102 snapshot.anchor_before(range.start)
4103 ..snapshot.anchor_after(range.end)
4104 })
4105 .collect::<Vec<_>>();
4106 if !buffer_matches.is_empty() {
4107 worker_matched_buffers
4108 .insert(buffer.clone(), buffer_matches);
4109 }
4110 }
4111 });
4112 }
4113 })
4114 .await;
4115 Ok(matched_buffers.into_iter().flatten().collect())
4116 })
4117 } else if let Some(project_id) = self.remote_id() {
4118 let request = self.client.request(query.to_proto(project_id));
4119 cx.spawn(|this, mut cx| async move {
4120 let response = request.await?;
4121 let mut result = HashMap::default();
4122 for location in response.locations {
4123 let target_buffer = this
4124 .update(&mut cx, |this, cx| {
4125 this.wait_for_buffer(location.buffer_id, cx)
4126 })
4127 .await?;
4128 let start = location
4129 .start
4130 .and_then(deserialize_anchor)
4131 .ok_or_else(|| anyhow!("missing target start"))?;
4132 let end = location
4133 .end
4134 .and_then(deserialize_anchor)
4135 .ok_or_else(|| anyhow!("missing target end"))?;
4136 result
4137 .entry(target_buffer)
4138 .or_insert(Vec::new())
4139 .push(start..end)
4140 }
4141 Ok(result)
4142 })
4143 } else {
4144 Task::ready(Ok(Default::default()))
4145 }
4146 }
4147
4148 fn request_lsp<R: LspCommand>(
4149 &self,
4150 buffer_handle: ModelHandle<Buffer>,
4151 request: R,
4152 cx: &mut ModelContext<Self>,
4153 ) -> Task<Result<R::Response>>
4154 where
4155 <R::LspRequest as lsp::request::Request>::Result: Send,
4156 {
4157 let buffer = buffer_handle.read(cx);
4158 if self.is_local() {
4159 let file = File::from_dyn(buffer.file()).and_then(File::as_local);
4160 if let Some((file, language_server)) = file.zip(
4161 self.language_server_for_buffer(buffer, cx)
4162 .map(|(_, server)| server.clone()),
4163 ) {
4164 let lsp_params = request.to_lsp(&file.abs_path(cx), cx);
4165 return cx.spawn(|this, cx| async move {
4166 if !request.check_capabilities(language_server.capabilities()) {
4167 return Ok(Default::default());
4168 }
4169
4170 let response = language_server
4171 .request::<R::LspRequest>(lsp_params)
4172 .await
4173 .context("lsp request failed")?;
4174 request
4175 .response_from_lsp(response, this, buffer_handle, cx)
4176 .await
4177 });
4178 }
4179 } else if let Some(project_id) = self.remote_id() {
4180 let rpc = self.client.clone();
4181 let message = request.to_proto(project_id, buffer);
4182 return cx.spawn(|this, cx| async move {
4183 let response = rpc.request(message).await?;
4184 if this.read_with(&cx, |this, _| this.is_read_only()) {
4185 Err(anyhow!("disconnected before completing request"))
4186 } else {
4187 request
4188 .response_from_proto(response, this, buffer_handle, cx)
4189 .await
4190 }
4191 });
4192 }
4193 Task::ready(Ok(Default::default()))
4194 }
4195
4196 pub fn find_or_create_local_worktree(
4197 &mut self,
4198 abs_path: impl AsRef<Path>,
4199 visible: bool,
4200 cx: &mut ModelContext<Self>,
4201 ) -> Task<Result<(ModelHandle<Worktree>, PathBuf)>> {
4202 let abs_path = abs_path.as_ref();
4203 if let Some((tree, relative_path)) = self.find_local_worktree(abs_path, cx) {
4204 Task::ready(Ok((tree, relative_path)))
4205 } else {
4206 let worktree = self.create_local_worktree(abs_path, visible, cx);
4207 cx.foreground()
4208 .spawn(async move { Ok((worktree.await?, PathBuf::new())) })
4209 }
4210 }
4211
4212 pub fn find_local_worktree(
4213 &self,
4214 abs_path: &Path,
4215 cx: &AppContext,
4216 ) -> Option<(ModelHandle<Worktree>, PathBuf)> {
4217 for tree in &self.worktrees {
4218 if let Some(tree) = tree.upgrade(cx) {
4219 if let Some(relative_path) = tree
4220 .read(cx)
4221 .as_local()
4222 .and_then(|t| abs_path.strip_prefix(t.abs_path()).ok())
4223 {
4224 return Some((tree.clone(), relative_path.into()));
4225 }
4226 }
4227 }
4228 None
4229 }
4230
4231 pub fn is_shared(&self) -> bool {
4232 match &self.client_state {
4233 Some(ProjectClientState::Local { .. }) => true,
4234 _ => false,
4235 }
4236 }
4237
4238 fn create_local_worktree(
4239 &mut self,
4240 abs_path: impl AsRef<Path>,
4241 visible: bool,
4242 cx: &mut ModelContext<Self>,
4243 ) -> Task<Result<ModelHandle<Worktree>>> {
4244 let fs = self.fs.clone();
4245 let client = self.client.clone();
4246 let next_entry_id = self.next_entry_id.clone();
4247 let path: Arc<Path> = abs_path.as_ref().into();
4248 let task = self
4249 .loading_local_worktrees
4250 .entry(path.clone())
4251 .or_insert_with(|| {
4252 cx.spawn(|project, mut cx| {
4253 async move {
4254 let worktree = Worktree::local(
4255 client.clone(),
4256 path.clone(),
4257 visible,
4258 fs,
4259 next_entry_id,
4260 &mut cx,
4261 )
4262 .await;
4263 project.update(&mut cx, |project, _| {
4264 project.loading_local_worktrees.remove(&path);
4265 });
4266 let worktree = worktree?;
4267
4268 project
4269 .update(&mut cx, |project, cx| project.add_worktree(&worktree, cx))
4270 .await;
4271
4272 if let Some(project_id) =
4273 project.read_with(&cx, |project, _| project.remote_id())
4274 {
4275 worktree.update(&mut cx, |worktree, cx| {
4276 worktree
4277 .as_local_mut()
4278 .unwrap()
4279 .share(project_id, cx)
4280 .detach_and_log_err(cx);
4281 });
4282 }
4283
4284 Ok(worktree)
4285 }
4286 .map_err(Arc::new)
4287 })
4288 .shared()
4289 })
4290 .clone();
4291 cx.foreground().spawn(async move {
4292 match task.await {
4293 Ok(worktree) => Ok(worktree),
4294 Err(err) => Err(anyhow!("{}", err)),
4295 }
4296 })
4297 }
4298
4299 pub fn remove_worktree(
4300 &mut self,
4301 id_to_remove: WorktreeId,
4302 cx: &mut ModelContext<Self>,
4303 ) -> impl Future<Output = ()> {
4304 self.worktrees.retain(|worktree| {
4305 if let Some(worktree) = worktree.upgrade(cx) {
4306 let id = worktree.read(cx).id();
4307 if id == id_to_remove {
4308 cx.emit(Event::WorktreeRemoved(id));
4309 false
4310 } else {
4311 true
4312 }
4313 } else {
4314 false
4315 }
4316 });
4317 self.metadata_changed(cx)
4318 }
4319
4320 fn add_worktree(
4321 &mut self,
4322 worktree: &ModelHandle<Worktree>,
4323 cx: &mut ModelContext<Self>,
4324 ) -> impl Future<Output = ()> {
4325 cx.observe(worktree, |_, _, cx| cx.notify()).detach();
4326 if worktree.read(cx).is_local() {
4327 cx.subscribe(worktree, |this, worktree, event, cx| match event {
4328 worktree::Event::UpdatedEntries => this.update_local_worktree_buffers(worktree, cx),
4329 worktree::Event::UpdatedGitRepositories(updated_repos) => {
4330 this.update_local_worktree_buffers_git_repos(worktree, updated_repos, cx)
4331 }
4332 })
4333 .detach();
4334 }
4335
4336 let push_strong_handle = {
4337 let worktree = worktree.read(cx);
4338 self.is_shared() || worktree.is_visible() || worktree.is_remote()
4339 };
4340 if push_strong_handle {
4341 self.worktrees
4342 .push(WorktreeHandle::Strong(worktree.clone()));
4343 } else {
4344 self.worktrees
4345 .push(WorktreeHandle::Weak(worktree.downgrade()));
4346 }
4347
4348 cx.observe_release(worktree, |this, worktree, cx| {
4349 let _ = this.remove_worktree(worktree.id(), cx);
4350 })
4351 .detach();
4352
4353 cx.emit(Event::WorktreeAdded);
4354 self.metadata_changed(cx)
4355 }
4356
4357 fn update_local_worktree_buffers(
4358 &mut self,
4359 worktree_handle: ModelHandle<Worktree>,
4360 cx: &mut ModelContext<Self>,
4361 ) {
4362 let snapshot = worktree_handle.read(cx).snapshot();
4363 let mut buffers_to_delete = Vec::new();
4364 let mut renamed_buffers = Vec::new();
4365 for (buffer_id, buffer) in &self.opened_buffers {
4366 if let Some(buffer) = buffer.upgrade(cx) {
4367 buffer.update(cx, |buffer, cx| {
4368 if let Some(old_file) = File::from_dyn(buffer.file()) {
4369 if old_file.worktree != worktree_handle {
4370 return;
4371 }
4372
4373 let new_file = if let Some(entry) = snapshot.entry_for_id(old_file.entry_id)
4374 {
4375 File {
4376 is_local: true,
4377 entry_id: entry.id,
4378 mtime: entry.mtime,
4379 path: entry.path.clone(),
4380 worktree: worktree_handle.clone(),
4381 is_deleted: false,
4382 }
4383 } else if let Some(entry) =
4384 snapshot.entry_for_path(old_file.path().as_ref())
4385 {
4386 File {
4387 is_local: true,
4388 entry_id: entry.id,
4389 mtime: entry.mtime,
4390 path: entry.path.clone(),
4391 worktree: worktree_handle.clone(),
4392 is_deleted: false,
4393 }
4394 } else {
4395 File {
4396 is_local: true,
4397 entry_id: old_file.entry_id,
4398 path: old_file.path().clone(),
4399 mtime: old_file.mtime(),
4400 worktree: worktree_handle.clone(),
4401 is_deleted: true,
4402 }
4403 };
4404
4405 let old_path = old_file.abs_path(cx);
4406 if new_file.abs_path(cx) != old_path {
4407 renamed_buffers.push((cx.handle(), old_path));
4408 }
4409
4410 if let Some(project_id) = self.remote_id() {
4411 self.client
4412 .send(proto::UpdateBufferFile {
4413 project_id,
4414 buffer_id: *buffer_id as u64,
4415 file: Some(new_file.to_proto()),
4416 })
4417 .log_err();
4418 }
4419 buffer.file_updated(Arc::new(new_file), cx).detach();
4420 }
4421 });
4422 } else {
4423 buffers_to_delete.push(*buffer_id);
4424 }
4425 }
4426
4427 for buffer_id in buffers_to_delete {
4428 self.opened_buffers.remove(&buffer_id);
4429 }
4430
4431 for (buffer, old_path) in renamed_buffers {
4432 self.unregister_buffer_from_language_server(&buffer, old_path, cx);
4433 self.assign_language_to_buffer(&buffer, cx);
4434 self.register_buffer_with_language_server(&buffer, cx);
4435 }
4436 }
4437
4438 fn update_local_worktree_buffers_git_repos(
4439 &mut self,
4440 worktree: ModelHandle<Worktree>,
4441 repos: &[GitRepositoryEntry],
4442 cx: &mut ModelContext<Self>,
4443 ) {
4444 for (_, buffer) in &self.opened_buffers {
4445 if let Some(buffer) = buffer.upgrade(cx) {
4446 let file = match File::from_dyn(buffer.read(cx).file()) {
4447 Some(file) => file,
4448 None => continue,
4449 };
4450 if file.worktree != worktree {
4451 continue;
4452 }
4453
4454 let path = file.path().clone();
4455
4456 let repo = match repos.iter().find(|repo| repo.manages(&path)) {
4457 Some(repo) => repo.clone(),
4458 None => return,
4459 };
4460
4461 let relative_repo = match path.strip_prefix(repo.content_path) {
4462 Ok(relative_repo) => relative_repo.to_owned(),
4463 Err(_) => return,
4464 };
4465
4466 let remote_id = self.remote_id();
4467 let client = self.client.clone();
4468
4469 cx.spawn(|_, mut cx| async move {
4470 let diff_base = cx
4471 .background()
4472 .spawn(async move { repo.repo.lock().load_index_text(&relative_repo) })
4473 .await;
4474
4475 let buffer_id = buffer.update(&mut cx, |buffer, cx| {
4476 buffer.set_diff_base(diff_base.clone(), cx);
4477 buffer.remote_id()
4478 });
4479
4480 if let Some(project_id) = remote_id {
4481 client
4482 .send(proto::UpdateDiffBase {
4483 project_id,
4484 buffer_id: buffer_id as u64,
4485 diff_base,
4486 })
4487 .log_err();
4488 }
4489 })
4490 .detach();
4491 }
4492 }
4493 }
4494
4495 pub fn set_active_path(&mut self, entry: Option<ProjectPath>, cx: &mut ModelContext<Self>) {
4496 let new_active_entry = entry.and_then(|project_path| {
4497 let worktree = self.worktree_for_id(project_path.worktree_id, cx)?;
4498 let entry = worktree.read(cx).entry_for_path(project_path.path)?;
4499 Some(entry.id)
4500 });
4501 if new_active_entry != self.active_entry {
4502 self.active_entry = new_active_entry;
4503 cx.emit(Event::ActiveEntryChanged(new_active_entry));
4504 }
4505 }
4506
4507 pub fn language_servers_running_disk_based_diagnostics(
4508 &self,
4509 ) -> impl Iterator<Item = usize> + '_ {
4510 self.language_server_statuses
4511 .iter()
4512 .filter_map(|(id, status)| {
4513 if status.has_pending_diagnostic_updates {
4514 Some(*id)
4515 } else {
4516 None
4517 }
4518 })
4519 }
4520
4521 pub fn diagnostic_summary(&self, cx: &AppContext) -> DiagnosticSummary {
4522 let mut summary = DiagnosticSummary::default();
4523 for (_, path_summary) in self.diagnostic_summaries(cx) {
4524 summary.error_count += path_summary.error_count;
4525 summary.warning_count += path_summary.warning_count;
4526 }
4527 summary
4528 }
4529
4530 pub fn diagnostic_summaries<'a>(
4531 &'a self,
4532 cx: &'a AppContext,
4533 ) -> impl Iterator<Item = (ProjectPath, DiagnosticSummary)> + 'a {
4534 self.visible_worktrees(cx).flat_map(move |worktree| {
4535 let worktree = worktree.read(cx);
4536 let worktree_id = worktree.id();
4537 worktree
4538 .diagnostic_summaries()
4539 .map(move |(path, summary)| (ProjectPath { worktree_id, path }, summary))
4540 })
4541 }
4542
4543 pub fn disk_based_diagnostics_started(
4544 &mut self,
4545 language_server_id: usize,
4546 cx: &mut ModelContext<Self>,
4547 ) {
4548 cx.emit(Event::DiskBasedDiagnosticsStarted { language_server_id });
4549 }
4550
4551 pub fn disk_based_diagnostics_finished(
4552 &mut self,
4553 language_server_id: usize,
4554 cx: &mut ModelContext<Self>,
4555 ) {
4556 cx.emit(Event::DiskBasedDiagnosticsFinished { language_server_id });
4557 }
4558
4559 pub fn active_entry(&self) -> Option<ProjectEntryId> {
4560 self.active_entry
4561 }
4562
4563 pub fn entry_for_path(&self, path: &ProjectPath, cx: &AppContext) -> Option<Entry> {
4564 self.worktree_for_id(path.worktree_id, cx)?
4565 .read(cx)
4566 .entry_for_path(&path.path)
4567 .cloned()
4568 }
4569
4570 pub fn path_for_entry(&self, entry_id: ProjectEntryId, cx: &AppContext) -> Option<ProjectPath> {
4571 let worktree = self.worktree_for_entry(entry_id, cx)?;
4572 let worktree = worktree.read(cx);
4573 let worktree_id = worktree.id();
4574 let path = worktree.entry_for_id(entry_id)?.path.clone();
4575 Some(ProjectPath { worktree_id, path })
4576 }
4577
4578 // RPC message handlers
4579
4580 async fn handle_unshare_project(
4581 this: ModelHandle<Self>,
4582 _: TypedEnvelope<proto::UnshareProject>,
4583 _: Arc<Client>,
4584 mut cx: AsyncAppContext,
4585 ) -> Result<()> {
4586 this.update(&mut cx, |this, cx| {
4587 if this.is_local() {
4588 this.unshare(cx)?;
4589 } else {
4590 this.disconnected_from_host(cx);
4591 }
4592 Ok(())
4593 })
4594 }
4595
4596 async fn handle_add_collaborator(
4597 this: ModelHandle<Self>,
4598 mut envelope: TypedEnvelope<proto::AddProjectCollaborator>,
4599 _: Arc<Client>,
4600 mut cx: AsyncAppContext,
4601 ) -> Result<()> {
4602 let collaborator = envelope
4603 .payload
4604 .collaborator
4605 .take()
4606 .ok_or_else(|| anyhow!("empty collaborator"))?;
4607
4608 let collaborator = Collaborator::from_proto(collaborator)?;
4609 this.update(&mut cx, |this, cx| {
4610 this.collaborators
4611 .insert(collaborator.peer_id, collaborator);
4612 cx.notify();
4613 });
4614
4615 Ok(())
4616 }
4617
4618 async fn handle_remove_collaborator(
4619 this: ModelHandle<Self>,
4620 envelope: TypedEnvelope<proto::RemoveProjectCollaborator>,
4621 _: Arc<Client>,
4622 mut cx: AsyncAppContext,
4623 ) -> Result<()> {
4624 this.update(&mut cx, |this, cx| {
4625 let peer_id = envelope
4626 .payload
4627 .peer_id
4628 .ok_or_else(|| anyhow!("invalid peer id"))?;
4629 let replica_id = this
4630 .collaborators
4631 .remove(&peer_id)
4632 .ok_or_else(|| anyhow!("unknown peer {:?}", peer_id))?
4633 .replica_id;
4634 for buffer in this.opened_buffers.values() {
4635 if let Some(buffer) = buffer.upgrade(cx) {
4636 buffer.update(cx, |buffer, cx| buffer.remove_peer(replica_id, cx));
4637 }
4638 }
4639 this.shared_buffers.remove(&peer_id);
4640
4641 cx.emit(Event::CollaboratorLeft(peer_id));
4642 cx.notify();
4643 Ok(())
4644 })
4645 }
4646
4647 async fn handle_update_project(
4648 this: ModelHandle<Self>,
4649 envelope: TypedEnvelope<proto::UpdateProject>,
4650 client: Arc<Client>,
4651 mut cx: AsyncAppContext,
4652 ) -> Result<()> {
4653 this.update(&mut cx, |this, cx| {
4654 let replica_id = this.replica_id();
4655 let remote_id = this.remote_id().ok_or_else(|| anyhow!("invalid project"))?;
4656
4657 let mut old_worktrees_by_id = this
4658 .worktrees
4659 .drain(..)
4660 .filter_map(|worktree| {
4661 let worktree = worktree.upgrade(cx)?;
4662 Some((worktree.read(cx).id(), worktree))
4663 })
4664 .collect::<HashMap<_, _>>();
4665
4666 for worktree in envelope.payload.worktrees {
4667 if let Some(old_worktree) =
4668 old_worktrees_by_id.remove(&WorktreeId::from_proto(worktree.id))
4669 {
4670 this.worktrees.push(WorktreeHandle::Strong(old_worktree));
4671 } else {
4672 let worktree =
4673 Worktree::remote(remote_id, replica_id, worktree, client.clone(), cx);
4674 let _ = this.add_worktree(&worktree, cx);
4675 }
4676 }
4677
4678 let _ = this.metadata_changed(cx);
4679 for (id, _) in old_worktrees_by_id {
4680 cx.emit(Event::WorktreeRemoved(id));
4681 }
4682
4683 Ok(())
4684 })
4685 }
4686
4687 async fn handle_update_worktree(
4688 this: ModelHandle<Self>,
4689 envelope: TypedEnvelope<proto::UpdateWorktree>,
4690 _: Arc<Client>,
4691 mut cx: AsyncAppContext,
4692 ) -> Result<()> {
4693 this.update(&mut cx, |this, cx| {
4694 let worktree_id = WorktreeId::from_proto(envelope.payload.worktree_id);
4695 if let Some(worktree) = this.worktree_for_id(worktree_id, cx) {
4696 worktree.update(cx, |worktree, _| {
4697 let worktree = worktree.as_remote_mut().unwrap();
4698 worktree.update_from_remote(envelope.payload);
4699 });
4700 }
4701 Ok(())
4702 })
4703 }
4704
4705 async fn handle_create_project_entry(
4706 this: ModelHandle<Self>,
4707 envelope: TypedEnvelope<proto::CreateProjectEntry>,
4708 _: Arc<Client>,
4709 mut cx: AsyncAppContext,
4710 ) -> Result<proto::ProjectEntryResponse> {
4711 let worktree = this.update(&mut cx, |this, cx| {
4712 let worktree_id = WorktreeId::from_proto(envelope.payload.worktree_id);
4713 this.worktree_for_id(worktree_id, cx)
4714 .ok_or_else(|| anyhow!("worktree not found"))
4715 })?;
4716 let worktree_scan_id = worktree.read_with(&cx, |worktree, _| worktree.scan_id());
4717 let entry = worktree
4718 .update(&mut cx, |worktree, cx| {
4719 let worktree = worktree.as_local_mut().unwrap();
4720 let path = PathBuf::from(envelope.payload.path);
4721 worktree.create_entry(path, envelope.payload.is_directory, cx)
4722 })
4723 .await?;
4724 Ok(proto::ProjectEntryResponse {
4725 entry: Some((&entry).into()),
4726 worktree_scan_id: worktree_scan_id as u64,
4727 })
4728 }
4729
4730 async fn handle_rename_project_entry(
4731 this: ModelHandle<Self>,
4732 envelope: TypedEnvelope<proto::RenameProjectEntry>,
4733 _: Arc<Client>,
4734 mut cx: AsyncAppContext,
4735 ) -> Result<proto::ProjectEntryResponse> {
4736 let entry_id = ProjectEntryId::from_proto(envelope.payload.entry_id);
4737 let worktree = this.read_with(&cx, |this, cx| {
4738 this.worktree_for_entry(entry_id, cx)
4739 .ok_or_else(|| anyhow!("worktree not found"))
4740 })?;
4741 let worktree_scan_id = worktree.read_with(&cx, |worktree, _| worktree.scan_id());
4742 let entry = worktree
4743 .update(&mut cx, |worktree, cx| {
4744 let new_path = PathBuf::from(envelope.payload.new_path);
4745 worktree
4746 .as_local_mut()
4747 .unwrap()
4748 .rename_entry(entry_id, new_path, cx)
4749 .ok_or_else(|| anyhow!("invalid entry"))
4750 })?
4751 .await?;
4752 Ok(proto::ProjectEntryResponse {
4753 entry: Some((&entry).into()),
4754 worktree_scan_id: worktree_scan_id as u64,
4755 })
4756 }
4757
4758 async fn handle_copy_project_entry(
4759 this: ModelHandle<Self>,
4760 envelope: TypedEnvelope<proto::CopyProjectEntry>,
4761 _: Arc<Client>,
4762 mut cx: AsyncAppContext,
4763 ) -> Result<proto::ProjectEntryResponse> {
4764 let entry_id = ProjectEntryId::from_proto(envelope.payload.entry_id);
4765 let worktree = this.read_with(&cx, |this, cx| {
4766 this.worktree_for_entry(entry_id, cx)
4767 .ok_or_else(|| anyhow!("worktree not found"))
4768 })?;
4769 let worktree_scan_id = worktree.read_with(&cx, |worktree, _| worktree.scan_id());
4770 let entry = worktree
4771 .update(&mut cx, |worktree, cx| {
4772 let new_path = PathBuf::from(envelope.payload.new_path);
4773 worktree
4774 .as_local_mut()
4775 .unwrap()
4776 .copy_entry(entry_id, new_path, cx)
4777 .ok_or_else(|| anyhow!("invalid entry"))
4778 })?
4779 .await?;
4780 Ok(proto::ProjectEntryResponse {
4781 entry: Some((&entry).into()),
4782 worktree_scan_id: worktree_scan_id as u64,
4783 })
4784 }
4785
4786 async fn handle_delete_project_entry(
4787 this: ModelHandle<Self>,
4788 envelope: TypedEnvelope<proto::DeleteProjectEntry>,
4789 _: Arc<Client>,
4790 mut cx: AsyncAppContext,
4791 ) -> Result<proto::ProjectEntryResponse> {
4792 let entry_id = ProjectEntryId::from_proto(envelope.payload.entry_id);
4793 let worktree = this.read_with(&cx, |this, cx| {
4794 this.worktree_for_entry(entry_id, cx)
4795 .ok_or_else(|| anyhow!("worktree not found"))
4796 })?;
4797 let worktree_scan_id = worktree.read_with(&cx, |worktree, _| worktree.scan_id());
4798 worktree
4799 .update(&mut cx, |worktree, cx| {
4800 worktree
4801 .as_local_mut()
4802 .unwrap()
4803 .delete_entry(entry_id, cx)
4804 .ok_or_else(|| anyhow!("invalid entry"))
4805 })?
4806 .await?;
4807 Ok(proto::ProjectEntryResponse {
4808 entry: None,
4809 worktree_scan_id: worktree_scan_id as u64,
4810 })
4811 }
4812
4813 async fn handle_update_diagnostic_summary(
4814 this: ModelHandle<Self>,
4815 envelope: TypedEnvelope<proto::UpdateDiagnosticSummary>,
4816 _: Arc<Client>,
4817 mut cx: AsyncAppContext,
4818 ) -> Result<()> {
4819 this.update(&mut cx, |this, cx| {
4820 let worktree_id = WorktreeId::from_proto(envelope.payload.worktree_id);
4821 if let Some(worktree) = this.worktree_for_id(worktree_id, cx) {
4822 if let Some(summary) = envelope.payload.summary {
4823 let project_path = ProjectPath {
4824 worktree_id,
4825 path: Path::new(&summary.path).into(),
4826 };
4827 worktree.update(cx, |worktree, _| {
4828 worktree
4829 .as_remote_mut()
4830 .unwrap()
4831 .update_diagnostic_summary(project_path.path.clone(), &summary);
4832 });
4833 cx.emit(Event::DiagnosticsUpdated {
4834 language_server_id: summary.language_server_id as usize,
4835 path: project_path,
4836 });
4837 }
4838 }
4839 Ok(())
4840 })
4841 }
4842
4843 async fn handle_start_language_server(
4844 this: ModelHandle<Self>,
4845 envelope: TypedEnvelope<proto::StartLanguageServer>,
4846 _: Arc<Client>,
4847 mut cx: AsyncAppContext,
4848 ) -> Result<()> {
4849 let server = envelope
4850 .payload
4851 .server
4852 .ok_or_else(|| anyhow!("invalid server"))?;
4853 this.update(&mut cx, |this, cx| {
4854 this.language_server_statuses.insert(
4855 server.id as usize,
4856 LanguageServerStatus {
4857 name: server.name,
4858 pending_work: Default::default(),
4859 has_pending_diagnostic_updates: false,
4860 progress_tokens: Default::default(),
4861 },
4862 );
4863 cx.notify();
4864 });
4865 Ok(())
4866 }
4867
4868 async fn handle_update_language_server(
4869 this: ModelHandle<Self>,
4870 envelope: TypedEnvelope<proto::UpdateLanguageServer>,
4871 _: Arc<Client>,
4872 mut cx: AsyncAppContext,
4873 ) -> Result<()> {
4874 let language_server_id = envelope.payload.language_server_id as usize;
4875 match envelope
4876 .payload
4877 .variant
4878 .ok_or_else(|| anyhow!("invalid variant"))?
4879 {
4880 proto::update_language_server::Variant::WorkStart(payload) => {
4881 this.update(&mut cx, |this, cx| {
4882 this.on_lsp_work_start(
4883 language_server_id,
4884 payload.token,
4885 LanguageServerProgress {
4886 message: payload.message,
4887 percentage: payload.percentage.map(|p| p as usize),
4888 last_update_at: Instant::now(),
4889 },
4890 cx,
4891 );
4892 })
4893 }
4894 proto::update_language_server::Variant::WorkProgress(payload) => {
4895 this.update(&mut cx, |this, cx| {
4896 this.on_lsp_work_progress(
4897 language_server_id,
4898 payload.token,
4899 LanguageServerProgress {
4900 message: payload.message,
4901 percentage: payload.percentage.map(|p| p as usize),
4902 last_update_at: Instant::now(),
4903 },
4904 cx,
4905 );
4906 })
4907 }
4908 proto::update_language_server::Variant::WorkEnd(payload) => {
4909 this.update(&mut cx, |this, cx| {
4910 this.on_lsp_work_end(language_server_id, payload.token, cx);
4911 })
4912 }
4913 proto::update_language_server::Variant::DiskBasedDiagnosticsUpdating(_) => {
4914 this.update(&mut cx, |this, cx| {
4915 this.disk_based_diagnostics_started(language_server_id, cx);
4916 })
4917 }
4918 proto::update_language_server::Variant::DiskBasedDiagnosticsUpdated(_) => {
4919 this.update(&mut cx, |this, cx| {
4920 this.disk_based_diagnostics_finished(language_server_id, cx)
4921 });
4922 }
4923 }
4924
4925 Ok(())
4926 }
4927
4928 async fn handle_update_buffer(
4929 this: ModelHandle<Self>,
4930 envelope: TypedEnvelope<proto::UpdateBuffer>,
4931 _: Arc<Client>,
4932 mut cx: AsyncAppContext,
4933 ) -> Result<()> {
4934 this.update(&mut cx, |this, cx| {
4935 let payload = envelope.payload.clone();
4936 let buffer_id = payload.buffer_id;
4937 let ops = payload
4938 .operations
4939 .into_iter()
4940 .map(language::proto::deserialize_operation)
4941 .collect::<Result<Vec<_>, _>>()?;
4942 let is_remote = this.is_remote();
4943 match this.opened_buffers.entry(buffer_id) {
4944 hash_map::Entry::Occupied(mut e) => match e.get_mut() {
4945 OpenBuffer::Strong(buffer) => {
4946 buffer.update(cx, |buffer, cx| buffer.apply_ops(ops, cx))?;
4947 }
4948 OpenBuffer::Operations(operations) => operations.extend_from_slice(&ops),
4949 OpenBuffer::Weak(_) => {}
4950 },
4951 hash_map::Entry::Vacant(e) => {
4952 assert!(
4953 is_remote,
4954 "received buffer update from {:?}",
4955 envelope.original_sender_id
4956 );
4957 e.insert(OpenBuffer::Operations(ops));
4958 }
4959 }
4960 Ok(())
4961 })
4962 }
4963
4964 async fn handle_create_buffer_for_peer(
4965 this: ModelHandle<Self>,
4966 envelope: TypedEnvelope<proto::CreateBufferForPeer>,
4967 _: Arc<Client>,
4968 mut cx: AsyncAppContext,
4969 ) -> Result<()> {
4970 this.update(&mut cx, |this, cx| {
4971 match envelope
4972 .payload
4973 .variant
4974 .ok_or_else(|| anyhow!("missing variant"))?
4975 {
4976 proto::create_buffer_for_peer::Variant::State(mut state) => {
4977 let mut buffer_file = None;
4978 if let Some(file) = state.file.take() {
4979 let worktree_id = WorktreeId::from_proto(file.worktree_id);
4980 let worktree = this.worktree_for_id(worktree_id, cx).ok_or_else(|| {
4981 anyhow!("no worktree found for id {}", file.worktree_id)
4982 })?;
4983 buffer_file = Some(Arc::new(File::from_proto(file, worktree.clone(), cx)?)
4984 as Arc<dyn language::File>);
4985 }
4986
4987 let buffer_id = state.id;
4988 let buffer = cx.add_model(|_| {
4989 Buffer::from_proto(this.replica_id(), state, buffer_file).unwrap()
4990 });
4991 this.incomplete_buffers.insert(buffer_id, buffer);
4992 }
4993 proto::create_buffer_for_peer::Variant::Chunk(chunk) => {
4994 let buffer = this
4995 .incomplete_buffers
4996 .get(&chunk.buffer_id)
4997 .ok_or_else(|| {
4998 anyhow!(
4999 "received chunk for buffer {} without initial state",
5000 chunk.buffer_id
5001 )
5002 })?
5003 .clone();
5004 let operations = chunk
5005 .operations
5006 .into_iter()
5007 .map(language::proto::deserialize_operation)
5008 .collect::<Result<Vec<_>>>()?;
5009 buffer.update(cx, |buffer, cx| buffer.apply_ops(operations, cx))?;
5010
5011 if chunk.is_last {
5012 this.incomplete_buffers.remove(&chunk.buffer_id);
5013 this.register_buffer(&buffer, cx)?;
5014 }
5015 }
5016 }
5017
5018 Ok(())
5019 })
5020 }
5021
5022 async fn handle_update_diff_base(
5023 this: ModelHandle<Self>,
5024 envelope: TypedEnvelope<proto::UpdateDiffBase>,
5025 _: Arc<Client>,
5026 mut cx: AsyncAppContext,
5027 ) -> Result<()> {
5028 this.update(&mut cx, |this, cx| {
5029 let buffer_id = envelope.payload.buffer_id;
5030 let diff_base = envelope.payload.diff_base;
5031 let buffer = this
5032 .opened_buffers
5033 .get_mut(&buffer_id)
5034 .and_then(|b| b.upgrade(cx))
5035 .ok_or_else(|| anyhow!("No such buffer {}", buffer_id))?;
5036
5037 buffer.update(cx, |buffer, cx| buffer.set_diff_base(diff_base, cx));
5038
5039 Ok(())
5040 })
5041 }
5042
5043 async fn handle_update_buffer_file(
5044 this: ModelHandle<Self>,
5045 envelope: TypedEnvelope<proto::UpdateBufferFile>,
5046 _: Arc<Client>,
5047 mut cx: AsyncAppContext,
5048 ) -> Result<()> {
5049 this.update(&mut cx, |this, cx| {
5050 let payload = envelope.payload.clone();
5051 let buffer_id = payload.buffer_id;
5052 let file = payload.file.ok_or_else(|| anyhow!("invalid file"))?;
5053 let worktree = this
5054 .worktree_for_id(WorktreeId::from_proto(file.worktree_id), cx)
5055 .ok_or_else(|| anyhow!("no such worktree"))?;
5056 let file = File::from_proto(file, worktree, cx)?;
5057 let buffer = this
5058 .opened_buffers
5059 .get_mut(&buffer_id)
5060 .and_then(|b| b.upgrade(cx))
5061 .ok_or_else(|| anyhow!("no such buffer"))?;
5062 buffer.update(cx, |buffer, cx| {
5063 buffer.file_updated(Arc::new(file), cx).detach();
5064 });
5065 this.assign_language_to_buffer(&buffer, cx);
5066 Ok(())
5067 })
5068 }
5069
5070 async fn handle_save_buffer(
5071 this: ModelHandle<Self>,
5072 envelope: TypedEnvelope<proto::SaveBuffer>,
5073 _: Arc<Client>,
5074 mut cx: AsyncAppContext,
5075 ) -> Result<proto::BufferSaved> {
5076 let buffer_id = envelope.payload.buffer_id;
5077 let requested_version = deserialize_version(envelope.payload.version);
5078
5079 let (project_id, buffer) = this.update(&mut cx, |this, cx| {
5080 let project_id = this.remote_id().ok_or_else(|| anyhow!("not connected"))?;
5081 let buffer = this
5082 .opened_buffers
5083 .get(&buffer_id)
5084 .and_then(|buffer| buffer.upgrade(cx))
5085 .ok_or_else(|| anyhow!("unknown buffer id {}", buffer_id))?;
5086 Ok::<_, anyhow::Error>((project_id, buffer))
5087 })?;
5088 buffer
5089 .update(&mut cx, |buffer, _| {
5090 buffer.wait_for_version(requested_version)
5091 })
5092 .await;
5093
5094 let (saved_version, fingerprint, mtime) =
5095 buffer.update(&mut cx, |buffer, cx| buffer.save(cx)).await?;
5096 Ok(proto::BufferSaved {
5097 project_id,
5098 buffer_id,
5099 version: serialize_version(&saved_version),
5100 mtime: Some(mtime.into()),
5101 fingerprint,
5102 })
5103 }
5104
5105 async fn handle_reload_buffers(
5106 this: ModelHandle<Self>,
5107 envelope: TypedEnvelope<proto::ReloadBuffers>,
5108 _: Arc<Client>,
5109 mut cx: AsyncAppContext,
5110 ) -> Result<proto::ReloadBuffersResponse> {
5111 let sender_id = envelope.original_sender_id()?;
5112 let reload = this.update(&mut cx, |this, cx| {
5113 let mut buffers = HashSet::default();
5114 for buffer_id in &envelope.payload.buffer_ids {
5115 buffers.insert(
5116 this.opened_buffers
5117 .get(buffer_id)
5118 .and_then(|buffer| buffer.upgrade(cx))
5119 .ok_or_else(|| anyhow!("unknown buffer id {}", buffer_id))?,
5120 );
5121 }
5122 Ok::<_, anyhow::Error>(this.reload_buffers(buffers, false, cx))
5123 })?;
5124
5125 let project_transaction = reload.await?;
5126 let project_transaction = this.update(&mut cx, |this, cx| {
5127 this.serialize_project_transaction_for_peer(project_transaction, sender_id, cx)
5128 });
5129 Ok(proto::ReloadBuffersResponse {
5130 transaction: Some(project_transaction),
5131 })
5132 }
5133
5134 async fn handle_format_buffers(
5135 this: ModelHandle<Self>,
5136 envelope: TypedEnvelope<proto::FormatBuffers>,
5137 _: Arc<Client>,
5138 mut cx: AsyncAppContext,
5139 ) -> Result<proto::FormatBuffersResponse> {
5140 let sender_id = envelope.original_sender_id()?;
5141 let format = this.update(&mut cx, |this, cx| {
5142 let mut buffers = HashSet::default();
5143 for buffer_id in &envelope.payload.buffer_ids {
5144 buffers.insert(
5145 this.opened_buffers
5146 .get(buffer_id)
5147 .and_then(|buffer| buffer.upgrade(cx))
5148 .ok_or_else(|| anyhow!("unknown buffer id {}", buffer_id))?,
5149 );
5150 }
5151 let trigger = FormatTrigger::from_proto(envelope.payload.trigger);
5152 Ok::<_, anyhow::Error>(this.format(buffers, false, trigger, cx))
5153 })?;
5154
5155 let project_transaction = format.await?;
5156 let project_transaction = this.update(&mut cx, |this, cx| {
5157 this.serialize_project_transaction_for_peer(project_transaction, sender_id, cx)
5158 });
5159 Ok(proto::FormatBuffersResponse {
5160 transaction: Some(project_transaction),
5161 })
5162 }
5163
5164 async fn handle_get_completions(
5165 this: ModelHandle<Self>,
5166 envelope: TypedEnvelope<proto::GetCompletions>,
5167 _: Arc<Client>,
5168 mut cx: AsyncAppContext,
5169 ) -> Result<proto::GetCompletionsResponse> {
5170 let buffer = this.read_with(&cx, |this, cx| {
5171 this.opened_buffers
5172 .get(&envelope.payload.buffer_id)
5173 .and_then(|buffer| buffer.upgrade(cx))
5174 .ok_or_else(|| anyhow!("unknown buffer id {}", envelope.payload.buffer_id))
5175 })?;
5176
5177 let position = envelope
5178 .payload
5179 .position
5180 .and_then(language::proto::deserialize_anchor)
5181 .map(|p| {
5182 buffer.read_with(&cx, |buffer, _| {
5183 buffer.clip_point_utf16(Unclipped(p.to_point_utf16(buffer)), Bias::Left)
5184 })
5185 })
5186 .ok_or_else(|| anyhow!("invalid position"))?;
5187
5188 let version = deserialize_version(envelope.payload.version);
5189 buffer
5190 .update(&mut cx, |buffer, _| buffer.wait_for_version(version))
5191 .await;
5192 let version = buffer.read_with(&cx, |buffer, _| buffer.version());
5193
5194 let completions = this
5195 .update(&mut cx, |this, cx| this.completions(&buffer, position, cx))
5196 .await?;
5197
5198 Ok(proto::GetCompletionsResponse {
5199 completions: completions
5200 .iter()
5201 .map(language::proto::serialize_completion)
5202 .collect(),
5203 version: serialize_version(&version),
5204 })
5205 }
5206
5207 async fn handle_apply_additional_edits_for_completion(
5208 this: ModelHandle<Self>,
5209 envelope: TypedEnvelope<proto::ApplyCompletionAdditionalEdits>,
5210 _: Arc<Client>,
5211 mut cx: AsyncAppContext,
5212 ) -> Result<proto::ApplyCompletionAdditionalEditsResponse> {
5213 let (buffer, completion) = this.update(&mut cx, |this, cx| {
5214 let buffer = this
5215 .opened_buffers
5216 .get(&envelope.payload.buffer_id)
5217 .and_then(|buffer| buffer.upgrade(cx))
5218 .ok_or_else(|| anyhow!("unknown buffer id {}", envelope.payload.buffer_id))?;
5219 let language = buffer.read(cx).language();
5220 let completion = language::proto::deserialize_completion(
5221 envelope
5222 .payload
5223 .completion
5224 .ok_or_else(|| anyhow!("invalid completion"))?,
5225 language.cloned(),
5226 );
5227 Ok::<_, anyhow::Error>((buffer, completion))
5228 })?;
5229
5230 let completion = completion.await?;
5231
5232 let apply_additional_edits = this.update(&mut cx, |this, cx| {
5233 this.apply_additional_edits_for_completion(buffer, completion, false, cx)
5234 });
5235
5236 Ok(proto::ApplyCompletionAdditionalEditsResponse {
5237 transaction: apply_additional_edits
5238 .await?
5239 .as_ref()
5240 .map(language::proto::serialize_transaction),
5241 })
5242 }
5243
5244 async fn handle_get_code_actions(
5245 this: ModelHandle<Self>,
5246 envelope: TypedEnvelope<proto::GetCodeActions>,
5247 _: Arc<Client>,
5248 mut cx: AsyncAppContext,
5249 ) -> Result<proto::GetCodeActionsResponse> {
5250 let start = envelope
5251 .payload
5252 .start
5253 .and_then(language::proto::deserialize_anchor)
5254 .ok_or_else(|| anyhow!("invalid start"))?;
5255 let end = envelope
5256 .payload
5257 .end
5258 .and_then(language::proto::deserialize_anchor)
5259 .ok_or_else(|| anyhow!("invalid end"))?;
5260 let buffer = this.update(&mut cx, |this, cx| {
5261 this.opened_buffers
5262 .get(&envelope.payload.buffer_id)
5263 .and_then(|buffer| buffer.upgrade(cx))
5264 .ok_or_else(|| anyhow!("unknown buffer id {}", envelope.payload.buffer_id))
5265 })?;
5266 buffer
5267 .update(&mut cx, |buffer, _| {
5268 buffer.wait_for_version(deserialize_version(envelope.payload.version))
5269 })
5270 .await;
5271
5272 let version = buffer.read_with(&cx, |buffer, _| buffer.version());
5273 let code_actions = this.update(&mut cx, |this, cx| {
5274 Ok::<_, anyhow::Error>(this.code_actions(&buffer, start..end, cx))
5275 })?;
5276
5277 Ok(proto::GetCodeActionsResponse {
5278 actions: code_actions
5279 .await?
5280 .iter()
5281 .map(language::proto::serialize_code_action)
5282 .collect(),
5283 version: serialize_version(&version),
5284 })
5285 }
5286
5287 async fn handle_apply_code_action(
5288 this: ModelHandle<Self>,
5289 envelope: TypedEnvelope<proto::ApplyCodeAction>,
5290 _: Arc<Client>,
5291 mut cx: AsyncAppContext,
5292 ) -> Result<proto::ApplyCodeActionResponse> {
5293 let sender_id = envelope.original_sender_id()?;
5294 let action = language::proto::deserialize_code_action(
5295 envelope
5296 .payload
5297 .action
5298 .ok_or_else(|| anyhow!("invalid action"))?,
5299 )?;
5300 let apply_code_action = this.update(&mut cx, |this, cx| {
5301 let buffer = this
5302 .opened_buffers
5303 .get(&envelope.payload.buffer_id)
5304 .and_then(|buffer| buffer.upgrade(cx))
5305 .ok_or_else(|| anyhow!("unknown buffer id {}", envelope.payload.buffer_id))?;
5306 Ok::<_, anyhow::Error>(this.apply_code_action(buffer, action, false, cx))
5307 })?;
5308
5309 let project_transaction = apply_code_action.await?;
5310 let project_transaction = this.update(&mut cx, |this, cx| {
5311 this.serialize_project_transaction_for_peer(project_transaction, sender_id, cx)
5312 });
5313 Ok(proto::ApplyCodeActionResponse {
5314 transaction: Some(project_transaction),
5315 })
5316 }
5317
5318 async fn handle_lsp_command<T: LspCommand>(
5319 this: ModelHandle<Self>,
5320 envelope: TypedEnvelope<T::ProtoRequest>,
5321 _: Arc<Client>,
5322 mut cx: AsyncAppContext,
5323 ) -> Result<<T::ProtoRequest as proto::RequestMessage>::Response>
5324 where
5325 <T::LspRequest as lsp::request::Request>::Result: Send,
5326 {
5327 let sender_id = envelope.original_sender_id()?;
5328 let buffer_id = T::buffer_id_from_proto(&envelope.payload);
5329 let buffer_handle = this.read_with(&cx, |this, _| {
5330 this.opened_buffers
5331 .get(&buffer_id)
5332 .and_then(|buffer| buffer.upgrade(&cx))
5333 .ok_or_else(|| anyhow!("unknown buffer id {}", buffer_id))
5334 })?;
5335 let request = T::from_proto(
5336 envelope.payload,
5337 this.clone(),
5338 buffer_handle.clone(),
5339 cx.clone(),
5340 )
5341 .await?;
5342 let buffer_version = buffer_handle.read_with(&cx, |buffer, _| buffer.version());
5343 let response = this
5344 .update(&mut cx, |this, cx| {
5345 this.request_lsp(buffer_handle, request, cx)
5346 })
5347 .await?;
5348 this.update(&mut cx, |this, cx| {
5349 Ok(T::response_to_proto(
5350 response,
5351 this,
5352 sender_id,
5353 &buffer_version,
5354 cx,
5355 ))
5356 })
5357 }
5358
5359 async fn handle_get_project_symbols(
5360 this: ModelHandle<Self>,
5361 envelope: TypedEnvelope<proto::GetProjectSymbols>,
5362 _: Arc<Client>,
5363 mut cx: AsyncAppContext,
5364 ) -> Result<proto::GetProjectSymbolsResponse> {
5365 let symbols = this
5366 .update(&mut cx, |this, cx| {
5367 this.symbols(&envelope.payload.query, cx)
5368 })
5369 .await?;
5370
5371 Ok(proto::GetProjectSymbolsResponse {
5372 symbols: symbols.iter().map(serialize_symbol).collect(),
5373 })
5374 }
5375
5376 async fn handle_search_project(
5377 this: ModelHandle<Self>,
5378 envelope: TypedEnvelope<proto::SearchProject>,
5379 _: Arc<Client>,
5380 mut cx: AsyncAppContext,
5381 ) -> Result<proto::SearchProjectResponse> {
5382 let peer_id = envelope.original_sender_id()?;
5383 let query = SearchQuery::from_proto(envelope.payload)?;
5384 let result = this
5385 .update(&mut cx, |this, cx| this.search(query, cx))
5386 .await?;
5387
5388 this.update(&mut cx, |this, cx| {
5389 let mut locations = Vec::new();
5390 for (buffer, ranges) in result {
5391 for range in ranges {
5392 let start = serialize_anchor(&range.start);
5393 let end = serialize_anchor(&range.end);
5394 let buffer_id = this.create_buffer_for_peer(&buffer, peer_id, cx);
5395 locations.push(proto::Location {
5396 buffer_id,
5397 start: Some(start),
5398 end: Some(end),
5399 });
5400 }
5401 }
5402 Ok(proto::SearchProjectResponse { locations })
5403 })
5404 }
5405
5406 async fn handle_open_buffer_for_symbol(
5407 this: ModelHandle<Self>,
5408 envelope: TypedEnvelope<proto::OpenBufferForSymbol>,
5409 _: Arc<Client>,
5410 mut cx: AsyncAppContext,
5411 ) -> Result<proto::OpenBufferForSymbolResponse> {
5412 let peer_id = envelope.original_sender_id()?;
5413 let symbol = envelope
5414 .payload
5415 .symbol
5416 .ok_or_else(|| anyhow!("invalid symbol"))?;
5417 let symbol = this
5418 .read_with(&cx, |this, _| this.deserialize_symbol(symbol))
5419 .await?;
5420 let symbol = this.read_with(&cx, |this, _| {
5421 let signature = this.symbol_signature(&symbol.path);
5422 if signature == symbol.signature {
5423 Ok(symbol)
5424 } else {
5425 Err(anyhow!("invalid symbol signature"))
5426 }
5427 })?;
5428 let buffer = this
5429 .update(&mut cx, |this, cx| this.open_buffer_for_symbol(&symbol, cx))
5430 .await?;
5431
5432 Ok(proto::OpenBufferForSymbolResponse {
5433 buffer_id: this.update(&mut cx, |this, cx| {
5434 this.create_buffer_for_peer(&buffer, peer_id, cx)
5435 }),
5436 })
5437 }
5438
5439 fn symbol_signature(&self, project_path: &ProjectPath) -> [u8; 32] {
5440 let mut hasher = Sha256::new();
5441 hasher.update(project_path.worktree_id.to_proto().to_be_bytes());
5442 hasher.update(project_path.path.to_string_lossy().as_bytes());
5443 hasher.update(self.nonce.to_be_bytes());
5444 hasher.finalize().as_slice().try_into().unwrap()
5445 }
5446
5447 async fn handle_open_buffer_by_id(
5448 this: ModelHandle<Self>,
5449 envelope: TypedEnvelope<proto::OpenBufferById>,
5450 _: Arc<Client>,
5451 mut cx: AsyncAppContext,
5452 ) -> Result<proto::OpenBufferResponse> {
5453 let peer_id = envelope.original_sender_id()?;
5454 let buffer = this
5455 .update(&mut cx, |this, cx| {
5456 this.open_buffer_by_id(envelope.payload.id, cx)
5457 })
5458 .await?;
5459 this.update(&mut cx, |this, cx| {
5460 Ok(proto::OpenBufferResponse {
5461 buffer_id: this.create_buffer_for_peer(&buffer, peer_id, cx),
5462 })
5463 })
5464 }
5465
5466 async fn handle_open_buffer_by_path(
5467 this: ModelHandle<Self>,
5468 envelope: TypedEnvelope<proto::OpenBufferByPath>,
5469 _: Arc<Client>,
5470 mut cx: AsyncAppContext,
5471 ) -> Result<proto::OpenBufferResponse> {
5472 let peer_id = envelope.original_sender_id()?;
5473 let worktree_id = WorktreeId::from_proto(envelope.payload.worktree_id);
5474 let open_buffer = this.update(&mut cx, |this, cx| {
5475 this.open_buffer(
5476 ProjectPath {
5477 worktree_id,
5478 path: PathBuf::from(envelope.payload.path).into(),
5479 },
5480 cx,
5481 )
5482 });
5483
5484 let buffer = open_buffer.await?;
5485 this.update(&mut cx, |this, cx| {
5486 Ok(proto::OpenBufferResponse {
5487 buffer_id: this.create_buffer_for_peer(&buffer, peer_id, cx),
5488 })
5489 })
5490 }
5491
5492 fn serialize_project_transaction_for_peer(
5493 &mut self,
5494 project_transaction: ProjectTransaction,
5495 peer_id: proto::PeerId,
5496 cx: &AppContext,
5497 ) -> proto::ProjectTransaction {
5498 let mut serialized_transaction = proto::ProjectTransaction {
5499 buffer_ids: Default::default(),
5500 transactions: Default::default(),
5501 };
5502 for (buffer, transaction) in project_transaction.0 {
5503 serialized_transaction
5504 .buffer_ids
5505 .push(self.create_buffer_for_peer(&buffer, peer_id, cx));
5506 serialized_transaction
5507 .transactions
5508 .push(language::proto::serialize_transaction(&transaction));
5509 }
5510 serialized_transaction
5511 }
5512
5513 fn deserialize_project_transaction(
5514 &mut self,
5515 message: proto::ProjectTransaction,
5516 push_to_history: bool,
5517 cx: &mut ModelContext<Self>,
5518 ) -> Task<Result<ProjectTransaction>> {
5519 cx.spawn(|this, mut cx| async move {
5520 let mut project_transaction = ProjectTransaction::default();
5521 for (buffer_id, transaction) in message.buffer_ids.into_iter().zip(message.transactions)
5522 {
5523 let buffer = this
5524 .update(&mut cx, |this, cx| this.wait_for_buffer(buffer_id, cx))
5525 .await?;
5526 let transaction = language::proto::deserialize_transaction(transaction)?;
5527 project_transaction.0.insert(buffer, transaction);
5528 }
5529
5530 for (buffer, transaction) in &project_transaction.0 {
5531 buffer
5532 .update(&mut cx, |buffer, _| {
5533 buffer.wait_for_edits(transaction.edit_ids.iter().copied())
5534 })
5535 .await;
5536
5537 if push_to_history {
5538 buffer.update(&mut cx, |buffer, _| {
5539 buffer.push_transaction(transaction.clone(), Instant::now());
5540 });
5541 }
5542 }
5543
5544 Ok(project_transaction)
5545 })
5546 }
5547
5548 fn create_buffer_for_peer(
5549 &mut self,
5550 buffer: &ModelHandle<Buffer>,
5551 peer_id: proto::PeerId,
5552 cx: &AppContext,
5553 ) -> u64 {
5554 let buffer_id = buffer.read(cx).remote_id();
5555 if let Some(project_id) = self.remote_id() {
5556 let shared_buffers = self.shared_buffers.entry(peer_id).or_default();
5557 if shared_buffers.insert(buffer_id) {
5558 let buffer = buffer.read(cx);
5559 let state = buffer.to_proto();
5560 let operations = buffer.serialize_ops(cx);
5561 let client = self.client.clone();
5562 cx.background()
5563 .spawn(
5564 async move {
5565 let mut operations = operations.await;
5566
5567 client.send(proto::CreateBufferForPeer {
5568 project_id,
5569 peer_id: Some(peer_id),
5570 variant: Some(proto::create_buffer_for_peer::Variant::State(state)),
5571 })?;
5572
5573 loop {
5574 #[cfg(any(test, feature = "test-support"))]
5575 const CHUNK_SIZE: usize = 5;
5576
5577 #[cfg(not(any(test, feature = "test-support")))]
5578 const CHUNK_SIZE: usize = 100;
5579
5580 let chunk = operations
5581 .drain(..cmp::min(CHUNK_SIZE, operations.len()))
5582 .collect();
5583 let is_last = operations.is_empty();
5584 client.send(proto::CreateBufferForPeer {
5585 project_id,
5586 peer_id: Some(peer_id),
5587 variant: Some(proto::create_buffer_for_peer::Variant::Chunk(
5588 proto::BufferChunk {
5589 buffer_id,
5590 operations: chunk,
5591 is_last,
5592 },
5593 )),
5594 })?;
5595
5596 if is_last {
5597 break;
5598 }
5599 }
5600
5601 Ok(())
5602 }
5603 .log_err(),
5604 )
5605 .detach();
5606 }
5607 }
5608
5609 buffer_id
5610 }
5611
5612 fn wait_for_buffer(
5613 &self,
5614 id: u64,
5615 cx: &mut ModelContext<Self>,
5616 ) -> Task<Result<ModelHandle<Buffer>>> {
5617 let mut opened_buffer_rx = self.opened_buffer.1.clone();
5618 cx.spawn(|this, mut cx| async move {
5619 let buffer = loop {
5620 let buffer = this.read_with(&cx, |this, cx| {
5621 this.opened_buffers
5622 .get(&id)
5623 .and_then(|buffer| buffer.upgrade(cx))
5624 });
5625 if let Some(buffer) = buffer {
5626 break buffer;
5627 } else if this.read_with(&cx, |this, _| this.is_read_only()) {
5628 return Err(anyhow!("disconnected before buffer {} could be opened", id));
5629 }
5630
5631 opened_buffer_rx
5632 .next()
5633 .await
5634 .ok_or_else(|| anyhow!("project dropped while waiting for buffer"))?;
5635 };
5636 buffer.update(&mut cx, |buffer, cx| buffer.git_diff_recalc(cx));
5637 Ok(buffer)
5638 })
5639 }
5640
5641 fn set_collaborators_from_proto(
5642 &mut self,
5643 messages: Vec<proto::Collaborator>,
5644 cx: &mut ModelContext<Self>,
5645 ) -> Result<()> {
5646 let mut collaborators = HashMap::default();
5647 for message in messages {
5648 let collaborator = Collaborator::from_proto(message)?;
5649 collaborators.insert(collaborator.peer_id, collaborator);
5650 }
5651 for old_peer_id in self.collaborators.keys() {
5652 if !collaborators.contains_key(old_peer_id) {
5653 cx.emit(Event::CollaboratorLeft(*old_peer_id));
5654 }
5655 }
5656 self.collaborators = collaborators;
5657 Ok(())
5658 }
5659
5660 fn deserialize_symbol(
5661 &self,
5662 serialized_symbol: proto::Symbol,
5663 ) -> impl Future<Output = Result<Symbol>> {
5664 let languages = self.languages.clone();
5665 async move {
5666 let source_worktree_id = WorktreeId::from_proto(serialized_symbol.source_worktree_id);
5667 let worktree_id = WorktreeId::from_proto(serialized_symbol.worktree_id);
5668 let start = serialized_symbol
5669 .start
5670 .ok_or_else(|| anyhow!("invalid start"))?;
5671 let end = serialized_symbol
5672 .end
5673 .ok_or_else(|| anyhow!("invalid end"))?;
5674 let kind = unsafe { mem::transmute(serialized_symbol.kind) };
5675 let path = ProjectPath {
5676 worktree_id,
5677 path: PathBuf::from(serialized_symbol.path).into(),
5678 };
5679 let language = languages.select_language(&path.path);
5680 Ok(Symbol {
5681 language_server_name: LanguageServerName(
5682 serialized_symbol.language_server_name.into(),
5683 ),
5684 source_worktree_id,
5685 path,
5686 label: {
5687 match language {
5688 Some(language) => {
5689 language
5690 .label_for_symbol(&serialized_symbol.name, kind)
5691 .await
5692 }
5693 None => None,
5694 }
5695 .unwrap_or_else(|| CodeLabel::plain(serialized_symbol.name.clone(), None))
5696 },
5697
5698 name: serialized_symbol.name,
5699 range: Unclipped(PointUtf16::new(start.row, start.column))
5700 ..Unclipped(PointUtf16::new(end.row, end.column)),
5701 kind,
5702 signature: serialized_symbol
5703 .signature
5704 .try_into()
5705 .map_err(|_| anyhow!("invalid signature"))?,
5706 })
5707 }
5708 }
5709
5710 async fn handle_buffer_saved(
5711 this: ModelHandle<Self>,
5712 envelope: TypedEnvelope<proto::BufferSaved>,
5713 _: Arc<Client>,
5714 mut cx: AsyncAppContext,
5715 ) -> Result<()> {
5716 let version = deserialize_version(envelope.payload.version);
5717 let mtime = envelope
5718 .payload
5719 .mtime
5720 .ok_or_else(|| anyhow!("missing mtime"))?
5721 .into();
5722
5723 this.update(&mut cx, |this, cx| {
5724 let buffer = this
5725 .opened_buffers
5726 .get(&envelope.payload.buffer_id)
5727 .and_then(|buffer| buffer.upgrade(cx));
5728 if let Some(buffer) = buffer {
5729 buffer.update(cx, |buffer, cx| {
5730 buffer.did_save(version, envelope.payload.fingerprint, mtime, None, cx);
5731 });
5732 }
5733 Ok(())
5734 })
5735 }
5736
5737 async fn handle_buffer_reloaded(
5738 this: ModelHandle<Self>,
5739 envelope: TypedEnvelope<proto::BufferReloaded>,
5740 _: Arc<Client>,
5741 mut cx: AsyncAppContext,
5742 ) -> Result<()> {
5743 let payload = envelope.payload;
5744 let version = deserialize_version(payload.version);
5745 let line_ending = deserialize_line_ending(
5746 proto::LineEnding::from_i32(payload.line_ending)
5747 .ok_or_else(|| anyhow!("missing line ending"))?,
5748 );
5749 let mtime = payload
5750 .mtime
5751 .ok_or_else(|| anyhow!("missing mtime"))?
5752 .into();
5753 this.update(&mut cx, |this, cx| {
5754 let buffer = this
5755 .opened_buffers
5756 .get(&payload.buffer_id)
5757 .and_then(|buffer| buffer.upgrade(cx));
5758 if let Some(buffer) = buffer {
5759 buffer.update(cx, |buffer, cx| {
5760 buffer.did_reload(version, payload.fingerprint, line_ending, mtime, cx);
5761 });
5762 }
5763 Ok(())
5764 })
5765 }
5766
5767 #[allow(clippy::type_complexity)]
5768 fn edits_from_lsp(
5769 &mut self,
5770 buffer: &ModelHandle<Buffer>,
5771 lsp_edits: impl 'static + Send + IntoIterator<Item = lsp::TextEdit>,
5772 version: Option<i32>,
5773 cx: &mut ModelContext<Self>,
5774 ) -> Task<Result<Vec<(Range<Anchor>, String)>>> {
5775 let snapshot = self.buffer_snapshot_for_lsp_version(buffer, version, cx);
5776 cx.background().spawn(async move {
5777 let snapshot = snapshot?;
5778 let mut lsp_edits = lsp_edits
5779 .into_iter()
5780 .map(|edit| (range_from_lsp(edit.range), edit.new_text))
5781 .collect::<Vec<_>>();
5782 lsp_edits.sort_by_key(|(range, _)| range.start);
5783
5784 let mut lsp_edits = lsp_edits.into_iter().peekable();
5785 let mut edits = Vec::new();
5786 while let Some((range, mut new_text)) = lsp_edits.next() {
5787 // Clip invalid ranges provided by the language server.
5788 let mut range = snapshot.clip_point_utf16(range.start, Bias::Left)
5789 ..snapshot.clip_point_utf16(range.end, Bias::Left);
5790
5791 // Combine any LSP edits that are adjacent.
5792 //
5793 // Also, combine LSP edits that are separated from each other by only
5794 // a newline. This is important because for some code actions,
5795 // Rust-analyzer rewrites the entire buffer via a series of edits that
5796 // are separated by unchanged newline characters.
5797 //
5798 // In order for the diffing logic below to work properly, any edits that
5799 // cancel each other out must be combined into one.
5800 while let Some((next_range, next_text)) = lsp_edits.peek() {
5801 if next_range.start.0 > range.end {
5802 if next_range.start.0.row > range.end.row + 1
5803 || next_range.start.0.column > 0
5804 || snapshot.clip_point_utf16(
5805 Unclipped(PointUtf16::new(range.end.row, u32::MAX)),
5806 Bias::Left,
5807 ) > range.end
5808 {
5809 break;
5810 }
5811 new_text.push('\n');
5812 }
5813 range.end = snapshot.clip_point_utf16(next_range.end, Bias::Left);
5814 new_text.push_str(next_text);
5815 lsp_edits.next();
5816 }
5817
5818 // For multiline edits, perform a diff of the old and new text so that
5819 // we can identify the changes more precisely, preserving the locations
5820 // of any anchors positioned in the unchanged regions.
5821 if range.end.row > range.start.row {
5822 let mut offset = range.start.to_offset(&snapshot);
5823 let old_text = snapshot.text_for_range(range).collect::<String>();
5824
5825 let diff = TextDiff::from_lines(old_text.as_str(), &new_text);
5826 let mut moved_since_edit = true;
5827 for change in diff.iter_all_changes() {
5828 let tag = change.tag();
5829 let value = change.value();
5830 match tag {
5831 ChangeTag::Equal => {
5832 offset += value.len();
5833 moved_since_edit = true;
5834 }
5835 ChangeTag::Delete => {
5836 let start = snapshot.anchor_after(offset);
5837 let end = snapshot.anchor_before(offset + value.len());
5838 if moved_since_edit {
5839 edits.push((start..end, String::new()));
5840 } else {
5841 edits.last_mut().unwrap().0.end = end;
5842 }
5843 offset += value.len();
5844 moved_since_edit = false;
5845 }
5846 ChangeTag::Insert => {
5847 if moved_since_edit {
5848 let anchor = snapshot.anchor_after(offset);
5849 edits.push((anchor..anchor, value.to_string()));
5850 } else {
5851 edits.last_mut().unwrap().1.push_str(value);
5852 }
5853 moved_since_edit = false;
5854 }
5855 }
5856 }
5857 } else if range.end == range.start {
5858 let anchor = snapshot.anchor_after(range.start);
5859 edits.push((anchor..anchor, new_text));
5860 } else {
5861 let edit_start = snapshot.anchor_after(range.start);
5862 let edit_end = snapshot.anchor_before(range.end);
5863 edits.push((edit_start..edit_end, new_text));
5864 }
5865 }
5866
5867 Ok(edits)
5868 })
5869 }
5870
5871 fn buffer_snapshot_for_lsp_version(
5872 &mut self,
5873 buffer: &ModelHandle<Buffer>,
5874 version: Option<i32>,
5875 cx: &AppContext,
5876 ) -> Result<TextBufferSnapshot> {
5877 const OLD_VERSIONS_TO_RETAIN: i32 = 10;
5878
5879 if let Some(version) = version {
5880 let buffer_id = buffer.read(cx).remote_id();
5881 let snapshots = self
5882 .buffer_snapshots
5883 .get_mut(&buffer_id)
5884 .ok_or_else(|| anyhow!("no snapshot found for buffer {}", buffer_id))?;
5885 let mut found_snapshot = None;
5886 snapshots.retain(|(snapshot_version, snapshot)| {
5887 if snapshot_version + OLD_VERSIONS_TO_RETAIN < version {
5888 false
5889 } else {
5890 if *snapshot_version == version {
5891 found_snapshot = Some(snapshot.clone());
5892 }
5893 true
5894 }
5895 });
5896
5897 found_snapshot.ok_or_else(|| {
5898 anyhow!(
5899 "snapshot not found for buffer {} at version {}",
5900 buffer_id,
5901 version
5902 )
5903 })
5904 } else {
5905 Ok((buffer.read(cx)).text_snapshot())
5906 }
5907 }
5908
5909 fn language_server_for_buffer(
5910 &self,
5911 buffer: &Buffer,
5912 cx: &AppContext,
5913 ) -> Option<(&Arc<CachedLspAdapter>, &Arc<LanguageServer>)> {
5914 if let Some((file, language)) = File::from_dyn(buffer.file()).zip(buffer.language()) {
5915 let name = language.lsp_adapter()?.name.clone();
5916 let worktree_id = file.worktree_id(cx);
5917 let key = (worktree_id, name);
5918
5919 if let Some(server_id) = self.language_server_ids.get(&key) {
5920 if let Some(LanguageServerState::Running {
5921 adapter, server, ..
5922 }) = self.language_servers.get(server_id)
5923 {
5924 return Some((adapter, server));
5925 }
5926 }
5927 }
5928
5929 None
5930 }
5931}
5932
5933impl WorktreeHandle {
5934 pub fn upgrade(&self, cx: &AppContext) -> Option<ModelHandle<Worktree>> {
5935 match self {
5936 WorktreeHandle::Strong(handle) => Some(handle.clone()),
5937 WorktreeHandle::Weak(handle) => handle.upgrade(cx),
5938 }
5939 }
5940}
5941
5942impl OpenBuffer {
5943 pub fn upgrade(&self, cx: &impl UpgradeModelHandle) -> Option<ModelHandle<Buffer>> {
5944 match self {
5945 OpenBuffer::Strong(handle) => Some(handle.clone()),
5946 OpenBuffer::Weak(handle) => handle.upgrade(cx),
5947 OpenBuffer::Operations(_) => None,
5948 }
5949 }
5950}
5951
5952pub struct PathMatchCandidateSet {
5953 pub snapshot: Snapshot,
5954 pub include_ignored: bool,
5955 pub include_root_name: bool,
5956}
5957
5958impl<'a> fuzzy::PathMatchCandidateSet<'a> for PathMatchCandidateSet {
5959 type Candidates = PathMatchCandidateSetIter<'a>;
5960
5961 fn id(&self) -> usize {
5962 self.snapshot.id().to_usize()
5963 }
5964
5965 fn len(&self) -> usize {
5966 if self.include_ignored {
5967 self.snapshot.file_count()
5968 } else {
5969 self.snapshot.visible_file_count()
5970 }
5971 }
5972
5973 fn prefix(&self) -> Arc<str> {
5974 if self.snapshot.root_entry().map_or(false, |e| e.is_file()) {
5975 self.snapshot.root_name().into()
5976 } else if self.include_root_name {
5977 format!("{}/", self.snapshot.root_name()).into()
5978 } else {
5979 "".into()
5980 }
5981 }
5982
5983 fn candidates(&'a self, start: usize) -> Self::Candidates {
5984 PathMatchCandidateSetIter {
5985 traversal: self.snapshot.files(self.include_ignored, start),
5986 }
5987 }
5988}
5989
5990pub struct PathMatchCandidateSetIter<'a> {
5991 traversal: Traversal<'a>,
5992}
5993
5994impl<'a> Iterator for PathMatchCandidateSetIter<'a> {
5995 type Item = fuzzy::PathMatchCandidate<'a>;
5996
5997 fn next(&mut self) -> Option<Self::Item> {
5998 self.traversal.next().map(|entry| {
5999 if let EntryKind::File(char_bag) = entry.kind {
6000 fuzzy::PathMatchCandidate {
6001 path: &entry.path,
6002 char_bag,
6003 }
6004 } else {
6005 unreachable!()
6006 }
6007 })
6008 }
6009}
6010
6011impl Entity for Project {
6012 type Event = Event;
6013
6014 fn release(&mut self, _: &mut gpui::MutableAppContext) {
6015 match &self.client_state {
6016 Some(ProjectClientState::Local { remote_id, .. }) => {
6017 self.client
6018 .send(proto::UnshareProject {
6019 project_id: *remote_id,
6020 })
6021 .log_err();
6022 }
6023 Some(ProjectClientState::Remote { remote_id, .. }) => {
6024 self.client
6025 .send(proto::LeaveProject {
6026 project_id: *remote_id,
6027 })
6028 .log_err();
6029 }
6030 _ => {}
6031 }
6032 }
6033
6034 fn app_will_quit(
6035 &mut self,
6036 _: &mut MutableAppContext,
6037 ) -> Option<std::pin::Pin<Box<dyn 'static + Future<Output = ()>>>> {
6038 let shutdown_futures = self
6039 .language_servers
6040 .drain()
6041 .map(|(_, server_state)| async {
6042 match server_state {
6043 LanguageServerState::Running { server, .. } => server.shutdown()?.await,
6044 LanguageServerState::Starting(starting_server) => {
6045 starting_server.await?.shutdown()?.await
6046 }
6047 }
6048 })
6049 .collect::<Vec<_>>();
6050
6051 Some(
6052 async move {
6053 futures::future::join_all(shutdown_futures).await;
6054 }
6055 .boxed(),
6056 )
6057 }
6058}
6059
6060impl Collaborator {
6061 fn from_proto(message: proto::Collaborator) -> Result<Self> {
6062 Ok(Self {
6063 peer_id: message.peer_id.ok_or_else(|| anyhow!("invalid peer id"))?,
6064 replica_id: message.replica_id as ReplicaId,
6065 })
6066 }
6067}
6068
6069impl<P: AsRef<Path>> From<(WorktreeId, P)> for ProjectPath {
6070 fn from((worktree_id, path): (WorktreeId, P)) -> Self {
6071 Self {
6072 worktree_id,
6073 path: path.as_ref().into(),
6074 }
6075 }
6076}
6077
6078fn serialize_symbol(symbol: &Symbol) -> proto::Symbol {
6079 proto::Symbol {
6080 language_server_name: symbol.language_server_name.0.to_string(),
6081 source_worktree_id: symbol.source_worktree_id.to_proto(),
6082 worktree_id: symbol.path.worktree_id.to_proto(),
6083 path: symbol.path.path.to_string_lossy().to_string(),
6084 name: symbol.name.clone(),
6085 kind: unsafe { mem::transmute(symbol.kind) },
6086 start: Some(proto::PointUtf16 {
6087 row: symbol.range.start.0.row,
6088 column: symbol.range.start.0.column,
6089 }),
6090 end: Some(proto::PointUtf16 {
6091 row: symbol.range.end.0.row,
6092 column: symbol.range.end.0.column,
6093 }),
6094 signature: symbol.signature.to_vec(),
6095 }
6096}
6097
6098fn relativize_path(base: &Path, path: &Path) -> PathBuf {
6099 let mut path_components = path.components();
6100 let mut base_components = base.components();
6101 let mut components: Vec<Component> = Vec::new();
6102 loop {
6103 match (path_components.next(), base_components.next()) {
6104 (None, None) => break,
6105 (Some(a), None) => {
6106 components.push(a);
6107 components.extend(path_components.by_ref());
6108 break;
6109 }
6110 (None, _) => components.push(Component::ParentDir),
6111 (Some(a), Some(b)) if components.is_empty() && a == b => (),
6112 (Some(a), Some(b)) if b == Component::CurDir => components.push(a),
6113 (Some(a), Some(_)) => {
6114 components.push(Component::ParentDir);
6115 for _ in base_components {
6116 components.push(Component::ParentDir);
6117 }
6118 components.push(a);
6119 components.extend(path_components.by_ref());
6120 break;
6121 }
6122 }
6123 }
6124 components.iter().map(|c| c.as_os_str()).collect()
6125}
6126
6127impl Item for Buffer {
6128 fn entry_id(&self, cx: &AppContext) -> Option<ProjectEntryId> {
6129 File::from_dyn(self.file()).and_then(|file| file.project_entry_id(cx))
6130 }
6131}