Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'alphanumeric sort' commands #4289

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
132 changes: 127 additions & 5 deletions helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1358,7 +1358,7 @@ fn sort(cx: &mut compositor::Context, args: &[Cow<str>], event: PromptEvent) ->
return Ok(());
}

sort_impl(cx, args, false)
sort_impl(cx, args, false, false)
}

fn sort_reverse(
Expand All @@ -1370,13 +1370,38 @@ fn sort_reverse(
return Ok(());
}

sort_impl(cx, args, true)
sort_impl(cx, args, true, false)
}

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

sort_impl(cx, args, false, true)
}

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

sort_impl(cx, args, true, true)
}

fn sort_impl(
cx: &mut compositor::Context,
_args: &[Cow<str>],
reverse: bool,
alphanumeric: bool,
) -> anyhow::Result<()> {
Comment on lines 1400 to 1405
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: you could remove both reverse and alphanumeric parameters and pass a single parameter called cmp_fn which is a FnMut(&Tendril, &Tendril) -> Ordering leaving to the caller to say how it want the ordering.

See: #8436

let (view, doc) = current!(cx.editor);
let text = doc.text().slice(..);
Expand All @@ -1388,9 +1413,11 @@ fn sort_impl(
.map(|fragment| fragment.chunks().collect())
.collect();

fragments.sort_by(match reverse {
true => |a: &Tendril, b: &Tendril| b.cmp(a),
false => |a: &Tendril, b: &Tendril| a.cmp(b),
fragments.sort_by(match (reverse, alphanumeric) {
(true, false) => |a: &Tendril, b: &Tendril| b.cmp(a),
(false, false) => |a: &Tendril, b: &Tendril| a.cmp(b),
(true, true) => |a: &Tendril, b: &Tendril| numeric_cmp(b, a),
(false, true) => |a: &Tendril, b: &Tendril| numeric_cmp(a, b),
});
Comment on lines +1416 to 1421
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @leandrobbraga - this block is a little dense and we can make the call sites for sort_impl a little cleaner by moving these closures to parameters of sort_impl.

So this function would replace the reverse parameter with a compare_fn: impl FnMut(&Tendril, &Tendril) -> std::cmp::Ordering, param and then this block can be just fragments.sort_by(compare_fn);.

We could also use this opportunity to drop the args parameter which is currently unused


let transaction = Transaction::change(
Expand All @@ -1407,6 +1434,87 @@ fn sort_impl(
Ok(())
}

/// Compares two str, char by char, except for groups of ascii digits which are
/// compared as single numbers (detail: leading zeroes make a number bigger)
fn numeric_cmp(a: impl AsRef<str>, b: impl AsRef<str>) -> std::cmp::Ordering {
use std::cmp::Ordering;

fn advance_once(c: &str) -> &str {
let mut c = c.chars();
let _ = c.next();
c.as_str()
}

fn advance_while(s: &str, mut f: impl FnMut(char) -> bool) -> (&str, &str) {
let i = s
.char_indices()
.find(|&(_, c)| !f(c))
.map_or(s.len(), |(i, _)| i);
s.split_at(i)
}

let mut a = a.as_ref();
let mut b = b.as_ref();

loop {
match (a.chars().next(), b.chars().next()) {
(Some(ca), Some(cb)) => {
if ca.is_ascii_digit() && cb.is_ascii_digit() {
let (a_zeroes, a_rem) = advance_while(a, |c| c == '0');
let (b_zeroes, b_rem) = advance_while(b, |c| c == '0');

let (a_digits, a_rem) = advance_while(a_rem, |c| c.is_ascii_digit());
let (b_digits, b_rem) = advance_while(b_rem, |c| c.is_ascii_digit());

match (a_digits.len().cmp(&b_digits.len()))
.then_with(|| a_digits.cmp(b_digits))
.then_with(|| a_zeroes.len().cmp(&b_zeroes.len()))
{
Ordering::Equal => {
a = a_rem;
b = b_rem;
}
x => return x,
}
} else if ca != cb {
return ca.cmp(&cb);
} else {
a = advance_once(a);
b = advance_once(b);
}
}
(Some(_), None) => return Ordering::Greater,
(None, Some(_)) => return Ordering::Less,
(None, None) => return Ordering::Equal,
}
}
}

#[test]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be moved into a

#[cfg(test)]
mod tests {
}

block at the bottom of the file.

fn test_numeric_cmp() {
let expected = [
"hi-1.png",
"hi-01.png",
"hi-2.png",
"hi-003.png",
"hi-4.png",
"hi-11.png",
];

let mut example = [
"hi-003.png",
"hi-01.png",
"hi-1.png",
"hi-11.png",
"hi-2.png",
"hi-4.png",
];

example.sort_by(|a, b| numeric_cmp(a, b));

assert_eq!(example, expected);
}

fn reflow(
cx: &mut compositor::Context,
args: &[Cow<str>],
Expand Down Expand Up @@ -2033,6 +2141,20 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
fun: sort_reverse,
completer: None,
},
TypableCommand {
name: "sortn",
aliases: &[],
doc: "Sort ranges in selection in alphanumeric order.",
fun: sort_numeric,
completer: None,
},
TypableCommand {
name: "rsortn",
aliases: &[],
doc: "Sort ranges in selection in reverse order in alphanumeric order.",
fun: sort_numeric_reverse,
completer: None,
},
TypableCommand {
name: "reflow",
aliases: &[],
Expand Down