ra_ap_ide_db 0.0.338

Core data structure representing IDE state for rust-analyzer.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
//! Handle syntactic aspects of merging UseTrees.
use std::cmp::Ordering;

use itertools::{EitherOrBoth, Itertools};
use parser::T;
use syntax::{
    ToSmolStr,
    ast::{
        self, AstNode, HasAttrs, HasName, HasVisibility, PathSegmentKind,
        syntax_factory::SyntaxFactory,
    },
    syntax_editor::{Position, SyntaxEditor},
};

use crate::syntax_helpers::node_ext::vis_eq;

/// What type of merges are allowed.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum MergeBehavior {
    /// Merge imports from the same crate into a single use statement.
    Crate,
    /// Merge imports from the same module into a single use statement.
    Module,
    /// Merge all imports into a single use statement as long as they have the same visibility
    /// and attributes.
    One,
}

impl MergeBehavior {
    fn is_tree_allowed(&self, tree: &ast::UseTree) -> bool {
        match self {
            MergeBehavior::Crate | MergeBehavior::One => true,
            // only simple single segment paths are allowed
            MergeBehavior::Module => {
                tree.use_tree_list().is_none() && tree.path().map(path_len) <= Some(1)
            }
        }
    }
}

/// Merge `rhs` into `lhs` keeping both intact.
pub fn try_merge_imports(
    make: &SyntaxFactory,
    lhs: &ast::Use,
    rhs: &ast::Use,
    merge_behavior: MergeBehavior,
) -> Option<ast::Use> {
    // don't merge imports with different visibilities
    if !eq_visibility(lhs.visibility(), rhs.visibility()) {
        return None;
    }
    if !eq_attrs(lhs.attrs(), rhs.attrs()) {
        return None;
    }

    let lhs_tree = lhs.use_tree()?;
    let rhs_tree = rhs.use_tree()?;
    let merged_tree = try_merge_trees_with_factory(lhs_tree, rhs_tree, merge_behavior, make)?;

    // Ignore `None` result because normalization should not affect the merge result.
    let use_tree = try_normalize_use_tree(merged_tree.clone(), merge_behavior.into(), make)
        .unwrap_or(merged_tree);

    make_use_with_tree(lhs, use_tree)
}

/// Merge `rhs` into `lhs` keeping both intact.
pub fn try_merge_trees(
    make: &SyntaxFactory,
    lhs: &ast::UseTree,
    rhs: &ast::UseTree,
    merge: MergeBehavior,
) -> Option<ast::UseTree> {
    let merged = try_merge_trees_with_factory(lhs.clone(), rhs.clone(), merge, make)?;

    // Ignore `None` result because normalization should not affect the merge result.
    Some(try_normalize_use_tree(merged.clone(), merge.into(), make).unwrap_or(merged))
}

fn try_merge_trees_with_factory(
    mut lhs: ast::UseTree,
    mut rhs: ast::UseTree,
    merge: MergeBehavior,
    make: &SyntaxFactory,
) -> Option<ast::UseTree> {
    if merge == MergeBehavior::One {
        lhs = wrap_in_tree_list(&lhs, make).unwrap_or(lhs);
        rhs = wrap_in_tree_list(&rhs, make).unwrap_or(rhs);
    } else {
        let lhs_path = lhs.path()?;
        let rhs_path = rhs.path()?;

        let (lhs_prefix, rhs_prefix) = common_prefix(&lhs_path, &rhs_path)?;
        if lhs.is_simple_path()
            && rhs.is_simple_path()
            && lhs_path == lhs_prefix
            && rhs_path == rhs_prefix
        {
            // we can't merge if the renames are different (`A as a` and `A as b`),
            // and we can safely return here
            let lhs_name = lhs.rename().and_then(|lhs_name| lhs_name.name());
            let rhs_name = rhs.rename().and_then(|rhs_name| rhs_name.name());
            if lhs_name.as_ref().map(|name| name.text())
                != rhs_name.as_ref().map(|name| name.text())
            {
                return None;
            }

            return Some(rhs);
        } else {
            lhs = split_prefix(&lhs, &lhs_prefix, make)?;
            rhs = split_prefix(&rhs, &rhs_prefix, make)?;
        }
    }
    recursive_merge(lhs, rhs, merge, make)
}

