Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reload! and reload-all! commands #7948

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions book/src/generated/typable-cmd.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@
| `:show-directory`, `:pwd` | Show the current working directory. |
| `:encoding` | Set encoding. Based on `https://encoding.spec.whatwg.org`. |
| `:character-info`, `:char` | Get info about the character under the primary cursor. |
| `:reload`, `:rl` | Discard changes and reload from the source file. |
| `:reload-all`, `:rla` | Discard changes and reload all documents from the source files. |
| `:reload`, `:rl` | Reload from the source file, if no changes were made. |
| `:reload!`, `:rl!` | Discard changes and reload from the source file. |
| `:reload-all`, `:rla` | Reload all documents from the source files, if no changes were made. |
| `:reload-all!`, `:rla!` | Discard changes and reload all documents from the source files. |
| `:update`, `:u` | Write changes only if the file has been modified. |
| `:lsp-workspace-command` | Open workspace command picker |
| `:lsp-restart` | Restarts the language servers used by the current doc |
Expand Down
70 changes: 67 additions & 3 deletions helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1254,11 +1254,29 @@ fn get_character_info(
Ok(())
}

/// Reload the [`Document`] from its source file.
// Reload the [`Document`] from its source file, if no changes were made.
fn reload(
cx: &mut compositor::Context,
_args: &[Cow<str>],
event: PromptEvent,
) -> anyhow::Result<()> {
reload_impl(cx, _args, event, false)
}

// Force reload the [`Document`] from its source file.
fn force_reload(
cx: &mut compositor::Context,
_args: &[Cow<str>],
event: PromptEvent,
) -> anyhow::Result<()> {
reload_impl(cx, _args, event, true)
}

fn reload_impl(
cx: &mut compositor::Context,
_args: &[Cow<str>],
event: PromptEvent,
force: bool,
) -> anyhow::Result<()> {
if event != PromptEvent::Validate {
return Ok(());
Expand All @@ -1267,6 +1285,9 @@ fn reload(
let scrolloff = cx.editor.config().scrolloff;
let redraw_handle = cx.editor.redraw_handle.clone();
let (view, doc) = current!(cx.editor);
if !force && doc.is_modified() {
bail!("Cannot reload unsaved buffer");
}
doc.reload(view, &cx.editor.diff_providers, redraw_handle)
.map(|_| {
view.ensure_cursor_in_view(doc, scrolloff);
Expand All @@ -1284,6 +1305,23 @@ fn reload_all(
cx: &mut compositor::Context,
_args: &[Cow<str>],
event: PromptEvent,
) -> anyhow::Result<()> {
reload_all_impl(cx, _args, event, false)
}

fn force_reload_all(
cx: &mut compositor::Context,
_args: &[Cow<str>],
event: PromptEvent,
) -> anyhow::Result<()> {
reload_all_impl(cx, _args, event, true)
}

fn reload_all_impl(
cx: &mut compositor::Context,
_args: &[Cow<str>],
event: PromptEvent,
force: bool,
) -> anyhow::Result<()> {
if event != PromptEvent::Validate {
return Ok(());
Expand All @@ -1307,8 +1345,13 @@ fn reload_all(
})
.collect();

let mut unsaved_buffer_count = 0;
for (doc_id, view_ids) in docs_view_ids {
let doc = doc_mut!(cx.editor, &doc_id);
if !force && doc.is_modified() {
unsaved_buffer_count += 1;
continue;
}

// Every doc is guaranteed to have at least 1 view at this point.
let view = view_mut!(cx.editor, view_ids[0]);
Expand All @@ -1333,6 +1376,13 @@ fn reload_all(
}
}

if !force && unsaved_buffer_count > 0 {
bail!(
"{} unsaved buffer(s) remaining, all saved buffers reloaded",
unsaved_buffer_count
);
}

Ok(())
}

Expand Down Expand Up @@ -2642,17 +2692,31 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
TypableCommand {
name: "reload",
aliases: &["rl"],
doc: "Discard changes and reload from the source file.",
doc: "Reload from the source file, if no changes were made.",
fun: reload,
signature: CommandSignature::none(),
},
TypableCommand {
name: "reload!",
aliases: &["rl!"],
doc: "Discard changes and reload from the source file.",
fun: force_reload,
signature: CommandSignature::none(),
},
TypableCommand {
name: "reload-all",
aliases: &["rla"],
doc: "Discard changes and reload all documents from the source files.",
doc: "Reload all documents from the source files, if no changes were made.",
fun: reload_all,
signature: CommandSignature::none(),
},
TypableCommand {
name: "reload-all!",
aliases: &["rla!"],
doc: "Discard changes and reload all documents from the source files.",
fun: force_reload_all,
signature: CommandSignature::none(),
},
TypableCommand {
name: "update",
aliases: &["u"],
Expand Down
Loading