Skip to content

Commit 8fbd985

Browse files
grv07Shekhinah Memmel
authored andcommitted
Add :update that will write the changes if the file has been modified. (helix-editor#4426)
* add command update that will write the changes if file hasn been modified * add docs * update the docs
1 parent 23acfdc commit 8fbd985

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

book/src/generated/typable-cmd.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
10321050
fn 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: &[],

0 commit comments

Comments
 (0)