From 75bcfc4cbd4a2db84b930ae952e6a2a1300f9006 Mon Sep 17 00:00:00 2001 From: Filipe Azevedo Date: Tue, 8 Nov 2022 22:17:41 +0000 Subject: [PATCH] add reload all command --- book/src/generated/typable-cmd.md | 1 + helix-term/src/commands/typed.rs | 50 +++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/book/src/generated/typable-cmd.md b/book/src/generated/typable-cmd.md index f858ba7255dfa..ef39ab27a65c2 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`, `:ra` | Discard changes and reload all documents from the source file. | | `:update` | Write changes only if the file has been modified. | | `:lsp-restart` | Restarts the Language Server that is in use by the current doc | | `:tree-sitter-scopes` | Display tree sitter scopes, primarily for theming and development. | diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs index 2f387bfd1a2e7..7038d089ddf0c 100644 --- a/helix-term/src/commands/typed.rs +++ b/helix-term/src/commands/typed.rs @@ -1034,6 +1034,49 @@ fn reload( }) } +fn reload_all( + cx: &mut compositor::Context, + _args: &[Cow], + event: PromptEvent, +) -> anyhow::Result<()> { + if event != PromptEvent::Validate { + return Ok(()); + } + + let scrolloff = cx.editor.config().scrolloff; + let view_id = { + let (view, _) = current_ref!(cx.editor); + view.id + }; + + let docs_views: Vec<(DocumentId, ViewId)> = cx + .editor + .documents_mut() + .map(|doc| { + let target_view = if doc.selections().contains_key(&view_id) { + view_id + } else if let Some(view) = doc.selections().keys().next() { + *view + } else { + doc.ensure_view_init(view_id); + view_id + }; + + (doc.id(), target_view) + }) + .collect(); + + for (doc_id, view_id) in docs_views { + let view = view_mut!(cx.editor, view_id); + let doc = doc_mut!(cx.editor, &doc_id); + + doc.reload(view).map(|_| { + view.ensure_cursor_in_view(doc, scrolloff); + })?; + } + + Ok(()) +} /// Update the [`Document`] if it has been modified. fn update( cx: &mut compositor::Context, @@ -1980,6 +2023,13 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[ fun: reload, completer: None, }, + TypableCommand { + name: "reload-all", + aliases: &["ra"], + doc: "Discard changes and reload all documents from the source file.", + fun: reload_all, + completer: None, + }, TypableCommand { name: "update", aliases: &[],