working serialized writes with panics on failure. Everything seems to be working

This commit is contained in:
Kay Simmons 2022-11-23 01:53:58 -08:00 committed by Mikayla Maki
parent b01243109e
commit 1cc3e4820a
34 changed files with 669 additions and 312 deletions

View file

@ -81,7 +81,7 @@ use std::{
pub use sum_tree::Bias;
use theme::{DiagnosticStyle, Theme};
use util::{post_inc, ResultExt, TryFutureExt};
use workspace::{ItemNavHistory, Workspace};
use workspace::{ItemNavHistory, Workspace, WorkspaceId};
use crate::git::diff_hunk_to_display;
@ -584,6 +584,7 @@ pub struct Editor {
pending_rename: Option<RenameState>,
searchable: bool,
cursor_shape: CursorShape,
workspace_id: Option<WorkspaceId>,
keymap_context_layers: BTreeMap<TypeId, gpui::keymap::Context>,
input_enabled: bool,
leader_replica_id: Option<u16>,
@ -1137,31 +1138,6 @@ impl Editor {
cx: &mut ViewContext<Self>,
) -> Self {
let buffer = cx.add_model(|cx| MultiBuffer::singleton(buffer, cx));
// if let Some(project) = project.as_ref() {
// if let Some(file) = buffer
// .read(cx)
// .as_singleton()
// .and_then(|buffer| buffer.read(cx).file())
// .and_then(|file| file.as_local())
// {
// // let item_id = cx.weak_handle().id();
// // let workspace_id = project
// // .read(cx)
// // .visible_worktrees(cx)
// // .map(|worktree| worktree.read(cx).abs_path())
// // .collect::<Vec<_>>()
// // .into();
// let path = file.abs_path(cx);
// dbg!(&path);
// // cx.background()
// // .spawn(async move {
// // DB.save_path(item_id, workspace_id, path).log_err();
// // })
// // .detach();
// }
// }
Self::new(EditorMode::Full, buffer, project, None, cx)
}
@ -1262,6 +1238,7 @@ impl Editor {
searchable: true,
override_text_style: None,
cursor_shape: Default::default(),
workspace_id: None,
keymap_context_layers: Default::default(),
input_enabled: true,
leader_replica_id: None,

View file

@ -17,7 +17,7 @@ use std::{
path::{Path, PathBuf},
};
use text::Selection;
use util::TryFutureExt;
use util::{ResultExt, TryFutureExt};
use workspace::{
item::{FollowableItem, Item, ItemEvent, ItemHandle, ProjectItem},
searchable::{Direction, SearchEvent, SearchableItem, SearchableItemHandle},
@ -554,6 +554,43 @@ impl Item for Editor {
Some(breadcrumbs)
}
fn added_to_workspace(&mut self, workspace: &mut Workspace, cx: &mut ViewContext<Self>) {
let workspace_id = workspace.database_id();
let item_id = cx.view_id();
fn serialize(
buffer: ModelHandle<Buffer>,
workspace_id: WorkspaceId,
item_id: ItemId,
cx: &mut MutableAppContext,
) {
if let Some(file) = buffer.read(cx).file().and_then(|file| file.as_local()) {
let path = file.abs_path(cx);
cx.background()
.spawn(async move {
DB.save_path(item_id, workspace_id, path.clone())
.await
.log_err()
})
.detach();
}
}
if let Some(buffer) = self.buffer().read(cx).as_singleton() {
serialize(buffer.clone(), workspace_id, item_id, cx);
cx.subscribe(&buffer, |this, buffer, event, cx| {
if let Some(workspace_id) = this.workspace_id {
if let language::Event::FileHandleChanged = event {
serialize(buffer, workspace_id, cx.view_id(), cx);
}
}
})
.detach();
}
}
fn serialized_item_kind() -> Option<&'static str> {
Some("Editor")
}

View file

@ -1,4 +1,4 @@
use std::path::{Path, PathBuf};
use std::path::PathBuf;
use db::{connection, sql_method};
use indoc::indoc;
@ -39,7 +39,7 @@ impl EditorDb {
}
sql_method! {
save_path(item_id: ItemId, workspace_id: WorkspaceId, path: &Path) -> Result<()>:
async save_path(item_id: ItemId, workspace_id: WorkspaceId, path: PathBuf) -> Result<()>:
indoc! {"
INSERT OR REPLACE INTO editors(item_id, workspace_id, path)
VALUES (?, ?, ?)"}

View file

@ -66,6 +66,7 @@ impl<'a> EditorLspTestContext<'a> {
let (window_id, workspace) = cx.add_window(|cx| {
Workspace::new(
Default::default(),
0,
project.clone(),
|_, _| unimplemented!(),
cx,