Skip to content

Commit

Permalink
store ViewPosition and Selection directly in FileHistoryEntry
Browse files Browse the repository at this point in the history
  • Loading branch information
intarga committed Feb 13, 2024
1 parent 1df7a4d commit 45b7c16
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 41 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion helix-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "0.1"
Expand Down
5 changes: 3 additions & 2 deletions helix-core/src/selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::{
movement::Direction,
Assoc, ChangeSet, RopeGraphemes, RopeSlice,
};
use serde::{Deserialize, Serialize};
use smallvec::{smallvec, SmallVec};
use std::borrow::Cow;

Expand Down Expand Up @@ -47,7 +48,7 @@ use std::borrow::Cow;
/// 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,
Expand Down Expand Up @@ -388,7 +389,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,
Expand Down
16 changes: 5 additions & 11 deletions helix-term/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use helix_view::{
graphics::Rect,
persistence, theme,
tree::Layout,
view::ViewPosition,
Align, Editor,
};
use serde_json::json;
Expand Down Expand Up @@ -149,16 +148,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?
Expand Down
26 changes: 13 additions & 13 deletions helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,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>,
Expand Down Expand Up @@ -1038,7 +1038,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();
Expand Down Expand Up @@ -1512,16 +1512,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();
Expand Down Expand Up @@ -1617,13 +1616,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.
Expand Down
21 changes: 8 additions & 13 deletions helix-view/src/persistence.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use helix_core::Selection;
use helix_loader::{
command_histfile, file_histfile,
persistence::{push_history, read_history},
Expand All @@ -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,
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion helix-view/src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use helix_core::{
Transaction,
VisualOffsetError::{PosAfterMaxRow, PosBeforeAnchorRow},
};
use serde::{Deserialize, Serialize};

use std::{
collections::{HashMap, VecDeque},
Expand Down Expand Up @@ -101,7 +102,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,
Expand Down

0 comments on commit 45b7c16

Please sign in to comment.