headless_project.rs

  1use ::proto::{FromProto, ToProto};
  2use anyhow::{Result, anyhow};
  3use dap::DapRegistry;
  4use extension::ExtensionHostProxy;
  5use extension_host::headless_host::HeadlessExtensionStore;
  6use fs::Fs;
  7use gpui::{App, AppContext as _, AsyncApp, Context, Entity, PromptLevel};
  8use http_client::HttpClient;
  9use language::{Buffer, BufferEvent, LanguageRegistry, proto::serialize_operation};
 10use node_runtime::NodeRuntime;
 11use project::{
 12    LspStore, LspStoreEvent, PrettierStore, ProjectPath, ToolchainStore, WorktreeId,
 13    buffer_store::{BufferStore, BufferStoreEvent},
 14    debugger::{breakpoint_store::BreakpointStore, dap_store::DapStore},
 15    git_store::GitStore,
 16    project_settings::SettingsObserver,
 17    search::SearchQuery,
 18    task_store::TaskStore,
 19    worktree_store::WorktreeStore,
 20};
 21use remote::ssh_session::ChannelClient;
 22use rpc::{
 23    AnyProtoClient, TypedEnvelope,
 24    proto::{self, SSH_PEER_ID, SSH_PROJECT_ID},
 25};
 26
 27use settings::initial_server_settings_content;
 28use smol::stream::StreamExt;
 29use std::{
 30    path::{Path, PathBuf},
 31    sync::{Arc, atomic::AtomicUsize},
 32};
 33use util::ResultExt;
 34use worktree::Worktree;
 35
 36pub struct HeadlessProject {
 37    pub fs: Arc<dyn Fs>,
 38    pub session: AnyProtoClient,
 39    pub worktree_store: Entity<WorktreeStore>,
 40    pub buffer_store: Entity<BufferStore>,
 41    pub lsp_store: Entity<LspStore>,
 42    pub task_store: Entity<TaskStore>,
 43    pub settings_observer: Entity<SettingsObserver>,
 44    pub next_entry_id: Arc<AtomicUsize>,
 45    pub languages: Arc<LanguageRegistry>,
 46    pub extensions: Entity<HeadlessExtensionStore>,
 47    pub git_store: Entity<GitStore>,
 48}
 49
 50pub struct HeadlessAppState {
 51    pub session: Arc<ChannelClient>,
 52    pub fs: Arc<dyn Fs>,
 53    pub http_client: Arc<dyn HttpClient>,
 54    pub node_runtime: NodeRuntime,
 55    pub languages: Arc<LanguageRegistry>,
 56    pub debug_adapters: Arc<DapRegistry>,
 57    pub extension_host_proxy: Arc<ExtensionHostProxy>,
 58}
 59
 60impl HeadlessProject {
 61    pub fn init(cx: &mut App) {
 62        settings::init(cx);
 63        language::init(cx);
 64        project::Project::init_settings(cx);
 65    }
 66
 67    pub fn new(
 68        HeadlessAppState {
 69            session,
 70            fs,
 71            http_client,
 72            node_runtime,
 73            languages,
 74            debug_adapters,
 75            extension_host_proxy: proxy,
 76        }: HeadlessAppState,
 77        cx: &mut Context<Self>,
 78    ) -> Self {
 79        language_extension::init(proxy.clone(), languages.clone());
 80        languages::init(languages.clone(), node_runtime.clone(), cx);
 81
 82        let worktree_store = cx.new(|cx| {
 83            let mut store = WorktreeStore::local(true, fs.clone());
 84            store.shared(SSH_PROJECT_ID, session.clone().into(), cx);
 85            store
 86        });
 87
 88        let environment = project::ProjectEnvironment::new(&worktree_store, None, cx);
 89
 90        let toolchain_store = cx.new(|cx| {
 91            ToolchainStore::local(
 92                languages.clone(),
 93                worktree_store.clone(),
 94                environment.clone(),
 95                cx,
 96            )
 97        });
 98
 99        let buffer_store = cx.new(|cx| {
100            let mut buffer_store = BufferStore::local(worktree_store.clone(), cx);
101            buffer_store.shared(SSH_PROJECT_ID, session.clone().into(), cx);
102            buffer_store
103        });
104
105        let breakpoint_store =
106            cx.new(|_| BreakpointStore::local(worktree_store.clone(), buffer_store.clone()));
107
108        let dap_store = cx.new(|cx| {
109            DapStore::new_local(
110                http_client.clone(),
111                node_runtime.clone(),
112                fs.clone(),
113                languages.clone(),
114                debug_adapters.clone(),
115                environment.clone(),
116                toolchain_store.read(cx).as_language_toolchain_store(),
117                breakpoint_store.clone(),
118                worktree_store.clone(),
119                cx,
120            )
121        });
122
123        let git_store = cx.new(|cx| {
124            let mut store = GitStore::local(
125                &worktree_store,
126                buffer_store.clone(),
127                environment.clone(),
128                fs.clone(),
129                cx,
130            );
131            store.shared(SSH_PROJECT_ID, session.clone().into(), cx);
132            store
133        });
134
135        let prettier_store = cx.new(|cx| {
136            PrettierStore::new(
137                node_runtime.clone(),
138                fs.clone(),
139                languages.clone(),
140                worktree_store.clone(),
141                cx,
142            )
143        });
144
145        let task_store = cx.new(|cx| {
146            let mut task_store = TaskStore::local(
147                buffer_store.downgrade(),
148                worktree_store.clone(),
149                toolchain_store.read(cx).as_language_toolchain_store(),
150                environment.clone(),
151                cx,
152            );
153            task_store.shared(SSH_PROJECT_ID, session.clone().into(), cx);
154            task_store
155        });
156        let settings_observer = cx.new(|cx| {
157            let mut observer = SettingsObserver::new_local(
158                fs.clone(),
159                worktree_store.clone(),
160                task_store.clone(),
161                cx,
162            );
163            observer.shared(SSH_PROJECT_ID, session.clone().into(), cx);
164            observer
165        });
166
167        let lsp_store = cx.new(|cx| {
168            let mut lsp_store = LspStore::new_local(
169                buffer_store.clone(),
170                worktree_store.clone(),
171                prettier_store.clone(),
172                toolchain_store.clone(),
173                environment,
174                languages.clone(),
175                http_client.clone(),
176                fs.clone(),
177                cx,
178            );
179            lsp_store.shared(SSH_PROJECT_ID, session.clone().into(), cx);
180            lsp_store
181        });
182
183        cx.subscribe(&lsp_store, Self::on_lsp_store_event).detach();
184
185        cx.subscribe(
186            &buffer_store,
187            |_this, _buffer_store, event, cx| match event {
188                BufferStoreEvent::BufferAdded(buffer) => {
189                    cx.subscribe(buffer, Self::on_buffer_event).detach();
190                }
191                _ => {}
192            },
193        )
194        .detach();
195
196        let extensions = HeadlessExtensionStore::new(
197            fs.clone(),
198            http_client.clone(),
199            paths::remote_extensions_dir().to_path_buf(),
200            proxy,
201            node_runtime,
202            cx,
203        );
204
205        let client: AnyProtoClient = session.clone().into();
206
207        // local_machine -> ssh handlers
208        session.subscribe_to_entity(SSH_PROJECT_ID, &worktree_store);
209        session.subscribe_to_entity(SSH_PROJECT_ID, &buffer_store);
210        session.subscribe_to_entity(SSH_PROJECT_ID, &cx.entity());
211        session.subscribe_to_entity(SSH_PROJECT_ID, &lsp_store);
212        session.subscribe_to_entity(SSH_PROJECT_ID, &task_store);
213        session.subscribe_to_entity(SSH_PROJECT_ID, &toolchain_store);
214        session.subscribe_to_entity(SSH_PROJECT_ID, &dap_store);
215        session.subscribe_to_entity(SSH_PROJECT_ID, &settings_observer);
216        session.subscribe_to_entity(SSH_PROJECT_ID, &git_store);
217
218        client.add_request_handler(cx.weak_entity(), Self::handle_list_remote_directory);
219        client.add_request_handler(cx.weak_entity(), Self::handle_get_path_metadata);
220        client.add_request_handler(cx.weak_entity(), Self::handle_shutdown_remote_server);
221        client.add_request_handler(cx.weak_entity(), Self::handle_ping);
222
223        client.add_entity_request_handler(Self::handle_add_worktree);
224        client.add_request_handler(cx.weak_entity(), Self::handle_remove_worktree);
225
226        client.add_entity_request_handler(Self::handle_open_buffer_by_path);
227        client.add_entity_request_handler(Self::handle_open_new_buffer);
228        client.add_entity_request_handler(Self::handle_find_search_candidates);
229        client.add_entity_request_handler(Self::handle_open_server_settings);
230
231        client.add_entity_request_handler(BufferStore::handle_update_buffer);
232        client.add_entity_message_handler(BufferStore::handle_close_buffer);
233
234        client.add_request_handler(
235            extensions.clone().downgrade(),
236            HeadlessExtensionStore::handle_sync_extensions,
237        );
238        client.add_request_handler(
239            extensions.clone().downgrade(),
240            HeadlessExtensionStore::handle_install_extension,
241        );
242
243        BufferStore::init(&client);
244        WorktreeStore::init(&client);
245        SettingsObserver::init(&client);
246        LspStore::init(&client);
247        TaskStore::init(Some(&client));
248        ToolchainStore::init(&client);
249        DapStore::init(&client);
250        // todo(debugger): Re init breakpoint store when we set it up for collab
251        // BreakpointStore::init(&client);
252        GitStore::init(&client);
253
254        HeadlessProject {
255            session: client,
256            settings_observer,
257            fs,
258            worktree_store,
259            buffer_store,
260            lsp_store,
261            task_store,
262            next_entry_id: Default::default(),
263            languages,
264            extensions,
265            git_store,
266        }
267    }
268
269    fn on_buffer_event(
270        &mut self,
271        buffer: Entity<Buffer>,
272        event: &BufferEvent,
273        cx: &mut Context<Self>,
274    ) {
275        match event {
276            BufferEvent::Operation {
277                operation,
278                is_local: true,
279            } => cx
280                .background_spawn(self.session.request(proto::UpdateBuffer {
281                    project_id: SSH_PROJECT_ID,
282                    buffer_id: buffer.read(cx).remote_id().to_proto(),
283                    operations: vec![serialize_operation(operation)],
284                }))
285                .detach(),
286            _ => {}
287        }
288    }
289
290    fn on_lsp_store_event(
291        &mut self,
292        _lsp_store: Entity<LspStore>,
293        event: &LspStoreEvent,
294        cx: &mut Context<Self>,
295    ) {
296        match event {
297            LspStoreEvent::LanguageServerUpdate {
298                language_server_id,
299                message,
300            } => {
301                self.session
302                    .send(proto::UpdateLanguageServer {
303                        project_id: SSH_PROJECT_ID,
304                        language_server_id: language_server_id.to_proto(),
305                        variant: Some(message.clone()),
306                    })
307                    .log_err();
308            }
309            LspStoreEvent::Notification(message) => {
310                self.session
311                    .send(proto::Toast {
312                        project_id: SSH_PROJECT_ID,
313                        notification_id: "lsp".to_string(),
314                        message: message.clone(),
315                    })
316                    .log_err();
317            }
318            LspStoreEvent::LanguageServerLog(language_server_id, log_type, message) => {
319                self.session
320                    .send(proto::LanguageServerLog {
321                        project_id: SSH_PROJECT_ID,
322                        language_server_id: language_server_id.to_proto(),
323                        message: message.clone(),
324                        log_type: Some(log_type.to_proto()),
325                    })
326                    .log_err();
327            }
328            LspStoreEvent::LanguageServerPrompt(prompt) => {
329                let request = self.session.request(proto::LanguageServerPromptRequest {
330                    project_id: SSH_PROJECT_ID,
331                    actions: prompt
332                        .actions
333                        .iter()
334                        .map(|action| action.title.to_string())
335                        .collect(),
336                    level: Some(prompt_to_proto(&prompt)),
337                    lsp_name: prompt.lsp_name.clone(),
338                    message: prompt.message.clone(),
339                });
340                let prompt = prompt.clone();
341                cx.background_spawn(async move {
342                    let response = request.await?;
343                    if let Some(action_response) = response.action_response {
344                        prompt.respond(action_response as usize).await;
345                    }
346                    anyhow::Ok(())
347                })
348                .detach();
349            }
350            _ => {}
351        }
352    }
353
354    pub async fn handle_add_worktree(
355        this: Entity<Self>,
356        message: TypedEnvelope<proto::AddWorktree>,
357        mut cx: AsyncApp,
358    ) -> Result<proto::AddWorktreeResponse> {
359        use client::ErrorCodeExt;
360        let fs = this.read_with(&mut cx, |this, _| this.fs.clone())?;
361        let path = PathBuf::from_proto(shellexpand::tilde(&message.payload.path).to_string());
362
363        let canonicalized = match fs.canonicalize(&path).await {
364            Ok(path) => path,
365            Err(e) => {
366                let mut parent = path
367                    .parent()
368                    .ok_or(e)
369                    .map_err(|_| anyhow!("{:?} does not exist", path))?;
370                if parent == Path::new("") {
371                    parent = util::paths::home_dir();
372                }
373                let parent = fs.canonicalize(parent).await.map_err(|_| {
374                    anyhow!(
375                        proto::ErrorCode::DevServerProjectPathDoesNotExist
376                            .with_tag("path", &path.to_string_lossy().as_ref())
377                    )
378                })?;
379                parent.join(path.file_name().unwrap())
380            }
381        };
382
383        let worktree = this
384            .update(&mut cx.clone(), |this, _| {
385                Worktree::local(
386                    Arc::from(canonicalized.as_path()),
387                    message.payload.visible,
388                    this.fs.clone(),
389                    this.next_entry_id.clone(),
390                    &mut cx,
391                )
392            })?
393            .await?;
394
395        let response = this.update(&mut cx, |_, cx| {
396            worktree.update(cx, |worktree, _| proto::AddWorktreeResponse {
397                worktree_id: worktree.id().to_proto(),
398                canonicalized_path: canonicalized.to_proto(),
399            })
400        })?;
401
402        // We spawn this asynchronously, so that we can send the response back
403        // *before* `worktree_store.add()` can send out UpdateProject requests
404        // to the client about the new worktree.
405        //
406        // That lets the client manage the reference/handles of the newly-added
407        // worktree, before getting interrupted by an UpdateProject request.
408        //
409        // This fixes the problem of the client sending the AddWorktree request,
410        // headless project sending out a project update, client receiving it
411        // and immediately dropping the reference of the new client, causing it
412        // to be dropped on the headless project, and the client only then
413        // receiving a response to AddWorktree.
414        cx.spawn(async move |cx| {
415            this.update(cx, |this, cx| {
416                this.worktree_store.update(cx, |worktree_store, cx| {
417                    worktree_store.add(&worktree, cx);
418                });
419            })
420            .log_err();
421        })
422        .detach();
423
424        Ok(response)
425    }
426
427    pub async fn handle_remove_worktree(
428        this: Entity<Self>,
429        envelope: TypedEnvelope<proto::RemoveWorktree>,
430        mut cx: AsyncApp,
431    ) -> Result<proto::Ack> {
432        let worktree_id = WorktreeId::from_proto(envelope.payload.worktree_id);
433        this.update(&mut cx, |this, cx| {
434            this.worktree_store.update(cx, |worktree_store, cx| {
435                worktree_store.remove_worktree(worktree_id, cx);
436            });
437        })?;
438        Ok(proto::Ack {})
439    }
440
441    pub async fn handle_open_buffer_by_path(
442        this: Entity<Self>,
443        message: TypedEnvelope<proto::OpenBufferByPath>,
444        mut cx: AsyncApp,
445    ) -> Result<proto::OpenBufferResponse> {
446        let worktree_id = WorktreeId::from_proto(message.payload.worktree_id);
447        let (buffer_store, buffer) = this.update(&mut cx, |this, cx| {
448            let buffer_store = this.buffer_store.clone();
449            let buffer = this.buffer_store.update(cx, |buffer_store, cx| {
450                buffer_store.open_buffer(
451                    ProjectPath {
452                        worktree_id,
453                        path: Arc::<Path>::from_proto(message.payload.path),
454                    },
455                    cx,
456                )
457            });
458            anyhow::Ok((buffer_store, buffer))
459        })??;
460
461        let buffer = buffer.await?;
462        let buffer_id = buffer.read_with(&cx, |b, _| b.remote_id())?;
463        buffer_store.update(&mut cx, |buffer_store, cx| {
464            buffer_store
465                .create_buffer_for_peer(&buffer, SSH_PEER_ID, cx)
466                .detach_and_log_err(cx);
467        })?;
468
469        Ok(proto::OpenBufferResponse {
470            buffer_id: buffer_id.to_proto(),
471        })
472    }
473
474    pub async fn handle_open_new_buffer(
475        this: Entity<Self>,
476        _message: TypedEnvelope<proto::OpenNewBuffer>,
477        mut cx: AsyncApp,
478    ) -> Result<proto::OpenBufferResponse> {
479        let (buffer_store, buffer) = this.update(&mut cx, |this, cx| {
480            let buffer_store = this.buffer_store.clone();
481            let buffer = this
482                .buffer_store
483                .update(cx, |buffer_store, cx| buffer_store.create_buffer(cx));
484            anyhow::Ok((buffer_store, buffer))
485        })??;
486
487        let buffer = buffer.await?;
488        let buffer_id = buffer.read_with(&cx, |b, _| b.remote_id())?;
489        buffer_store.update(&mut cx, |buffer_store, cx| {
490            buffer_store
491                .create_buffer_for_peer(&buffer, SSH_PEER_ID, cx)
492                .detach_and_log_err(cx);
493        })?;
494
495        Ok(proto::OpenBufferResponse {
496            buffer_id: buffer_id.to_proto(),
497        })
498    }
499
500    pub async fn handle_open_server_settings(
501        this: Entity<Self>,
502        _: TypedEnvelope<proto::OpenServerSettings>,
503        mut cx: AsyncApp,
504    ) -> Result<proto::OpenBufferResponse> {
505        let settings_path = paths::settings_file();
506        let (worktree, path) = this
507            .update(&mut cx, |this, cx| {
508                this.worktree_store.update(cx, |worktree_store, cx| {
509                    worktree_store.find_or_create_worktree(settings_path, false, cx)
510                })
511            })?
512            .await?;
513
514        let (buffer, buffer_store) = this.update(&mut cx, |this, cx| {
515            let buffer = this.buffer_store.update(cx, |buffer_store, cx| {
516                buffer_store.open_buffer(
517                    ProjectPath {
518                        worktree_id: worktree.read(cx).id(),
519                        path: path.into(),
520                    },
521                    cx,
522                )
523            });
524
525            (buffer, this.buffer_store.clone())
526        })?;
527
528        let buffer = buffer.await?;
529
530        let buffer_id = cx.update(|cx| {
531            if buffer.read(cx).is_empty() {
532                buffer.update(cx, |buffer, cx| {
533                    buffer.edit([(0..0, initial_server_settings_content())], None, cx)
534                });
535            }
536
537            let buffer_id = buffer.read_with(cx, |b, _| b.remote_id());
538
539            buffer_store.update(cx, |buffer_store, cx| {
540                buffer_store
541                    .create_buffer_for_peer(&buffer, SSH_PEER_ID, cx)
542                    .detach_and_log_err(cx);
543            });
544
545            buffer_id
546        })?;
547
548        Ok(proto::OpenBufferResponse {
549            buffer_id: buffer_id.to_proto(),
550        })
551    }
552
553    pub async fn handle_find_search_candidates(
554        this: Entity<Self>,
555        envelope: TypedEnvelope<proto::FindSearchCandidates>,
556        mut cx: AsyncApp,
557    ) -> Result<proto::FindSearchCandidatesResponse> {
558        let message = envelope.payload;
559        let query = SearchQuery::from_proto(
560            message
561                .query
562                .ok_or_else(|| anyhow!("missing query field"))?,
563        )?;
564        let results = this.update(&mut cx, |this, cx| {
565            this.buffer_store.update(cx, |buffer_store, cx| {
566                buffer_store.find_search_candidates(&query, message.limit as _, this.fs.clone(), cx)
567            })
568        })?;
569
570        let mut response = proto::FindSearchCandidatesResponse {
571            buffer_ids: Vec::new(),
572        };
573
574        let buffer_store = this.read_with(&cx, |this, _| this.buffer_store.clone())?;
575
576        while let Ok(buffer) = results.recv().await {
577            let buffer_id = buffer.update(&mut cx, |this, _| this.remote_id())?;
578            response.buffer_ids.push(buffer_id.to_proto());
579            buffer_store
580                .update(&mut cx, |buffer_store, cx| {
581                    buffer_store.create_buffer_for_peer(&buffer, SSH_PEER_ID, cx)
582                })?
583                .await?;
584        }
585
586        Ok(response)
587    }
588
589    pub async fn handle_list_remote_directory(
590        this: Entity<Self>,
591        envelope: TypedEnvelope<proto::ListRemoteDirectory>,
592        cx: AsyncApp,
593    ) -> Result<proto::ListRemoteDirectoryResponse> {
594        let fs = cx.read_entity(&this, |this, _| this.fs.clone())?;
595        let expanded = PathBuf::from_proto(shellexpand::tilde(&envelope.payload.path).to_string());
596        let check_info = envelope
597            .payload
598            .config
599            .as_ref()
600            .is_some_and(|config| config.is_dir);
601
602        let mut entries = Vec::new();
603        let mut entry_info = Vec::new();
604        let mut response = fs.read_dir(&expanded).await?;
605        while let Some(path) = response.next().await {
606            let path = path?;
607            if let Some(file_name) = path.file_name() {
608                entries.push(file_name.to_string_lossy().to_string());
609                if check_info {
610                    let is_dir = fs.is_dir(&path).await;
611                    entry_info.push(proto::EntryInfo { is_dir });
612                }
613            }
614        }
615        Ok(proto::ListRemoteDirectoryResponse {
616            entries,
617            entry_info,
618        })
619    }
620
621    pub async fn handle_get_path_metadata(
622        this: Entity<Self>,
623        envelope: TypedEnvelope<proto::GetPathMetadata>,
624        cx: AsyncApp,
625    ) -> Result<proto::GetPathMetadataResponse> {
626        let fs = cx.read_entity(&this, |this, _| this.fs.clone())?;
627        let expanded = PathBuf::from_proto(shellexpand::tilde(&envelope.payload.path).to_string());
628
629        let metadata = fs.metadata(&expanded).await?;
630        let is_dir = metadata.map(|metadata| metadata.is_dir).unwrap_or(false);
631
632        Ok(proto::GetPathMetadataResponse {
633            exists: metadata.is_some(),
634            is_dir,
635            path: expanded.to_proto(),
636        })
637    }
638
639    pub async fn handle_shutdown_remote_server(
640        _this: Entity<Self>,
641        _envelope: TypedEnvelope<proto::ShutdownRemoteServer>,
642        cx: AsyncApp,
643    ) -> Result<proto::Ack> {
644        cx.spawn(async move |cx| {
645            cx.update(|cx| {
646                // TODO: This is a hack, because in a headless project, shutdown isn't executed
647                // when calling quit, but it should be.
648                cx.shutdown();
649                cx.quit();
650            })
651        })
652        .detach();
653
654        Ok(proto::Ack {})
655    }
656
657    pub async fn handle_ping(
658        _this: Entity<Self>,
659        _envelope: TypedEnvelope<proto::Ping>,
660        _cx: AsyncApp,
661    ) -> Result<proto::Ack> {
662        log::debug!("Received ping from client");
663        Ok(proto::Ack {})
664    }
665}
666
667fn prompt_to_proto(
668    prompt: &project::LanguageServerPromptRequest,
669) -> proto::language_server_prompt_request::Level {
670    match prompt.level {
671        PromptLevel::Info => proto::language_server_prompt_request::Level::Info(
672            proto::language_server_prompt_request::Info {},
673        ),
674        PromptLevel::Warning => proto::language_server_prompt_request::Level::Warning(
675            proto::language_server_prompt_request::Warning {},
676        ),
677        PromptLevel::Critical => proto::language_server_prompt_request::Level::Critical(
678            proto::language_server_prompt_request::Critical {},
679        ),
680    }
681}