-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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( | ||
|
@@ -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<()> { | ||
let (view, doc) = current!(cx.editor); | ||
let text = doc.text().slice(..); | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 So this function would replace the We could also use this opportunity to drop the |
||
|
||
let transaction = Transaction::change( | ||
|
@@ -1407,6 +1434,62 @@ 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, | ||
} | ||
} | ||
} | ||
|
||
fn reflow( | ||
cx: &mut compositor::Context, | ||
args: &[Cow<str>], | ||
|
@@ -2033,6 +2116,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 alphanumeric order.", | ||
fun: sort_numeric_reverse, | ||
completer: None, | ||
}, | ||
TypableCommand { | ||
name: "reflow", | ||
aliases: &[], | ||
|
@@ -2205,3 +2302,31 @@ pub fn command_mode(cx: &mut Context) { | |
prompt.recalculate_completion(cx.editor); | ||
cx.push_layer(Box::new(prompt)); | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
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| super::numeric_cmp(a, b)); | ||
|
||
assert_eq!(example, expected); | ||
} | ||
} |
There was a problem hiding this comment.
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
andalphanumeric
parameters and pass a single parameter calledcmp_fn
which is aFnMut(&Tendril, &Tendril) -> Ordering
leaving to the caller to say how it want the ordering.See: #8436