1use super::{SerializedAxis, SerializedWindowBounds};
2use crate::{
3 Member, Pane, PaneAxis, SerializableItemRegistry, Workspace, WorkspaceId, item::ItemHandle,
4 path_list::PathList,
5};
6use anyhow::{Context, Result};
7use async_recursion::async_recursion;
8use collections::IndexSet;
9use db::sqlez::{
10 bindable::{Bind, Column, StaticColumnCount},
11 statement::Statement,
12};
13use gpui::{AsyncWindowContext, Entity, WeakEntity};
14
15use language::{Toolchain, ToolchainScope};
16use project::{Project, debugger::breakpoint_store::SourceBreakpoint};
17use remote::RemoteConnectionOptions;
18use std::{
19 collections::BTreeMap,
20 path::{Path, PathBuf},
21 sync::Arc,
22};
23use util::ResultExt;
24use uuid::Uuid;
25
26#[derive(
27 Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, serde::Serialize, serde::Deserialize,
28)]
29pub(crate) struct RemoteConnectionId(pub u64);
30
31#[derive(Debug, PartialEq, Eq, Clone, Copy)]
32pub(crate) enum RemoteConnectionKind {
33 Ssh,
34 Wsl,
35}
36
37#[derive(Debug, PartialEq, Clone)]
38pub enum SerializedWorkspaceLocation {
39 Local,
40 Remote(RemoteConnectionOptions),
41}
42
43impl SerializedWorkspaceLocation {
44 /// Get sorted paths
45 pub fn sorted_paths(&self) -> Arc<Vec<PathBuf>> {
46 unimplemented!()
47 }
48}
49
50#[derive(Debug, PartialEq, Clone)]
51pub(crate) struct SerializedWorkspace {
52 pub(crate) id: WorkspaceId,
53 pub(crate) location: SerializedWorkspaceLocation,
54 pub(crate) paths: PathList,
55 pub(crate) center_group: SerializedPaneGroup,
56 pub(crate) window_bounds: Option<SerializedWindowBounds>,
57 pub(crate) centered_layout: bool,
58 pub(crate) display: Option<Uuid>,
59 pub(crate) docks: DockStructure,
60 pub(crate) session_id: Option<String>,
61 pub(crate) breakpoints: BTreeMap<Arc<Path>, Vec<SourceBreakpoint>>,
62 pub(crate) user_toolchains: BTreeMap<ToolchainScope, IndexSet<Toolchain>>,
63 pub(crate) window_id: Option<u64>,
64}
65
66#[derive(Debug, PartialEq, Clone, Default)]
67pub struct DockStructure {
68 pub(crate) left: DockData,
69 pub(crate) right: DockData,
70 pub(crate) bottom: DockData,
71}
72
73impl RemoteConnectionKind {
74 pub(crate) fn serialize(&self) -> &'static str {
75 match self {
76 RemoteConnectionKind::Ssh => "ssh",
77 RemoteConnectionKind::Wsl => "wsl",
78 }
79 }
80
81 pub(crate) fn deserialize(text: &str) -> Option<Self> {
82 match text {
83 "ssh" => Some(Self::Ssh),
84 "wsl" => Some(Self::Wsl),
85 _ => None,
86 }
87 }
88}
89
90impl Column for DockStructure {
91 fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
92 let (left, next_index) = DockData::column(statement, start_index)?;
93 let (right, next_index) = DockData::column(statement, next_index)?;
94 let (bottom, next_index) = DockData::column(statement, next_index)?;
95 Ok((
96 DockStructure {
97 left,
98 right,
99 bottom,
100 },
101 next_index,
102 ))
103 }
104}
105
106impl Bind for DockStructure {
107 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
108 let next_index = statement.bind(&self.left, start_index)?;
109 let next_index = statement.bind(&self.right, next_index)?;
110 statement.bind(&self.bottom, next_index)
111 }
112}
113
114#[derive(Debug, PartialEq, Clone, Default)]
115pub struct DockData {
116 pub(crate) visible: bool,
117 pub(crate) active_panel: Option<String>,
118 pub(crate) zoom: bool,
119}
120
121impl Column for DockData {
122 fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
123 let (visible, next_index) = Option::<bool>::column(statement, start_index)?;
124 let (active_panel, next_index) = Option::<String>::column(statement, next_index)?;
125 let (zoom, next_index) = Option::<bool>::column(statement, next_index)?;
126 Ok((
127 DockData {
128 visible: visible.unwrap_or(false),
129 active_panel,
130 zoom: zoom.unwrap_or(false),
131 },
132 next_index,
133 ))
134 }
135}
136
137impl Bind for DockData {
138 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
139 let next_index = statement.bind(&self.visible, start_index)?;
140 let next_index = statement.bind(&self.active_panel, next_index)?;
141 statement.bind(&self.zoom, next_index)
142 }
143}
144
145#[derive(Debug, PartialEq, Clone)]
146pub(crate) enum SerializedPaneGroup {
147 Group {
148 axis: SerializedAxis,
149 flexes: Option<Vec<f32>>,
150 children: Vec<SerializedPaneGroup>,
151 },
152 Pane(SerializedPane),
153}
154
155#[cfg(test)]
156impl Default for SerializedPaneGroup {
157 fn default() -> Self {
158 Self::Pane(SerializedPane {
159 children: vec![SerializedItem::default()],
160 active: false,
161 pinned_count: 0,
162 })
163 }
164}
165
166impl SerializedPaneGroup {
167 #[async_recursion(?Send)]
168 pub(crate) async fn deserialize(
169 self,
170 project: &Entity<Project>,
171 workspace_id: WorkspaceId,
172 workspace: WeakEntity<Workspace>,
173 cx: &mut AsyncWindowContext,
174 ) -> Option<(
175 Member,
176 Option<Entity<Pane>>,
177 Vec<Option<Box<dyn ItemHandle>>>,
178 )> {
179 match self {
180 SerializedPaneGroup::Group {
181 axis,
182 children,
183 flexes,
184 } => {
185 let mut current_active_pane = None;
186 let mut members = Vec::new();
187 let mut items = Vec::new();
188 for child in children {
189 if let Some((new_member, active_pane, new_items)) = child
190 .deserialize(project, workspace_id, workspace.clone(), cx)
191 .await
192 {
193 members.push(new_member);
194 items.extend(new_items);
195 current_active_pane = current_active_pane.or(active_pane);
196 }
197 }
198
199 if members.is_empty() {
200 return None;
201 }
202
203 if members.len() == 1 {
204 return Some((members.remove(0), current_active_pane, items));
205 }
206
207 Some((
208 Member::Axis(PaneAxis::load(axis.0, members, flexes)),
209 current_active_pane,
210 items,
211 ))
212 }
213 SerializedPaneGroup::Pane(serialized_pane) => {
214 let pane = workspace
215 .update_in(cx, |workspace, window, cx| {
216 workspace.add_pane(window, cx).downgrade()
217 })
218 .log_err()?;
219 let active = serialized_pane.active;
220 let new_items = serialized_pane
221 .deserialize_to(project, &pane, workspace_id, workspace.clone(), cx)
222 .await
223 .context("Could not deserialize pane)")
224 .log_err()?;
225
226 if pane
227 .read_with(cx, |pane, _| pane.items_len() != 0)
228 .log_err()?
229 {
230 let pane = pane.upgrade()?;
231 Some((
232 Member::Pane(pane.clone()),
233 active.then_some(pane),
234 new_items,
235 ))
236 } else {
237 let pane = pane.upgrade()?;
238 workspace
239 .update_in(cx, |workspace, window, cx| {
240 workspace.force_remove_pane(&pane, &None, window, cx)
241 })
242 .log_err()?;
243 None
244 }
245 }
246 }
247 }
248}
249
250#[derive(Debug, PartialEq, Eq, Default, Clone)]
251pub struct SerializedPane {
252 pub(crate) active: bool,
253 pub(crate) children: Vec<SerializedItem>,
254 pub(crate) pinned_count: usize,
255}
256
257impl SerializedPane {
258 pub fn new(children: Vec<SerializedItem>, active: bool, pinned_count: usize) -> Self {
259 SerializedPane {
260 children,
261 active,
262 pinned_count,
263 }
264 }
265
266 pub async fn deserialize_to(
267 &self,
268 project: &Entity<Project>,
269 pane: &WeakEntity<Pane>,
270 workspace_id: WorkspaceId,
271 workspace: WeakEntity<Workspace>,
272 cx: &mut AsyncWindowContext,
273 ) -> Result<Vec<Option<Box<dyn ItemHandle>>>> {
274 let mut item_tasks = Vec::new();
275 let mut active_item_index = None;
276 let mut preview_item_index = None;
277 for (index, item) in self.children.iter().enumerate() {
278 let project = project.clone();
279 item_tasks.push(pane.update_in(cx, |_, window, cx| {
280 SerializableItemRegistry::deserialize(
281 &item.kind,
282 project,
283 workspace.clone(),
284 workspace_id,
285 item.item_id,
286 window,
287 cx,
288 )
289 })?);
290 if item.active {
291 active_item_index = Some(index);
292 }
293 if item.preview {
294 preview_item_index = Some(index);
295 }
296 }
297
298 let mut items = Vec::new();
299 for item_handle in futures::future::join_all(item_tasks).await {
300 let item_handle = item_handle.log_err();
301 items.push(item_handle.clone());
302
303 if let Some(item_handle) = item_handle {
304 pane.update_in(cx, |pane, window, cx| {
305 pane.add_item(item_handle.clone(), true, true, None, window, cx);
306 })?;
307 }
308 }
309
310 if let Some(active_item_index) = active_item_index {
311 pane.update_in(cx, |pane, window, cx| {
312 pane.activate_item(active_item_index, false, false, window, cx);
313 })?;
314 }
315
316 if let Some(preview_item_index) = preview_item_index {
317 pane.update(cx, |pane, cx| {
318 if let Some(item) = pane.item_for_index(preview_item_index) {
319 pane.set_preview_item_id(Some(item.item_id()), cx);
320 }
321 })?;
322 }
323 pane.update(cx, |pane, _| {
324 pane.set_pinned_count(self.pinned_count.min(items.len()));
325 })?;
326
327 anyhow::Ok(items)
328 }
329}
330
331pub type GroupId = i64;
332pub type PaneId = i64;
333pub type ItemId = u64;
334
335#[derive(Debug, PartialEq, Eq, Clone)]
336pub struct SerializedItem {
337 pub kind: Arc<str>,
338 pub item_id: ItemId,
339 pub active: bool,
340 pub preview: bool,
341}
342
343impl SerializedItem {
344 pub fn new(kind: impl AsRef<str>, item_id: ItemId, active: bool, preview: bool) -> Self {
345 Self {
346 kind: Arc::from(kind.as_ref()),
347 item_id,
348 active,
349 preview,
350 }
351 }
352}
353
354#[cfg(test)]
355impl Default for SerializedItem {
356 fn default() -> Self {
357 SerializedItem {
358 kind: Arc::from("Terminal"),
359 item_id: 100000,
360 active: false,
361 preview: false,
362 }
363 }
364}
365
366impl StaticColumnCount for SerializedItem {
367 fn column_count() -> usize {
368 4
369 }
370}
371impl Bind for &SerializedItem {
372 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
373 let next_index = statement.bind(&self.kind, start_index)?;
374 let next_index = statement.bind(&self.item_id, next_index)?;
375 let next_index = statement.bind(&self.active, next_index)?;
376 statement.bind(&self.preview, next_index)
377 }
378}
379
380impl Column for SerializedItem {
381 fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
382 let (kind, next_index) = Arc::<str>::column(statement, start_index)?;
383 let (item_id, next_index) = ItemId::column(statement, next_index)?;
384 let (active, next_index) = bool::column(statement, next_index)?;
385 let (preview, next_index) = bool::column(statement, next_index)?;
386 Ok((
387 SerializedItem {
388 kind,
389 item_id,
390 active,
391 preview,
392 },
393 next_index,
394 ))
395 }
396}