2577 lines · 93.1 KB · Rust
RawDownload
pub mod model; use std::{    borrow::Cow,    collections::BTreeMap,    path::{Path, PathBuf},    str::FromStr,    sync::Arc,}; use anyhow::{Context, Result, anyhow, bail};use client::DevServerProjectId;use db::{define_connection, query, sqlez::connection::Connection, sqlez_macros::sql};use gpui::{Axis, Bounds, WindowBounds, WindowId, point, size};use itertools::Itertools;use project::debugger::breakpoint_store::{BreakpointState, SourceBreakpoint}; use language::{LanguageName, Toolchain};use project::WorktreeId;use remote::ssh_session::SshProjectId;use sqlez::{    bindable::{Bind, Column, StaticColumnCount},    statement::{SqlType, Statement},}; use ui::px;use util::{ResultExt, maybe};use uuid::Uuid; use crate::WorkspaceId; use model::{    GroupId, LocalPaths, PaneId, SerializedItem, SerializedPane, SerializedPaneGroup,    SerializedSshProject, SerializedWorkspace,}; use self::model::{DockStructure, LocalPathsOrder, SerializedWorkspaceLocation}; #[derive(Copy, Clone, Debug, PartialEq)]pub(crate) struct SerializedAxis(pub(crate) gpui::Axis);impl sqlez::bindable::StaticColumnCount for SerializedAxis {}impl sqlez::bindable::Bind for SerializedAxis {    fn bind(        &self,        statement: &sqlez::statement::Statement,        start_index: i32,    ) -> anyhow::Result<i32> {        match self.0 {            gpui::Axis::Horizontal => "Horizontal",            gpui::Axis::Vertical => "Vertical",        }        .bind(statement, start_index)    }} impl sqlez::bindable::Column for SerializedAxis {    fn column(        statement: &mut sqlez::statement::Statement,        start_index: i32,    ) -> anyhow::Result<(Self, i32)> {        String::column(statement, start_index).and_then(|(axis_text, next_index)| {            Ok((                match axis_text.as_str() {                    "Horizontal" => Self(Axis::Horizontal),                    "Vertical" => Self(Axis::Vertical),                    _ => anyhow::bail!("Stored serialized item kind is incorrect"),                },                next_index,            ))        })    }} #[derive(Copy, Clone, Debug, PartialEq, Default)]pub(crate) struct SerializedWindowBounds(pub(crate) WindowBounds); impl StaticColumnCount for SerializedWindowBounds {    fn column_count() -> usize {        5    }} impl Bind for SerializedWindowBounds {    fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {        match self.0 {            WindowBounds::Windowed(bounds) => {                let next_index = statement.bind(&"Windowed", start_index)?;                statement.bind(                    &(                        SerializedPixels(bounds.origin.x),                        SerializedPixels(bounds.origin.y),                        SerializedPixels(bounds.size.width),                        SerializedPixels(bounds.size.height),                    ),                    next_index,                )            }            WindowBounds::Maximized(bounds) => {                let next_index = statement.bind(&"Maximized", start_index)?;                statement.bind(                    &(                        SerializedPixels(bounds.origin.x),                        SerializedPixels(bounds.origin.y),                        SerializedPixels(bounds.size.width),                        SerializedPixels(bounds.size.height),                    ),                    next_index,                )            }            WindowBounds::Fullscreen(bounds) => {                let next_index = statement.bind(&"FullScreen", start_index)?;                statement.bind(                    &(                        SerializedPixels(bounds.origin.x),                        SerializedPixels(bounds.origin.y),                        SerializedPixels(bounds.size.width),                        SerializedPixels(bounds.size.height),                    ),                    next_index,                )            }        }    }} impl Column for SerializedWindowBounds {    fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {        let (window_state, next_index) = String::column(statement, start_index)?;        let ((x, y, width, height), _): ((i32, i32, i32, i32), _) =            Column::column(statement, next_index)?;        let bounds = Bounds {            origin: point(px(x as f32), px(y as f32)),            size: size(px(width as f32), px(height as f32)),        };         let status = match window_state.as_str() {            "Windowed" | "Fixed" => SerializedWindowBounds(WindowBounds::Windowed(bounds)),            "Maximized" => SerializedWindowBounds(WindowBounds::Maximized(bounds)),            "FullScreen" => SerializedWindowBounds(WindowBounds::Fullscreen(bounds)),            _ => bail!("Window State did not have a valid string"),        };         Ok((status, next_index + 4))    }} #[derive(Debug)]pub struct Breakpoint {    pub position: u32,    pub message: Option<Arc<str>>,    pub condition: Option<Arc<str>>,    pub hit_condition: Option<Arc<str>>,    pub state: BreakpointState,} /// Wrapper for DB type of a breakpointstruct BreakpointStateWrapper<'a>(Cow<'a, BreakpointState>); impl From<BreakpointState> for BreakpointStateWrapper<'static> {    fn from(kind: BreakpointState) -> Self {        BreakpointStateWrapper(Cow::Owned(kind))    }}impl StaticColumnCount for BreakpointStateWrapper<'_> {    fn column_count() -> usize {        1    }} impl Bind for BreakpointStateWrapper<'_> {    fn bind(&self, statement: &Statement, start_index: i32) -> anyhow::Result<i32> {        statement.bind(&self.0.to_int(), start_index)    }} impl Column for BreakpointStateWrapper<'_> {    fn column(statement: &mut Statement, start_index: i32) -> anyhow::Result<(Self, i32)> {        let state = statement.column_int(start_index)?;         match state {            0 => Ok((BreakpointState::Enabled.into(), start_index + 1)),            1 => Ok((BreakpointState::Disabled.into(), start_index + 1)),            _ => Err(anyhow::anyhow!("Invalid BreakpointState discriminant")),        }    }} /// This struct is used to implement traits on Vec<breakpoint>#[derive(Debug)]#[allow(dead_code)]struct Breakpoints(Vec<Breakpoint>); impl sqlez::bindable::StaticColumnCount for Breakpoint {    fn column_count() -> usize {        // Position, log message, condition message, and hit condition message        4 + BreakpointStateWrapper::column_count()    }} impl sqlez::bindable::Bind for Breakpoint {    fn bind(        &self,        statement: &sqlez::statement::Statement,        start_index: i32,    ) -> anyhow::Result<i32> {        let next_index = statement.bind(&self.position, start_index)?;        let next_index = statement.bind(&self.message, next_index)?;        let next_index = statement.bind(&self.condition, next_index)?;        let next_index = statement.bind(&self.hit_condition, next_index)?;        statement.bind(            &BreakpointStateWrapper(Cow::Borrowed(&self.state)),            next_index,        )    }} impl Column for Breakpoint {    fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {        let position = statement            .column_int(start_index)            .with_context(|| format!("Failed to read BreakPoint at index {start_index}"))?            as u32;        let (message, next_index) = Option::<String>::column(statement, start_index + 1)?;        let (condition, next_index) = Option::<String>::column(statement, next_index)?;        let (hit_condition, next_index) = Option::<String>::column(statement, next_index)?;        let (state, next_index) = BreakpointStateWrapper::column(statement, next_index)?;         Ok((            Breakpoint {                position,                message: message.map(Arc::from),                condition: condition.map(Arc::from),                hit_condition: hit_condition.map(Arc::from),                state: state.0.into_owned(),            },            next_index,        ))    }} impl Column for Breakpoints {    fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {        let mut breakpoints = Vec::new();        let mut index = start_index;         loop {            match statement.column_type(index) {                Ok(SqlType::Null) => break,                _ => {                    let (breakpoint, next_index) = Breakpoint::column(statement, index)?;                     breakpoints.push(breakpoint);                    index = next_index;                }            }        }        Ok((Breakpoints(breakpoints), index))    }} #[derive(Clone, Debug, PartialEq)]struct SerializedPixels(gpui::Pixels);impl sqlez::bindable::StaticColumnCount for SerializedPixels {} impl sqlez::bindable::Bind for SerializedPixels {    fn bind(        &self,        statement: &sqlez::statement::Statement,        start_index: i32,    ) -> anyhow::Result<i32> {        let this: i32 = self.0.0 as i32;        this.bind(statement, start_index)    }} define_connection! {    // Current schema shape using pseudo-rust syntax:    //    // workspaces(    //   workspace_id: usize, // Primary key for workspaces    //   local_paths: Bincode<Vec<PathBuf>>,    //   local_paths_order: Bincode<Vec<usize>>,    //   dock_visible: bool, // Deprecated    //   dock_anchor: DockAnchor, // Deprecated    //   dock_pane: Option<usize>, // Deprecated    //   left_sidebar_open: boolean,    //   timestamp: String, // UTC YYYY-MM-DD HH:MM:SS    //   window_state: String, // WindowBounds Discriminant    //   window_x: Option<f32>, // WindowBounds::Fixed RectF x    //   window_y: Option<f32>, // WindowBounds::Fixed RectF y    //   window_width: Option<f32>, // WindowBounds::Fixed RectF width    //   window_height: Option<f32>, // WindowBounds::Fixed RectF height    //   display: Option<Uuid>, // Display id    //   fullscreen: Option<bool>, // Is the window fullscreen?    //   centered_layout: Option<bool>, // Is the Centered Layout mode activated?    //   session_id: Option<String>, // Session id    //   window_id: Option<u64>, // Window Id    // )    //    // pane_groups(    //   group_id: usize, // Primary key for pane_groups    //   workspace_id: usize, // References workspaces table    //   parent_group_id: Option<usize>, // None indicates that this is the root node    //   position: Option<usize>, // None indicates that this is the root node    //   axis: Option<Axis>, // 'Vertical', 'Horizontal'    //   flexes: Option<Vec<f32>>, // A JSON array of floats    // )    //    // panes(    //     pane_id: usize, // Primary key for panes    //     workspace_id: usize, // References workspaces table    //     active: bool,    // )    //    // center_panes(    //     pane_id: usize, // Primary key for center_panes    //     parent_group_id: Option<usize>, // References pane_groups. If none, this is the root    //     position: Option<usize>, // None indicates this is the root    // )    //    // CREATE TABLE items(    //     item_id: usize, // This is the item's view id, so this is not unique    //     workspace_id: usize, // References workspaces table    //     pane_id: usize, // References panes table    //     kind: String, // Indicates which view this connects to. This is the key in the item_deserializers global    //     position: usize, // Position of the item in the parent pane. This is equivalent to panes' position column    //     active: bool, // Indicates if this item is the active one in the pane    //     preview: bool // Indicates if this item is a preview item    // )    //    // CREATE TABLE breakpoints(    //      workspace_id: usize Foreign Key, // References workspace table    //      path: PathBuf, // The absolute path of the file that this breakpoint belongs to    //      breakpoint_location: Vec<u32>, // A list of the locations of breakpoints    //      kind: int, // The kind of breakpoint (standard, log)    //      log_message: String, // log message for log breakpoints, otherwise it's Null    // )    pub static ref DB: WorkspaceDb<()> =    &[        sql!(        CREATE TABLE workspaces(            workspace_id INTEGER PRIMARY KEY,            workspace_location BLOB UNIQUE,            dock_visible INTEGER, // Deprecated. Preserving so users can downgrade Zed.            dock_anchor TEXT, // Deprecated. Preserving so users can downgrade Zed.            dock_pane INTEGER, // Deprecated.  Preserving so users can downgrade Zed.            left_sidebar_open INTEGER, // Boolean            timestamp TEXT DEFAULT CURRENT_TIMESTAMP NOT NULL,            FOREIGN KEY(dock_pane) REFERENCES panes(pane_id)        ) STRICT;         CREATE TABLE pane_groups(            group_id INTEGER PRIMARY KEY,            workspace_id INTEGER NOT NULL,            parent_group_id INTEGER, // NULL indicates that this is a root node            position INTEGER, // NULL indicates that this is a root node            axis TEXT NOT NULL, // Enum: 'Vertical' / 'Horizontal'            FOREIGN KEY(workspace_id) REFERENCES workspaces(workspace_id)            ON DELETE CASCADE            ON UPDATE CASCADE,            FOREIGN KEY(parent_group_id) REFERENCES pane_groups(group_id) ON DELETE CASCADE        ) STRICT;         CREATE TABLE panes(            pane_id INTEGER PRIMARY KEY,            workspace_id INTEGER NOT NULL,            active INTEGER NOT NULL, // Boolean            FOREIGN KEY(workspace_id) REFERENCES workspaces(workspace_id)            ON DELETE CASCADE            ON UPDATE CASCADE        ) STRICT;         CREATE TABLE center_panes(            pane_id INTEGER PRIMARY KEY,            parent_group_id INTEGER, // NULL means that this is a root pane            position INTEGER, // NULL means that this is a root pane            FOREIGN KEY(pane_id) REFERENCES panes(pane_id)            ON DELETE CASCADE,            FOREIGN KEY(parent_group_id) REFERENCES pane_groups(group_id) ON DELETE CASCADE        ) STRICT;         CREATE TABLE items(            item_id INTEGER NOT NULL, // This is the item's view id, so this is not unique            workspace_id INTEGER NOT NULL,            pane_id INTEGER NOT NULL,            kind TEXT NOT NULL,            position INTEGER NOT NULL,            active INTEGER NOT NULL,            FOREIGN KEY(workspace_id) REFERENCES workspaces(workspace_id)            ON DELETE CASCADE            ON UPDATE CASCADE,            FOREIGN KEY(pane_id) REFERENCES panes(pane_id)            ON DELETE CASCADE,            PRIMARY KEY(item_id, workspace_id)        ) STRICT;    ),    sql!(        ALTER TABLE workspaces ADD COLUMN window_state TEXT;        ALTER TABLE workspaces ADD COLUMN window_x REAL;        ALTER TABLE workspaces ADD COLUMN window_y REAL;        ALTER TABLE workspaces ADD COLUMN window_width REAL;        ALTER TABLE workspaces ADD COLUMN window_height REAL;        ALTER TABLE workspaces ADD COLUMN display BLOB;    ),    // Drop foreign key constraint from workspaces.dock_pane to panes table.    sql!(        CREATE TABLE workspaces_2(            workspace_id INTEGER PRIMARY KEY,            workspace_location BLOB UNIQUE,            dock_visible INTEGER, // Deprecated. Preserving so users can downgrade Zed.            dock_anchor TEXT, // Deprecated. Preserving so users can downgrade Zed.            dock_pane INTEGER, // Deprecated.  Preserving so users can downgrade Zed.            left_sidebar_open INTEGER, // Boolean            timestamp TEXT DEFAULT CURRENT_TIMESTAMP NOT NULL,            window_state TEXT,            window_x REAL,            window_y REAL,            window_width REAL,            window_height REAL,            display BLOB        ) STRICT;        INSERT INTO workspaces_2 SELECT * FROM workspaces;        DROP TABLE workspaces;        ALTER TABLE workspaces_2 RENAME TO workspaces;    ),    // Add panels related information    sql!(        ALTER TABLE workspaces ADD COLUMN left_dock_visible INTEGER; //bool        ALTER TABLE workspaces ADD COLUMN left_dock_active_panel TEXT;        ALTER TABLE workspaces ADD COLUMN right_dock_visible INTEGER; //bool        ALTER TABLE workspaces ADD COLUMN right_dock_active_panel TEXT;        ALTER TABLE workspaces ADD COLUMN bottom_dock_visible INTEGER; //bool        ALTER TABLE workspaces ADD COLUMN bottom_dock_active_panel TEXT;    ),    // Add panel zoom persistence    sql!(        ALTER TABLE workspaces ADD COLUMN left_dock_zoom INTEGER; //bool        ALTER TABLE workspaces ADD COLUMN right_dock_zoom INTEGER; //bool        ALTER TABLE workspaces ADD COLUMN bottom_dock_zoom INTEGER; //bool    ),    // Add pane group flex data    sql!(        ALTER TABLE pane_groups ADD COLUMN flexes TEXT;    ),    // Add fullscreen field to workspace    // Deprecated, `WindowBounds` holds the fullscreen state now.    // Preserving so users can downgrade Zed.    sql!(        ALTER TABLE workspaces ADD COLUMN fullscreen INTEGER; //bool    ),    // Add preview field to items    sql!(        ALTER TABLE items ADD COLUMN preview INTEGER; //bool    ),    // Add centered_layout field to workspace    sql!(        ALTER TABLE workspaces ADD COLUMN centered_layout INTEGER; //bool    ),    sql!(        CREATE TABLE remote_projects (            remote_project_id INTEGER NOT NULL UNIQUE,            path TEXT,            dev_server_name TEXT        );        ALTER TABLE workspaces ADD COLUMN remote_project_id INTEGER;        ALTER TABLE workspaces RENAME COLUMN workspace_location TO local_paths;    ),    sql!(        DROP TABLE remote_projects;        CREATE TABLE dev_server_projects (            id INTEGER NOT NULL UNIQUE,            path TEXT,            dev_server_name TEXT        );        ALTER TABLE workspaces DROP COLUMN remote_project_id;        ALTER TABLE workspaces ADD COLUMN dev_server_project_id INTEGER;    ),    sql!(        ALTER TABLE workspaces ADD COLUMN local_paths_order BLOB;    ),    sql!(        ALTER TABLE workspaces ADD COLUMN session_id TEXT DEFAULT NULL;    ),    sql!(        ALTER TABLE workspaces ADD COLUMN window_id INTEGER DEFAULT NULL;    ),    sql!(        ALTER TABLE panes ADD COLUMN pinned_count INTEGER DEFAULT 0;    ),    sql!(        CREATE TABLE ssh_projects (            id INTEGER PRIMARY KEY,            host TEXT NOT NULL,            port INTEGER,            path TEXT NOT NULL,            user TEXT        );        ALTER TABLE workspaces ADD COLUMN ssh_project_id INTEGER REFERENCES ssh_projects(id) ON DELETE CASCADE;    ),    sql!(

Lines 1–500 of 2577.