Skip to content

Commit

Permalink
Version gate raw identifier use statement sorting
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
ytmimi committed Jun 13, 2022
1 parent 397f0d5 commit 902a18e
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 7 deletions.
24 changes: 18 additions & 6 deletions src/imports.rs
Expand Up @@ -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;
Expand All @@ -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,
}
}
Expand Down
@@ -1,3 +1,5 @@
// rustfmt-version:One

use websocket::client::ClientBuilder;
use websocket::r#async::futures::Stream;
use websocket::result::WebSocketError;
@@ -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;
5 changes: 5 additions & 0 deletions tests/target/imports_raw_identifiers/version_One.rs
@@ -0,0 +1,5 @@
// rustfmt-version:One

use websocket::client::ClientBuilder;
use websocket::r#async::futures::Stream;
use websocket::result::WebSocketError;
5 changes: 5 additions & 0 deletions tests/target/imports_raw_identifiers/version_Two.rs
@@ -0,0 +1,5 @@
// rustfmt-version:Two

use websocket::r#async::futures::Stream;
use websocket::client::ClientBuilder;
use websocket::result::WebSocketError;

0 comments on commit 902a18e

Please sign in to comment.