From ba9e50e93b43f102de352ead57c28d86c34a7c73 Mon Sep 17 00:00:00 2001 From: Gaurav Tyagi Date: Wed, 26 Oct 2022 08:28:49 +0530 Subject: 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--- helix-term/src/commands/typed.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'helix-term/src') diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs index 7ea4c801..9b79c3e6 100644 --- a/helix-term/src/commands/typed.rs +++ b/helix-term/src/commands/typed.rs @@ -1029,6 +1029,24 @@ fn reload( }) } +/// Update the [`Document`] if it has been modified. +fn update( + cx: &mut compositor::Context, + args: &[Cow], + event: PromptEvent, +) -> anyhow::Result<()> { + if event != PromptEvent::Validate { + return Ok(()); + } + + let (_view, doc) = current!(cx.editor); + if doc.is_modified() { + write(cx, args, event) + } else { + Ok(()) + } +} + fn lsp_restart( cx: &mut compositor::Context, _args: &[Cow], @@ -1957,6 +1975,13 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[ fun: reload, completer: None, }, + TypableCommand { + name: "update", + aliases: &[], + doc: "Write changes only if the file has been modified.", + fun: update, + completer: None, + }, TypableCommand { name: "lsp-restart", aliases: &[], -- cgit v1.2.3-70-g09d2