1#![allow(unused_variables, unused_mut)]
2//todo!()
3
4mod assets;
5pub mod languages;
6mod only_instance;
7mod open_listener;
8
9pub use assets::*;
10use collections::VecDeque;
11use editor::{Editor, MultiBuffer};
12use gpui::{
13 actions, point, px, AppContext, Context, FocusableView, PromptLevel, TitlebarOptions,
14 ViewContext, VisualContext, WindowBounds, WindowKind, WindowOptions,
15};
16pub use only_instance::*;
17pub use open_listener::*;
18
19use anyhow::{anyhow, Context as _};
20use project_panel::ProjectPanel;
21use settings::{initial_local_settings_content, Settings};
22use std::{borrow::Cow, ops::Deref, sync::Arc};
23use terminal_view::terminal_panel::TerminalPanel;
24use util::{
25 asset_str,
26 channel::{AppCommitSha, ReleaseChannel},
27 paths::{self, LOCAL_SETTINGS_RELATIVE_PATH},
28 ResultExt,
29};
30use uuid::Uuid;
31use workspace::{
32 create_and_open_local_file, dock::PanelHandle,
33 notifications::simple_message_notification::MessageNotification, open_new, AppState, NewFile,
34 NewWindow, Workspace, WorkspaceSettings,
35};
36use zed_actions::{OpenBrowser, OpenZedURL};
37
38actions!(
39 About,
40 DebugElements,
41 DecreaseBufferFontSize,
42 Hide,
43 HideOthers,
44 IncreaseBufferFontSize,
45 Minimize,
46 OpenDefaultKeymap,
47 OpenDefaultSettings,
48 OpenKeymap,
49 OpenLicenses,
50 OpenLocalSettings,
51 OpenLog,
52 OpenSettings,
53 OpenTelemetryLog,
54 Quit,
55 ResetBufferFontSize,
56 ResetDatabase,
57 ShowAll,
58 ToggleFullScreen,
59 Zoom,
60);
61
62pub fn build_window_options(
63 bounds: Option<WindowBounds>,
64 display_uuid: Option<Uuid>,
65 cx: &mut AppContext,
66) -> WindowOptions {
67 let bounds = bounds.unwrap_or(WindowBounds::Maximized);
68 let display = display_uuid.and_then(|uuid| {
69 cx.displays()
70 .into_iter()
71 .find(|display| display.uuid().ok() == Some(uuid))
72 });
73
74 WindowOptions {
75 bounds,
76 titlebar: Some(TitlebarOptions {
77 title: None,
78 appears_transparent: true,
79 traffic_light_position: Some(point(px(8.), px(8.))),
80 }),
81 center: false,
82 focus: false,
83 show: false,
84 kind: WindowKind::Normal,
85 is_movable: true,
86 display_id: display.map(|display| display.id()),
87 }
88}
89
90pub fn initialize_workspace(app_state: Arc<AppState>, cx: &mut AppContext) {
91 cx.observe_new_views(move |workspace: &mut Workspace, cx| {
92 let workspace_handle = cx.view().clone();
93 cx.subscribe(&workspace_handle, {
94 move |workspace, _, event, cx| {
95 if let workspace::Event::PaneAdded(pane) = event {
96 pane.update(cx, |pane, cx| {
97 pane.toolbar().update(cx, |toolbar, cx| {
98 // todo!()
99 // let breadcrumbs = cx.add_view(|_| Breadcrumbs::new(workspace));
100 // toolbar.add_item(breadcrumbs, cx);
101 let buffer_search_bar = cx.build_view(search::BufferSearchBar::new);
102 toolbar.add_item(buffer_search_bar.clone(), cx);
103 // let quick_action_bar = cx.add_view(|_| {
104 // QuickActionBar::new(buffer_search_bar, workspace)
105 // });
106 // toolbar.add_item(quick_action_bar, cx);
107 let diagnostic_editor_controls =
108 cx.build_view(|_| diagnostics::ToolbarControls::new());
109 // toolbar.add_item(diagnostic_editor_controls, cx);
110 // let project_search_bar = cx.add_view(|_| ProjectSearchBar::new());
111 // toolbar.add_item(project_search_bar, cx);
112 // let submit_feedback_button =
113 // cx.add_view(|_| SubmitFeedbackButton::new());
114 // toolbar.add_item(submit_feedback_button, cx);
115 // let feedback_info_text = cx.add_view(|_| FeedbackInfoText::new());
116 // toolbar.add_item(feedback_info_text, cx);
117 // let lsp_log_item =
118 // cx.add_view(|_| language_tools::LspLogToolbarItemView::new());
119 // toolbar.add_item(lsp_log_item, cx);
120 // let syntax_tree_item = cx
121 // .add_view(|_| language_tools::SyntaxTreeToolbarItemView::new());
122 // toolbar.add_item(syntax_tree_item, cx);
123 })
124 });
125 }
126 }
127 })
128 .detach();
129
130 // cx.emit(workspace2::Event::PaneAdded(
131 // workspace.active_pane().clone(),
132 // ));
133
134 // let collab_titlebar_item =
135 // cx.add_view(|cx| CollabTitlebarItem::new(workspace, &workspace_handle, cx));
136 // workspace.set_titlebar_item(collab_titlebar_item.into_any(), cx);
137
138 // let copilot =
139 // cx.add_view(|cx| copilot_button::CopilotButton::new(app_state.fs.clone(), cx));
140 let diagnostic_summary =
141 cx.build_view(|cx| diagnostics::items::DiagnosticIndicator::new(workspace, cx));
142 // let activity_indicator = activity_indicator::ActivityIndicator::new(
143 // workspace,
144 // app_state.languages.clone(),
145 // cx,
146 // );
147 // let active_buffer_language =
148 // cx.add_view(|_| language_selector::ActiveBufferLanguage::new(workspace));
149 // let vim_mode_indicator = cx.add_view(|cx| vim::ModeIndicator::new(cx));
150 // let feedback_button = cx.add_view(|_| {
151 // feedback::deploy_feedback_button::DeployFeedbackButton::new(workspace)
152 // });
153 // let cursor_position = cx.add_view(|_| editor::items::CursorPosition::new());
154 workspace.status_bar().update(cx, |status_bar, cx| {
155 status_bar.add_left_item(diagnostic_summary, cx);
156 // status_bar.add_left_item(activity_indicator, cx);
157
158 // status_bar.add_right_item(feedback_button, cx);
159 // status_bar.add_right_item(copilot, cx);
160 // status_bar.add_right_item(active_buffer_language, cx);
161 // status_bar.add_right_item(vim_mode_indicator, cx);
162 // status_bar.add_right_item(cursor_position, cx);
163 });
164
165 auto_update::notify_of_any_new_update(cx);
166
167 // vim::observe_keystrokes(cx);
168
169 let handle = cx.view().downgrade();
170 cx.on_window_should_close(move |cx| {
171 handle
172 .update(cx, |workspace, cx| {
173 if let Some(task) = workspace.close(&Default::default(), cx) {
174 task.detach_and_log_err(cx);
175 }
176 false
177 })
178 .unwrap_or(true)
179 });
180
181 cx.spawn(|workspace_handle, mut cx| async move {
182 let project_panel = ProjectPanel::load(workspace_handle.clone(), cx.clone());
183 let terminal_panel = TerminalPanel::load(workspace_handle.clone(), cx.clone());
184 // let assistant_panel = AssistantPanel::load(workspace_handle.clone(), cx.clone());
185 let channels_panel =
186 collab_ui::collab_panel::CollabPanel::load(workspace_handle.clone(), cx.clone());
187 // let chat_panel =
188 // collab_ui::chat_panel::ChatPanel::load(workspace_handle.clone(), cx.clone());
189 // let notification_panel = collab_ui::notification_panel::NotificationPanel::load(
190 // workspace_handle.clone(),
191 // cx.clone(),
192 // );
193 let (
194 project_panel,
195 terminal_panel,
196 // assistant_panel,
197 channels_panel,
198 // chat_panel,
199 // notification_panel,
200 ) = futures::try_join!(
201 project_panel,
202 terminal_panel,
203 // assistant_panel,
204 channels_panel,
205 // chat_panel,
206 // notification_panel,
207 )?;
208
209 workspace_handle.update(&mut cx, |workspace, cx| {
210 let project_panel_position = project_panel.position(cx);
211 workspace.add_panel(project_panel, cx);
212 workspace.add_panel(terminal_panel, cx);
213 // workspace.add_panel(assistant_panel, cx);
214 workspace.add_panel(channels_panel, cx);
215 // workspace.add_panel(chat_panel, cx);
216 // workspace.add_panel(notification_panel, cx);
217
218 // if !was_deserialized
219 // && workspace
220 // .project()
221 // .read(cx)
222 // .visible_worktrees(cx)
223 // .any(|tree| {
224 // tree.read(cx)
225 // .root_entry()
226 // .map_or(false, |entry| entry.is_dir())
227 // })
228 // {
229 // workspace.toggle_dock(project_panel_position, cx);
230 // }
231 // cx.focus_self();
232 })
233 })
234 .detach();
235
236 workspace
237 .register_action(about)
238 .register_action(|_, _: &Hide, cx| {
239 cx.hide();
240 })
241 .register_action(|_, _: &HideOthers, cx| {
242 cx.hide_other_apps();
243 })
244 .register_action(|_, _: &ShowAll, cx| {
245 cx.unhide_other_apps();
246 })
247 .register_action(|_, _: &Minimize, cx| {
248 cx.minimize_window();
249 })
250 .register_action(|_, _: &Zoom, cx| {
251 cx.zoom_window();
252 })
253 .register_action(|_, _: &ToggleFullScreen, cx| {
254 cx.toggle_full_screen();
255 })
256 .register_action(quit)
257 .register_action(|_, action: &OpenZedURL, cx| {
258 cx.global::<Arc<OpenListener>>()
259 .open_urls(&[action.url.clone()])
260 })
261 .register_action(|_, action: &OpenBrowser, cx| cx.open_url(&action.url))
262 //todo!(buffer font size)
263 // cx.add_global_action(move |_: &IncreaseBufferFontSize, cx| {
264 // theme::adjust_font_size(cx, |size| *size += 1.0)
265 // });
266 // cx.add_global_action(move |_: &DecreaseBufferFontSize, cx| {
267 // theme::adjust_font_size(cx, |size| *size -= 1.0)
268 // });
269 // cx.add_global_action(move |_: &ResetBufferFontSize, cx| theme::reset_font_size(cx));
270 .register_action(|_, _: &install_cli::Install, cx| {
271 cx.spawn(|_, cx| async move {
272 install_cli::install_cli(cx.deref())
273 .await
274 .context("error creating CLI symlink")
275 })
276 .detach_and_log_err(cx);
277 })
278 .register_action(|workspace, _: &OpenLog, cx| {
279 open_log_file(workspace, cx);
280 })
281 .register_action(|workspace, _: &OpenLicenses, cx| {
282 open_bundled_file(
283 workspace,
284 asset_str::<Assets>("licenses.md"),
285 "Open Source License Attribution",
286 "Markdown",
287 cx,
288 );
289 })
290 .register_action(
291 move |workspace: &mut Workspace,
292 _: &OpenTelemetryLog,
293 cx: &mut ViewContext<Workspace>| {
294 open_telemetry_log_file(workspace, cx);
295 },
296 )
297 .register_action(
298 move |_: &mut Workspace, _: &OpenKeymap, cx: &mut ViewContext<Workspace>| {
299 create_and_open_local_file(&paths::KEYMAP, cx, Default::default)
300 .detach_and_log_err(cx);
301 },
302 )
303 .register_action(
304 move |_: &mut Workspace, _: &OpenSettings, cx: &mut ViewContext<Workspace>| {
305 create_and_open_local_file(&paths::SETTINGS, cx, || {
306 settings::initial_user_settings_content().as_ref().into()
307 })
308 .detach_and_log_err(cx);
309 },
310 )
311 .register_action(open_local_settings_file)
312 .register_action(
313 move |workspace: &mut Workspace,
314 _: &OpenDefaultKeymap,
315 cx: &mut ViewContext<Workspace>| {
316 open_bundled_file(
317 workspace,
318 settings::default_keymap(),
319 "Default Key Bindings",
320 "JSON",
321 cx,
322 );
323 },
324 )
325 .register_action(
326 move |workspace: &mut Workspace,
327 _: &OpenDefaultSettings,
328 cx: &mut ViewContext<Workspace>| {
329 open_bundled_file(
330 workspace,
331 settings::default_settings(),
332 "Default Settings",
333 "JSON",
334 cx,
335 );
336 },
337 )
338 //todo!()
339 // cx.add_action({
340 // move |workspace: &mut Workspace, _: &DebugElements, cx: &mut ViewContext<Workspace>| {
341 // let app_state = workspace.app_state().clone();
342 // let markdown = app_state.languages.language_for_name("JSON");
343 // let window = cx.window();
344 // cx.spawn(|workspace, mut cx| async move {
345 // let markdown = markdown.await.log_err();
346 // let content = to_string_pretty(&window.debug_elements(&cx).ok_or_else(|| {
347 // anyhow!("could not debug elements for window {}", window.id())
348 // })?)
349 // .unwrap();
350 // workspace
351 // .update(&mut cx, |workspace, cx| {
352 // workspace.with_local_workspace(cx, move |workspace, cx| {
353 // let project = workspace.project().clone();
354 // let buffer = project
355 // .update(cx, |project, cx| {
356 // project.create_buffer(&content, markdown, cx)
357 // })
358 // .expect("creating buffers on a local workspace always succeeds");
359 // let buffer = cx.add_model(|cx| {
360 // MultiBuffer::singleton(buffer, cx)
361 // .with_title("Debug Elements".into())
362 // });
363 // workspace.add_item(
364 // Box::new(cx.add_view(|cx| {
365 // Editor::for_multibuffer(buffer, Some(project.clone()), cx)
366 // })),
367 // cx,
368 // );
369 // })
370 // })?
371 // .await
372 // })
373 // .detach_and_log_err(cx);
374 // }
375 // });
376 // .register_action(
377 // |workspace: &mut Workspace,
378 // _: &project_panel::ToggleFocus,
379 // cx: &mut ViewContext<Workspace>| {
380 // workspace.toggle_panel_focus::<ProjectPanel>(cx);
381 // },
382 // );
383 // cx.add_action(
384 // |workspace: &mut Workspace,
385 // _: &collab_ui::collab_panel::ToggleFocus,
386 // cx: &mut ViewContext<Workspace>| {
387 // workspace.toggle_panel_focus::<collab_ui::collab_panel::CollabPanel>(cx);
388 // },
389 // );
390 // cx.add_action(
391 // |workspace: &mut Workspace,
392 // _: &collab_ui::chat_panel::ToggleFocus,
393 // cx: &mut ViewContext<Workspace>| {
394 // workspace.toggle_panel_focus::<collab_ui::chat_panel::ChatPanel>(cx);
395 // },
396 // );
397 // cx.add_action(
398 // |workspace: &mut Workspace,
399 // _: &collab_ui::notification_panel::ToggleFocus,
400 // cx: &mut ViewContext<Workspace>| {
401 // workspace.toggle_panel_focus::<collab_ui::notification_panel::NotificationPanel>(cx);
402 // },
403 // );
404 // cx.add_action(
405 // |workspace: &mut Workspace,
406 // _: &terminal_panel::ToggleFocus,
407 // cx: &mut ViewContext<Workspace>| {
408 // workspace.toggle_panel_focus::<TerminalPanel>(cx);
409 // },
410 // );
411 .register_action({
412 let app_state = Arc::downgrade(&app_state);
413 move |_, _: &NewWindow, cx| {
414 if let Some(app_state) = app_state.upgrade() {
415 open_new(&app_state, cx, |workspace, cx| {
416 Editor::new_file(workspace, &Default::default(), cx)
417 })
418 .detach();
419 }
420 }
421 })
422 .register_action({
423 let app_state = Arc::downgrade(&app_state);
424 move |_, _: &NewFile, cx| {
425 if let Some(app_state) = app_state.upgrade() {
426 open_new(&app_state, cx, |workspace, cx| {
427 Editor::new_file(workspace, &Default::default(), cx)
428 })
429 .detach();
430 }
431 }
432 });
433
434 workspace.focus_handle(cx).focus(cx);
435 //todo!()
436 // load_default_keymap(cx);
437 })
438 .detach();
439}
440
441fn about(_: &mut Workspace, _: &About, cx: &mut gpui::ViewContext<Workspace>) {
442 use std::fmt::Write as _;
443
444 let app_name = cx.global::<ReleaseChannel>().display_name();
445 let version = env!("CARGO_PKG_VERSION");
446 let mut message = format!("{app_name} {version}");
447 if let Some(sha) = cx.try_global::<AppCommitSha>() {
448 write!(&mut message, "\n\n{}", sha.0).unwrap();
449 }
450
451 let prompt = cx.prompt(PromptLevel::Info, &message, &["OK"]);
452 cx.foreground_executor()
453 .spawn(async {
454 prompt.await.ok();
455 })
456 .detach();
457}
458
459fn quit(_: &mut Workspace, _: &Quit, cx: &mut gpui::ViewContext<Workspace>) {
460 let should_confirm = WorkspaceSettings::get_global(cx).confirm_quit;
461 cx.spawn(|_, mut cx| async move {
462 let mut workspace_windows = cx.update(|_, cx| {
463 cx.windows()
464 .into_iter()
465 .filter_map(|window| window.downcast::<Workspace>())
466 .collect::<Vec<_>>()
467 })?;
468
469 // If multiple windows have unsaved changes, and need a save prompt,
470 // prompt in the active window before switching to a different window.
471 cx.update(|_, cx| {
472 workspace_windows.sort_by_key(|window| window.is_active(&cx) == Some(false));
473 })
474 .log_err();
475
476 if let (true, Some(window)) = (should_confirm, workspace_windows.first().copied()) {
477 let answer = cx
478 .update(|_, cx| {
479 cx.prompt(
480 PromptLevel::Info,
481 "Are you sure you want to quit?",
482 &["Quit", "Cancel"],
483 )
484 })
485 .log_err();
486
487 if let Some(mut answer) = answer {
488 let answer = answer.await.ok();
489 if answer != Some(0) {
490 return Ok(());
491 }
492 }
493 }
494
495 // If the user cancels any save prompt, then keep the app open.
496 for window in workspace_windows {
497 if let Some(should_close) = window
498 .update(&mut cx, |workspace, cx| {
499 workspace.prepare_to_close(true, cx)
500 })
501 .log_err()
502 {
503 if !should_close.await? {
504 return Ok(());
505 }
506 }
507 }
508 cx.update(|_, cx| {
509 cx.quit();
510 })?;
511 anyhow::Ok(())
512 })
513 .detach_and_log_err(cx);
514}
515
516fn open_log_file(workspace: &mut Workspace, cx: &mut ViewContext<Workspace>) {
517 const MAX_LINES: usize = 1000;
518 workspace
519 .with_local_workspace(cx, move |workspace, cx| {
520 let fs = workspace.app_state().fs.clone();
521 cx.spawn(|workspace, mut cx| async move {
522 let (old_log, new_log) =
523 futures::join!(fs.load(&paths::OLD_LOG), fs.load(&paths::LOG));
524
525 let mut lines = VecDeque::with_capacity(MAX_LINES);
526 for line in old_log
527 .iter()
528 .flat_map(|log| log.lines())
529 .chain(new_log.iter().flat_map(|log| log.lines()))
530 {
531 if lines.len() == MAX_LINES {
532 lines.pop_front();
533 }
534 lines.push_back(line);
535 }
536 let log = lines
537 .into_iter()
538 .flat_map(|line| [line, "\n"])
539 .collect::<String>();
540
541 workspace
542 .update(&mut cx, |workspace, cx| {
543 let project = workspace.project().clone();
544 let buffer = project
545 .update(cx, |project, cx| project.create_buffer("", None, cx))
546 .expect("creating buffers on a local workspace always succeeds");
547 buffer.update(cx, |buffer, cx| buffer.edit([(0..0, log)], None, cx));
548
549 let buffer = cx.build_model(|cx| {
550 MultiBuffer::singleton(buffer, cx).with_title("Log".into())
551 });
552 workspace.add_item(
553 Box::new(cx.build_view(|cx| {
554 Editor::for_multibuffer(buffer, Some(project), cx)
555 })),
556 cx,
557 );
558 })
559 .log_err();
560 })
561 .detach();
562 })
563 .detach();
564}
565
566fn open_local_settings_file(
567 workspace: &mut Workspace,
568 _: &OpenLocalSettings,
569 cx: &mut ViewContext<Workspace>,
570) {
571 let project = workspace.project().clone();
572 let worktree = project
573 .read(cx)
574 .visible_worktrees(cx)
575 .find_map(|tree| tree.read(cx).root_entry()?.is_dir().then_some(tree));
576 if let Some(worktree) = worktree {
577 let tree_id = worktree.read(cx).id();
578 cx.spawn(|workspace, mut cx| async move {
579 let file_path = &*LOCAL_SETTINGS_RELATIVE_PATH;
580
581 if let Some(dir_path) = file_path.parent() {
582 if worktree.update(&mut cx, |tree, _| tree.entry_for_path(dir_path).is_none())? {
583 project
584 .update(&mut cx, |project, cx| {
585 project.create_entry((tree_id, dir_path), true, cx)
586 })?
587 .ok_or_else(|| anyhow!("worktree was removed"))?
588 .await?;
589 }
590 }
591
592 if worktree.update(&mut cx, |tree, _| tree.entry_for_path(file_path).is_none())? {
593 project
594 .update(&mut cx, |project, cx| {
595 project.create_entry((tree_id, file_path), false, cx)
596 })?
597 .ok_or_else(|| anyhow!("worktree was removed"))?
598 .await?;
599 }
600
601 let editor = workspace
602 .update(&mut cx, |workspace, cx| {
603 workspace.open_path((tree_id, file_path), None, true, cx)
604 })?
605 .await?
606 .downcast::<Editor>()
607 .ok_or_else(|| anyhow!("unexpected item type"))?;
608
609 editor
610 .downgrade()
611 .update(&mut cx, |editor, cx| {
612 if let Some(buffer) = editor.buffer().read(cx).as_singleton() {
613 if buffer.read(cx).is_empty() {
614 buffer.update(cx, |buffer, cx| {
615 buffer.edit([(0..0, initial_local_settings_content())], None, cx)
616 });
617 }
618 }
619 })
620 .ok();
621
622 anyhow::Ok(())
623 })
624 .detach();
625 } else {
626 workspace.show_notification(0, cx, |cx| {
627 cx.build_view(|_| MessageNotification::new("This project has no folders open."))
628 })
629 }
630}
631
632fn open_telemetry_log_file(workspace: &mut Workspace, cx: &mut ViewContext<Workspace>) {
633 workspace.with_local_workspace(cx, move |workspace, cx| {
634 let app_state = workspace.app_state().clone();
635 cx.spawn(|workspace, mut cx| async move {
636 async fn fetch_log_string(app_state: &Arc<AppState>) -> Option<String> {
637 let path = app_state.client.telemetry().log_file_path()?;
638 app_state.fs.load(&path).await.log_err()
639 }
640
641 let log = fetch_log_string(&app_state).await.unwrap_or_else(|| "// No data has been collected yet".to_string());
642
643 const MAX_TELEMETRY_LOG_LEN: usize = 5 * 1024 * 1024;
644 let mut start_offset = log.len().saturating_sub(MAX_TELEMETRY_LOG_LEN);
645 if let Some(newline_offset) = log[start_offset..].find('\n') {
646 start_offset += newline_offset + 1;
647 }
648 let log_suffix = &log[start_offset..];
649 let json = app_state.languages.language_for_name("JSON").await.log_err();
650
651 workspace.update(&mut cx, |workspace, cx| {
652 let project = workspace.project().clone();
653 let buffer = project
654 .update(cx, |project, cx| project.create_buffer("", None, cx))
655 .expect("creating buffers on a local workspace always succeeds");
656 buffer.update(cx, |buffer, cx| {
657 buffer.set_language(json, cx);
658 buffer.edit(
659 [(
660 0..0,
661 concat!(
662 "// Zed collects anonymous usage data to help us understand how people are using the app.\n",
663 "// Telemetry can be disabled via the `settings.json` file.\n",
664 "// Here is the data that has been reported for the current session:\n",
665 "\n"
666 ),
667 )],
668 None,
669 cx,
670 );
671 buffer.edit([(buffer.len()..buffer.len(), log_suffix)], None, cx);
672 });
673
674 let buffer = cx.build_model(|cx| {
675 MultiBuffer::singleton(buffer, cx).with_title("Telemetry Log".into())
676 });
677 workspace.add_item(
678 Box::new(cx.build_view(|cx| Editor::for_multibuffer(buffer, Some(project), cx))),
679 cx,
680 );
681 }).log_err()?;
682
683 Some(())
684 })
685 .detach();
686 }).detach();
687}
688
689fn open_bundled_file(
690 workspace: &mut Workspace,
691 text: Cow<'static, str>,
692 title: &'static str,
693 language: &'static str,
694 cx: &mut ViewContext<Workspace>,
695) {
696 let language = workspace.app_state().languages.language_for_name(language);
697 cx.spawn(|workspace, mut cx| async move {
698 let language = language.await.log_err();
699 workspace
700 .update(&mut cx, |workspace, cx| {
701 workspace.with_local_workspace(cx, |workspace, cx| {
702 let project = workspace.project();
703 let buffer = project.update(cx, move |project, cx| {
704 project
705 .create_buffer(text.as_ref(), language, cx)
706 .expect("creating buffers on a local workspace always succeeds")
707 });
708 let buffer = cx.build_model(|cx| {
709 MultiBuffer::singleton(buffer, cx).with_title(title.into())
710 });
711 workspace.add_item(
712 Box::new(cx.build_view(|cx| {
713 Editor::for_multibuffer(buffer, Some(project.clone()), cx)
714 })),
715 cx,
716 );
717 })
718 })?
719 .await
720 })
721 .detach_and_log_err(cx);
722}