Skip to content

Commit e44380b

Browse files
ytmimicalebcartwright
authored andcommitted
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`.
1 parent 795efb2 commit e44380b

File tree

5 files changed

+33
-7
lines changed

5 files changed

+33
-7
lines changed

src/imports.rs

+18-6
Original file line numberDiff line numberDiff line change
@@ -890,14 +890,21 @@ impl Ord for UseSegment {
890890
| (Super(ref a), Super(ref b))
891891
| (Crate(ref a), Crate(ref b)) => match (a, b) {
892892
(Some(sa), Some(sb)) => {
893-
sa.trim_start_matches("r#").cmp(sb.trim_start_matches("r#"))
893+
if self.version == Version::Two {
894+
sa.trim_start_matches("r#").cmp(sb.trim_start_matches("r#"))
895+
} else {
896+
a.cmp(b)
897+
}
894898
}
895899
(_, _) => a.cmp(b),
896900
},
897901
(Glob, Glob) => Ordering::Equal,
898902
(Ident(ref pia, ref aa), Ident(ref pib, ref ab)) => {
899-
let ia = pia.trim_start_matches("r#");
900-
let ib = pib.trim_start_matches("r#");
903+
let (ia, ib) = if self.version == Version::Two {
904+
(pia.trim_start_matches("r#"), pib.trim_start_matches("r#"))
905+
} else {
906+
(pia.as_str(), pib.as_str())
907+
};
901908
// snake_case < CamelCase < UPPER_SNAKE_CASE
902909
if ia.starts_with(char::is_uppercase) && ib.starts_with(char::is_lowercase) {
903910
return Ordering::Greater;
@@ -918,9 +925,14 @@ impl Ord for UseSegment {
918925
match (aa, ab) {
919926
(None, Some(_)) => Ordering::Less,
920927
(Some(_), None) => Ordering::Greater,
921-
(Some(aas), Some(abs)) => aas
922-
.trim_start_matches("r#")
923-
.cmp(abs.trim_start_matches("r#")),
928+
(Some(aas), Some(abs)) => {
929+
if self.version == Version::Two {
930+
aas.trim_start_matches("r#")
931+
.cmp(abs.trim_start_matches("r#"))
932+
} else {
933+
aas.cmp(abs)
934+
}
935+
}
924936
(None, None) => Ordering::Equal,
925937
}
926938
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// rustfmt-version:One
2+
13
use websocket::client::ClientBuilder;
24
use websocket::r#async::futures::Stream;
35
use websocket::result::WebSocketError;
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
use websocket::r#async::futures::Stream;
1+
// rustfmt-version:Two
2+
23
use websocket::client::ClientBuilder;
4+
use websocket::r#async::futures::Stream;
35
use websocket::result::WebSocketError;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// rustfmt-version:One
2+
3+
use websocket::client::ClientBuilder;
4+
use websocket::r#async::futures::Stream;
5+
use websocket::result::WebSocketError;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// rustfmt-version:Two
2+
3+
use websocket::r#async::futures::Stream;
4+
use websocket::client::ClientBuilder;
5+
use websocket::result::WebSocketError;

0 commit comments

Comments
 (0)