Skip to content

Commit

Permalink
add trim_whitespace command
Browse files Browse the repository at this point in the history
  • Loading branch information
kirawi committed Sep 3, 2022
1 parent c93d52c commit 7eb1d6f
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1515,6 +1515,28 @@ fn run_shell_command(
Ok(())
}

fn trim_whitespace(
cx: &mut compositor::Context,
_: &[Cow<str>],
_: PromptEvent,
) -> anyhow::Result<()> {
let mut pos = 0;
let (view, doc) = current!(cx.editor);
let transaction = Transaction::change(
doc.text(),
doc.text().lines().filter_map(|line| {
let start = pos
+ movement::backwards_skip_while(line, line.len_chars(), |x| x.is_whitespace())?;
let end = pos + line.len_chars();
pos += line.len_chars();
Some((start, end, None))
}),
);
doc.apply(&transaction, view.id);
doc.append_changes_to_history(view.id);
Ok(())
}

pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
TypableCommand {
name: "quit",
Expand Down Expand Up @@ -1995,6 +2017,13 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
fun: run_shell_command,
completer: Some(completers::directory),
},
TypableCommand {
name: "trim_whitespace",
aliases: &["trim"],
doc: "Trim whitespace at the end of all lines.",
fun: trim_whitespace,
completer: None,
},
];

pub static TYPABLE_COMMAND_MAP: Lazy<HashMap<&'static str, &'static TypableCommand>> =
Expand Down

0 comments on commit 7eb1d6f

Please sign in to comment.