Restore editor state on reopen (#27672)
Closes https://github.com/zed-industries/zed/issues/11626 Part of https://github.com/zed-industries/zed/issues/12853 `"restore_on_file_reopen": true` in workspace settings can now be used to enable and disable editor data between file reopens in the same pane: https://github.com/user-attachments/assets/8d938ee1-d854-42a8-bbc3-2a4e4d7d5933 The settings are generic and panes' data store can be extended for further entities, beyond editors. --------------- Impl details: Currently, the project entry IDs seem to be stable across file reopens, unlike BufferIds, so those were used. Originally, the DB data was considered over in-memory one as editors serialize their state anyway, but managing and exposing PaneIds out of the DB is quite tedious and joining the DB data otherwise is not possible. Release Notes: - Started to restore editor state on reopen
This commit is contained in:
parent
bbd1e628f0
commit
e11e7df724
13 changed files with 611 additions and 48 deletions
|
@ -1597,6 +1597,17 @@ impl Editor {
|
|||
}
|
||||
this.tasks_update_task = Some(this.refresh_runnables(window, cx));
|
||||
this._subscriptions.extend(project_subscriptions);
|
||||
this._subscriptions
|
||||
.push(cx.subscribe_self(|editor, e: &EditorEvent, cx| {
|
||||
if let EditorEvent::SelectionsChanged { local } = e {
|
||||
if *local {
|
||||
let new_anchor = editor.scroll_manager.anchor();
|
||||
editor.update_restoration_data(cx, move |data| {
|
||||
data.scroll_anchor = new_anchor;
|
||||
});
|
||||
}
|
||||
}
|
||||
}));
|
||||
|
||||
this.end_selection(window, cx);
|
||||
this.scroll_manager.show_scrollbars(window, cx);
|
||||
|
@ -2317,18 +2328,24 @@ impl Editor {
|
|||
if selections.len() == 1 {
|
||||
cx.emit(SearchEvent::ActiveMatchChanged)
|
||||
}
|
||||
if local
|
||||
&& self.is_singleton(cx)
|
||||
&& WorkspaceSettings::get(None, cx).restore_on_startup != RestoreOnStartupBehavior::None
|
||||
{
|
||||
if let Some(workspace_id) = self.workspace.as_ref().and_then(|workspace| workspace.1) {
|
||||
let background_executor = cx.background_executor().clone();
|
||||
let editor_id = cx.entity().entity_id().as_u64() as ItemId;
|
||||
let snapshot = self.buffer().read(cx).snapshot(cx);
|
||||
let selections = selections.clone();
|
||||
self.serialize_selections = cx.background_spawn(async move {
|
||||
if local && self.is_singleton(cx) {
|
||||
let inmemory_selections = selections.iter().map(|s| s.range()).collect();
|
||||
self.update_restoration_data(cx, |data| {
|
||||
data.selections = inmemory_selections;
|
||||
});
|
||||
|
||||
if WorkspaceSettings::get(None, cx).restore_on_startup != RestoreOnStartupBehavior::None
|
||||
{
|
||||
if let Some(workspace_id) =
|
||||
self.workspace.as_ref().and_then(|workspace| workspace.1)
|
||||
{
|
||||
let snapshot = self.buffer().read(cx).snapshot(cx);
|
||||
let selections = selections.clone();
|
||||
let background_executor = cx.background_executor().clone();
|
||||
let editor_id = cx.entity().entity_id().as_u64() as ItemId;
|
||||
self.serialize_selections = cx.background_spawn(async move {
|
||||
background_executor.timer(SERIALIZATION_THROTTLE_TIME).await;
|
||||
let selections = selections
|
||||
let db_selections = selections
|
||||
.iter()
|
||||
.map(|selection| {
|
||||
(
|
||||
|
@ -2338,11 +2355,12 @@ impl Editor {
|
|||
})
|
||||
.collect();
|
||||
|
||||
DB.save_editor_selections(editor_id, workspace_id, selections)
|
||||
DB.save_editor_selections(editor_id, workspace_id, db_selections)
|
||||
.await
|
||||
.with_context(|| format!("persisting editor selections for editor {editor_id}, workspace {workspace_id:?}"))
|
||||
.log_err();
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2356,13 +2374,24 @@ impl Editor {
|
|||
return;
|
||||
}
|
||||
|
||||
let snapshot = self.buffer().read(cx).snapshot(cx);
|
||||
let inmemory_folds = self.display_map.update(cx, |display_map, cx| {
|
||||
display_map
|
||||
.snapshot(cx)
|
||||
.folds_in_range(0..snapshot.len())
|
||||
.map(|fold| fold.range.deref().clone())
|
||||
.collect()
|
||||
});
|
||||
self.update_restoration_data(cx, |data| {
|
||||
data.folds = inmemory_folds;
|
||||
});
|
||||
|
||||
let Some(workspace_id) = self.workspace.as_ref().and_then(|workspace| workspace.1) else {
|
||||
return;
|
||||
};
|
||||
let background_executor = cx.background_executor().clone();
|
||||
let editor_id = cx.entity().entity_id().as_u64() as ItemId;
|
||||
let snapshot = self.buffer().read(cx).snapshot(cx);
|
||||
let folds = self.display_map.update(cx, |display_map, cx| {
|
||||
let db_folds = self.display_map.update(cx, |display_map, cx| {
|
||||
display_map
|
||||
.snapshot(cx)
|
||||
.folds_in_range(0..snapshot.len())
|
||||
|
@ -2376,7 +2405,7 @@ impl Editor {
|
|||
});
|
||||
self.serialize_folds = cx.background_spawn(async move {
|
||||
background_executor.timer(SERIALIZATION_THROTTLE_TIME).await;
|
||||
DB.save_editor_folds(editor_id, workspace_id, folds)
|
||||
DB.save_editor_folds(editor_id, workspace_id, db_folds)
|
||||
.await
|
||||
.with_context(|| format!("persisting editor folds for editor {editor_id}, workspace {workspace_id:?}"))
|
||||
.log_err();
|
||||
|
@ -17454,19 +17483,6 @@ impl Editor {
|
|||
{
|
||||
let buffer_snapshot = OnceCell::new();
|
||||
|
||||
if let Some(selections) = DB.get_editor_selections(item_id, workspace_id).log_err() {
|
||||
if !selections.is_empty() {
|
||||
let snapshot =
|
||||
buffer_snapshot.get_or_init(|| self.buffer.read(cx).snapshot(cx));
|
||||
self.change_selections(None, window, cx, |s| {
|
||||
s.select_ranges(selections.into_iter().map(|(start, end)| {
|
||||
snapshot.clip_offset(start, Bias::Left)
|
||||
..snapshot.clip_offset(end, Bias::Right)
|
||||
}));
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
if let Some(folds) = DB.get_editor_folds(item_id, workspace_id).log_err() {
|
||||
if !folds.is_empty() {
|
||||
let snapshot =
|
||||
|
@ -17485,6 +17501,19 @@ impl Editor {
|
|||
);
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(selections) = DB.get_editor_selections(item_id, workspace_id).log_err() {
|
||||
if !selections.is_empty() {
|
||||
let snapshot =
|
||||
buffer_snapshot.get_or_init(|| self.buffer.read(cx).snapshot(cx));
|
||||
self.change_selections(None, window, cx, |s| {
|
||||
s.select_ranges(selections.into_iter().map(|(start, end)| {
|
||||
snapshot.clip_offset(start, Bias::Left)
|
||||
..snapshot.clip_offset(end, Bias::Right)
|
||||
}));
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
self.read_scroll_position_from_db(item_id, workspace_id, window, cx);
|
||||
|
|
|
@ -52,7 +52,7 @@ use util::{
|
|||
};
|
||||
use workspace::{
|
||||
item::{FollowEvent, FollowableItem, Item, ItemHandle},
|
||||
NavigationEntry, ViewId,
|
||||
CloseAllItems, CloseInactiveItems, NavigationEntry, ViewId,
|
||||
};
|
||||
|
||||
#[gpui::test]
|
||||
|
@ -18284,6 +18284,396 @@ async fn test_apply_code_lens_actions_with_commands(cx: &mut gpui::TestAppContex
|
|||
});
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
async fn test_editor_restore_data_different_in_panes(cx: &mut TestAppContext) {
|
||||
init_test(cx, |_| {});
|
||||
|
||||
let fs = FakeFs::new(cx.executor());
|
||||
let main_text = r#"fn main() {
|
||||
println!("1");
|
||||
println!("2");
|
||||
println!("3");
|
||||
println!("4");
|
||||
println!("5");
|
||||
}"#;
|
||||
let lib_text = "mod foo {}";
|
||||
fs.insert_tree(
|
||||
path!("/a"),
|
||||
json!({
|
||||
"lib.rs": lib_text,
|
||||
"main.rs": main_text,
|
||||
}),
|
||||
)
|
||||
.await;
|
||||
|
||||
let project = Project::test(fs, [path!("/a").as_ref()], cx).await;
|
||||
let (workspace, cx) =
|
||||
cx.add_window_view(|window, cx| Workspace::test_new(project.clone(), window, cx));
|
||||
let worktree_id = workspace.update(cx, |workspace, cx| {
|
||||
workspace.project().update(cx, |project, cx| {
|
||||
project.worktrees(cx).next().unwrap().read(cx).id()
|
||||
})
|
||||
});
|
||||
|
||||
let expected_ranges = vec![
|
||||
Point::new(0, 0)..Point::new(0, 0),
|
||||
Point::new(1, 0)..Point::new(1, 1),
|
||||
Point::new(2, 0)..Point::new(2, 2),
|
||||
Point::new(3, 0)..Point::new(3, 3),
|
||||
];
|
||||
|
||||
let pane_1 = workspace.update(cx, |workspace, _| workspace.active_pane().clone());
|
||||
let editor_1 = workspace
|
||||
.update_in(cx, |workspace, window, cx| {
|
||||
workspace.open_path(
|
||||
(worktree_id, "main.rs"),
|
||||
Some(pane_1.downgrade()),
|
||||
true,
|
||||
window,
|
||||
cx,
|
||||
)
|
||||
})
|
||||
.unwrap()
|
||||
.await
|
||||
.downcast::<Editor>()
|
||||
.unwrap();
|
||||
pane_1.update(cx, |pane, cx| {
|
||||
let open_editor = pane.active_item().unwrap().downcast::<Editor>().unwrap();
|
||||
open_editor.update(cx, |editor, cx| {
|
||||
assert_eq!(
|
||||
editor.display_text(cx),
|
||||
main_text,
|
||||
"Original main.rs text on initial open",
|
||||
);
|
||||
assert_eq!(
|
||||
editor
|
||||
.selections
|
||||
.all::<Point>(cx)
|
||||
.into_iter()
|
||||
.map(|s| s.range())
|
||||
.collect::<Vec<_>>(),
|
||||
vec![Point::zero()..Point::zero()],
|
||||
"Default selections on initial open",
|
||||
);
|
||||
})
|
||||
});
|
||||
editor_1.update_in(cx, |editor, window, cx| {
|
||||
editor.change_selections(None, window, cx, |s| {
|
||||
s.select_ranges(expected_ranges.clone());
|
||||
});
|
||||
});
|
||||
|
||||
let pane_2 = workspace.update_in(cx, |workspace, window, cx| {
|
||||
workspace.split_pane(pane_1.clone(), SplitDirection::Right, window, cx)
|
||||
});
|
||||
let editor_2 = workspace
|
||||
.update_in(cx, |workspace, window, cx| {
|
||||
workspace.open_path(
|
||||
(worktree_id, "main.rs"),
|
||||
Some(pane_2.downgrade()),
|
||||
true,
|
||||
window,
|
||||
cx,
|
||||
)
|
||||
})
|
||||
.unwrap()
|
||||
.await
|
||||
.downcast::<Editor>()
|
||||
.unwrap();
|
||||
pane_2.update(cx, |pane, cx| {
|
||||
let open_editor = pane.active_item().unwrap().downcast::<Editor>().unwrap();
|
||||
open_editor.update(cx, |editor, cx| {
|
||||
assert_eq!(
|
||||
editor.display_text(cx),
|
||||
main_text,
|
||||
"Original main.rs text on initial open in another panel",
|
||||
);
|
||||
assert_eq!(
|
||||
editor
|
||||
.selections
|
||||
.all::<Point>(cx)
|
||||
.into_iter()
|
||||
.map(|s| s.range())
|
||||
.collect::<Vec<_>>(),
|
||||
vec![Point::zero()..Point::zero()],
|
||||
"Default selections on initial open in another panel",
|
||||
);
|
||||
})
|
||||
});
|
||||
|
||||
editor_2.update_in(cx, |editor, window, cx| {
|
||||
editor.fold_ranges(expected_ranges.clone(), false, window, cx);
|
||||
});
|
||||
|
||||
let _other_editor_1 = workspace
|
||||
.update_in(cx, |workspace, window, cx| {
|
||||
workspace.open_path(
|
||||
(worktree_id, "lib.rs"),
|
||||
Some(pane_1.downgrade()),
|
||||
true,
|
||||
window,
|
||||
cx,
|
||||
)
|
||||
})
|
||||
.unwrap()
|
||||
.await
|
||||
.downcast::<Editor>()
|
||||
.unwrap();
|
||||
pane_1
|
||||
.update_in(cx, |pane, window, cx| {
|
||||
pane.close_inactive_items(&CloseInactiveItems::default(), window, cx)
|
||||
.unwrap()
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
drop(editor_1);
|
||||
pane_1.update(cx, |pane, cx| {
|
||||
pane.active_item()
|
||||
.unwrap()
|
||||
.downcast::<Editor>()
|
||||
.unwrap()
|
||||
.update(cx, |editor, cx| {
|
||||
assert_eq!(
|
||||
editor.display_text(cx),
|
||||
lib_text,
|
||||
"Other file should be open and active",
|
||||
);
|
||||
});
|
||||
assert_eq!(pane.items().count(), 1, "No other editors should be open");
|
||||
});
|
||||
|
||||
let _other_editor_2 = workspace
|
||||
.update_in(cx, |workspace, window, cx| {
|
||||
workspace.open_path(
|
||||
(worktree_id, "lib.rs"),
|
||||
Some(pane_2.downgrade()),
|
||||
true,
|
||||
window,
|
||||
cx,
|
||||
)
|
||||
})
|
||||
.unwrap()
|
||||
.await
|
||||
.downcast::<Editor>()
|
||||
.unwrap();
|
||||
pane_2
|
||||
.update_in(cx, |pane, window, cx| {
|
||||
pane.close_inactive_items(&CloseInactiveItems::default(), window, cx)
|
||||
.unwrap()
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
drop(editor_2);
|
||||
pane_2.update(cx, |pane, cx| {
|
||||
let open_editor = pane.active_item().unwrap().downcast::<Editor>().unwrap();
|
||||
open_editor.update(cx, |editor, cx| {
|
||||
assert_eq!(
|
||||
editor.display_text(cx),
|
||||
lib_text,
|
||||
"Other file should be open and active in another panel too",
|
||||
);
|
||||
});
|
||||
assert_eq!(
|
||||
pane.items().count(),
|
||||
1,
|
||||
"No other editors should be open in another pane",
|
||||
);
|
||||
});
|
||||
|
||||
let _editor_1_reopened = workspace
|
||||
.update_in(cx, |workspace, window, cx| {
|
||||
workspace.open_path(
|
||||
(worktree_id, "main.rs"),
|
||||
Some(pane_1.downgrade()),
|
||||
true,
|
||||
window,
|
||||
cx,
|
||||
)
|
||||
})
|
||||
.unwrap()
|
||||
.await
|
||||
.downcast::<Editor>()
|
||||
.unwrap();
|
||||
let _editor_2_reopened = workspace
|
||||
.update_in(cx, |workspace, window, cx| {
|
||||
workspace.open_path(
|
||||
(worktree_id, "main.rs"),
|
||||
Some(pane_2.downgrade()),
|
||||
true,
|
||||
window,
|
||||
cx,
|
||||
)
|
||||
})
|
||||
.unwrap()
|
||||
.await
|
||||
.downcast::<Editor>()
|
||||
.unwrap();
|
||||
pane_1.update(cx, |pane, cx| {
|
||||
let open_editor = pane.active_item().unwrap().downcast::<Editor>().unwrap();
|
||||
open_editor.update(cx, |editor, cx| {
|
||||
assert_eq!(
|
||||
editor.display_text(cx),
|
||||
main_text,
|
||||
"Previous editor in the 1st panel had no extra text manipulations and should get none on reopen",
|
||||
);
|
||||
assert_eq!(
|
||||
editor
|
||||
.selections
|
||||
.all::<Point>(cx)
|
||||
.into_iter()
|
||||
.map(|s| s.range())
|
||||
.collect::<Vec<_>>(),
|
||||
expected_ranges,
|
||||
"Previous editor in the 1st panel had selections and should get them restored on reopen",
|
||||
);
|
||||
})
|
||||
});
|
||||
pane_2.update(cx, |pane, cx| {
|
||||
let open_editor = pane.active_item().unwrap().downcast::<Editor>().unwrap();
|
||||
open_editor.update(cx, |editor, cx| {
|
||||
assert_eq!(
|
||||
editor.display_text(cx),
|
||||
r#"fn main() {
|
||||
⋯rintln!("1");
|
||||
⋯intln!("2");
|
||||
⋯ntln!("3");
|
||||
println!("4");
|
||||
println!("5");
|
||||
}"#,
|
||||
"Previous editor in the 2nd pane had folds and should restore those on reopen in the same pane",
|
||||
);
|
||||
assert_eq!(
|
||||
editor
|
||||
.selections
|
||||
.all::<Point>(cx)
|
||||
.into_iter()
|
||||
.map(|s| s.range())
|
||||
.collect::<Vec<_>>(),
|
||||
vec![Point::zero()..Point::zero()],
|
||||
"Previous editor in the 2nd pane had no selections changed hence should restore none",
|
||||
);
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
async fn test_editor_does_not_restore_data_when_turned_off(cx: &mut TestAppContext) {
|
||||
init_test(cx, |_| {});
|
||||
|
||||
let fs = FakeFs::new(cx.executor());
|
||||
let main_text = r#"fn main() {
|
||||
println!("1");
|
||||
println!("2");
|
||||
println!("3");
|
||||
println!("4");
|
||||
println!("5");
|
||||
}"#;
|
||||
let lib_text = "mod foo {}";
|
||||
fs.insert_tree(
|
||||
path!("/a"),
|
||||
json!({
|
||||
"lib.rs": lib_text,
|
||||
"main.rs": main_text,
|
||||
}),
|
||||
)
|
||||
.await;
|
||||
|
||||
let project = Project::test(fs, [path!("/a").as_ref()], cx).await;
|
||||
let (workspace, cx) =
|
||||
cx.add_window_view(|window, cx| Workspace::test_new(project.clone(), window, cx));
|
||||
let worktree_id = workspace.update(cx, |workspace, cx| {
|
||||
workspace.project().update(cx, |project, cx| {
|
||||
project.worktrees(cx).next().unwrap().read(cx).id()
|
||||
})
|
||||
});
|
||||
|
||||
let pane = workspace.update(cx, |workspace, _| workspace.active_pane().clone());
|
||||
let editor = workspace
|
||||
.update_in(cx, |workspace, window, cx| {
|
||||
workspace.open_path(
|
||||
(worktree_id, "main.rs"),
|
||||
Some(pane.downgrade()),
|
||||
true,
|
||||
window,
|
||||
cx,
|
||||
)
|
||||
})
|
||||
.unwrap()
|
||||
.await
|
||||
.downcast::<Editor>()
|
||||
.unwrap();
|
||||
pane.update(cx, |pane, cx| {
|
||||
let open_editor = pane.active_item().unwrap().downcast::<Editor>().unwrap();
|
||||
open_editor.update(cx, |editor, cx| {
|
||||
assert_eq!(
|
||||
editor.display_text(cx),
|
||||
main_text,
|
||||
"Original main.rs text on initial open",
|
||||
);
|
||||
})
|
||||
});
|
||||
editor.update_in(cx, |editor, window, cx| {
|
||||
editor.fold_ranges(vec![Point::new(0, 0)..Point::new(0, 0)], false, window, cx);
|
||||
});
|
||||
|
||||
cx.update_global(|store: &mut SettingsStore, cx| {
|
||||
store.update_user_settings::<WorkspaceSettings>(cx, |s| {
|
||||
s.restore_on_file_reopen = Some(false);
|
||||
});
|
||||
});
|
||||
editor.update_in(cx, |editor, window, cx| {
|
||||
editor.fold_ranges(
|
||||
vec![
|
||||
Point::new(1, 0)..Point::new(1, 1),
|
||||
Point::new(2, 0)..Point::new(2, 2),
|
||||
Point::new(3, 0)..Point::new(3, 3),
|
||||
],
|
||||
false,
|
||||
window,
|
||||
cx,
|
||||
);
|
||||
});
|
||||
pane.update_in(cx, |pane, window, cx| {
|
||||
pane.close_all_items(&CloseAllItems::default(), window, cx)
|
||||
.unwrap()
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
pane.update(cx, |pane, _| {
|
||||
assert!(pane.active_item().is_none());
|
||||
});
|
||||
cx.update_global(|store: &mut SettingsStore, cx| {
|
||||
store.update_user_settings::<WorkspaceSettings>(cx, |s| {
|
||||
s.restore_on_file_reopen = Some(true);
|
||||
});
|
||||
});
|
||||
|
||||
let _editor_reopened = workspace
|
||||
.update_in(cx, |workspace, window, cx| {
|
||||
workspace.open_path(
|
||||
(worktree_id, "main.rs"),
|
||||
Some(pane.downgrade()),
|
||||
true,
|
||||
window,
|
||||
cx,
|
||||
)
|
||||
})
|
||||
.unwrap()
|
||||
.await
|
||||
.downcast::<Editor>()
|
||||
.unwrap();
|
||||
pane.update(cx, |pane, cx| {
|
||||
let open_editor = pane.active_item().unwrap().downcast::<Editor>().unwrap();
|
||||
open_editor.update(cx, |editor, cx| {
|
||||
assert_eq!(
|
||||
editor.display_text(cx),
|
||||
main_text,
|
||||
"No folds: even after enabling the restoration, previous editor's data should not be saved to be used for the restoration"
|
||||
);
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
fn empty_range(row: usize, column: usize) -> Range<DisplayPoint> {
|
||||
let point = DisplayPoint::new(DisplayRow(row as u32), column as u32);
|
||||
point..point
|
||||
|
|
|
@ -6,7 +6,8 @@ use crate::{
|
|||
MultiBuffer, MultiBufferSnapshot, NavigationData, SearchWithinRange, ToPoint as _,
|
||||
};
|
||||
use anyhow::{anyhow, Context as _, Result};
|
||||
use collections::HashSet;
|
||||
use clock::Global;
|
||||
use collections::{HashMap, HashSet};
|
||||
use file_icons::FileIcons;
|
||||
use futures::future::try_join_all;
|
||||
use git::status::GitSummary;
|
||||
|
@ -21,7 +22,7 @@ use language::{
|
|||
use lsp::DiagnosticSeverity;
|
||||
use project::{
|
||||
lsp_store::FormatTrigger, project_settings::ProjectSettings, search::SearchQuery, Project,
|
||||
ProjectItem as _, ProjectPath,
|
||||
ProjectEntryId, ProjectItem as _, ProjectPath,
|
||||
};
|
||||
use rpc::proto::{self, update_view, PeerId};
|
||||
use settings::Settings;
|
||||
|
@ -29,6 +30,7 @@ use std::{
|
|||
any::TypeId,
|
||||
borrow::Cow,
|
||||
cmp::{self, Ordering},
|
||||
collections::hash_map,
|
||||
iter,
|
||||
ops::Range,
|
||||
path::Path,
|
||||
|
@ -39,9 +41,9 @@ use theme::{Theme, ThemeSettings};
|
|||
use ui::{prelude::*, IconDecorationKind};
|
||||
use util::{paths::PathExt, ResultExt, TryFutureExt};
|
||||
use workspace::{
|
||||
item::{BreadcrumbText, FollowEvent},
|
||||
item::{BreadcrumbText, FollowEvent, ProjectItemKind},
|
||||
searchable::SearchOptions,
|
||||
OpenVisible,
|
||||
OpenVisible, Pane, WorkspaceSettings,
|
||||
};
|
||||
use workspace::{
|
||||
item::{Dedup, ItemSettings, SerializableItem, TabContentParams},
|
||||
|
@ -1250,21 +1252,119 @@ impl SerializableItem for Editor {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
struct EditorRestorationData {
|
||||
entries: HashMap<ProjectEntryId, RestorationData>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct RestorationData {
|
||||
pub scroll_anchor: ScrollAnchor,
|
||||
pub folds: Vec<Range<Anchor>>,
|
||||
pub selections: Vec<Range<Anchor>>,
|
||||
pub buffer_version: Global,
|
||||
}
|
||||
|
||||
impl Default for RestorationData {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
scroll_anchor: ScrollAnchor::new(),
|
||||
folds: Vec::new(),
|
||||
selections: Vec::new(),
|
||||
buffer_version: Global::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ProjectItem for Editor {
|
||||
type Item = Buffer;
|
||||
|
||||
fn project_item_kind() -> Option<ProjectItemKind> {
|
||||
Some(ProjectItemKind("Editor"))
|
||||
}
|
||||
|
||||
fn for_project_item(
|
||||
project: Entity<Project>,
|
||||
pane: &Pane,
|
||||
buffer: Entity<Buffer>,
|
||||
window: &mut Window,
|
||||
cx: &mut Context<Self>,
|
||||
) -> Self {
|
||||
Self::for_buffer(buffer, Some(project), window, cx)
|
||||
let mut editor = Self::for_buffer(buffer.clone(), Some(project), window, cx);
|
||||
|
||||
if WorkspaceSettings::get(None, cx).restore_on_file_reopen {
|
||||
if let Some(restoration_data) = Self::project_item_kind()
|
||||
.and_then(|kind| pane.project_item_restoration_data.get(&kind))
|
||||
.and_then(|data| data.downcast_ref::<EditorRestorationData>())
|
||||
.and_then(|data| data.entries.get(&buffer.read(cx).entry_id(cx)?))
|
||||
.filter(|data| !buffer.read(cx).version.changed_since(&data.buffer_version))
|
||||
{
|
||||
editor.fold_ranges(restoration_data.folds.clone(), false, window, cx);
|
||||
if !restoration_data.selections.is_empty() {
|
||||
editor.change_selections(None, window, cx, |s| {
|
||||
s.select_ranges(restoration_data.selections.clone());
|
||||
});
|
||||
}
|
||||
editor.set_scroll_anchor(restoration_data.scroll_anchor, window, cx);
|
||||
}
|
||||
}
|
||||
|
||||
editor
|
||||
}
|
||||
}
|
||||
|
||||
impl EventEmitter<SearchEvent> for Editor {}
|
||||
|
||||
impl Editor {
|
||||
pub fn update_restoration_data(
|
||||
&self,
|
||||
cx: &mut Context<Self>,
|
||||
write: impl for<'a> FnOnce(&'a mut RestorationData) + 'static,
|
||||
) {
|
||||
if !WorkspaceSettings::get(None, cx).restore_on_file_reopen {
|
||||
return;
|
||||
}
|
||||
|
||||
let editor = cx.entity();
|
||||
cx.defer(move |cx| {
|
||||
editor.update(cx, |editor, cx| {
|
||||
let kind = Editor::project_item_kind()?;
|
||||
let pane = editor.workspace()?.read(cx).pane_for(&cx.entity())?;
|
||||
let buffer = editor.buffer().read(cx).as_singleton()?;
|
||||
let entry_id = buffer.read(cx).entry_id(cx)?;
|
||||
let buffer_version = buffer.read(cx).version();
|
||||
pane.update(cx, |pane, _| {
|
||||
let data = pane
|
||||
.project_item_restoration_data
|
||||
.entry(kind)
|
||||
.or_insert_with(|| Box::new(EditorRestorationData::default()) as Box<_>);
|
||||
let data = match data.downcast_mut::<EditorRestorationData>() {
|
||||
Some(data) => data,
|
||||
None => {
|
||||
*data = Box::new(EditorRestorationData::default());
|
||||
data.downcast_mut::<EditorRestorationData>()
|
||||
.expect("just written the type downcasted to")
|
||||
}
|
||||
};
|
||||
|
||||
let data = match data.entries.entry(entry_id) {
|
||||
hash_map::Entry::Occupied(o) => {
|
||||
if buffer_version.changed_since(&o.get().buffer_version) {
|
||||
return None;
|
||||
}
|
||||
o.into_mut()
|
||||
}
|
||||
hash_map::Entry::Vacant(v) => v.insert(RestorationData::default()),
|
||||
};
|
||||
write(data);
|
||||
data.buffer_version = buffer_version;
|
||||
Some(())
|
||||
})
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) enum BufferSearchHighlights {}
|
||||
impl SearchableItem for Editor {
|
||||
type Match = Range<Anchor>;
|
||||
|
|
|
@ -38,7 +38,7 @@ pub struct ScrollAnchor {
|
|||
}
|
||||
|
||||
impl ScrollAnchor {
|
||||
fn new() -> Self {
|
||||
pub(super) fn new() -> Self {
|
||||
Self {
|
||||
offset: gpui::Point::default(),
|
||||
anchor: Anchor::min(),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue