From 84a3b52a108cfa3763a77373a15114986f784489 Mon Sep 17 00:00:00 2001
From: davidsemakula <hello@davidsemakula.com>
Date: Wed, 17 Jan 2024 21:46:19 +0300
Subject: [PATCH] properly order raw idents when ordering use trees

---
 crates/ide-db/src/imports/insert_use/tests.rs | 83 +++++++++++++++++--
 crates/ide-db/src/imports/merge_imports.rs    | 10 +--
 2 files changed, 82 insertions(+), 11 deletions(-)

diff --git a/crates/ide-db/src/imports/insert_use/tests.rs b/crates/ide-db/src/imports/insert_use/tests.rs
index 4e626ada83e4..13d3e501e75d 100644
--- a/crates/ide-db/src/imports/insert_use/tests.rs
+++ b/crates/ide-db/src/imports/insert_use/tests.rs
@@ -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]
@@ -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]
@@ -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]
@@ -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]
@@ -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]
@@ -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]
@@ -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]
diff --git a/crates/ide-db/src/imports/merge_imports.rs b/crates/ide-db/src/imports/merge_imports.rs
index 740c50f79f12..f647896bf630 100644
--- a/crates/ide-db/src/imports/merge_imports.rs
+++ b/crates/ide-db/src/imports/merge_imports.rs
@@ -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;
@@ -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)
                     {
@@ -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#")),
                 ),
         },
     };