Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
kirawi committed Jan 28, 2023
1 parent 31178f0 commit 884a131
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 28 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

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

Binary file added helix-core/.DS_Store
Binary file not shown.
1 change: 0 additions & 1 deletion helix-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ regex = "1"
bitflags = "1.3"
ahash = "0.8.2"
hashbrown = { version = "0.13.1", features = ["raw"] }
walkdir = "2.3"

log = "0.4"
serde = { version = "1.0", features = ["derive"] }
Expand Down
46 changes: 31 additions & 15 deletions helix-core/src/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,30 +71,25 @@ struct Revision {
const HEADER_TAG: &str = "Helix Undofile";
const CURRENT_VERSION: u8 = 1;

pub fn serialize_history(file: File, history: &History) -> std::io::Result<()> {
let mut writer = std::io::BufWriter::new(file);

write_string(&mut writer, HEADER_TAG)?;
write_byte(&mut writer, CURRENT_VERSION)?;
write_usize(&mut writer, history.current)?;
write_vec(&mut writer, &history.revisions, serialize_revision)?;
pub fn serialize_history<W: Write>(writer: &mut W, history: &History) -> std::io::Result<()> {
write_string(writer, HEADER_TAG)?;
write_byte(writer, CURRENT_VERSION)?;
write_usize(writer, history.current)?;
write_vec(writer, &history.revisions, serialize_revision)?;
Ok(())
}

pub fn deserialize_history(file: File) -> std::io::Result<History> {
let mut reader = std::io::BufReader::new(file);
if HEADER_TAG != read_string(&mut reader)? {
pub fn deserialize_history<R: Read>(reader: &mut R) -> std::io::Result<History> {
if HEADER_TAG != read_string(reader)? {
Err(std::io::Error::new(
std::io::ErrorKind::Other,
"missing undofile header",
))
} else {
let _version = read_byte(&mut reader)?;
let _version = read_byte(reader)?;
let timestamp = Instant::now();
let current = read_usize(&mut reader)?;
let revisions = read_vec(&mut reader, |reader| {
deserialize_revision(reader, timestamp)
})?;
let current = read_usize(reader)?;
let revisions = read_vec(reader, |reader| deserialize_revision(reader, timestamp))?;
Ok(History { current, revisions })
}
}
Expand Down Expand Up @@ -446,6 +441,8 @@ impl std::str::FromStr for UndoKind {

#[cfg(test)]
mod test {
use quickcheck::quickcheck;

use super::*;
use crate::Selection;

Expand Down Expand Up @@ -690,4 +687,23 @@ mod test {
Err("duration too large".to_string())
);
}

quickcheck!(
fn serde_history(a: String, b: String) -> bool {
let old = Rope::from(a);
let new = Rope::from(b);
let transaction = crate::diff::compare_ropes(&old, &new);

let mut buf = Vec::new();
let mut history = History::default();
let state = State {
doc: old,
selection: Selection::point(0),
};
history.commit_revision(&transaction, &state);
serialize_history(&mut buf, &history).unwrap();
deserialize_history(&mut buf.as_slice()).unwrap();
true
}
);
}
Binary file added helix-core/tests/.DS_Store
Binary file not shown.
Binary file added helix-view/.DS_Store
Binary file not shown.
Binary file added helix-view/src/.DS_Store
Binary file not shown.
6 changes: 3 additions & 3 deletions helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use crate::{
graphics::{CursorKind, Rect},
info::Info,
input::KeyEvent,
session::Session,
theme::{self, Theme},
tree::{self, Tree},
workspace::Workspace,
Align, Document, DocumentId, View, ViewId,
};
use helix_vcs::DiffProviderRegistry;
Expand Down Expand Up @@ -830,8 +830,8 @@ impl Editor {
}
}

pub fn session(&self) -> anyhow::Result<Session> {
Session::new(std::env::current_dir()?)
pub fn session(&self) -> anyhow::Result<Workspace> {
Workspace::new(std::env::current_dir()?)
}

/// Current editing mode for the [`Editor`].
Expand Down
2 changes: 1 addition & 1 deletion helix-view/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub mod editor;
pub mod env;
pub mod graphics;
pub mod gutter;
pub mod session;
pub mod workspace;
pub mod handlers {
pub mod dap;
pub mod lsp;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub struct Workspace {
lock: Option<FileLock>,
}

pub fn path_as_bytes(path: PathBuf) -> Vec<u8> {
fn path_as_bytes(path: PathBuf) -> Vec<u8> {
#[cfg(windows)]
return path.to_str().unwrap().into();

Expand All @@ -26,7 +26,7 @@ impl Workspace {
pub fn new(path: PathBuf) -> Result<Self> {
let bytes = path_as_bytes(path);
let hash = Sha1::from(bytes).digest().to_string();
let path = helix_loader::cache_dir().join("sessions").join(hash);
let path = helix_loader::cache_dir().join("workspaces").join(hash);
Ok(Self { path, lock: None })
}

Expand All @@ -48,7 +48,6 @@ impl Workspace {
.context("failed to open file")
}

// TODO: Return a FileLockGuard instead.
pub fn get_mut(&mut self, filename: &str) -> Result<File> {
if self.lock.is_none() {
let lock = FileLock::exclusive(self.path.join(".helix.lock"))?;
Expand Down Expand Up @@ -141,10 +140,6 @@ mod sys {
minwinbase::LOCKFILE_EXCLUSIVE_LOCK,
};

pub(super) fn path_as_bytes(path: &Path) -> &[u8] {
path.to_str().unwrap().as_bytes()
}

/// Blocks until the lock is acquired.
pub(super) fn lock(file: &File, shared: bool) -> anyhow::Result<()> {
let flag = if shared { 0 } else { LOCKFILE_EXCLUSIVE_LOCK };
Expand Down
File renamed without changes.

0 comments on commit 884a131

Please sign in to comment.