/// Recursively merges rhs to lhs
#[must_use]
fn recursive_merge(
    lhs: ast::UseTree,
    rhs: ast::UseTree,
    merge: MergeBehavior,
    make: &SyntaxFactory,
) -> Option<ast::UseTree> {
    let mut use_trees: Vec<ast::UseTree> = lhs
        .use_tree_list()?
        .use_trees()
        // We use Option here to early return from this function. This is not the
        // same as a `filter` op.
        .map(|tree| merge.is_tree_allowed(&tree).then_some(tree))
        .collect::<Option<_>>()?;

    // Sorts the use trees similar to rustfmt's algorithm for ordering imports
    // (see `use_tree_cmp` doc).
    use_trees.sort_unstable_by(use_tree_cmp);

    for rhs_t in rhs.use_tree_list()?.use_trees() {
        if !merge.is_tree_allowed(&rhs_t) {
            return None;
        }

        match use_trees.binary_search_by(|lhs_t| use_tree_cmp_bin_search(lhs_t, &rhs_t)) {
            Ok(idx) => {
                let mut lhs_t = use_trees[idx].clone();
                let lhs_path = lhs_t.path()?;
                let rhs_path = rhs_t.path()?;
                let (lhs_prefix, rhs_prefix) = common_prefix(&lhs_path, &rhs_path)?;

                if lhs_prefix == lhs_path && rhs_prefix == rhs_path {
                    let tree_is_self = |tree: &ast::UseTree| {
                        tree.path().as_ref().map(path_is_self).unwrap_or(false)
                    };
                    // Check if only one of the two trees has a tree list, and
                    // whether that then contains `self` or not. If this is the
                    // case we can skip this iteration since the path without
                    // the list is already included in the other one via `self`.
                    let tree_contains_self = |tree: &ast::UseTree| {
                        tree.use_tree_list()
                            .map(|tree_list| tree_list.use_trees().any(|it| tree_is_self(&it)))
                            // Glob imports aren't part of the use-tree lists,
                            // so they need to be handled explicitly
                            .or_else(|| tree.star_token().map(|_| false))
                    };

                    if lhs_t.rename().and_then(|x| x.underscore_token()).is_some() {
                        use_trees[idx] = rhs_t;
                        continue;
                    }

                    match (tree_contains_self(&lhs_t), tree_contains_self(&rhs_t)) {
                        (Some(true), None) => {
                            lhs_t = remove_subtree_if_only_self(lhs_t, make)?;
                            use_trees[idx] = lhs_t;
                            continue;
                        }
                        (None, Some(true)) => {
                            lhs_t = rhs_t;
                            lhs_t = remove_subtree_if_only_self(lhs_t, make)?;
                            use_trees[idx] = lhs_t;
                            continue;
                        }
                        _ => (),
                    }

                    if lhs_t.is_simple_path() && rhs_t.is_simple_path() {
                        continue;
                    }
                }

                lhs_t = split_prefix(&lhs_t, &lhs_prefix, make)?;
                let rhs_t = split_prefix(&rhs_t, &rhs_prefix, make)?;
                lhs_t = recursive_merge(lhs_t, rhs_t, merge, make)?;
                use_trees[idx] = lhs_t;
            }
            Err(_)
                if merge == MergeBehavior::Module
                    && !use_trees.is_empty()
                    && rhs_t.use_tree_list().is_some() =>
            {
                return None;
            }
            Err(insert_idx) => {
                use_trees.insert(insert_idx, rhs_t);
            }
        }
    }

    with_use_tree_list(&lhs, use_trees, make)
}

/// Style to follow when normalizing a use tree.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum NormalizationStyle {
    /// Merges all descendant use tree lists with only one child use tree into their parent use tree.
    ///
    /// Examples:
    /// - `foo::{bar::{Qux}}` -> `foo::bar::Qux`
    /// - `foo::{bar::{self}}` -> `foo::bar`
    /// - `{foo::bar}` -> `foo::bar`
    Default,
    /// Same as default but wraps the root use tree in a use tree list.
    ///
    /// Examples:
    /// - `foo::{bar::{Qux}}` -> `{foo::bar::Qux}`
    /// - `foo::{bar::{self}}` -> `{foo::bar}`
    /// - `{foo::bar}` -> `{foo::bar}`
    One,
}

impl From<MergeBehavior> for NormalizationStyle {
    fn from(mb: MergeBehavior) -> Self {
        match mb {
            MergeBehavior::One => NormalizationStyle::One,
            _ => NormalizationStyle::Default,
        }
    }
}

/// Normalizes a use item by:
/// - Ordering all use trees
/// - Merging use trees with common prefixes
/// - Removing redundant braces based on the specified normalization style
///   (see [`NormalizationStyle`] doc)
///
/// Examples:
///
/// Using the "Default" normalization style
///
/// - `foo::{bar::Qux, bar::{self}}` -> `foo::bar::{self, Qux}`
/// - `foo::bar::{self}` -> `foo::bar`
/// - `{foo::bar}` -> `foo::bar`
///
/// Using the "One" normalization style
///
/// - `foo::{bar::Qux, bar::{self}}` -> `{foo::bar::{self, Qux}}`
/// - `foo::bar::{self}` -> `{foo::bar}`
/// - `foo::bar` -> `{foo::bar}`
pub fn try_normalize_import(
    make: &SyntaxFactory,
    use_item: &ast::Use,
    style: NormalizationStyle,
) -> Option<ast::Use> {
    let use_tree = try_normalize_use_tree(use_item.use_tree()?, style, make)?;

    make_use_with_tree(use_item, use_tree)
}

