1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
use anyhow::{anyhow, Context, Result};
use cli::{ipc, IpcHandshake};
use cli::{ipc::IpcSender, CliRequest, CliResponse};
use editor::scroll::autoscroll::Autoscroll;
use editor::Editor;
use futures::channel::mpsc::{UnboundedReceiver, UnboundedSender};
use futures::channel::{mpsc, oneshot};
use futures::{FutureExt, SinkExt, StreamExt};
use gpui::AsyncAppContext;
use language::{Bias, Point};
use std::collections::HashMap;
use std::ffi::OsStr;
use std::os::unix::prelude::OsStrExt;
use std::path::Path;
use std::sync::atomic::Ordering;
use std::sync::Arc;
use std::thread;
use std::time::Duration;
use std::{path::PathBuf, sync::atomic::AtomicBool};
use util::channel::parse_zed_link;
use util::paths::PathLikeWithPosition;
use util::ResultExt;
use workspace::AppState;

pub enum OpenRequest {
    Paths {
        paths: Vec<PathBuf>,
    },
    CliConnection {
        connection: (mpsc::Receiver<CliRequest>, IpcSender<CliResponse>),
    },
    JoinChannel {
        channel_id: u64,
    },
    OpenChannelNotes {
        channel_id: u64,
    },
}

pub struct OpenListener {
    tx: UnboundedSender<OpenRequest>,
    pub triggered: AtomicBool,
}

impl OpenListener {
    pub fn new() -> (Self, UnboundedReceiver<OpenRequest>) {
        let (tx, rx) = mpsc::unbounded();
        (
            OpenListener {
                tx,
                triggered: AtomicBool::new(false),
            },
            rx,
        )
    }

    pub fn open_urls(&self, urls: &[String]) {
        self.triggered.store(true, Ordering::Release);
        let request = if let Some(server_name) =
            urls.first().and_then(|url| url.strip_prefix("zed-cli://"))
        {
            self.handle_cli_connection(server_name)
        } else if let Some(request_path) = urls.first().and_then(|url| parse_zed_link(url)) {
            self.handle_zed_url_scheme(request_path)
        } else {
            self.handle_file_urls(urls)
        };

        if let Some(request) = request {
            self.tx
                .unbounded_send(request)
                .map_err(|_| anyhow!("no listener for open requests"))
                .log_err();
        }
    }

    fn handle_cli_connection(&self, server_name: &str) -> Option<OpenRequest> {
        if let Some(connection) = connect_to_cli(server_name).log_err() {
            return Some(OpenRequest::CliConnection { connection });
        }

        None
    }

    fn handle_zed_url_scheme(&self, request_path: &str) -> Option<OpenRequest> {
        let mut parts = request_path.split("/");
        if parts.next() == Some("channel") {
            if let Some(slug) = parts.next() {
                if let Some(id_str) = slug.split("-").last() {
                    if let Ok(channel_id) = id_str.parse::<u64>() {
                        if Some("notes") == parts.next() {
                            return Some(OpenRequest::OpenChannelNotes { channel_id });
                        } else {
                            return Some(OpenRequest::JoinChannel { channel_id });
                        }
                    }
                }
            }
        }
        log::error!("invalid zed url: {}", request_path);
        None
    }

    fn handle_file_urls(&self, urls: &[String]) -> Option<OpenRequest> {
        let paths: Vec<_> = urls
            .iter()
            .flat_map(|url| url.strip_prefix("file://"))
            .map(|url| {
                let decoded = urlencoding::decode_binary(url.as_bytes());
                PathBuf::from(OsStr::from_bytes(decoded.as_ref()))
            })
            .collect();

        Some(OpenRequest::Paths { paths })
    }
}

fn connect_to_cli(
    server_name: &str,
) -> Result<(mpsc::Receiver<CliRequest>, IpcSender<CliResponse>)> {
    let handshake_tx = cli::ipc::IpcSender::<IpcHandshake>::connect(server_name.to_string())
        .context("error connecting to cli")?;
    let (request_tx, request_rx) = ipc::channel::<CliRequest>()?;
    let (response_tx, response_rx) = ipc::channel::<CliResponse>()?;

    handshake_tx
        .send(IpcHandshake {
            requests: request_tx,
            responses: response_rx,
        })
        .context("error sending ipc handshake")?;

    let (mut async_request_tx, async_request_rx) =
        futures::channel::mpsc::channel::<CliRequest>(16);
    thread::spawn(move || {
        while let Ok(cli_request) = request_rx.recv() {
            if smol::block_on(async_request_tx.send(cli_request)).is_err() {
                break;
            }
        }
        Ok::<_, anyhow::Error>(())
    });

    Ok((async_request_rx, response_tx))
}

