zed2.rs

  1#![allow(unused_variables, dead_code, unused_mut)]
  2// todo!() this is to make transition easier.
  3
  4mod assets;
  5pub mod languages;
  6mod only_instance;
  7mod open_listener;
  8
  9pub use assets::*;
 10use collections::HashMap;
 11use gpui::{
 12    point, px, AppContext, AsyncAppContext, AsyncWindowContext, Point, Task, TitlebarOptions,
 13    WeakView, WindowBounds, WindowKind, WindowOptions,
 14};
 15pub use only_instance::*;
 16pub use open_listener::*;
 17
 18use anyhow::{Context, Result};
 19use cli::{
 20    ipc::{self, IpcSender},
 21    CliRequest, CliResponse, IpcHandshake,
 22};
 23use futures::{
 24    channel::{mpsc, oneshot},
 25    FutureExt, SinkExt, StreamExt,
 26};
 27use std::{path::Path, sync::Arc, thread, time::Duration};
 28use util::{paths::PathLikeWithPosition, ResultExt};
 29use uuid::Uuid;
 30use workspace2::{AppState, Workspace};
 31
 32pub fn connect_to_cli(
 33    server_name: &str,
 34) -> Result<(mpsc::Receiver<CliRequest>, IpcSender<CliResponse>)> {
 35    let handshake_tx = cli::ipc::IpcSender::<IpcHandshake>::connect(server_name.to_string())
 36        .context("error connecting to cli")?;
 37    let (request_tx, request_rx) = ipc::channel::<CliRequest>()?;
 38    let (response_tx, response_rx) = ipc::channel::<CliResponse>()?;
 39
 40    handshake_tx
 41        .send(IpcHandshake {
 42            requests: request_tx,
 43            responses: response_rx,
 44        })
 45        .context("error sending ipc handshake")?;
 46
 47    let (mut async_request_tx, async_request_rx) =
 48        futures::channel::mpsc::channel::<CliRequest>(16);
 49    thread::spawn(move || {
 50        while let Ok(cli_request) = request_rx.recv() {
 51            if smol::block_on(async_request_tx.send(cli_request)).is_err() {
 52                break;
 53            }
 54        }
 55        Ok::<_, anyhow::Error>(())
 56    });
 57
 58    Ok((async_request_rx, response_tx))
 59}
 60
 61pub async fn handle_cli_connection(
 62    (mut requests, responses): (mpsc::Receiver<CliRequest>, IpcSender<CliResponse>),
 63    app_state: Arc<AppState>,
 64    mut cx: AsyncAppContext,
 65) {
 66    if let Some(request) = requests.next().await {
 67        match request {
 68            CliRequest::Open { paths, wait } => {
 69                let mut caret_positions = HashMap::default();
 70
 71                let paths = if paths.is_empty() {
 72                    workspace2::last_opened_workspace_paths()
 73                        .await
 74                        .map(|location| location.paths().to_vec())
 75                        .unwrap_or_default()
 76                } else {
 77                    paths
 78                        .into_iter()
 79                        .filter_map(|path_with_position_string| {
 80                            let path_with_position = PathLikeWithPosition::parse_str(
 81                                &path_with_position_string,
 82                                |path_str| {
 83                                    Ok::<_, std::convert::Infallible>(
 84                                        Path::new(path_str).to_path_buf(),
 85                                    )
 86                                },
 87                            )
 88                            .expect("Infallible");
 89                            let path = path_with_position.path_like;
 90                            if let Some(row) = path_with_position.row {
 91                                if path.is_file() {
 92                                    let row = row.saturating_sub(1);
 93                                    let col =
 94                                        path_with_position.column.unwrap_or(0).saturating_sub(1);
 95                                    caret_positions.insert(path.clone(), Point::new(row, col));
 96                                }
 97                            }
 98                            Some(path)
 99                        })
100                        .collect::<Vec<_>>()
101                };
102
103                let mut errored = false;
104
105                if let Some(open_paths_task) = cx
106                    .update(|cx| workspace2::open_paths(&paths, &app_state, None, cx))
107                    .log_err()
108                {
109                    match open_paths_task.await {
110                        Ok((workspace, items)) => {
111                            let mut item_release_futures = Vec::new();
112
113                            for (item, path) in items.into_iter().zip(&paths) {
114                                match item {
115                                    Some(Ok(mut item)) => {
116                                        if let Some(point) = caret_positions.remove(path) {
117                                            todo!()
118                                            // if let Some(active_editor) = item.downcast::<Editor>() {
119                                            //     active_editor
120                                            //         .downgrade()
121                                            //         .update(&mut cx, |editor, cx| {
122                                            //             let snapshot =
123                                            //                 editor.snapshot(cx).display_snapshot;
124                                            //             let point = snapshot
125                                            //                 .buffer_snapshot
126                                            //                 .clip_point(point, Bias::Left);
127                                            //             editor.change_selections(
128                                            //                 Some(Autoscroll::center()),
129                                            //                 cx,
130                                            //                 |s| s.select_ranges([point..point]),
131                                            //             );
132                                            //         })
133                                            //         .log_err();
134                                            // }
135                                        }
136
137                                        let released = oneshot::channel();
138                                        cx.update(move |cx| {
139                                            item.on_release(
140                                                cx,
141                                                Box::new(move |_| {
142                                                    let _ = released.0.send(());
143                                                }),
144                                            )
145                                            .detach();
146                                        })
147                                        .ok();
148                                        item_release_futures.push(released.1);
149                                    }
150                                    Some(Err(err)) => {
151                                        responses
152                                            .send(CliResponse::Stderr {
153                                                message: format!(
154                                                    "error opening {:?}: {}",
155                                                    path, err
156                                                ),
157                                            })
158                                            .log_err();
159                                        errored = true;
160                                    }
161                                    None => {}
162                                }
163                            }
164
165                            if wait {
166                                let executor = cx.background_executor().clone();
167                                let wait = async move {
168                                    if paths.is_empty() {
169                                        let (done_tx, done_rx) = oneshot::channel();
170                                        let _subscription =
171                                            workspace.update(&mut cx, move |_, cx| {
172                                                cx.on_release(|_, _| {
173                                                    let _ = done_tx.send(());
174                                                })
175                                            });
176                                        let _ = done_rx.await;
177                                    } else {
178                                        let _ = futures::future::try_join_all(item_release_futures)
179                                            .await;
180                                    };
181                                }
182                                .fuse();
183                                futures::pin_mut!(wait);
184
185                                loop {
186                                    // Repeatedly check if CLI is still open to avoid wasting resources
187                                    // waiting for files or workspaces to close.
188                                    let mut timer = executor.timer(Duration::from_secs(1)).fuse();
189                                    futures::select_biased! {
190                                        _ = wait => break,
191                                        _ = timer => {
192                                            if responses.send(CliResponse::Ping).is_err() {
193                                                break;
194                                            }
195                                        }
196                                    }
197                                }
198                            }
199                        }
200                        Err(error) => {
201                            errored = true;
202                            responses
203                                .send(CliResponse::Stderr {
204                                    message: format!("error opening {:?}: {}", paths, error),
205                                })
206                                .log_err();
207                        }
208                    }
209
210                    responses
211                        .send(CliResponse::Exit {
212                            status: i32::from(errored),
213                        })
214                        .log_err();
215                }
216            }
217        }
218    }
219}
220
221pub fn build_window_options(
222    bounds: Option<WindowBounds>,
223    display_uuid: Option<Uuid>,
224    cx: &mut AppContext,
225) -> WindowOptions {
226    let bounds = bounds.unwrap_or(WindowBounds::Maximized);
227    let display = display_uuid.and_then(|uuid| {
228        cx.displays()
229            .into_iter()
230            .find(|display| display.uuid().ok() == Some(uuid))
231    });
232
233    WindowOptions {
234        bounds,
235        titlebar: Some(TitlebarOptions {
236            title: None,
237            appears_transparent: true,
238            traffic_light_position: Some(point(px(8.), px(8.))),
239        }),
240        center: false,
241        focus: false,
242        show: false,
243        kind: WindowKind::Normal,
244        is_movable: true,
245        display_id: display.map(|display| display.id()),
246    }
247}
248
249pub fn initialize_workspace(
250    workspace_handle: WeakView<Workspace>,
251    was_deserialized: bool,
252    app_state: Arc<AppState>,
253    cx: AsyncWindowContext,
254) -> Task<Result<()>> {
255    cx.spawn(|mut cx| async move {
256        workspace_handle.update(&mut cx, |workspace, cx| {
257            let workspace_handle = cx.view();
258            cx.subscribe(&workspace_handle, {
259                move |workspace, _, event, cx| {
260                    if let workspace2::Event::PaneAdded(pane) = event {
261                        pane.update(cx, |pane, cx| {
262                            pane.toolbar().update(cx, |toolbar, cx| {
263                                // todo!()
264                                //     let breadcrumbs = cx.add_view(|_| Breadcrumbs::new(workspace));
265                                //     toolbar.add_item(breadcrumbs, cx);
266                                //     let buffer_search_bar = cx.add_view(BufferSearchBar::new);
267                                //     toolbar.add_item(buffer_search_bar.clone(), cx);
268                                //     let quick_action_bar = cx.add_view(|_| {
269                                //         QuickActionBar::new(buffer_search_bar, workspace)
270                                //     });
271                                //     toolbar.add_item(quick_action_bar, cx);
272                                //     let diagnostic_editor_controls =
273                                //         cx.add_view(|_| diagnostics2::ToolbarControls::new());
274                                //     toolbar.add_item(diagnostic_editor_controls, cx);
275                                //     let project_search_bar = cx.add_view(|_| ProjectSearchBar::new());
276                                //     toolbar.add_item(project_search_bar, cx);
277                                //     let submit_feedback_button =
278                                //         cx.add_view(|_| SubmitFeedbackButton::new());
279                                //     toolbar.add_item(submit_feedback_button, cx);
280                                //     let feedback_info_text = cx.add_view(|_| FeedbackInfoText::new());
281                                //     toolbar.add_item(feedback_info_text, cx);
282                                //     let lsp_log_item =
283                                //         cx.add_view(|_| language_tools::LspLogToolbarItemView::new());
284                                //     toolbar.add_item(lsp_log_item, cx);
285                                //     let syntax_tree_item = cx
286                                //         .add_view(|_| language_tools::SyntaxTreeToolbarItemView::new());
287                                //     toolbar.add_item(syntax_tree_item, cx);
288                            })
289                        });
290                    }
291                }
292            })
293            .detach();
294
295            //     cx.emit(workspace2::Event::PaneAdded(
296            //         workspace.active_pane().clone(),
297            //     ));
298
299            //     let collab_titlebar_item =
300            //         cx.add_view(|cx| CollabTitlebarItem::new(workspace, &workspace_handle, cx));
301            //     workspace.set_titlebar_item(collab_titlebar_item.into_any(), cx);
302
303            //     let copilot =
304            //         cx.add_view(|cx| copilot_button::CopilotButton::new(app_state.fs.clone(), cx));
305            //     let diagnostic_summary =
306            //         cx.add_view(|cx| diagnostics::items::DiagnosticIndicator::new(workspace, cx));
307            //     let activity_indicator = activity_indicator::ActivityIndicator::new(
308            //         workspace,
309            //         app_state.languages.clone(),
310            //         cx,
311            //     );
312            //     let active_buffer_language =
313            //         cx.add_view(|_| language_selector::ActiveBufferLanguage::new(workspace));
314            //     let vim_mode_indicator = cx.add_view(|cx| vim::ModeIndicator::new(cx));
315            //     let feedback_button = cx.add_view(|_| {
316            //         feedback::deploy_feedback_button::DeployFeedbackButton::new(workspace)
317            //     });
318            //     let cursor_position = cx.add_view(|_| editor::items::CursorPosition::new());
319            workspace.status_bar().update(cx, |status_bar, cx| {
320                // status_bar.add_left_item(diagnostic_summary, cx);
321                // status_bar.add_left_item(activity_indicator, cx);
322
323                // status_bar.add_right_item(feedback_button, cx);
324                // status_bar.add_right_item(copilot, cx);
325                // status_bar.add_right_item(active_buffer_language, cx);
326                // status_bar.add_right_item(vim_mode_indicator, cx);
327                // status_bar.add_right_item(cursor_position, cx);
328            });
329
330            //     auto_update::notify_of_any_new_update(cx.weak_handle(), cx);
331
332            //     vim::observe_keystrokes(cx);
333
334            //     cx.on_window_should_close(|workspace, cx| {
335            //         if let Some(task) = workspace.close(&Default::default(), cx) {
336            //             task.detach_and_log_err(cx);
337            //         }
338            //         false
339            //     });
340            // })?;
341
342            // let project_panel = ProjectPanel::load(workspace_handle.clone(), cx.clone());
343            // let terminal_panel = TerminalPanel::load(workspace_handle.clone(), cx.clone());
344            // let assistant_panel = AssistantPanel::load(workspace_handle.clone(), cx.clone());
345            // let channels_panel =
346            //     collab_ui::collab_panel::CollabPanel::load(workspace_handle.clone(), cx.clone());
347            // let chat_panel =
348            //     collab_ui::chat_panel::ChatPanel::load(workspace_handle.clone(), cx.clone());
349            // let notification_panel = collab_ui::notification_panel::NotificationPanel::load(
350            //     workspace_handle.clone(),
351            //     cx.clone(),
352            // );
353            // let (
354            //     project_panel,
355            //     terminal_panel,
356            //     assistant_panel,
357            //     channels_panel,
358            //     chat_panel,
359            //     notification_panel,
360            // ) = futures::try_join!(
361            //     project_panel,
362            //     terminal_panel,
363            //     assistant_panel,
364            //     channels_panel,
365            //     chat_panel,
366            //     notification_panel,
367            // )?;
368            // workspace_handle.update(&mut cx, |workspace, cx| {
369            //     let project_panel_position = project_panel.position(cx);
370            //     workspace.add_panel_with_extra_event_handler(
371            //         project_panel,
372            //         cx,
373            //         |workspace, _, event, cx| match event {
374            //             project_panel::Event::NewSearchInDirectory { dir_entry } => {
375            //                 search::ProjectSearchView::new_search_in_directory(workspace, dir_entry, cx)
376            //             }
377            //             project_panel::Event::ActivatePanel => {
378            //                 workspace.focus_panel::<ProjectPanel>(cx);
379            //             }
380            //             _ => {}
381            //         },
382            //     );
383            //     workspace.add_panel(terminal_panel, cx);
384            //     workspace.add_panel(assistant_panel, cx);
385            //     workspace.add_panel(channels_panel, cx);
386            //     workspace.add_panel(chat_panel, cx);
387            //     workspace.add_panel(notification_panel, cx);
388
389            //     if !was_deserialized
390            //         && workspace
391            //             .project()
392            //             .read(cx)
393            //             .visible_worktrees(cx)
394            //             .any(|tree| {
395            //                 tree.read(cx)
396            //                     .root_entry()
397            //                     .map_or(false, |entry| entry.is_dir())
398            //             })
399            //     {
400            //         workspace.toggle_dock(project_panel_position, cx);
401            //     }
402            //     cx.focus_self();
403        })?;
404        Ok(())
405    })
406}