fn try_normalize_use_tree(
    use_tree: ast::UseTree,
    style: NormalizationStyle,
    make: &SyntaxFactory,
) -> Option<ast::UseTree> {
    if style == NormalizationStyle::One {
        let mut use_tree = use_tree;
        let mut modified = false;
        if let Some(wrapped) = wrap_in_tree_list(&use_tree, make) {
            use_tree = wrapped;
            modified = true;
        }
        if let Some(normalized) = recursive_normalize(use_tree.clone(), style, make) {
            use_tree = normalized;
            modified = true;
        }
        return modified.then_some(use_tree);
    }

    recursive_normalize(use_tree, NormalizationStyle::Default, make)
}

/// Recursively normalizes a use tree and its subtrees (if any).
fn recursive_normalize(
    use_tree: ast::UseTree,
    style: NormalizationStyle,
    make: &SyntaxFactory,
) -> Option<ast::UseTree> {
    let use_tree_list = use_tree.use_tree_list()?;
    let mut subtrees = use_tree_list.use_trees().collect::<Vec<_>>();
    if subtrees.len() == 1 {
        if style == NormalizationStyle::One {
            let subtree = subtrees.pop()?;
            let normalized = recursive_normalize(subtree, NormalizationStyle::Default, make)?;
            return with_use_tree_list(&use_tree, vec![normalized], make);
        }

        let merged = merge_single_subtree_into_parent_tree(use_tree, make)?;
        return Some(recursive_normalize(merged.clone(), style, make).unwrap_or(merged));
    }

    let mut modified = false;
    let mut new_use_tree_list = Vec::new();
    for subtree in subtrees {
        if one_style_tree_list(&subtree).is_some() {
            let mut elements = Vec::new();
            flatten_one_style_tree(subtree, &mut elements, &mut modified, make);
            new_use_tree_list.extend(elements);
            modified = true;
        } else if let Some(normalized) =
            recursive_normalize(subtree.clone(), NormalizationStyle::Default, make)
        {
            new_use_tree_list.push(normalized);
            modified = true;
        } else {
            new_use_tree_list.push(subtree);
        }
    }

    let mut use_tree =
        if modified { with_use_tree_list(&use_tree, new_use_tree_list, make)? } else { use_tree };

    let mut use_tree_list = use_tree.use_tree_list()?.use_trees().collect::<Vec<_>>();
    let mut anchor_idx = 0;
    let mut merged_any = false;
    while anchor_idx < use_tree_list.len() {
        let mut candidate_idx = anchor_idx + 1;
        while candidate_idx < use_tree_list.len() {
            if let Some(mut merged) = try_merge_trees_with_factory(
                use_tree_list[anchor_idx].clone(),
                use_tree_list[candidate_idx].clone(),
                MergeBehavior::Crate,
                make,
            ) {
                if let Some(normalized) =
                    recursive_normalize(merged.clone(), NormalizationStyle::Default, make)
                {
                    merged = normalized;
                }

                use_tree_list[anchor_idx] = merged;
                use_tree_list.remove(candidate_idx);
                merged_any = true;
            } else {
                candidate_idx += 1;
            }
        }

        anchor_idx += 1;
    }
    if merged_any {
        use_tree = with_use_tree_list(&use_tree, use_tree_list, make)?;
        modified = true;
    }

    if style != NormalizationStyle::One {
        let subtrees = use_tree.use_tree_list()?.use_trees().collect::<Vec<_>>();
        if subtrees.len() == 1
            && let Some(merged) = merge_single_subtree_into_parent_tree(use_tree.clone(), make)
        {
            use_tree = merged;
            modified = true;
        }
    }

    if let Some(list) = use_tree.use_tree_list() {
        let mut use_tree_list = list.use_trees().collect::<Vec<_>>();
        if use_tree_list
            .windows(2)
            .any(|trees| use_tree_cmp_bin_search(&trees[0], &trees[1]).is_gt())
        {
            use_tree_list.sort_unstable_by(use_tree_cmp_bin_search);
            use_tree = with_use_tree_list(&use_tree, use_tree_list, make)?;
            modified = true;
        }
    }

    modified.then_some(use_tree)
}

