1use anyhow::Result;
2use async_recursion::async_recursion;
3use collections::HashSet;
4use futures::{StreamExt as _, stream::FuturesUnordered};
5use gpui::{AppContext as _, AsyncWindowContext, Axis, Entity, Task, WeakEntity};
6use project::{Project, terminals::TerminalKind};
7use serde::{Deserialize, Serialize};
8use std::path::{Path, PathBuf};
9use ui::{App, Context, Pixels, Window};
10use util::ResultExt as _;
11
12use db::{define_connection, query, sqlez::statement::Statement, sqlez_macros::sql};
13use workspace::{
14 ItemHandle, ItemId, Member, Pane, PaneAxis, PaneGroup, SerializableItem as _, Workspace,
15 WorkspaceDb, WorkspaceId,
16};
17
18use crate::{
19 TerminalView, default_working_directory,
20 terminal_panel::{TerminalPanel, new_terminal_pane},
21};
22
23pub(crate) fn serialize_pane_group(
24 pane_group: &PaneGroup,
25 active_pane: &Entity<Pane>,
26 cx: &mut App,
27) -> SerializedPaneGroup {
28 build_serialized_pane_group(&pane_group.root, active_pane, cx)
29}
30
31fn build_serialized_pane_group(
32 pane_group: &Member,
33 active_pane: &Entity<Pane>,
34 cx: &mut App,
35) -> SerializedPaneGroup {
36 match pane_group {
37 Member::Axis(PaneAxis {
38 axis,
39 members,
40 flexes,
41 bounding_boxes: _,
42 }) => SerializedPaneGroup::Group {
43 axis: SerializedAxis(*axis),
44 children: members
45 .iter()
46 .map(|member| build_serialized_pane_group(member, active_pane, cx))
47 .collect::<Vec<_>>(),
48 flexes: Some(flexes.lock().clone()),
49 },
50 Member::Pane(pane_handle) => {
51 SerializedPaneGroup::Pane(serialize_pane(pane_handle, pane_handle == active_pane, cx))
52 }
53 }
54}
55
56fn serialize_pane(pane: &Entity<Pane>, active: bool, cx: &mut App) -> SerializedPane {
57 let mut items_to_serialize = HashSet::default();
58 let pane = pane.read(cx);
59 let children = pane
60 .items()
61 .filter_map(|item| {
62 let terminal_view = item.act_as::<TerminalView>(cx)?;
63 if terminal_view.read(cx).terminal().read(cx).task().is_some() {
64 None
65 } else {
66 let id = item.item_id().as_u64();
67 items_to_serialize.insert(id);
68 Some(id)
69 }
70 })
71 .collect::<Vec<_>>();
72 let active_item = pane
73 .active_item()
74 .map(|item| item.item_id().as_u64())
75 .filter(|active_id| items_to_serialize.contains(active_id));
76
77 SerializedPane {
78 active,
79 children,
80 active_item,
81 }
82}
83
84pub(crate) fn deserialize_terminal_panel(
85 workspace: WeakEntity<Workspace>,
86 project: Entity<Project>,
87 database_id: WorkspaceId,
88 serialized_panel: SerializedTerminalPanel,
89 window: &mut Window,
90 cx: &mut App,
91) -> Task<anyhow::Result<Entity<TerminalPanel>>> {
92 window.spawn(cx, async move |cx| {
93 let terminal_panel = workspace.update_in(cx, |workspace, window, cx| {
94 cx.new(|cx| {
95 let mut panel = TerminalPanel::new(workspace, window, cx);
96 panel.height = serialized_panel.height.map(|h| h.round());
97 panel.width = serialized_panel.width.map(|w| w.round());
98 panel
99 })
100 })?;
101 match &serialized_panel.items {
102 SerializedItems::NoSplits(item_ids) => {
103 let items = deserialize_terminal_views(
104 database_id,
105 project,
106 workspace,
107 item_ids.as_slice(),
108 cx,
109 )
110 .await;
111 let active_item = serialized_panel.active_item_id;
112 terminal_panel.update_in(cx, |terminal_panel, window, cx| {
113 terminal_panel.active_pane.update(cx, |pane, cx| {
114 populate_pane_items(pane, items, active_item, window, cx);
115 });
116 })?;
117 }
118 SerializedItems::WithSplits(serialized_pane_group) => {
119 let center_pane = deserialize_pane_group(
120 workspace,
121 project,
122 terminal_panel.clone(),
123 database_id,
124 serialized_pane_group,
125 cx,
126 )
127 .await;
128 if let Some((center_group, active_pane)) = center_pane {
129 terminal_panel.update(cx, |terminal_panel, _| {
130 terminal_panel.center = PaneGroup::with_root(center_group);
131 terminal_panel.active_pane =
132 active_pane.unwrap_or_else(|| terminal_panel.center.first_pane());
133 })?;
134 }
135 }
136 }
137
138 Ok(terminal_panel)
139 })
140}
141
142fn populate_pane_items(
143 pane: &mut Pane,
144 items: Vec<Entity<TerminalView>>,
145 active_item: Option<u64>,
146 window: &mut Window,
147 cx: &mut Context<Pane>,
148) {
149 let mut item_index = pane.items_len();
150 let mut active_item_index = None;
151 for item in items {
152 if Some(item.item_id().as_u64()) == active_item {
153 active_item_index = Some(item_index);
154 }
155 pane.add_item(Box::new(item), false, false, None, window, cx);
156 item_index += 1;
157 }
158 if let Some(index) = active_item_index {
159 pane.activate_item(index, false, false, window, cx);
160 }
161}
162
163#[async_recursion(?Send)]
164async fn deserialize_pane_group(
165 workspace: WeakEntity<Workspace>,
166 project: Entity<Project>,
167 panel: Entity<TerminalPanel>,
168 workspace_id: WorkspaceId,
169 serialized: &SerializedPaneGroup,
170 cx: &mut AsyncWindowContext,
171) -> Option<(Member, Option<Entity<Pane>>)> {
172 match serialized {
173 SerializedPaneGroup::Group {
174 axis,
175 flexes,
176 children,
177 } => {
178 let mut current_active_pane = None;
179 let mut members = Vec::new();
180 for child in children {
181 if let Some((new_member, active_pane)) = deserialize_pane_group(
182 workspace.clone(),
183 project.clone(),
184 panel.clone(),
185 workspace_id,
186 child,
187 cx,
188 )
189 .await
190 {
191 members.push(new_member);
192 current_active_pane = current_active_pane.or(active_pane);
193 }
194 }
195
196 if members.is_empty() {
197 return None;
198 }
199
200 if members.len() == 1 {
201 return Some((members.remove(0), current_active_pane));
202 }
203
204 Some((
205 Member::Axis(PaneAxis::load(axis.0, members, flexes.clone())),
206 current_active_pane,
207 ))
208 }
209 SerializedPaneGroup::Pane(serialized_pane) => {
210 let active = serialized_pane.active;
211 let new_items = deserialize_terminal_views(
212 workspace_id,
213 project.clone(),
214 workspace.clone(),
215 serialized_pane.children.as_slice(),
216 cx,
217 )
218 .await;
219
220 let pane = panel
221 .update_in(cx, |terminal_panel, window, cx| {
222 new_terminal_pane(
223 workspace.clone(),
224 project.clone(),
225 terminal_panel.active_pane.read(cx).is_zoomed(),
226 window,
227 cx,
228 )
229 })
230 .log_err()?;
231 let active_item = serialized_pane.active_item;
232
233 let terminal = pane
234 .update_in(cx, |pane, window, cx| {
235 populate_pane_items(pane, new_items, active_item, window, cx);
236 // Avoid blank panes in splits
237 if pane.items_len() == 0 {
238 let working_directory = workspace
239 .update(cx, |workspace, cx| default_working_directory(workspace, cx))
240 .ok()
241 .flatten();
242 let kind = TerminalKind::Shell(
243 working_directory.as_deref().map(Path::to_path_buf),
244 );
245 let window = window.window_handle();
246 let terminal = project
247 .update(cx, |project, cx| project.create_terminal(kind, window, cx));
248 Some(Some(terminal))
249 } else {
250 Some(None)
251 }
252 })
253 .ok()
254 .flatten()?;
255 if let Some(terminal) = terminal {
256 let terminal = terminal.await.ok()?;
257 pane.update_in(cx, |pane, window, cx| {
258 let terminal_view = Box::new(cx.new(|cx| {
259 TerminalView::new(
260 terminal,
261 workspace.clone(),
262 Some(workspace_id),
263 project.downgrade(),
264 false,
265 window,
266 cx,
267 )
268 }));
269 pane.add_item(terminal_view, true, false, None, window, cx);
270 })
271 .ok()?;
272 }
273 Some((Member::Pane(pane.clone()), active.then_some(pane)))
274 }
275 }
276}
277
278async fn deserialize_terminal_views(
279 workspace_id: WorkspaceId,
280 project: Entity<Project>,
281 workspace: WeakEntity<Workspace>,
282 item_ids: &[u64],
283 cx: &mut AsyncWindowContext,
284) -> Vec<Entity<TerminalView>> {
285 let mut items = Vec::with_capacity(item_ids.len());
286 let mut deserialized_items = item_ids
287 .iter()
288 .map(|item_id| {
289 cx.update(|window, cx| {
290 TerminalView::deserialize(
291 project.clone(),
292 workspace.clone(),
293 workspace_id,
294 *item_id,
295 window,
296 cx,
297 )
298 })
299 .unwrap_or_else(|e| Task::ready(Err(e.context("no window present"))))
300 })
301 .collect::<FuturesUnordered<_>>();
302 while let Some(item) = deserialized_items.next().await {
303 if let Some(item) = item.log_err() {
304 items.push(item);
305 }
306 }
307 items
308}
309
310#[derive(Debug, Serialize, Deserialize)]
311pub(crate) struct SerializedTerminalPanel {
312 pub items: SerializedItems,
313 // A deprecated field, kept for backwards compatibility for the code before terminal splits were introduced.
314 pub active_item_id: Option<u64>,
315 pub width: Option<Pixels>,
316 pub height: Option<Pixels>,
317}
318
319#[derive(Debug, Serialize, Deserialize)]
320#[serde(untagged)]
321pub(crate) enum SerializedItems {
322 // The data stored before terminal splits were introduced.
323 NoSplits(Vec<u64>),
324 WithSplits(SerializedPaneGroup),
325}
326
327#[derive(Debug, Serialize, Deserialize)]
328pub(crate) enum SerializedPaneGroup {
329 Pane(SerializedPane),
330 Group {
331 axis: SerializedAxis,
332 flexes: Option<Vec<f32>>,
333 children: Vec<SerializedPaneGroup>,
334 },
335}
336
337#[derive(Debug, Serialize, Deserialize)]
338pub(crate) struct SerializedPane {
339 pub active: bool,
340 pub children: Vec<u64>,
341 pub active_item: Option<u64>,
342}
343
344#[derive(Debug)]
345pub(crate) struct SerializedAxis(pub Axis);
346
347impl Serialize for SerializedAxis {
348 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
349 where
350 S: serde::Serializer,
351 {
352 match self.0 {
353 Axis::Horizontal => serializer.serialize_str("horizontal"),
354 Axis::Vertical => serializer.serialize_str("vertical"),
355 }
356 }
357}
358
359impl<'de> Deserialize<'de> for SerializedAxis {
360 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
361 where
362 D: serde::Deserializer<'de>,
363 {
364 let s = String::deserialize(deserializer)?;
365 match s.as_str() {
366 "horizontal" => Ok(SerializedAxis(Axis::Horizontal)),
367 "vertical" => Ok(SerializedAxis(Axis::Vertical)),
368 invalid => Err(serde::de::Error::custom(format!(
369 "Invalid axis value: '{invalid}'"
370 ))),
371 }
372 }
373}
374
375define_connection! {
376 pub static ref TERMINAL_DB: TerminalDb<WorkspaceDb> =
377 &[sql!(
378 CREATE TABLE terminals (
379 workspace_id INTEGER,
380 item_id INTEGER UNIQUE,
381 working_directory BLOB,
382 PRIMARY KEY(workspace_id, item_id),
383 FOREIGN KEY(workspace_id) REFERENCES workspaces(workspace_id)
384 ON DELETE CASCADE
385 ) STRICT;
386 ),
387 // Remove the unique constraint on the item_id table
388 // SQLite doesn't have a way of doing this automatically, so
389 // we have to do this silly copying.
390 sql!(
391 CREATE TABLE terminals2 (
392 workspace_id INTEGER,
393 item_id INTEGER,
394 working_directory BLOB,
395 PRIMARY KEY(workspace_id, item_id),
396 FOREIGN KEY(workspace_id) REFERENCES workspaces(workspace_id)
397 ON DELETE CASCADE
398 ) STRICT;
399
400 INSERT INTO terminals2 (workspace_id, item_id, working_directory)
401 SELECT workspace_id, item_id, working_directory FROM terminals;
402
403 DROP TABLE terminals;
404
405 ALTER TABLE terminals2 RENAME TO terminals;
406 ),
407 sql! (
408 ALTER TABLE terminals ADD COLUMN working_directory_path TEXT;
409 UPDATE terminals SET working_directory_path = CAST(working_directory AS TEXT);
410 ),
411 ];
412}
413
414impl TerminalDb {
415 query! {
416 pub async fn update_workspace_id(
417 new_id: WorkspaceId,
418 old_id: WorkspaceId,
419 item_id: ItemId
420 ) -> Result<()> {
421 UPDATE terminals
422 SET workspace_id = ?
423 WHERE workspace_id = ? AND item_id = ?
424 }
425 }
426
427 pub async fn save_working_directory(
428 &self,
429 item_id: ItemId,
430 workspace_id: WorkspaceId,
431 working_directory: PathBuf,
432 ) -> Result<()> {
433 log::debug!(
434 "Saving working directory {working_directory:?} for item {item_id} in workspace {workspace_id:?}"
435 );
436 let query =
437 "INSERT INTO terminals(item_id, workspace_id, working_directory, working_directory_path)
438 VALUES (?1, ?2, ?3, ?4)
439 ON CONFLICT DO UPDATE SET
440 item_id = ?1,
441 workspace_id = ?2,
442 working_directory = ?3,
443 working_directory_path = ?4"
444 ;
445 self.write(move |conn| {
446 let mut statement = Statement::prepare(conn, query)?;
447 let mut next_index = statement.bind(&item_id, 1)?;
448 next_index = statement.bind(&workspace_id, next_index)?;
449 next_index = statement.bind(&working_directory, next_index)?;
450 statement.bind(&working_directory.to_string_lossy().to_string(), next_index)?;
451 statement.exec()
452 })
453 .await
454 }
455
456 query! {
457 pub fn get_working_directory(item_id: ItemId, workspace_id: WorkspaceId) -> Result<Option<PathBuf>> {
458 SELECT working_directory
459 FROM terminals
460 WHERE item_id = ? AND workspace_id = ?
461 }
462 }
463}