From 9e9f327341e049a59bf69244a08060daeb16ca5c Mon Sep 17 00:00:00 2001 From: Gaurav Tyagi Date: Sun, 23 Oct 2022 12:06:50 +0530 Subject: [PATCH 1/3] add command update that will write the changes if file hasn been modified --- helix-term/src/commands/typed.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs index 7ea4c8018b0c..293e715ba31b 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 it has been modified.", + fun: update, + completer: None, + }, TypableCommand { name: "lsp-restart", aliases: &[], From fec8f9f487a64fcb044bd231d9bfc45661197849 Mon Sep 17 00:00:00 2001 From: Gaurav Tyagi Date: Mon, 24 Oct 2022 09:52:57 +0530 Subject: [PATCH 2/3] add docs --- book/src/generated/typable-cmd.md | 1 + 1 file changed, 1 insertion(+) diff --git a/book/src/generated/typable-cmd.md b/book/src/generated/typable-cmd.md index 05a0985b41f8..e8de2eb5f6d4 100644 --- a/book/src/generated/typable-cmd.md +++ b/book/src/generated/typable-cmd.md @@ -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. | +| `:update` | Write changes only if it 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. | | `:debug-start`, `:dbg` | Start a debug session from a given template with given parameters. | From 7fe7910ad383401b8227978750a2a0eb7a229167 Mon Sep 17 00:00:00 2001 From: Gaurav Tyagi Date: Mon, 24 Oct 2022 21:23:37 +0530 Subject: [PATCH 3/3] update the docs --- book/src/generated/typable-cmd.md | 2 +- helix-term/src/commands/typed.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/book/src/generated/typable-cmd.md b/book/src/generated/typable-cmd.md index e8de2eb5f6d4..49e7e7780a66 100644 --- a/book/src/generated/typable-cmd.md +++ b/book/src/generated/typable-cmd.md @@ -44,7 +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. | -| `:update` | Write changes only if it has been modified. | +| `: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. | | `:debug-start`, `:dbg` | Start a debug session from a given template with given parameters. | diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs index 293e715ba31b..9b79c3e6c589 100644 --- a/helix-term/src/commands/typed.rs +++ b/helix-term/src/commands/typed.rs @@ -1978,7 +1978,7 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[ TypableCommand { name: "update", aliases: &[], - doc: "Write changes only if it has been modified.", + doc: "Write changes only if the file has been modified.", fun: update, completer: None, },