fn flatten_one_style_tree(
    subtree: ast::UseTree,
    elements: &mut Vec<ast::UseTree>,
    modified: &mut bool,
    make: &SyntaxFactory,
) {
    let Some(one_tree_list) = one_style_tree_list(&subtree) else { return };
    let mut one_tree_list_iter = one_tree_list.use_trees();
    let mut prev_skipped = Vec::new();
    loop {
        let mut prev_skipped_iter = prev_skipped.into_iter();
        let mut curr_skipped = Vec::new();

        while let Some(sub_sub_tree) =
            one_tree_list_iter.next().or_else(|| prev_skipped_iter.next())
        {
            if let Some(sub_one_tree_list) = one_style_tree_list(&sub_sub_tree) {
                curr_skipped.extend(sub_one_tree_list.use_trees());
            } else if let Some(normalized) =
                recursive_normalize(sub_sub_tree.clone(), NormalizationStyle::Default, make)
            {
                *modified = true;
                elements.push(normalized);
            } else {
                elements.push(sub_sub_tree);
            }
        }

        if curr_skipped.is_empty() {
            break;
        }
        prev_skipped = curr_skipped;
    }
}

fn merge_single_subtree_into_parent_tree(
    use_tree: ast::UseTree,
    make: &SyntaxFactory,
) -> Option<ast::UseTree> {
    let single_subtree = get_single_subtree(&use_tree)?;
    let subtree_is_only_self = single_subtree.path().as_ref().is_some_and(path_is_self);

    let merged_path = match (use_tree.path(), single_subtree.path()) {
        _ if subtree_is_only_self => None,
        (None, None) => None,
        (Some(outer), None) => Some(outer),
        (None, Some(inner)) => Some(inner),
        (Some(outer), Some(inner)) => Some(make.path_concat(outer, inner)),
    };

    let list = single_subtree.use_tree_list();
    let list_is_none = list.is_none();
    let star = single_subtree.star_token().is_some();
    if merged_path.is_some() || list.is_some() || star {
        let rename = (!star && list_is_none).then(|| single_subtree.rename()).flatten();
        make_use_tree_from_parts(make, merged_path, list, rename, star)
    } else {
        None
    }
}

fn one_style_tree_list(subtree: &ast::UseTree) -> Option<ast::UseTreeList> {
    (subtree.path().is_none() && subtree.star_token().is_none() && subtree.rename().is_none())
        .then(|| subtree.use_tree_list())
        .flatten()
}

fn remove_subtree_if_only_self(
    use_tree: ast::UseTree,
    make: &SyntaxFactory,
) -> Option<ast::UseTree> {
    let Some(single_subtree) = get_single_subtree(&use_tree) else {
        return Some(use_tree);
    };
    match (use_tree.path(), single_subtree.path()) {
        (Some(path), Some(inner)) if path_is_self(&inner) => {
            Some(make.use_tree(path, None, use_tree.rename(), false))
        }
        _ => Some(use_tree),
    }
}

/// Traverses both paths until they differ, returning the common prefix of both.
pub fn common_prefix(lhs: &ast::Path, rhs: &ast::Path) -> Option<(ast::Path, ast::Path)> {
    let mut res = None;
    let mut lhs_curr = lhs.first_qualifier_or_self();
    let mut rhs_curr = rhs.first_qualifier_or_self();
    loop {
        match (lhs_curr.segment(), rhs_curr.segment()) {
            (Some(lhs), Some(rhs)) if lhs.syntax().text() == rhs.syntax().text() => (),
            _ => break res,
        }
        res = Some((lhs_curr.clone(), rhs_curr.clone()));

        match lhs_curr.parent_path().zip(rhs_curr.parent_path()) {
            Some((lhs, rhs)) => {
                lhs_curr = lhs;
                rhs_curr = rhs;
            }
            _ => break res,
        }
    }
}

/// Use tree comparison func for binary searching for merging.
fn use_tree_cmp_bin_search(lhs: &ast::UseTree, rhs: &ast::UseTree) -> Ordering {
    let lhs_is_simple_path = lhs.is_simple_path() && lhs.rename().is_none();
    let rhs_is_simple_path = rhs.is_simple_path() && rhs.rename().is_none();
    let lhs_segment = lhs.path().and_then(|path| path.first_segment());
    let rhs_segment = rhs.path().and_then(|path| path.first_segment());
    match (lhs_segment, rhs_segment) {
        (None, None) => match (lhs_is_simple_path, rhs_is_simple_path) {
            (true, true) => Ordering::Equal,
            (true, false) => Ordering::Less,
            (false, true) => Ordering::Greater,
            (false, false) => use_tree_cmp_by_tree_list_glob_or_alias(lhs, rhs, false),
        },
        (Some(_), None) if !rhs_is_simple_path => Ordering::Less,
        (Some(_), None) => Ordering::Greater,
        (None, Some(_)) if !lhs_is_simple_path => Ordering::Greater,
        (None, Some(_)) => Ordering::Less,
        (Some(a), Some(b)) => path_segment_cmp(&a, &b),
    }
}

