Skip to content

Commit 9ce94fc

Browse files
committed
inline workspace commands
1 parent 683800b commit 9ce94fc

File tree

3 files changed

+101
-118
lines changed

3 files changed

+101
-118
lines changed

helix-term/src/commands/typed.rs

Lines changed: 100 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1865,20 +1865,116 @@ fn save_workspace(
18651865
saved_files.append(&mut new_files);
18661866
UndoIndex(saved_files)
18671867
};
1868-
1869-
cx.editor.save_workspace()
1868+
log::debug!("Saving undo index: {:?}", index);
1869+
1870+
index
1871+
.serialize(&mut index_file)
1872+
.context("failed to save index")?;
1873+
1874+
// Save the histories of open buffers.
1875+
for doc in cx.editor.documents_mut().filter(|doc| doc.path().is_some()) {
1876+
let path = doc.path().unwrap().clone();
1877+
let last_saved_revision = doc.get_last_saved_revision();
1878+
let history = doc.history.get_mut();
1879+
let mtime = std::fs::metadata(path.clone())?
1880+
.modified()?
1881+
.duration_since(std::time::UNIX_EPOCH)?
1882+
.as_secs();
1883+
1884+
let mut file = workspace.get_mut(&index.find_id(&path).unwrap().to_string())?;
1885+
history
1886+
.serialize(
1887+
&mut file,
1888+
&mut std::fs::File::open(&path)?,
1889+
last_saved_revision,
1890+
mtime,
1891+
)
1892+
.context(format!(
1893+
"failed to save history for {}",
1894+
path.to_string_lossy()
1895+
))?;
1896+
log::debug!("Saved history for: {}", path.to_string_lossy());
1897+
}
1898+
Ok(())
18701899
}
18711900

18721901
fn open_workspace(
18731902
cx: &mut compositor::Context,
18741903
_args: &[Cow<str>],
18751904
event: PromptEvent,
18761905
) -> anyhow::Result<()> {
1906+
use helix_view::workspace::undo::UndoIndex;
1907+
use helix_view::workspace::Workspace;
1908+
18771909
if event != PromptEvent::Validate {
18781910
return Ok(());
18791911
}
18801912

1881-
cx.editor.open_workspace()
1913+
let mut workspace = Workspace::new()?;
1914+
let index = UndoIndex::deserialize(&mut workspace.get(".index")?)
1915+
.context("failed to load index")?
1916+
.0;
1917+
let scrolloff = cx.editor.config().scrolloff;
1918+
log::debug!("Loaded undo index: {:?}", index);
1919+
1920+
// Open the documents in the index and load their histories.
1921+
for (id, path) in index {
1922+
if !path.exists() {
1923+
continue;
1924+
}
1925+
1926+
// Close open buffers for the doc.
1927+
let doc_id = cx
1928+
.editor
1929+
.documents()
1930+
.find_map(|doc| (doc.path() == Some(&path)).then_some(doc.id()));
1931+
if let Some(id) = doc_id {
1932+
buffer_close_by_ids_impl(cx, &[id], false)?;
1933+
}
1934+
1935+
let mut file = workspace.get(&id.to_string())?;
1936+
let last_mtime = std::fs::metadata(path.clone())?
1937+
.modified()?
1938+
.duration_since(std::time::UNIX_EPOCH)?
1939+
.as_secs();
1940+
let id = cx.editor.open(path.as_path(), Action::Load)?;
1941+
let doc = doc_mut!(cx.editor, &id);
1942+
let (last_saved_revision, history) = match helix_core::history::History::deserialize(
1943+
&mut file,
1944+
&mut std::fs::File::open(&path)?,
1945+
last_mtime,
1946+
) {
1947+
Ok(res) => res,
1948+
Err(e) => {
1949+
cx.editor.set_error(format!(
1950+
"failed to load undo file for {} because {e}",
1951+
path.to_string_lossy()
1952+
));
1953+
continue;
1954+
}
1955+
};
1956+
1957+
// Jump to saved revision if the doc wasn't saved.
1958+
if history.current_revision() != last_saved_revision {
1959+
let view_id = doc
1960+
.selections()
1961+
.keys()
1962+
.next()
1963+
.copied()
1964+
.expect("No view_id available");
1965+
let view = view_mut!(cx.editor, view_id);
1966+
apply_transaction(
1967+
&history.changes_since(last_saved_revision).unwrap(),
1968+
doc,
1969+
&view,
1970+
);
1971+
view.ensure_cursor_in_view(&doc, scrolloff);
1972+
}
1973+
doc.history.set(history);
1974+
doc.set_last_saved_revision(last_saved_revision);
1975+
log::debug!("Loaded history for: {}", path.to_string_lossy());
1976+
}
1977+
Ok(())
18821978
}
18831979

