on master help sign in
Add `:update` that will write the changes if the file has been modified. (#4426)

* add command update that will write the changes if file hasn been modified * add docs * update the docs

commitba9e50e93b43f102de352ead57c28d86c34a7c73 authorGaurav Tyagi <grvtyagi22@gmail.com> dateWed 26 Oct 2022, 08:28 (4 years ago) parent65edf9c
2 files changed+26
book/src/generated/typable-cmd.md+1 helix-term/src/commands/typed.rs+25
book/src/generated/typable-cmd.md+1
@@ -44,6 +44,7 @@
4444| `:show-directory`, `:pwd` | Show the current working directory. |
4545| `:encoding` | Set encoding. Based on `https://encoding.spec.whatwg.org`. |
4646| `:reload` | Discard changes and reload from the source file. |
47+| `:update` | Write changes only if the file has been modified. |
4748| `:lsp-restart` | Restarts the Language Server that is in use by the current doc |
4849| `:tree-sitter-scopes` | Display tree sitter scopes, primarily for theming and development. |
4950| `:debug-start`, `:dbg` | Start a debug session from a given template with given parameters. |
helix-term/src/commands/typed.rs+25
@@ -1029,6 +1029,24 @@ fn reload(
10291029 })
10301030}
10311031
1032+/// Update the [`Document`] if it has been modified.
1033+fn update(
1034+ cx: &mut compositor::Context,
1035+ args: &[Cow<str>],
1036+ event: PromptEvent,
1037+) -> anyhow::Result<()> {
1038+ if event != PromptEvent::Validate {
1039+ return Ok(());
1040+ }
1041+
1042+ let (_view, doc) = current!(cx.editor);
1043+ if doc.is_modified() {
1044+ write(cx, args, event)
1045+ } else {
1046+ Ok(())
1047+ }
1048+}
1049+
10321050fn lsp_restart(
10331051 cx: &mut compositor::Context,
10341052 _args: &[Cow<str>],
@@ -1957,6 +1975,13 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
19571975 fun: reload,
19581976 completer: None,
19591977 },
1978+ TypableCommand {
1979+ name: "update",
1980+ aliases: &[],
1981+ doc: "Write changes only if the file has been modified.",
1982+ fun: update,
1983+ completer: None,
1984+ },
19601985 TypableCommand {
19611986 name: "lsp-restart",
19621987 aliases: &[],