pub async fn handle_cli_connection(
    (mut requests, responses): (mpsc::Receiver<CliRequest>, IpcSender<CliResponse>),
    app_state: Arc<AppState>,
    mut cx: AsyncAppContext,
) {
    if let Some(request) = requests.next().await {
        match request {
            CliRequest::Open { paths, wait } => {
                let mut caret_positions = HashMap::new();

                let paths = if paths.is_empty() {
                    workspace::last_opened_workspace_paths()
                        .await
                        .map(|location| location.paths().to_vec())
                        .unwrap_or_default()
                } else {
                    paths
                        .into_iter()
                        .filter_map(|path_with_position_string| {
                            let path_with_position = PathLikeWithPosition::parse_str(
                                &path_with_position_string,
                                |path_str| {
                                    Ok::<_, std::convert::Infallible>(
                                        Path::new(path_str).to_path_buf(),
                                    )
                                },
                            )
                            .expect("Infallible");
                            let path = path_with_position.path_like;
                            if let Some(row) = path_with_position.row {
                                if path.is_file() {
                                    let row = row.saturating_sub(1);
                                    let col =
                                        path_with_position.column.unwrap_or(0).saturating_sub(1);
                                    caret_positions.insert(path.clone(), Point::new(row, col));
                                }
                            }
                            Some(path)
                        })
                        .collect()
                };

                let mut errored = false;

                match cx.update(|cx| workspace::open_paths(&paths, &app_state, None, cx)) {
                    Ok(task) => match task.await {
                        Ok((workspace, items)) => {
                            let mut item_release_futures = Vec::new();

                            for (item, path) in items.into_iter().zip(&paths) {
                                match item {
                                    Some(Ok(item)) => {
                                        if let Some(point) = caret_positions.remove(path) {
                                            if let Some(active_editor) = item.downcast::<Editor>() {
                                                workspace
                                                    .update(&mut cx, |_, cx| {
                                                        active_editor.update(cx, |editor, cx| {
                                                            let snapshot = editor
                                                                .snapshot(cx)
                                                                .display_snapshot;
                                                            let point = snapshot
                                                                .buffer_snapshot
                                                                .clip_point(point, Bias::Left);
                                                            editor.change_selections(
                                                                Some(Autoscroll::center()),
                                                                cx,
                                                                |s| s.select_ranges([point..point]),
                                                            );
                                                        });
                                                    })
                                                    .log_err();
                                            }
                                        }

                                        cx.update(|cx| {
                                            let released = oneshot::channel();
                                            item.on_release(
                                                cx,
                                                Box::new(move |_| {
                                                    let _ = released.0.send(());
                                                }),
                                            )
                                            .detach();
                                            item_release_futures.push(released.1);
                                        })
                                        .log_err();
                                    }
                                    Some(Err(err)) => {
                                        responses
                                            .send(CliResponse::Stderr {
                                                message: format!(
                                                    "error opening {:?}: {}",
                                                    path, err
                                                ),
                                            })
                                            .log_err();
                                        errored = true;
                                    }
                                    None => {}
                                }
                            }

                            if wait {
                                let background = cx.background_executor().clone();
                                let wait = async move {
                                    if paths.is_empty() {
                                        let (done_tx, done_rx) = oneshot::channel();
                                        let _subscription =
                                            workspace.update(&mut cx, |workspace, cx| {
                                                cx.on_release(move |_, _| {
                                                    let _ = done_tx.send(());
                                                })
                                            });
                                        let _ = done_rx.await;
                                    } else {
                                        let _ = futures::future::try_join_all(item_release_futures)
                                            .await;
                                    };
                                }
                                .fuse();
                                futures::pin_mut!(wait);

                                loop {
                                    // Repeatedly check if CLI is still open to avoid wasting resources
                                    // waiting for files or workspaces to close.
                                    let mut timer = background.timer(Duration::from_secs(1)).fuse();
                                    futures::select_biased! {
                                        _ = wait => break,
                                        _ = timer => {
                                            if responses.send(CliResponse::Ping).is_err() {
                                                break;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        Err(error) => {
                            errored = true;
                            responses
                                .send(CliResponse::Stderr {
                                    message: format!("error opening {:?}: {}", paths, error),
                                })
                                .log_err();
                        }
                    },
                    Err(_) => errored = true,
                }

                responses
                    .send(CliResponse::Exit {
                        status: i32::from(errored),
                    })
                    .log_err();
            }
        }
    }
}