headless_project.rs

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