/// Orders use trees following `rustfmt`'s version sorting algorithm for ordering imports.
///
/// Example: `foo::{self, Baz, FOO_BAZ, Qux, baz, foo, *, {Bar}}`
///
/// Ref:
///   - <https://doc.rust-lang.org/style-guide/index.html#sorting>
///   - <https://doc.rust-lang.org/edition-guide/rust-2024/rustfmt.html>
pub(super) fn use_tree_cmp(a: &ast::UseTree, b: &ast::UseTree) -> Ordering {
    let a_is_simple_path = a.is_simple_path() && a.rename().is_none();
    let b_is_simple_path = b.is_simple_path() && b.rename().is_none();
    match (a.path(), b.path()) {
        (None, None) => match (a_is_simple_path, b_is_simple_path) {
            (true, true) => Ordering::Equal,
            (true, false) => Ordering::Less,
            (false, true) => Ordering::Greater,
            (false, false) => use_tree_cmp_by_tree_list_glob_or_alias(a, b, true),
        },
        (Some(_), None) if !b_is_simple_path => Ordering::Less,
        (Some(_), None) => Ordering::Greater,
        (None, Some(_)) if !a_is_simple_path => Ordering::Greater,
        (None, Some(_)) => Ordering::Less,
        (Some(a_path), Some(b_path)) => {
            // cmp_by would be useful for us here but that is currently unstable
            // cmp doesn't work due the lifetimes on text's return type
            a_path
                .segments()
                .zip_longest(b_path.segments())
                .find_map(|zipped| match zipped {
                    EitherOrBoth::Both(a_segment, b_segment) => {
                        match path_segment_cmp(&a_segment, &b_segment) {
                            Ordering::Equal => None,
                            ord => Some(ord),
                        }
                    }
                    EitherOrBoth::Left(_) if b_is_simple_path => Some(Ordering::Greater),
                    EitherOrBoth::Left(_) => Some(Ordering::Less),
                    EitherOrBoth::Right(_) if a_is_simple_path => Some(Ordering::Less),
                    EitherOrBoth::Right(_) => Some(Ordering::Greater),
                })
                .unwrap_or_else(|| use_tree_cmp_by_tree_list_glob_or_alias(a, b, true))
        }
    }
}

fn path_segment_cmp(a: &ast::PathSegment, b: &ast::PathSegment) -> Ordering {
    match (a.kind(), b.kind()) {
        (None, None) => Ordering::Equal,
        (Some(_), None) => Ordering::Greater,
        (None, Some(_)) => Ordering::Less,
        // self
        (Some(PathSegmentKind::SelfKw), Some(PathSegmentKind::SelfKw)) => Ordering::Equal,
        (Some(PathSegmentKind::SelfKw), _) => Ordering::Less,
        (_, Some(PathSegmentKind::SelfKw)) => Ordering::Greater,
        // super
        (Some(PathSegmentKind::SuperKw), Some(PathSegmentKind::SuperKw)) => Ordering::Equal,
        (Some(PathSegmentKind::SuperKw), _) => Ordering::Less,
        (_, Some(PathSegmentKind::SuperKw)) => Ordering::Greater,
        // crate
        (Some(PathSegmentKind::CrateKw), Some(PathSegmentKind::CrateKw)) => Ordering::Equal,
        (Some(PathSegmentKind::CrateKw), _) => Ordering::Less,
        (_, Some(PathSegmentKind::CrateKw)) => Ordering::Greater,
        // identifiers (everything else is treated as an identifier).
        _ => {
            match (
                a.name_ref().as_ref().map(ast::NameRef::text),
                b.name_ref().as_ref().map(ast::NameRef::text),
            ) {
                (None, None) => Ordering::Equal,
                (Some(_), None) => Ordering::Greater,
                (None, Some(_)) => Ordering::Less,
                (Some(a_name), Some(b_name)) => {
                    let a_text = a_name.as_str().trim_start_matches("r#");
                    let b_text = b_name.as_str().trim_start_matches("r#");
                    version_sort::version_sort(a_text, b_text)
                }
            }
        }
    }
}

