From 852faa0c8dad4a915b6e51abb155fc822bdb9a2c Mon Sep 17 00:00:00 2001 From: Filipe Azevedo Date: Tue, 8 Nov 2022 22:17:41 +0000 Subject: [PATCH 1/3] 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 b7496d338c4e..cf9d60405283 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-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 4bbb20824d50..e0c0492ac6f1 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, @@ -2051,6 +2094,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: &[], From 6ba4c4f8572a98851d3ce6331919d3ae285a5203 Mon Sep 17 00:00:00 2001 From: Filipe Azevedo Date: Thu, 10 Nov 2022 17:12:06 +0000 Subject: [PATCH 2/3] refresh all views cursors --- helix-term/src/commands/typed.rs | 35 ++++++++++++++++---------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs index e0c0492ac6f1..6d4145cd1af2 100644 --- a/helix-term/src/commands/typed.rs +++ b/helix-term/src/commands/typed.rs @@ -1044,39 +1044,40 @@ fn reload_all( } let scrolloff = cx.editor.config().scrolloff; - let view_id = { - let (view, _) = current_ref!(cx.editor); - view.id - }; + let view_id = view!(cx.editor).id; - let docs_views: Vec<(DocumentId, ViewId)> = cx + let docs_views: Vec<(DocumentId, Vec)> = 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 { + let mut views: Vec<_> = doc.selections().keys().cloned().collect(); + + if views.is_empty() { doc.ensure_view_init(view_id); - view_id + views = vec![view_id]; }; - (doc.id(), target_view) + (doc.id(), views) }) .collect(); - for (doc_id, view_id) in docs_views { - let view = view_mut!(cx.editor, view_id); + for (doc_id, view_ids) in docs_views { let doc = doc_mut!(cx.editor, &doc_id); - doc.reload(view).map(|_| { + // 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, @@ -2096,7 +2097,7 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[ }, TypableCommand { name: "reload-all", - aliases: &["ra"], + aliases: &[], doc: "Discard changes and reload all documents from the source file.", fun: reload_all, completer: None, From e8426316333c16a61fb176c63cba61c132cfc108 Mon Sep 17 00:00:00 2001 From: Filipe Azevedo Date: Mon, 14 Nov 2022 10:11:04 +0000 Subject: [PATCH 3/3] generate docs --- book/src/generated/typable-cmd.md | 2 +- helix-term/src/commands/typed.rs | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/book/src/generated/typable-cmd.md b/book/src/generated/typable-cmd.md index cf9d60405283..6390ef858e7b 100644 --- a/book/src/generated/typable-cmd.md +++ b/book/src/generated/typable-cmd.md @@ -44,7 +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. | +| `: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 6d4145cd1af2..b8f99ff369d2 100644 --- a/helix-term/src/commands/typed.rs +++ b/helix-term/src/commands/typed.rs @@ -1046,22 +1046,22 @@ fn reload_all( let scrolloff = cx.editor.config().scrolloff; let view_id = view!(cx.editor).id; - let docs_views: Vec<(DocumentId, Vec)> = cx + let docs_view_ids: Vec<(DocumentId, Vec)> = cx .editor .documents_mut() .map(|doc| { - let mut views: Vec<_> = doc.selections().keys().cloned().collect(); + let mut view_ids: Vec<_> = doc.selections().keys().cloned().collect(); - if views.is_empty() { + if view_ids.is_empty() { doc.ensure_view_init(view_id); - views = vec![view_id]; + view_ids.push(view_id); }; - (doc.id(), views) + (doc.id(), view_ids) }) .collect(); - for (doc_id, view_ids) in docs_views { + 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. @@ -2098,7 +2098,7 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[ TypableCommand { name: "reload-all", aliases: &[], - doc: "Discard changes and reload all documents from the source file.", + doc: "Discard changes and reload all documents from the source files.", fun: reload_all, completer: None, },