zed2.rs

  1mod assets;
  2pub mod languages;
  3mod only_instance;
  4mod open_listener;
  5
  6pub use assets::*;
  7use collections::HashMap;
  8use gpui2::{
  9    point, px, AsyncAppContext, Point, Styled, TitlebarOptions, WindowBounds, WindowKind,
 10    WindowOptions,
 11};
 12pub use only_instance::*;
 13pub use open_listener::*;
 14
 15use anyhow::{Context, Result};
 16use cli::{
 17    ipc::{self, IpcSender},
 18    CliRequest, CliResponse, IpcHandshake,
 19};
 20use futures::{
 21    channel::{mpsc, oneshot},
 22    FutureExt, SinkExt, StreamExt,
 23};
 24use std::{path::Path, sync::Arc, thread, time::Duration};
 25use util::{paths::PathLikeWithPosition, ResultExt};
 26use uuid::Uuid;
 27use workspace2::AppState;
 28
 29pub fn connect_to_cli(
 30    server_name: &str,
 31) -> Result<(mpsc::Receiver<CliRequest>, IpcSender<CliResponse>)> {
 32    let handshake_tx = cli::ipc::IpcSender::<IpcHandshake>::connect(server_name.to_string())
 33        .context("error connecting to cli")?;
 34    let (request_tx, request_rx) = ipc::channel::<CliRequest>()?;
 35    let (response_tx, response_rx) = ipc::channel::<CliResponse>()?;
 36
 37    handshake_tx
 38        .send(IpcHandshake {
 39            requests: request_tx,
 40            responses: response_rx,
 41        })
 42        .context("error sending ipc handshake")?;
 43
 44    let (mut async_request_tx, async_request_rx) =
 45        futures::channel::mpsc::channel::<CliRequest>(16);
 46    thread::spawn(move || {
 47        while let Ok(cli_request) = request_rx.recv() {
 48            if smol::block_on(async_request_tx.send(cli_request)).is_err() {
 49                break;
 50            }
 51        }
 52        Ok::<_, anyhow::Error>(())
 53    });
 54
 55    Ok((async_request_rx, response_tx))
 56}
 57
 58pub async fn handle_cli_connection(
 59    (mut requests, responses): (mpsc::Receiver<CliRequest>, IpcSender<CliResponse>),
 60    app_state: Arc<AppState>,
 61    mut cx: AsyncAppContext,
 62) {
 63    if let Some(request) = requests.next().await {
 64        match request {
 65            CliRequest::Open { paths, wait } => {
 66                let mut caret_positions = HashMap::default();
 67
 68                let paths = if paths.is_empty() {
 69                    todo!()
 70                    //     workspace::last_opened_workspace_paths()
 71                    //         .await
 72                    //         .map(|location| location.paths().to_vec())
 73                    //         .unwrap_or_default()
 74                } else {
 75                    paths
 76                        .into_iter()
 77                        .filter_map(|path_with_position_string| {
 78                            let path_with_position = PathLikeWithPosition::parse_str(
 79                                &path_with_position_string,
 80                                |path_str| {
 81                                    Ok::<_, std::convert::Infallible>(
 82                                        Path::new(path_str).to_path_buf(),
 83                                    )
 84                                },
 85                            )
 86                            .expect("Infallible");
 87                            let path = path_with_position.path_like;
 88                            if let Some(row) = path_with_position.row {
 89                                if path.is_file() {
 90                                    let row = row.saturating_sub(1);
 91                                    let col =
 92                                        path_with_position.column.unwrap_or(0).saturating_sub(1);
 93                                    caret_positions.insert(path.clone(), Point::new(row, col));
 94                                }
 95                            }
 96                            Some(path)
 97                        })
 98                        .collect::<Vec<_>>()
 99                };
100
101                let mut errored = false;
102
103                if let Some(open_paths_task) = cx
104                    .update(|cx| workspace2::open_paths(&paths, &app_state, None, cx))
105                    .log_err()
106                {
107                    match open_paths_task.await {
108                        Ok((workspace, items)) => {
109                            let mut item_release_futures = Vec::new();
110
111                            for (item, path) in items.into_iter().zip(&paths) {
112                                match item {
113                                    Some(Ok(mut item)) => {
114                                        if let Some(point) = caret_positions.remove(path) {
115                                            todo!()
116                                            // if let Some(active_editor) = item.downcast::<Editor>() {
117                                            //     active_editor
118                                            //         .downgrade()
119                                            //         .update(&mut cx, |editor, cx| {
120                                            //             let snapshot =
121                                            //                 editor.snapshot(cx).display_snapshot;
122                                            //             let point = snapshot
123                                            //                 .buffer_snapshot
124                                            //                 .clip_point(point, Bias::Left);
125                                            //             editor.change_selections(
126                                            //                 Some(Autoscroll::center()),
127                                            //                 cx,
128                                            //                 |s| s.select_ranges([point..point]),
129                                            //             );
130                                            //         })
131                                            //         .log_err();
132                                            // }
133                                        }
134
135                                        let released = oneshot::channel();
136                                        cx.update(move |cx| {
137                                            item.on_release(
138                                                cx,
139                                                Box::new(move |_| {
140                                                    let _ = released.0.send(());
141                                                }),
142                                            )
143                                            .detach();
144                                        });
145                                        item_release_futures.push(released.1);
146                                    }
147                                    Some(Err(err)) => {
148                                        responses
149                                            .send(CliResponse::Stderr {
150                                                message: format!(
151                                                    "error opening {:?}: {}",
152                                                    path, err
153                                                ),
154                                            })
155                                            .log_err();
156                                        errored = true;
157                                    }
158                                    None => {}
159                                }
160                            }
161
162                            if wait {
163                                let executor = cx.executor().clone();
164                                let wait = async move {
165                                    if paths.is_empty() {
166                                        let (done_tx, done_rx) = oneshot::channel();
167                                        let _subscription =
168                                            cx.update_window_root(&workspace, move |_, cx| {
169                                                cx.on_release(|_, _| {
170                                                    let _ = done_tx.send(());
171                                                })
172                                            });
173                                        drop(workspace);
174                                        let _ = done_rx.await;
175                                    } else {
176                                        let _ = futures::future::try_join_all(item_release_futures)
177                                            .await;
178                                    };
179                                }
180                                .fuse();
181                                futures::pin_mut!(wait);
182
183                                loop {
184                                    // Repeatedly check if CLI is still open to avoid wasting resources
185                                    // waiting for files or workspaces to close.
186                                    let mut timer = executor.timer(Duration::from_secs(1)).fuse();
187                                    futures::select_biased! {
188                                        _ = wait => break,
189                                        _ = timer => {
190                                            if responses.send(CliResponse::Ping).is_err() {
191                                                break;
192                                            }
193                                        }
194                                    }
195                                }
196                            }
197                        }
198                        Err(error) => {
199                            errored = true;
200                            responses
201                                .send(CliResponse::Stderr {
202                                    message: format!("error opening {:?}: {}", paths, error),
203                                })
204                                .log_err();
205                        }
206                    }
207
208                    responses
209                        .send(CliResponse::Exit {
210                            status: i32::from(errored),
211                        })
212                        .log_err();
213                }
214            }
215        }
216    }
217}
218
219pub fn build_window_options(
220    bounds: Option<WindowBounds>,
221    display: Option<Uuid>,
222    platform: &dyn Platform,
223) -> WindowOptions {
224    let bounds = bounds.unwrap_or(WindowBounds::Maximized);
225    let display_id = display.and_then(|display| platform.screen_by_id(display));
226
227    WindowOptions {
228        bounds,
229        titlebar: Some(TitlebarOptions {
230            title: None,
231            appears_transparent: true,
232            traffic_light_position: Some(point(px(8.), px(8.))),
233        }),
234        center: false,
235        focus: false,
236        show: false,
237        kind: WindowKind::Normal,
238        is_movable: false,
239        display_id,
240    }
241}