diff --git a/crates/db/src/db.rs b/crates/db/src/db.rs index 20b2ac142a..bde69fead7 100644 --- a/crates/db/src/db.rs +++ b/crates/db/src/db.rs @@ -83,7 +83,7 @@ macro_rules! connection { #[macro_export] macro_rules! sql_method { - ($id:ident() -> Result<()>: $sql:literal) => { + ($id:ident() -> Result<()>: $sql:expr) => { pub fn $id(&self) -> $crate::sqlez::anyhow::Result<()> { use $crate::anyhow::Context; @@ -94,7 +94,7 @@ macro_rules! sql_method { )) } }; - ($id:ident($($arg:ident: $arg_type:ty),+) -> Result<()>: $sql:literal) => { + ($id:ident($($arg:ident: $arg_type:ty),+) -> Result<()>: $sql:expr) => { pub fn $id(&self, $($arg: $arg_type),+) -> $crate::sqlez::anyhow::Result<()> { use $crate::anyhow::Context; @@ -106,7 +106,7 @@ macro_rules! sql_method { )) } }; - ($id:ident() -> Result>: $sql:literal) => { + ($id:ident() -> Result>: $sql:expr) => { pub fn $id(&self) -> $crate::sqlez::anyhow::Result> { use $crate::anyhow::Context; @@ -118,7 +118,7 @@ macro_rules! sql_method { )) } }; - ($id:ident($($arg:ident: $arg_type:ty),+) -> Result>: $sql:literal) => { + ($id:ident($($arg:ident: $arg_type:ty),+) -> Result>: $sql:expr) => { pub fn $id(&self, $($arg: $arg_type),+) -> $crate::sqlez::anyhow::Result> { use $crate::anyhow::Context; @@ -130,7 +130,7 @@ macro_rules! sql_method { )) } }; - ($id:ident() -> Result>: $sql:literal) => { + ($id:ident() -> Result>: $sql:expr) => { pub fn $id(&self) -> $crate::sqlez::anyhow::Result> { use $crate::anyhow::Context; @@ -142,7 +142,7 @@ macro_rules! sql_method { )) } }; - ($id:ident($($arg:ident: $arg_type:ty),+) -> Result>: $sql:literal) => { + ($id:ident($($arg:ident: $arg_type:ty),+) -> Result>: $sql:expr) => { pub fn $id(&self, $($arg: $arg_type),+) -> $crate::sqlez::anyhow::Result> { use $crate::anyhow::Context; @@ -155,4 +155,38 @@ macro_rules! sql_method { } }; + ($id:ident() -> Result<$return_type:ty>>: $sql:expr) => { + pub fn $id(&self) -> $crate::sqlez::anyhow::Result<$return_type> { + use $crate::anyhow::Context; + + self.select_row::<$return_type>($sql)?(($($arg),+)) + .context(::std::format!( + "Error in {}, select_row_bound failed to execute or parse for: {}", + ::std::stringify!($id), + ::std::stringify!($sql), + ))? + .context(::std::format!( + "Error in {}, select_row_bound expected single row result but found none for: {}", + ::std::stringify!($id), + ::std::stringify!($sql), + )) + } + }; + ($id:ident($($arg:ident: $arg_type:ty),+) -> Result<$return_type:ty>>: $sql:expr) => { + pub fn $id(&self, $($arg: $arg_type),+) -> $crate::sqlez::anyhow::Result<$return_type> { + use $crate::anyhow::Context; + + self.select_row_bound::<($($arg_type),+), $return_type>($sql)?(($($arg),+)) + .context(::std::format!( + "Error in {}, select_row_bound failed to execute or parse for: {}", + ::std::stringify!($id), + ::std::stringify!($sql), + ))? + .context(::std::format!( + "Error in {}, select_row_bound expected single row result but found none for: {}", + ::std::stringify!($id), + ::std::stringify!($sql), + )) + } + }; } diff --git a/crates/editor/src/items.rs b/crates/editor/src/items.rs index 1e695d2364..aea0d8b437 100644 --- a/crates/editor/src/items.rs +++ b/crates/editor/src/items.rs @@ -568,6 +568,7 @@ impl Item for Editor { if let Some(project_item) = project.update(cx, |project, cx| { // Look up the path with this key associated, create a self with that path let path = DB.get_path(item_id, workspace_id).ok()?; + let (worktree, path) = project.find_local_worktree(&path, cx)?; let project_path = ProjectPath { worktree_id: worktree.read(cx).id(), diff --git a/crates/editor/src/persistence.rs b/crates/editor/src/persistence.rs index b2186e2432..5747558700 100644 --- a/crates/editor/src/persistence.rs +++ b/crates/editor/src/persistence.rs @@ -1,6 +1,5 @@ use std::path::{Path, PathBuf}; -use anyhow::{Context, Result}; use db::{connection, sql_method}; use indoc::indoc; use sqlez::domain::Domain; @@ -32,16 +31,17 @@ impl Domain for Editor { } impl EditorDb { - pub fn get_path(&self, item_id: ItemId, workspace_id: WorkspaceId) -> Result { - self.select_row_bound(indoc! {" - SELECT path FROM editors - WHERE item_id = ? AND workspace_id = ?"})?((item_id, workspace_id))? - .context("Path not found for serialized editor") + sql_method! { + get_path(item_id: ItemId, workspace_id: WorkspaceId) -> Result>: + indoc! {" + SELECT path FROM editors + WHERE item_id = ? AND workspace_id = ?"} } sql_method! { save_path(item_id: ItemId, workspace_id: WorkspaceId, path: &Path) -> Result<()>: - "INSERT OR REPLACE INTO editors(item_id, workspace_id, path) - VALUES (?, ?, ?)" + indoc! {" + INSERT OR REPLACE INTO editors(item_id, workspace_id, path) + VALUES (?, ?, ?)"} } }