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