on master help sign in
Add `:reload-all` command (#4663)
8dac863Filipe Azevedo
book/src/generated/typable-cmd.md+1
@@ -44,6 +44,7 @@
44| `:show-directory`, `:pwd` | Show the current working directory. |
44| `:show-directory`, `:pwd` | Show the current working directory. |
45| `:encoding` | Set encoding. Based on `https://encoding.spec.whatwg.org`. |
45| `:encoding` | Set encoding. Based on `https://encoding.spec.whatwg.org`. |
46| `:reload` | Discard changes and reload from the source file. |
46| `:reload` | Discard changes and reload from the source file. |
47+| `:reload-all` | Discard changes and reload all documents from the source files. |
47| `:update` | Write changes only if the file has been modified. |
48| `:update` | Write changes only if the file has been modified. |
48| `:lsp-workspace-command` | Open workspace command picker |
49| `:lsp-workspace-command` | Open workspace command picker |
49| `:lsp-restart` | Restarts the Language Server that is in use by the current doc |
50| `:lsp-restart` | Restarts the Language Server that is in use by the current doc |
helix-term/src/commands/typed.rs+51
@@ -1034,6 +1034,50 @@ fn reload(
1034 })
1034 })
1035}
1035}
1036
1036
1037+fn reload_all(
1038+ cx: &mut compositor::Context,
1039+ _args: &[Cow<str>],
1040+ event: PromptEvent,
1041+) -> anyhow::Result<()> {
1042+ if event != PromptEvent::Validate {
1043+ return Ok(());
1044+ }
1045+
1046+ let scrolloff = cx.editor.config().scrolloff;
1047+ let view_id = view!(cx.editor).id;
1048+
1049+ let docs_view_ids: Vec<(DocumentId, Vec<ViewId>)> = cx
1050+ .editor
1051+ .documents_mut()
1052+ .map(|doc| {
1053+ let mut view_ids: Vec<_> = doc.selections().keys().cloned().collect();
1054+
1055+ if view_ids.is_empty() {
1056+ doc.ensure_view_init(view_id);
1057+ view_ids.push(view_id);
1058+ };
1059+
1060+ (doc.id(), view_ids)
1061+ })
1062+ .collect();
1063+
1064+ for (doc_id, view_ids) in docs_view_ids {
1065+ let doc = doc_mut!(cx.editor, &doc_id);
1066+
1067+ // Every doc is guaranteed to have at least 1 view at this point.
1068+ let view = view_mut!(cx.editor, view_ids[0]);
1069+ doc.reload(view)?;
1070+
1071+ for view_id in view_ids {
1072+ let view = view_mut!(cx.editor, view_id);
1073+
1074+ view.ensure_cursor_in_view(doc, scrolloff);
1075+ }
1076+ }
1077+
1078+ Ok(())
1079+}
1080+
1037/// Update the [`Document`] if it has been modified.
1081/// Update the [`Document`] if it has been modified.
1038fn update(
1082fn update(
1039 cx: &mut compositor::Context,
1083 cx: &mut compositor::Context,
@@ -2051,6 +2095,13 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
2051 fun: reload,
2095 fun: reload,
2052 completer: None,
2096 completer: None,
2053 },
2097 },
2098+ TypableCommand {
2099+ name: "reload-all",
2100+ aliases: &[],
2101+ doc: "Discard changes and reload all documents from the source files.",
2102+ fun: reload_all,
2103+ completer: None,
2104+ },
2054 TypableCommand {
2105 TypableCommand {
2055 name: "update",
2106 name: "update",
2056 aliases: &[],
2107 aliases: &[],