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