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