aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFilipe Azevedo2022-11-20 18:39:26 +0000
committerGitHub2022-11-20 18:39:26 +0000
commit8dac863a5b5b7fac08022a14f031fcf3c479a298 (patch)
treee8d2b286f3bb6a1ea5ec1e89688355dffd6cf1cc
parent46eb185d45c22f95de36b8467f323247cdb76fda (diff)
Add `:reload-all` command (#4663)
-rw-r--r--book/src/generated/typable-cmd.md1
-rw-r--r--helix-term/src/commands/typed.rs51
2 files changed, 52 insertions, 0 deletions
diff --git a/book/src/generated/typable-cmd.md b/book/src/generated/typable-cmd.md
index b7496d33..6390ef85 100644
--- a/book/src/generated/typable-cmd.md
+++ b/book/src/generated/typable-cmd.md
@@ -44,6 +44,7 @@
| `:show-directory`, `:pwd` | Show the current working directory. |
| `:encoding` | Set encoding. Based on `https://encoding.spec.whatwg.org`. |
| `:reload` | Discard changes and reload from the source file. |
+| `:reload-all` | Discard changes and reload all documents from the source files. |
| `:update` | Write changes only if the file has been modified. |
| `:lsp-workspace-command` | Open workspace command picker |
| `:lsp-restart` | Restarts the Language Server that is in use by the current doc |
diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs
index 4bbb2082..b8f99ff3 100644
--- a/helix-term/src/commands/typed.rs
+++ b/helix-term/src/commands/typed.rs
@@ -1034,6 +1034,50 @@ fn reload(
})
}
+fn reload_all(
+ cx: &mut compositor::Context,
+ _args: &[Cow<str>],
+ event: PromptEvent,
+) -> anyhow::Result<()> {
+ if event != PromptEvent::Validate {
+ return Ok(());
+ }
+
+ let scrolloff = cx.editor.config().scrolloff;
+ let view_id = view!(cx.editor).id;
+
+ let docs_view_ids: Vec<(DocumentId, Vec<ViewId>)> = cx
+ .editor
+ .documents_mut()
+ .map(|doc| {
+ let mut view_ids: Vec<_> = doc.selections().keys().cloned().collect();
+
+ if view_ids.is_empty() {
+ doc.ensure_view_init(view_id);
+ view_ids.push(view_id);
+ };
+
+ (doc.id(), view_ids)
+ })
+ .collect();
+
+ for (doc_id, view_ids) in docs_view_ids {
+ let doc = doc_mut!(cx.editor, &doc_id);
+
+ // Every doc is guaranteed to have at least 1 view at this point.
+ let view = view_mut!(cx.editor, view_ids[0]);
+ doc.reload(view)?;
+
+ for view_id in view_ids {
+ let view = view_mut!(cx.editor, view_id);
+
+ view.ensure_cursor_in_view(doc, scrolloff);
+ }
+ }
+
+ Ok(())
+}
+
/// Update the [`Document`] if it has been modified.
fn update(
cx: &mut compositor::Context,
@@ -2052,6 +2096,13 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
completer: None,
},
TypableCommand {
+ name: "reload-all",
+ aliases: &[],
+ doc: "Discard changes and reload all documents from the source files.",
+ fun: reload_all,
+ completer: None,
+ },
+ TypableCommand {
name: "update",
aliases: &[],
doc: "Write changes only if the file has been modified.",