18841980
pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
@@ -2406,7 +2502,7 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
24062502
TypableCommand {
24072503
name: "open-workspace",
24082504
aliases: &["ow"],
2409-
doc: "Open document undo history",
2505+
doc: "Open document undo history, overriding open buffers.",
24102506
fun: open_workspace,
24112507
completer: None,
24122508
},

helix-view/src/document.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1008,7 +1008,7 @@ impl Document {
10081008
}
10091009

10101010
/// Get the document's latest saved revision.
1011-
pub fn get_last_saved_revision(&mut self) -> usize {
1011+
pub fn get_last_saved_revision(&self) -> usize {
10121012
self.last_saved_revision
10131013
}
10141014

helix-view/src/editor.rs

Lines changed: 0 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -831,119 +831,6 @@ impl Editor {
831831
}
832832
}
833833

834-
// TODO: Async?
835-
pub fn save_workspace(&mut self) -> anyhow::Result<()> {
836-
let mut workspace = Workspace::new()?;
837-
let mut index_file = workspace.get_mut("index.undo")?;
838-
let index = {
839-
let mut current_index =
840-
UndoIndex::deserialize(&mut index_file).unwrap_or(UndoIndex::default());
841-
let new_files = self.documents().filter_map(|doc| {
842-
doc.path().filter(|path| {
843-
!current_index
844-
.0
845-
.iter()
846-
.any(|(_, indexed_path)| indexed_path == *path)
847-
})
848-
});
849-
let mut last_id = current_index.0.last().map(|(id, _)| *id).unwrap_or(0);
850-
current_index.0.append(
851-
&mut new_files
852-
.map(|path| {
853-
let current_id = last_id;
854-
last_id += 1;
855-
(current_id, path.clone())
856-
})
857-
.collect(),
858-
);
859-
current_index
860-
};
861-
log::debug!("Saving undo index: {:?}", index);
862-
863-
index
864-
.serialize(&mut index_file)
865-
.context("failed to serialize index")?;
866-
for doc in self.documents_mut().filter(|doc| doc.path().is_some()) {
867-
let history = doc.history.take();
868-
let last_saved_revision = doc.get_last_saved_revision();
869-
let path = doc.path().unwrap();
870-
let mtime = std::fs::metadata(path.clone())?
871-
.modified()?
872-
.duration_since(std::time::UNIX_EPOCH)?
873-
.as_secs();
874-
let id = index.find_id(path).unwrap();
875-
let mut undo_file = workspace.get_mut(&id.to_string())?;
876-
877-
history
878-
.serialize(
879-
&mut undo_file,
880-
&mut File::open(path)?,
881-
last_saved_revision,
882-
mtime,
883-
)
884-
.context(format!(
885-
"failed to save history for {}",
886-
path.to_string_lossy()
887-
))?;
888-
doc.history.set(history);
889-
}
890-
Ok(())
891-
}
892-
893-
pub fn open_workspace(&mut self) -> anyhow::Result<()> {
894-
let mut workspace = Workspace::new()?;
895-
let index = UndoIndex::deserialize(&mut workspace.get("index.undo")?)
896-
.context("failed to load index")?;
897-
898-
let scrolloff = self.config().scrolloff;
899-
for (id, path) in index.0 {
900-
if !path.exists() {
901-
continue;
902-
}
903-
let current_view_id = view!(&self).id;
904-
905-
let mut undo_file = workspace.get(&id.to_string())?;
906-
let last_mtime = std::fs::metadata(path.clone())?
907-
.modified()?
908-
.duration_since(std::time::UNIX_EPOCH)?
909-
.as_secs();
910-
let id = self.open(path.as_path(), Action::Load)?;
911-
let doc = doc_mut!(self, &id);
912-
let (last_saved_revision, history) = helix_core::history::History::deserialize(
913-
&mut undo_file,
914-
&mut File::open(path)?,
915-
last_mtime,
916-
)
917-
.context("failed to load history")?;
918-
919-
if history.current_revision() != last_saved_revision {
920-
let selections = doc.selections();
921-
let view_id = if selections.contains_key(&current_view_id) {
922-
// use current if possible
923-
current_view_id
924-
} else {
925-
// Hack: we take the first available view_id
926-
selections
927-
.keys()
928-
.next()
929-
.copied()
930-
.expect("No view_id available")
931-
};
932-
let view = view_mut!(self, view_id);
933-
apply_transaction(
934-
&history.changes_since(last_saved_revision).unwrap(),
935-
doc,
936-
&view,
937-
);
938-
view.ensure_cursor_in_view(&doc, scrolloff);
939-
}
940-
doc.history.set(history);
941-
doc.set_last_saved_revision(last_saved_revision);
942-
}
943-
944-
Ok(())
945-
}
946-
947834
/// Current editing mode for the [`Editor`].
948835
pub fn mode(&self) -> Mode {
949836
self.mode

0 commit comments

Comments
 (0)