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