/// Orders for use trees with equal paths (see `use_tree_cmp` for details about use tree ordering).
///
/// If the `strict` parameter is set to true and both trees have tree lists, the tree lists are
/// ordered by calling `use_tree_cmp` on their "sub-tree" pairs until either the tie is broken
/// or tree list equality is confirmed, otherwise (i.e. if either `strict` is false or at least
/// one of the trees does *not* have tree list), this potentially recursive step is skipped,
/// and only the presence of a glob pattern or an alias is used to determine the ordering.
fn use_tree_cmp_by_tree_list_glob_or_alias(
    a: &ast::UseTree,
    b: &ast::UseTree,
    strict: bool,
) -> Ordering {
    let cmp_by_glob_or_alias = || match (a.star_token().is_some(), b.star_token().is_some()) {
        (true, false) => Ordering::Greater,
        (false, true) => Ordering::Less,
        _ => match (a.rename(), b.rename()) {
            (None, None) => Ordering::Equal,
            (Some(_), None) => Ordering::Greater,
            (None, Some(_)) => Ordering::Less,
            (Some(a_rename), Some(b_rename)) => a_rename
                .name()
                .as_ref()
                .map(ast::Name::text)
                .as_ref()
                .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("_", |b_name| b_name.as_str().trim_start_matches("r#")),
                ),
        },
    };

    match (a.use_tree_list(), b.use_tree_list()) {
        (Some(_), None) => Ordering::Greater,
        (None, Some(_)) => Ordering::Less,
        (Some(a_list), Some(b_list)) if strict => a_list
            .use_trees()
            .zip_longest(b_list.use_trees())
            .find_map(|zipped| match zipped {
                EitherOrBoth::Both(a_tree, b_tree) => match use_tree_cmp(&a_tree, &b_tree) {
                    Ordering::Equal => None,
                    ord => Some(ord),
                },
                EitherOrBoth::Left(_) => Some(Ordering::Greater),
                EitherOrBoth::Right(_) => Some(Ordering::Less),
            })
            .unwrap_or_else(cmp_by_glob_or_alias),
        _ => cmp_by_glob_or_alias(),
    }
}

pub fn eq_visibility(vis0: Option<ast::Visibility>, vis1: Option<ast::Visibility>) -> bool {
    match (vis0, vis1) {
        (None, None) => true,
        (Some(vis0), Some(vis1)) => vis_eq(&vis0, &vis1),
        _ => false,
    }
}

pub fn eq_attrs(
    attrs0: impl Iterator<Item = ast::Attr>,
    attrs1: impl Iterator<Item = ast::Attr>,
) -> bool {
    let mut attrs0: Vec<_> = attrs0.map(|attr| attr.syntax().text().to_smolstr()).collect();
    let mut attrs1: Vec<_> = attrs1.map(|attr| attr.syntax().text().to_smolstr()).collect();
    attrs0.sort_unstable();
    attrs1.sort_unstable();

    attrs0 == attrs1
}

fn path_is_self(path: &ast::Path) -> bool {
    path.segment().and_then(|seg| seg.self_token()).is_some() && path.qualifier().is_none()
}

fn path_len(path: ast::Path) -> usize {
    path.segments().count()
}

fn get_single_subtree(use_tree: &ast::UseTree) -> Option<ast::UseTree> {
    use_tree
        .use_tree_list()
        .and_then(|tree_list| tree_list.use_trees().collect_tuple())
        .map(|(single_subtree,)| single_subtree)
}

fn make_use_with_tree(original: &ast::Use, use_tree: ast::UseTree) -> Option<ast::Use> {
    let (editor, use_item) = SyntaxEditor::with_ast_node(original);
    let original_tree = use_item.use_tree()?;
    editor.replace(original_tree.syntax(), use_tree.syntax());
    let edit = editor.finish();
    ast::Use::cast(edit.new_root().clone())
}

fn make_use_tree_list(
    make: &SyntaxFactory,
    use_trees: Vec<ast::UseTree>,
    style_source: Option<&ast::UseTreeList>,
) -> Option<ast::UseTreeList> {
    let use_tree_list = make.use_tree_list(use_trees);
    let Some(style_source) = style_source else {
        return Some(use_tree_list);
    };

    let source_l_curly = style_source.l_curly_token()?;
    let source_r_curly = style_source.r_curly_token()?;

    let leading_ws = source_l_curly.next_token().filter(|token| token.kind().is_trivia());

    let trailing_ws = source_r_curly.prev_token().filter(|token| token.kind().is_trivia());

    let source_trailing_token = trailing_ws
        .as_ref()
        .and_then(|token| token.prev_token())
        .or_else(|| source_r_curly.prev_token());

    let source_has_trailing_comma =
        source_trailing_token.is_some_and(|token| token.kind() == T![,]);

    let (editor, use_tree_list) = SyntaxEditor::with_ast_node(&use_tree_list);
    let make = editor.make();

    if let Some(leading_ws) = leading_ws {
        editor.insert(
            Position::after(use_tree_list.l_curly_token()?),
            make.whitespace(leading_ws.text()),
        );
    }

    let r_curly = use_tree_list.r_curly_token()?;

    let generated_has_trailing_comma = r_curly
        .prev_token()
        .and_then(|token| if token.kind().is_trivia() { token.prev_token() } else { Some(token) })
        .is_some_and(|token| token.kind() == T![,]);

    let mut trailing = Vec::new();

    if source_has_trailing_comma
        && !generated_has_trailing_comma
        && use_tree_list.use_trees().next().is_some()
    {
        trailing.push(make.token(T![,]).into());
    }

    if let Some(trailing_ws) = trailing_ws {
        trailing.push(make.whitespace(trailing_ws.text()).into());
    }

    if !trailing.is_empty() {
        editor.insert_all(Position::before(r_curly), trailing);
    }

    let edit = editor.finish();
    ast::UseTreeList::cast(edit.new_root().clone())
}

