properly order raw idents when ordering use trees

This commit is contained in:
davidsemakula 2024-01-17 21:46:19 +03:00
parent 5b62ebc23f
commit 84a3b52a10
2 changed files with 82 additions and 11 deletions

View file

@ -137,6 +137,16 @@ fn insert_start_indent() {
use std::bar::B; use std::bar::B;
use std::bar::C;", use std::bar::C;",
); );
check_none(
"std::bar::r#AA",
r"
use std::bar::B;
use std::bar::C;",
r"
use std::bar::r#AA;
use std::bar::B;
use std::bar::C;",
);
} }
#[test] #[test]
@ -173,7 +183,21 @@ fn insert_middle_indent() {
use std::bar::EE; use std::bar::EE;
use std::bar::F; use std::bar::F;
use std::bar::G;", use std::bar::G;",
) );
check_none(
"std::bar::r#EE",
r"
use std::bar::A;
use std::bar::D;
use std::bar::F;
use std::bar::G;",
r"
use std::bar::A;
use std::bar::D;
use std::bar::r#EE;
use std::bar::F;
use std::bar::G;",
);
} }
#[test] #[test]
@ -210,7 +234,21 @@ fn insert_end_indent() {
use std::bar::F; use std::bar::F;
use std::bar::G; use std::bar::G;
use std::bar::ZZ;", use std::bar::ZZ;",
) );
check_none(
"std::bar::r#ZZ",
r"
use std::bar::A;
use std::bar::D;
use std::bar::F;
use std::bar::G;",
r"
use std::bar::A;
use std::bar::D;
use std::bar::F;
use std::bar::G;
use std::bar::r#ZZ;",
);
} }
#[test] #[test]
@ -228,7 +266,21 @@ use std::bar::EE;
use std::bar::{D, Z}; // example of weird imports due to user use std::bar::{D, Z}; // example of weird imports due to user
use std::bar::F; use std::bar::F;
use std::bar::G;", use std::bar::G;",
) );
check_none(
"std::bar::r#EE",
r"
use std::bar::A;
use std::bar::{D, Z}; // example of weird imports due to user
use std::bar::F;
use std::bar::G;",
r"
use std::bar::A;
use std::bar::r#EE;
use std::bar::{D, Z}; // example of weird imports due to user
use std::bar::F;
use std::bar::G;",
);
} }
#[test] #[test]
@ -596,7 +648,16 @@ fn merge_groups_full() {
#[test] #[test]
fn merge_groups_long_full() { fn merge_groups_long_full() {
check_crate("std::foo::bar::Baz", r"use std::foo::bar::Qux;", r"use std::foo::bar::{Baz, Qux};") check_crate(
"std::foo::bar::Baz",
r"use std::foo::bar::Qux;",
r"use std::foo::bar::{Baz, Qux};",
);
check_crate(
"std::foo::bar::r#Baz",
r"use std::foo::bar::Qux;",
r"use std::foo::bar::{r#Baz, Qux};",
);
} }
#[test] #[test]
@ -614,7 +675,12 @@ fn merge_groups_long_full_list() {
"std::foo::bar::Baz", "std::foo::bar::Baz",
r"use std::foo::bar::{Qux, Quux};", r"use std::foo::bar::{Qux, Quux};",
r"use std::foo::bar::{Baz, Quux, Qux};", r"use std::foo::bar::{Baz, Quux, Qux};",
) );
check_crate(
"std::foo::bar::r#Baz",
r"use std::foo::bar::{Qux, Quux};",
r"use std::foo::bar::{r#Baz, Quux, Qux};",
);
} }
#[test] #[test]
@ -632,7 +698,12 @@ fn merge_groups_long_full_nested() {
"std::foo::bar::Baz", "std::foo::bar::Baz",
r"use std::foo::bar::{Qux, quux::{Fez, Fizz}};", r"use std::foo::bar::{Qux, quux::{Fez, Fizz}};",
r"use std::foo::bar::{quux::{Fez, Fizz}, Baz, Qux};", r"use std::foo::bar::{quux::{Fez, Fizz}, Baz, Qux};",
) );
check_crate(
"std::foo::bar::r#Baz",
r"use std::foo::bar::{Qux, quux::{Fez, Fizz}};",
r"use std::foo::bar::{quux::{Fez, Fizz}, r#Baz, Qux};",
);
} }
#[test] #[test]

View file

@ -9,7 +9,7 @@ use syntax::{
algo, algo,
ast::{self, make, AstNode, HasAttrs, HasName, HasVisibility, PathSegmentKind}, ast::{self, make, AstNode, HasAttrs, HasName, HasVisibility, PathSegmentKind},
ted::{self, Position}, ted::{self, Position},
Direction, TokenText, Direction,
}; };
use crate::syntax_helpers::node_ext::vis_eq; use crate::syntax_helpers::node_ext::vis_eq;
@ -339,8 +339,8 @@ fn path_segment_cmp(a: &ast::PathSegment, b: &ast::PathSegment) -> Ordering {
(None, Some(_)) => Ordering::Less, (None, Some(_)) => Ordering::Less,
(Some(a_name), Some(b_name)) => { (Some(a_name), Some(b_name)) => {
// snake_case < CamelCase < UPPER_SNAKE_CASE // snake_case < CamelCase < UPPER_SNAKE_CASE
let a_text = a_name.as_str(); let a_text = a_name.as_str().trim_start_matches("r#");
let b_text = b_name.as_str(); let b_text = b_name.as_str().trim_start_matches("r#");
if a_text.starts_with(char::is_lowercase) if a_text.starts_with(char::is_lowercase)
&& b_text.starts_with(char::is_uppercase) && b_text.starts_with(char::is_uppercase)
{ {
@ -388,14 +388,14 @@ fn use_tree_cmp_by_tree_list_glob_or_alias(
.as_ref() .as_ref()
.map(ast::Name::text) .map(ast::Name::text)
.as_ref() .as_ref()
.map_or("_", TokenText::as_str) .map_or("_", |a_name| a_name.as_str().trim_start_matches("r#"))
.cmp( .cmp(
b_rename b_rename
.name() .name()
.as_ref() .as_ref()
.map(ast::Name::text) .map(ast::Name::text)
.as_ref() .as_ref()
.map_or("_", TokenText::as_str), .map_or("_", |b_name| b_name.as_str().trim_start_matches("r#")),
), ),
}, },
}; };