Skip to content

Commit

Permalink
add reload all command
Browse files Browse the repository at this point in the history
  • Loading branch information
mangas committed Nov 8, 2022
1 parent a19eee6 commit 75bcfc4
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions book/src/generated/typable-cmd.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand Down
50 changes: 50 additions & 0 deletions helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1034,6 +1034,49 @@ 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 = {
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,
Expand Down Expand Up @@ -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: &[],
Expand Down

0 comments on commit 75bcfc4

Please sign in to comment.