fn make_use_tree_from_list(make: &SyntaxFactory, list: ast::UseTreeList) -> Option<ast::UseTree> {
    let placeholder = make.use_tree_glob();
    let (editor, use_tree) = SyntaxEditor::with_ast_node(&placeholder);
    let first_child = use_tree.syntax().first_child_or_token()?;
    let last_child = use_tree.syntax().last_child_or_token()?;
    editor.replace_all(first_child..=last_child, vec![list.syntax().clone().into()]);
    let edit = editor.finish();
    ast::UseTree::cast(edit.new_root().clone())
}

fn make_use_tree_from_parts(
    make: &SyntaxFactory,
    path: Option<ast::Path>,
    list: Option<ast::UseTreeList>,
    rename: Option<ast::Rename>,
    star: bool,
) -> Option<ast::UseTree> {
    match (path, list, star) {
        (Some(path), list, star) => Some(make.use_tree(path, list, rename, star)),
        (None, Some(list), false) if rename.is_none() => make_use_tree_from_list(make, list),
        (None, None, true) if rename.is_none() => Some(make.use_tree_glob()),
        (None, None, false) if rename.is_none() => None,
        _ => None,
    }
}

fn with_use_tree_list(
    use_tree: &ast::UseTree,
    use_trees: Vec<ast::UseTree>,
    make: &SyntaxFactory,
) -> Option<ast::UseTree> {
    let list = make_use_tree_list(make, use_trees, use_tree.use_tree_list().as_ref())?;
    make_use_tree_from_parts(
        make,
        use_tree.path(),
        Some(list),
        use_tree.rename(),
        use_tree.star_token().is_some(),
    )
}

pub(crate) fn wrap_in_tree_list(
    use_tree: &ast::UseTree,
    make: &SyntaxFactory,
) -> Option<ast::UseTree> {
    if use_tree.path().is_none()
        && use_tree.use_tree_list().is_some()
        && use_tree.rename().is_none()
        && use_tree.star_token().is_none()
    {
        return None;
    }

    let list = make_use_tree_list(make, vec![use_tree.clone()], None)?;
    make_use_tree_from_list(make, list)
}

fn split_prefix(
    use_tree: &ast::UseTree,
    prefix: &ast::Path,
    make: &SyntaxFactory,
) -> Option<ast::UseTree> {
    let path = use_tree.path()?;
    if path == *prefix && use_tree.use_tree_list().is_some() {
        return Some(use_tree.clone());
    }

    let suffix = if path == *prefix {
        if use_tree.star_token().is_some() {
            make.use_tree_glob()
        } else {
            let self_path = make.path_unqualified(make.path_segment_self());
            make.use_tree(self_path, None, use_tree.rename(), false)
        }
    } else {
        let suffix_segments = path.segments().skip(prefix.segments().count());
        let suffix_path = make.path_from_segments(suffix_segments, false);
        make.use_tree(
            suffix_path,
            use_tree.use_tree_list(),
            use_tree.rename(),
            use_tree.star_token().is_some(),
        )
    };

    let list = make_use_tree_list(make, vec![suffix], None)?;
    Some(make.use_tree(prefix.clone(), Some(list), None, false))
}

// Taken from rustfmt
// https://github.com/rust-lang/rustfmt/blob/0332da01486508710f2a542111e40513bfb215aa/src/sort.rs
mod version_sort {
    // Original rustfmt code contains some clippy lints.
    // Suppress them to minimize changes from upstream.
    #![allow(clippy::all)]

    use std::cmp::Ordering;

    use itertools::{EitherOrBoth, Itertools};

