store ViewPosition and Selection directly in FileHistoryEntry

pull/9143/head
Ingrid 10 months ago
parent 677a3dd3be
commit 5f3849dc24

3
Cargo.lock generated

@ -2251,6 +2251,9 @@ name = "smallvec"
version = "1.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67"
dependencies = [
"serde",
]
[[package]]
name = "smartstring"

@ -20,7 +20,7 @@ helix-stdx = { path = "../helix-stdx" }
helix-loader = { path = "../helix-loader" }
ropey = { version = "1.6.1", default-features = false, features = ["simd"] }
smallvec = "1.13"
smallvec = { version = "1.13", features = ["serde"] }
smartstring = "1.0.1"
unicode-segmentation = "1.11"
# unicode-width is changing width definitions

@ -12,6 +12,7 @@ use crate::{
Assoc, ChangeSet, RopeGraphemes, RopeSlice,
};
use helix_stdx::rope::{self, RopeSliceExt};
use serde::{Deserialize, Serialize};
use smallvec::{smallvec, SmallVec};
use std::{borrow::Cow, iter, slice};
use tree_sitter::Node;
@ -50,7 +51,7 @@ use tree_sitter::Node;
/// single grapheme inward from the range's edge. There are a
/// variety of helper methods on `Range` for working in terms of
/// that block cursor, all of which have `cursor` in their name.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct Range {
/// The anchor of the range: the side that doesn't move when extending.
pub anchor: usize,
@ -403,7 +404,7 @@ impl From<(usize, usize)> for Range {
/// A selection consists of one or more selection ranges.
/// invariant: A selection can never be empty (always contains at least primary range).
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Selection {
ranges: SmallVec<[Range; 1]>,
primary_index: usize,

@ -15,7 +15,6 @@ use helix_view::{
graphics::Rect,
persistence, theme,
tree::Layout,
view::ViewPosition,
Align, Editor,
};
use serde_json::json;
@ -152,16 +151,11 @@ impl Application {
&config.editor
})),
handlers,
HashMap::from_iter(persistence::read_file_history().iter().map(|entry| {
(
entry.path.clone(),
ViewPosition {
anchor: entry.anchor,
horizontal_offset: entry.horizontal_offset,
vertical_offset: entry.vertical_offset,
},
)
})),
HashMap::from_iter(
persistence::read_file_history()
.into_iter()
.map(|entry| (entry.path.clone(), (entry.view_position, entry.selection))),
),
);
// TODO: do most of this in the background?

@ -1034,7 +1034,7 @@ pub struct Editor {
pub debugger_events: SelectAll<UnboundedReceiverStream<dap::Payload>>,
pub breakpoints: HashMap<PathBuf, Vec<Breakpoint>>,
pub old_file_locs: HashMap<PathBuf, ViewPosition>,
pub old_file_locs: HashMap<PathBuf, (ViewPosition, Selection)>,
pub syn_loader: Arc<ArcSwap<syntax::Loader>>,
pub theme_loader: Arc<theme::Loader>,
@ -1152,7 +1152,7 @@ impl Editor {
syn_loader: Arc<ArcSwap<syntax::Loader>>,
config: Arc<dyn DynAccess<Config>>,
handlers: Handlers,
old_file_locs: HashMap<PathBuf, ViewPosition>,
old_file_locs: HashMap<PathBuf, (ViewPosition, Selection)>,
) -> Self {
let language_servers = helix_lsp::Registry::new(syn_loader.clone());
let conf = config.load();
@ -1641,16 +1641,15 @@ impl Editor {
// initialize selection for view
let doc = doc_mut!(self, &id);
let view = self.tree.get_mut(view_id);
view.offset = self
if let Some((view_position, selection)) = self
.old_file_locs
.get(doc.path().unwrap())
.map(|x| x.to_owned())
.unwrap_or_default();
doc.set_selection(
view_id,
Selection::single(view.offset.anchor, view.offset.anchor),
);
{
let view = self.tree.get_mut(view_id);
view.offset = view_position;
doc.set_selection(view_id, selection);
}
doc.ensure_view_init(view_id);
doc.mark_as_focused();
@ -1746,13 +1745,14 @@ impl Editor {
// TODO: do something about this unwrap
let doc = self.document(view.doc).unwrap();
if let Some(path) = doc.path() {
// TODO: can the arg here be a reference? would save cloning
persistence::push_file_history(FileHistoryEntry::new(
path.clone(),
view.offset.anchor,
view.offset.vertical_offset,
view.offset.horizontal_offset,
view.offset,
doc.selection(id).clone(),
));
self.old_file_locs.insert(path.to_owned(), view.offset);
self.old_file_locs
.insert(path.to_owned(), (view.offset, doc.selection(id).clone()));
};
// Remove selections for the closed view on all documents.

@ -1,3 +1,4 @@
use helix_core::Selection;
use helix_loader::{
command_histfile, file_histfile,
persistence::{push_history, read_history},
@ -6,27 +7,21 @@ use helix_loader::{
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
// TODO: should this contain a ViewPosition?
use crate::view::ViewPosition;
#[derive(Debug, Serialize, Deserialize)]
pub struct FileHistoryEntry {
pub path: PathBuf,
pub anchor: usize,
pub vertical_offset: usize,
pub horizontal_offset: usize,
pub view_position: ViewPosition,
pub selection: Selection,
}
impl FileHistoryEntry {
pub fn new(
path: PathBuf,
anchor: usize,
vertical_offset: usize,
horizontal_offset: usize,
) -> Self {
pub fn new(path: PathBuf, view_position: ViewPosition, selection: Selection) -> Self {
Self {
path,
anchor,
vertical_offset,
horizontal_offset,
view_position,
selection,
}
}
}

@ -17,6 +17,7 @@ use helix_core::{
Transaction,
VisualOffsetError::{PosAfterMaxRow, PosBeforeAnchorRow},
};
use serde::{Deserialize, Serialize};
use std::{
collections::{HashMap, VecDeque},
@ -118,7 +119,7 @@ impl JumpList {
}
}
#[derive(Clone, Debug, PartialEq, Eq, Copy, Default)]
#[derive(Clone, Debug, PartialEq, Eq, Copy, Default, Serialize, Deserialize)]
pub struct ViewPosition {
pub anchor: usize,
pub horizontal_offset: usize,

Loading…
Cancel
Save