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