1use ::proto::{FromProto, ToProto};
2use anyhow::{Result, anyhow};
3
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 dap_store: Entity<DapStore>,
45 pub settings_observer: Entity<SettingsObserver>,
46 pub next_entry_id: Arc<AtomicUsize>,
47 pub languages: Arc<LanguageRegistry>,
48 pub extensions: Entity<HeadlessExtensionStore>,
49 pub git_store: Entity<GitStore>,
50}
51
52pub struct HeadlessAppState {
53 pub session: Arc<ChannelClient>,
54 pub fs: Arc<dyn Fs>,
55 pub http_client: Arc<dyn HttpClient>,
56 pub node_runtime: NodeRuntime,
57 pub languages: Arc<LanguageRegistry>,
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 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 = cx.new(|_| ProjectEnvironment::new(None));
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 let mut dap_store = DapStore::new_local(
110 http_client.clone(),
111 node_runtime.clone(),
112 fs.clone(),
113 environment.clone(),
114 toolchain_store.read(cx).as_language_toolchain_store(),
115 worktree_store.clone(),
116 breakpoint_store.clone(),
117 cx,
118 );
119 dap_store.shared(SSH_PROJECT_ID, session.clone().into(), cx);
120 dap_store
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, cx);
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 dap_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}