Version gate raw identifier use statement sorting

When useing `version=One` rustfmt will treat the leading `r#` as part of
the `UseSegment` used for sorting. When using `version=Two` rustfmt will
ignore the leading `r#` and only consider the name of the identifier
when sorting the `UseSegment`.
This commit is contained in:
Yacin Tmimi 2022-06-13 11:36:20 -04:00 committed by Caleb Cartwright
parent 795efb2068
commit e44380b341
5 changed files with 33 additions and 7 deletions

View File

@ -890,14 +890,21 @@ impl Ord for UseSegment {
| (Super(ref a), Super(ref b))
| (Crate(ref a), Crate(ref b)) => match (a, b) {
(Some(sa), Some(sb)) => {
sa.trim_start_matches("r#").cmp(sb.trim_start_matches("r#"))
if self.version == Version::Two {
sa.trim_start_matches("r#").cmp(sb.trim_start_matches("r#"))
} else {
a.cmp(b)
}
}
(_, _) => a.cmp(b),
},
(Glob, Glob) => Ordering::Equal,
(Ident(ref pia, ref aa), Ident(ref pib, ref ab)) => {
let ia = pia.trim_start_matches("r#");
let ib = pib.trim_start_matches("r#");
let (ia, ib) = if self.version == Version::Two {
(pia.trim_start_matches("r#"), pib.trim_start_matches("r#"))
} else {
(pia.as_str(), pib.as_str())
};
// snake_case < CamelCase < UPPER_SNAKE_CASE
if ia.starts_with(char::is_uppercase) && ib.starts_with(char::is_lowercase) {
return Ordering::Greater;
@ -918,9 +925,14 @@ impl Ord for UseSegment {
match (aa, ab) {
(None, Some(_)) => Ordering::Less,
(Some(_), None) => Ordering::Greater,
(Some(aas), Some(abs)) => aas
.trim_start_matches("r#")
.cmp(abs.trim_start_matches("r#")),
(Some(aas), Some(abs)) => {
if self.version == Version::Two {
aas.trim_start_matches("r#")
.cmp(abs.trim_start_matches("r#"))
} else {
aas.cmp(abs)
}
}
(None, None) => Ordering::Equal,
}
}

View File

@ -1,3 +1,5 @@
// rustfmt-version:One
use websocket::client::ClientBuilder;
use websocket::r#async::futures::Stream;
use websocket::result::WebSocketError;

View File

@ -1,3 +1,5 @@
use websocket::r#async::futures::Stream;
// rustfmt-version:Two
use websocket::client::ClientBuilder;
use websocket::r#async::futures::Stream;
use websocket::result::WebSocketError;

View File

@ -0,0 +1,5 @@
// rustfmt-version:One
use websocket::client::ClientBuilder;
use websocket::r#async::futures::Stream;
use websocket::result::WebSocketError;

View File

@ -0,0 +1,5 @@
// rustfmt-version:Two
use websocket::r#async::futures::Stream;
use websocket::client::ClientBuilder;
use websocket::result::WebSocketError;