zed2.rs

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