Skip to content

Commit

Permalink
close buffer after checking if the saved state
Browse files Browse the repository at this point in the history
is valid
  • Loading branch information
kirawi committed Jan 31, 2023
1 parent 50d8bf1 commit 587ad62
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 16 deletions.
2 changes: 1 addition & 1 deletion helix-core/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ pub fn path_as_bytes<P: AsRef<Path>>(path: P) -> Vec<u8> {

pub fn path_from_bytes(slice: &[u8]) -> Result<PathBuf, Utf8Error> {
#[cfg(windows)]
return Ok(PathBuf::from(std::str::from_utf8(slice)));
return Ok(PathBuf::from(std::str::from_utf8(slice)?));

#[cfg(unix)]
return Ok(PathBuf::from(
Expand Down
22 changes: 11 additions & 11 deletions helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1915,27 +1915,18 @@ fn open_workspace(
log::debug!("Loaded undo index: {:?}", index);

// Open the documents in the index and load their histories.
for (id, path) in index {
for (index_id, path) in index {
if !path.exists() {
continue;
}

// Close open buffers for the doc.
let doc_id = cx
.editor
.documents()
.find_map(|doc| (doc.path() == Some(&path)).then(|| doc.id()));
if let Some(id) = doc_id {
buffer_close_by_ids_impl(cx, &[id], false)?;
}

let mut file = workspace.get(&id.to_string())?;
let mut file = workspace.get(&index_id.to_string())?;
let last_mtime = std::fs::metadata(path.clone())?
.modified()?
.duration_since(std::time::UNIX_EPOCH)?
.as_secs();
let id = cx.editor.open(path.as_path(), Action::Load)?;
let doc = doc_mut!(cx.editor, &id);
let (last_saved_revision, history) = match helix_core::history::History::deserialize(
&mut file,
&mut std::fs::File::open(&path)?,
Expand All @@ -1951,6 +1942,15 @@ fn open_workspace(
}
};

let doc_id = cx
.editor
.documents()
.find_map(|doc| (doc.path() == Some(&path)).then(|| doc.id()));
if let Some(id) = doc_id {
buffer_close_by_ids_impl(cx, &[id], false)?;
}
let id = cx.editor.open(path.as_path(), Action::Load)?;
let doc = doc_mut!(cx.editor, &id);
// Jump to saved revision if the doc wasn't saved.
if history.current_revision() != last_saved_revision {
let view_id = doc
Expand Down
5 changes: 1 addition & 4 deletions helix-term/tests/test/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,10 +367,7 @@ async fn test_workspace_serde() -> anyhow::Result<()> {
Some("ihello<esc>:sw<ret>:bc!<ret>:ow<ret>"),
Some(&|app| {
let mut docs: Vec<_> = app.editor.documents().collect();
assert_eq!(1, docs.len());

let doc = docs.pop().unwrap();
assert_eq!(Some(file.path()), doc.path().map(PathBuf::as_path));
assert_eq!(2, docs.len());
}),
false,
)
Expand Down

0 comments on commit 587ad62

Please sign in to comment.