Skip to content

Commit

Permalink
refactor: add --reverse flag to sort
Browse files Browse the repository at this point in the history
  • Loading branch information
RoloEdits committed Dec 18, 2024
1 parent 910d370 commit 9b9b50b
Showing 1 changed file with 16 additions and 28 deletions.
44 changes: 16 additions & 28 deletions helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2123,35 +2123,33 @@ fn language(

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

sort_impl(cx, args, flags, false)
}
if let Some(flag) = args.next() {
let flag = flag.trim_start_matches("--").trim_start_matches('-');

fn sort_reverse(
cx: &mut compositor::Context,
args: Args,
flags: &[Flag],
event: PromptEvent,
) -> anyhow::Result<()> {
if event != PromptEvent::Validate {
return Ok(());
if flags
.iter()
.any(|x| x.long == flag || x.short == Some(flag))
{
sort_impl(cx, true);
} else {
bail!("There is no flag `--{flag}` on sort");
}
} else {
sort_impl(cx, false);
}
sort_impl(cx, args, flags, true)

Ok(())
}

fn sort_impl(
cx: &mut compositor::Context,
_args: Args,
_flags: &[Flag],
reverse: bool,
) -> anyhow::Result<()> {
fn sort_impl(cx: &mut compositor::Context, reverse: bool) {
let scrolloff = cx.editor.config().scrolloff;
let (view, doc) = current!(cx.editor);
let text = doc.text().slice(..);
Expand Down Expand Up @@ -2179,8 +2177,6 @@ fn sort_impl(
doc.apply(&transaction, view.id);
doc.append_changes_to_history(view);
view.ensure_cursor_in_view(doc, scrolloff);

Ok(())
}

fn reflow(
Expand Down Expand Up @@ -3206,14 +3202,6 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
fun: sort,
signature: CommandSignature::none(),
},
TypableCommand {
name: "rsort",
aliases: &[],
flags: &[],
doc: "Sort ranges in selection in reverse order.",
fun: sort_reverse,
signature: CommandSignature::none(),
},
TypableCommand {
name: "reflow",
aliases: &[],
Expand Down

0 comments on commit 9b9b50b

Please sign in to comment.