    struct VersionChunkIter<'a> {
        ident: &'a str,
        start: usize,
    }

    impl<'a> VersionChunkIter<'a> {
        pub(crate) fn new(ident: &'a str) -> Self {
            Self { ident, start: 0 }
        }

        fn parse_numeric_chunk(
            &mut self,
            mut chars: std::str::CharIndices<'a>,
        ) -> Option<VersionChunk<'a>> {
            let mut end = self.start;
            let mut is_end_of_chunk = false;

            while let Some((idx, c)) = chars.next() {
                end = self.start + idx;

                if c.is_ascii_digit() {
                    continue;
                }

                is_end_of_chunk = true;
                break;
            }

            let source = if is_end_of_chunk {
                let value = &self.ident[self.start..end];
                self.start = end;
                value
            } else {
                let value = &self.ident[self.start..];
                self.start = self.ident.len();
                value
            };

            let zeros = source.chars().take_while(|c| *c == '0').count();
            let value = source.parse::<usize>().ok()?;

            Some(VersionChunk::Number { value, zeros, source })
        }

        fn parse_str_chunk(
            &mut self,
            mut chars: std::str::CharIndices<'a>,
        ) -> Option<VersionChunk<'a>> {
            let mut end = self.start;
            let mut is_end_of_chunk = false;

            while let Some((idx, c)) = chars.next() {
                end = self.start + idx;

                if c == '_' {
                    is_end_of_chunk = true;
                    break;
                }

                if !c.is_ascii_digit() {
                    continue;
                }

                is_end_of_chunk = true;
                break;
            }

            let source = if is_end_of_chunk {
                let value = &self.ident[self.start..end];
                self.start = end;
                value
            } else {
                let value = &self.ident[self.start..];
                self.start = self.ident.len();
                value
            };

            Some(VersionChunk::Str(source))
        }
    }

    impl<'a> Iterator for VersionChunkIter<'a> {
        type Item = VersionChunk<'a>;

        fn next(&mut self) -> Option<Self::Item> {
            let mut chars = self.ident[self.start..].char_indices();
            let (_, next) = chars.next()?;

            if next == '_' {
                self.start = self.start + next.len_utf8();
                return Some(VersionChunk::Underscore);
            }

            if next.is_ascii_digit() {
                return self.parse_numeric_chunk(chars);
            }

            self.parse_str_chunk(chars)
        }
    }

    /// Represents a chunk in the version-sort algorithm
    #[derive(Debug, PartialEq, Eq)]
    enum VersionChunk<'a> {
        /// A single `_` in an identifier. Underscores are sorted before all other characters.
        Underscore,
        /// A &str chunk in the version sort.
        Str(&'a str),
        /// A numeric chunk in the version sort. Keeps track of the numeric value and leading zeros.
        Number { value: usize, zeros: usize, source: &'a str },
    }

    /// Determine which side of the version-sort comparison had more leading zeros.
    #[derive(Debug, PartialEq, Eq)]
    enum MoreLeadingZeros {
        Left,
        Right,
        Equal,
    }

    pub(super) fn version_sort(a: &str, b: &str) -> Ordering {
        let iter_a = VersionChunkIter::new(a);
        let iter_b = VersionChunkIter::new(b);
        let mut more_leading_zeros = MoreLeadingZeros::Equal;

        for either_or_both in iter_a.zip_longest(iter_b) {
            match either_or_both {
                EitherOrBoth::Left(_) => return std::cmp::Ordering::Greater,
                EitherOrBoth::Right(_) => return std::cmp::Ordering::Less,
                EitherOrBoth::Both(a, b) => match (a, b) {
                    (VersionChunk::Underscore, VersionChunk::Underscore) => {
                        continue;
                    }
                    (VersionChunk::Underscore, _) => return std::cmp::Ordering::Less,
                    (_, VersionChunk::Underscore) => return std::cmp::Ordering::Greater,
                    (VersionChunk::Str(ca), VersionChunk::Str(cb))
                    | (VersionChunk::Str(ca), VersionChunk::Number { source: cb, .. })
                    | (VersionChunk::Number { source: ca, .. }, VersionChunk::Str(cb)) => {
                        match ca.cmp(&cb) {
                            std::cmp::Ordering::Equal => {
                                continue;
                            }
                            order @ _ => return order,
                        }
                    }
                    (
                        VersionChunk::Number { value: va, zeros: lza, .. },
                        VersionChunk::Number { value: vb, zeros: lzb, .. },
                    ) => match va.cmp(&vb) {
                        std::cmp::Ordering::Equal => {
                            if lza == lzb {
                                continue;
                            }

                            if more_leading_zeros == MoreLeadingZeros::Equal && lza > lzb {
                                more_leading_zeros = MoreLeadingZeros::Left;
                            } else if more_leading_zeros == MoreLeadingZeros::Equal && lza < lzb {
                                more_leading_zeros = MoreLeadingZeros::Right;
                            }
                            continue;
                        }
                        order @ _ => return order,
                    },
                },
            }
        }

        match more_leading_zeros {
            MoreLeadingZeros::Equal => std::cmp::Ordering::Equal,
            MoreLeadingZeros::Left => std::cmp::Ordering::Less,
            MoreLeadingZeros::Right => std::cmp::Ordering::Greater,
        }
    }
}