Skip to content

Commit

Permalink
Auto merge of #16389 - davidsemakula:order-use-tree-raw-ident, r=lnicola
Browse files Browse the repository at this point in the history
internal: properly order raw idents when ordering use trees

Follow up to #16352 to properly order raw identifiers.
  • Loading branch information
bors committed Jan 17, 2024
2 parents a19372f + 84a3b52 commit 9d9b343
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 11 deletions.
83 changes: 77 additions & 6 deletions crates/ide-db/src/imports/insert_use/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,16 @@ fn insert_start_indent() {
use std::bar::B;
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]
Expand Down Expand Up @@ -173,7 +183,21 @@ fn insert_middle_indent() {
use std::bar::EE;
use std::bar::F;
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]
Expand Down Expand Up @@ -210,7 +234,21 @@ fn insert_end_indent() {
use std::bar::F;
use std::bar::G;
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]
Expand All @@ -228,7 +266,21 @@ use std::bar::EE;
use std::bar::{D, Z}; // example of weird imports due to user
use std::bar::F;
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]
Expand Down Expand Up @@ -596,7 +648,16 @@ fn merge_groups_full() {

#[test]
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]
Expand All @@ -614,7 +675,12 @@ fn merge_groups_long_full_list() {
"std::foo::bar::Baz",
r"use std::foo::bar::{Qux, Quux};",
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]
Expand All @@ -632,7 +698,12 @@ fn merge_groups_long_full_nested() {
"std::foo::bar::Baz",
r"use std::foo::bar::{Qux, quux::{Fez, Fizz}};",
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]
Expand Down
10 changes: 5 additions & 5 deletions crates/ide-db/src/imports/merge_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use syntax::{
algo,
ast::{self, make, AstNode, HasAttrs, HasName, HasVisibility, PathSegmentKind},
ted::{self, Position},
Direction, TokenText,
Direction,
};

use crate::syntax_helpers::node_ext::vis_eq;
Expand Down Expand Up @@ -339,8 +339,8 @@ fn path_segment_cmp(a: &ast::PathSegment, b: &ast::PathSegment) -> Ordering {
(None, Some(_)) => Ordering::Less,
(Some(a_name), Some(b_name)) => {
// snake_case < CamelCase < UPPER_SNAKE_CASE
let a_text = a_name.as_str();
let b_text = b_name.as_str();
let a_text = a_name.as_str().trim_start_matches("r#");
let b_text = b_name.as_str().trim_start_matches("r#");
if a_text.starts_with(char::is_lowercase)
&& b_text.starts_with(char::is_uppercase)
{
Expand Down Expand Up @@ -388,14 +388,14 @@ fn use_tree_cmp_by_tree_list_glob_or_alias(
.as_ref()
.map(ast::Name::text)
.as_ref()
.map_or("_", TokenText::as_str)
.map_or("_", |a_name| a_name.as_str().trim_start_matches("r#"))
.cmp(
b_rename
.name()
.as_ref()
.map(ast::Name::text)
.as_ref()
.map_or("_", TokenText::as_str),
.map_or("_", |b_name| b_name.as_str().trim_start_matches("r#")),
),
},
};
Expand Down

0 comments on commit 9d9b343

Please sign in to comment.