Skip to content

Commit

Permalink
feat: add command to set diff source for files
Browse files Browse the repository at this point in the history
  • Loading branch information
poliorcetics committed Apr 6, 2024
1 parent 8cfb0f0 commit 2477079
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 1 deletion.
1 change: 1 addition & 0 deletions book/src/generated/typable-cmd.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
| `:pipe-to` | Pipe each selection to the shell command, ignoring output. |
| `:run-shell-command`, `:sh` | Run a shell command |
| `:reset-diff-change`, `:diffget`, `:diffg` | Reset the diff change at the cursor position. |
| `:diff-source` | Set the diff source for the file. If no argument is provided, show the current source. |
| `:clear-register` | Clear given register. If no argument is provided, clear all registers. |
| `:redraw` | Clear and re-render the whole UI |
| `:move` | Move the current buffer and its corresponding file to a different path |
Expand Down
38 changes: 38 additions & 0 deletions helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2338,6 +2338,37 @@ fn reset_diff_change(
Ok(())
}

fn diff_source(
cx: &mut compositor::Context,
args: &[Cow<str>],
event: PromptEvent,
) -> anyhow::Result<()> {
if event != PromptEvent::Validate {
return Ok(());
}

let editor = &mut cx.editor;
let doc = doc_mut!(editor);
let current_diff_source = doc.diff_source();

let arg = match args {
[arg] => arg.as_ref(),
[_, _, ..] => bail!(":diff-source takes at most 1 argument"),
[] => {
editor.set_status(format!("Active diff source: {current_diff_source}"));
return Ok(());
}
};

match arg.parse() {
Ok(ds) if ds == current_diff_source => editor.set_status("Diff source unchanged"),
Ok(ds) => doc.set_diff_data(ds),
Err(err) => editor.set_error(format!("Could not set diff source: {err}")),
}

Ok(())
}

fn clear_register(
cx: &mut compositor::Context,
args: &[Cow<str>],
Expand Down Expand Up @@ -3040,6 +3071,13 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
fun: reset_diff_change,
signature: CommandSignature::none(),
},
TypableCommand {
name: "diff-source",
aliases: &[],
doc: "Set the diff source for the file. If no argument is provided, show the current source.",
fun: diff_source,
signature: CommandSignature::none(),
},
TypableCommand {
name: "clear-register",
aliases: &[],
Expand Down
54 changes: 53 additions & 1 deletion helix-vcs/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use anyhow::{anyhow, Context, Result};
use anyhow::{anyhow, bail, Context, Result};
use arc_swap::ArcSwap;
use std::fmt::Display;
use std::path::PathBuf;
use std::str::FromStr;
use std::{path::Path, sync::Arc};

#[cfg(feature = "git")]
Expand Down Expand Up @@ -124,3 +126,53 @@ impl DiffSource {
});
}
}

impl FromStr for DiffSource {
type Err = anyhow::Error;

fn from_str(s: &str) -> Result<Self> {
match s {
"none" => Ok(Self::None),
"file" => Ok(Self::File),
#[cfg(feature = "git")]
"git" => Ok(Self::Git),
s => bail!("invalid diff source '{s}', pick one of 'none', 'file' or 'git'"),
}
}
}

impl Display for DiffSource {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match self {
Self::None => "none",
Self::File => "file",
#[cfg(feature = "git")]
Self::Git => "git",
})
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_diff_source_parse() {
assert_eq!(DiffSource::from_str("none").unwrap(), DiffSource::None);
assert_eq!(DiffSource::from_str("file").unwrap(), DiffSource::File);
#[cfg(feature = "git")]
assert_eq!(DiffSource::from_str("git").unwrap(), DiffSource::Git);

assert!(DiffSource::from_str("Git").is_err());
assert!(DiffSource::from_str("NONE").is_err());
assert!(DiffSource::from_str("fIlE").is_err());
}

#[test]
fn test_diff_source_display() {
assert_eq!(DiffSource::None.to_string(), "none");
assert_eq!(DiffSource::File.to_string(), "file");
#[cfg(feature = "git")]
assert_eq!(DiffSource::Git.to_string(), "git");
}
}

0 comments on commit 2477079

Please sign in to comment.