agent-file-tools 0.35.2

Agent File Tools — tree-sitter powered code analysis for AI agents
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
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
//! Handler for the `organize_imports` command: re-group, sort, deduplicate, and
//! optionally merge imports in a file.
//!
//! For all languages: extracts imports, groups by convention, sorts alphabetically
//! within groups, deduplicates, and regenerates the import block with blank-line
//! separators between groups.
//!
//! For Rust: merges separate `use` declarations sharing a common prefix into
//! `use` trees (e.g. `use std::path::Path;` + `use std::path::PathBuf;` →
//! `use std::path::{Path, PathBuf};`). This implements D045's deferred merging.

use std::collections::BTreeMap;
use std::ops::Range;
use std::path::Path;

use crate::context::AppContext;
use crate::edit;
use crate::imports::{self, ImportForm, ImportGroup, ImportKind, ImportStatement};
use crate::parser::{detect_language, LangId};
use crate::protocol::{RawRequest, Response};

/// Handle an `organize_imports` request.
///
/// Params:
///   - `file` (string, required) — target file path
///
/// Returns: `{ file, groups: [{name, count}], removed_duplicates, syntax_valid?, backup_id? }`
pub fn handle_organize_imports(req: &RawRequest, ctx: &AppContext) -> Response {
    let op_id = crate::backup::new_op_id();
    // --- Extract params ---
    let file = match req.params.get("file").and_then(|v| v.as_str()) {
        Some(f) => f,
        None => {
            return Response::error(
                &req.id,
                "invalid_request",
                "organize_imports: missing required param 'file'",
            );
        }
    };

    // --- Validate ---
    let path = match ctx.validate_path(&req.id, Path::new(file)) {
        Ok(path) => path,
        Err(resp) => return resp,
    };
    if !path.exists() {
        return Response::error(
            &req.id,
            "file_not_found",
            format!("organize_imports: file not found: {}", file),
        );
    }

    let lang = match detect_language(&path) {
        Some(l) => l,
        None => {
            return Response::error(
                &req.id,
                "unsupported_language",
                format!(
                    "organize_imports: unsupported file extension: {}",
                    path.extension()
                        .and_then(|e| e.to_str())
                        .unwrap_or("<none>")
                ),
            );
        }
    };

    if !imports::is_supported(lang) {
        return Response::error(
            &req.id,
            "unsupported_language",
            format!(
                "organize_imports: import management not yet supported for {:?}",
                lang
            ),
        );
    }

    // --- Parse file and imports ---
    let (source, _tree, block) = match imports::parse_file_imports(&path, lang) {
        Ok(result) => result,
        Err(e) => {
            return Response::error(&req.id, e.code(), e.to_string());
        }
    };

    if lang == LangId::Vue {
        if let Err(err) = imports::vue_single_script_content_range(&_tree) {
            return Response::error(&req.id, err.code(), err.message("organize_imports"));
        }
    }

    if block.imports.is_empty() {
        log::debug!("organize_imports: {} (no imports)", file);
        return Response::success(
            &req.id,
            serde_json::json!({
                "file": file,
                "groups": [],
                "removed_duplicates": 0,
                "no_op": true,
            }),
        );
    }

    let spans_multiple_regions = if lang == LangId::Go {
        go_import_declarations_span_multiple_code_regions(&source, &_tree)
    } else {
        imports_span_multiple_code_regions(&source, lang, &block.imports)
    };

    if spans_multiple_regions {
        return Response::error_with_data(
            &req.id,
            "multi_region_imports",
            format!(
                "organize_imports: imports in {file} span multiple code regions; refusing to organize because replacing the combined import range would corrupt intervening code"
            ),
            serde_json::json!({ "file": file }),
        );
    }

    if lang == LangId::Go && go_grouped_import_block_has_comments(&_tree) {
        return Response::error_with_data(
            &req.id,
            "unsupported_import_comments",
            format!(
                "organize_imports: Go grouped import block in {file} contains comments; refusing to organize because regrouping would drop or detach them"
            ),
            serde_json::json!({ "file": file }),
        );
    }

    // --- Auto-backup ---
    let backup_id = match edit::auto_backup(
        ctx,
        req.session(),
        &path,
        "organize_imports: pre-edit backup",
        Some(&op_id),
    ) {
        Ok(id) => id,
        Err(e) => {
            return Response::error(&req.id, e.code(), e.to_string());
        }
    };

    // --- Organize: group, sort, dedup ---
    let original_count = block.imports.len();
    let comment_gaps = import_gaps_contain_comments(&source, lang, &block.imports);
    let (mut grouped, mut removed_duplicates) = organize(&block.imports, lang);

    // --- Generate new import block ---
    let grouped_go_range = if matches!(lang, LangId::Go) {
        imports::go_has_grouped_import(&source, &_tree)
    } else {
        None
    };
    let go_import_declarations_range = if matches!(lang, LangId::Go) && grouped_go_range.is_some() {
        imports::go_import_declarations_range(&source, &_tree)
    } else {
        None
    };
    let new_import_text = if matches!(lang, LangId::Go) && grouped_go_range.is_some() {
        generate_go_grouped_block(&grouped)
    } else if comment_gaps {
        let (preserved_grouped, preserved_removed, preserved_text) =
            organize_preserving_comment_gaps(&source, lang, &block.imports);
        grouped = preserved_grouped;
        removed_duplicates = preserved_removed;
        preserved_text
    } else {
        generate_organized_block(&grouped, lang)
    };

    // --- Replace import region ---
    let import_range = match go_import_declarations_range
        .as_ref()
        .or(grouped_go_range.as_ref())
        .or(block.byte_range.as_ref())
    {
        Some(range) => range,
        None => {
            return Response::error(
                &req.id,
                "parse_error",
                format!(
                    "organize_imports: missing import byte range for {} despite parsed imports",
                    file
                ),
            );
        }
    };
    let new_source = format!(
        "{}{}{}",
        &source[..import_range.start],
        new_import_text,
        &source[import_range.end..],
    );

    // --- Write, format, and validate ---
    let mut write_result =
        match edit::write_format_validate(&path, &new_source, &ctx.config(), &req.params) {
            Ok(r) => r,
            Err(e) => {
                return Response::error(&req.id, e.code(), e.to_string());
            }
        };

    if let Ok(final_content) = std::fs::read_to_string(&path) {
        write_result.lsp_outcome = ctx.lsp_post_write(&path, &final_content, &req.params);
    }

    // A rollback means post-write syntax validation failed and the file was
    // restored — imports were NOT reorganized. Report that honestly with an
    // error instead of claiming `organized: true`.
    if write_result.rolled_back {
        return Response::error(
            &req.id,
            "generated_invalid_syntax",
            format!(
                "organize_imports: reorganizing imports in {file} would produce invalid syntax; file left unchanged"
            ),
        );
    }

    log::debug!("organize_imports: {}", file);

    // --- Build response ---
    let groups_info: Vec<serde_json::Value> = grouped
        .iter()
        .map(|(group, imps)| {
            serde_json::json!({
                "name": group.label(),
                "count": imps.len(),
            })
        })
        .collect();

    let _ = original_count; // used for removed_duplicates calculation above

    let mut result = serde_json::json!({
        "file": file,
        "groups": groups_info,
        "removed_duplicates": removed_duplicates,
        "formatted": write_result.formatted,
    });

    if let Some(valid) = write_result.syntax_valid {
        result["syntax_valid"] = serde_json::json!(valid);
    }

    if let Some(ref reason) = write_result.format_skipped_reason {
        result["format_skipped_reason"] = serde_json::json!(reason);
    }

    if write_result.validate_requested {
        result["validation_errors"] = serde_json::json!(write_result.validation_errors);
    }
    if let Some(ref reason) = write_result.validate_skipped_reason {
        result["validate_skipped_reason"] = serde_json::json!(reason);
    }

    if let Some(ref id) = backup_id {
        result["backup_id"] = serde_json::json!(id);
    }

    write_result.append_lsp_diagnostics_to(&mut result);
    Response::success(&req.id, result)
}

fn go_import_declarations_span_multiple_code_regions(
    source: &str,
    tree: &tree_sitter::Tree,
) -> bool {
    let root = tree.root_node();
    let mut cursor = root.walk();
    let mut ranges: Vec<Range<usize>> = Vec::new();
    if cursor.goto_first_child() {
        loop {
            let node = cursor.node();
            if node.kind() == "import_declaration" {
                ranges.push(node.byte_range());
            }
            if !cursor.goto_next_sibling() {
                break;
            }
        }
    }

    ranges.windows(2).any(|pair| {
        let previous = &pair[0];
        let next = &pair[1];
        previous.end > next.start
            || !import_gap_is_trivia(source, LangId::Go, previous.end..next.start)
    })
}

fn go_grouped_import_block_has_comments(tree: &tree_sitter::Tree) -> bool {
    let root = tree.root_node();
    let mut cursor = root.walk();
    if cursor.goto_first_child() {
        loop {
            let node = cursor.node();
            if node.kind() == "import_declaration"
                && go_import_declaration_is_grouped(&node)
                && node_contains_comment(node)
            {
                return true;
            }
            if !cursor.goto_next_sibling() {
                break;
            }
        }
    }

    false
}

fn go_import_declaration_is_grouped(node: &tree_sitter::Node<'_>) -> bool {
    let mut cursor = node.walk();
    if cursor.goto_first_child() {
        loop {
            if cursor.node().kind() == "import_spec_list" {
                return true;
            }
            if !cursor.goto_next_sibling() {
                break;
            }
        }
    }

    false
}

fn node_contains_comment(node: tree_sitter::Node<'_>) -> bool {
    if node.kind() == "comment" {
        return true;
    }

    let mut cursor = node.walk();
    if cursor.goto_first_child() {
        loop {
            if node_contains_comment(cursor.node()) {
                return true;
            }
            if !cursor.goto_next_sibling() {
                break;
            }
        }
    }

    false
}

pub(crate) fn imports_span_multiple_code_regions(
    source: &str,
    lang: LangId,
    imports: &[ImportStatement],
) -> bool {
    imports.windows(2).any(|pair| {
        let previous = &pair[0];
        let next = &pair[1];
        if previous.byte_range.end > next.byte_range.start {
            return true;
        }

        !import_gap_is_trivia(source, lang, previous.byte_range.end..next.byte_range.start)
    })
}

#[derive(Debug, Clone, Copy)]
struct ImportGapTrivia {
    has_comment: bool,
}

fn import_gap_is_trivia(source: &str, lang: LangId, range: Range<usize>) -> bool {
    scan_import_gap(source, lang, range).is_some()
}

fn import_gap_has_comment(source: &str, lang: LangId, range: Range<usize>) -> bool {
    scan_import_gap(source, lang, range)
        .map(|gap| gap.has_comment)
        .unwrap_or(false)
}

fn scan_import_gap(source: &str, lang: LangId, range: Range<usize>) -> Option<ImportGapTrivia> {
    let gap = source.get(range)?;

    let mut offset = 0;
    let mut has_comment = false;
    while offset < gap.len() {
        let rest = &gap[offset..];
        let ch = rest
            .chars()
            .next()
            .expect("offset is within the trivia gap");

        if ch.is_whitespace() {
            offset += ch.len_utf8();
            continue;
        }

        if lang == LangId::Lua && rest.starts_with("--[[") {
            let end = rest.find("]]")?;
            offset += end + 2;
            has_comment = true;
            continue;
        }

        if lang == LangId::Lua && rest.starts_with("--") {
            offset += line_comment_len(rest);
            has_comment = true;
            continue;
        }

        if supports_slash_line_comments(lang) && rest.starts_with("//") {
            offset += line_comment_len(rest);
            has_comment = true;
            continue;
        }

        if supports_block_comments(lang) && rest.starts_with("/*") {
            let end = rest.find("*/")?;
            offset += end + 2;
            has_comment = true;
            continue;
        }

        if supports_hash_line_comments(lang) && rest.starts_with('#') {
            offset += line_comment_len(rest);
            has_comment = true;
            continue;
        }

        return None;
    }

    Some(ImportGapTrivia { has_comment })
}

fn line_comment_len(rest: &str) -> usize {
    rest.find('\n').unwrap_or(rest.len())
}

fn supports_slash_line_comments(lang: LangId) -> bool {
    matches!(
        lang,
        LangId::TypeScript
            | LangId::Tsx
            | LangId::JavaScript
            | LangId::Go
            | LangId::Rust
            | LangId::Solidity
            | LangId::Java
            | LangId::Kotlin
            | LangId::Scala
            | LangId::CSharp
            | LangId::Php
            | LangId::Swift
            | LangId::C
            | LangId::Cpp
            | LangId::Vue
    )
}

fn supports_block_comments(lang: LangId) -> bool {
    supports_slash_line_comments(lang)
}

fn supports_hash_line_comments(lang: LangId) -> bool {
    matches!(
        lang,
        LangId::Python | LangId::Ruby | LangId::Perl | LangId::Php
    )
}

fn import_gaps_contain_comments(source: &str, lang: LangId, imports: &[ImportStatement]) -> bool {
    imports.windows(2).any(|pair| {
        import_gap_has_comment(
            source,
            lang,
            pair[0].byte_range.end..pair[1].byte_range.start,
        )
    })
}

fn organize_preserving_comment_gaps(
    source: &str,
    lang: LangId,
    imports: &[ImportStatement],
) -> (Vec<(ImportGroup, Vec<OrganizedImport>)>, usize, String) {
    let mut grouped = Vec::new();
    let mut removed_duplicates = 0;
    let mut output = String::new();
    let mut segment_start = 0;

    for idx in 0..imports.len() {
        let next_gap = imports.get(idx + 1).map(|next| {
            let range = imports[idx].byte_range.end..next.byte_range.start;
            (import_gap_has_comment(source, lang, range.clone()), range)
        });
        let is_boundary = next_gap
            .as_ref()
            .map(|(has_comment, _)| *has_comment)
            .unwrap_or(true);

        if !is_boundary {
            continue;
        }

        let mut refs: Vec<&ImportStatement> = imports[segment_start..=idx].iter().collect();
        refs.sort_by_key(|imp| imp.byte_range.start);
        let (segment_grouped, segment_removed) = organize_ordered_import_refs(&refs, lang);
        output.push_str(&generate_organized_block(&segment_grouped, lang));
        grouped.extend(segment_grouped);
        removed_duplicates += segment_removed;

        if let Some((true, range)) = next_gap {
            if let Some(gap) = source.get(range) {
                output.push_str(gap);
            }
        }

        segment_start = idx + 1;
    }

    (grouped, removed_duplicates, output)
}

/// Organize imports: group by convention, sort within groups, deduplicate.
/// Returns (grouped imports in order, count of removed duplicates).
fn organize(
    imports: &[ImportStatement],
    lang: LangId,
) -> (Vec<(ImportGroup, Vec<OrganizedImport>)>, usize) {
    let mut refs: Vec<&ImportStatement> = imports.iter().collect();
    refs.sort_by_key(|imp| imp.byte_range.start);
    organize_ordered_import_refs(&refs, lang)
}

fn organize_ordered_import_refs(
    refs: &[&ImportStatement],
    lang: LangId,
) -> (Vec<(ImportGroup, Vec<OrganizedImport>)>, usize) {
    if preserves_side_effect_order(lang)
        && refs.iter().any(|imp| imp.kind == ImportKind::SideEffect)
    {
        return organize_preserving_side_effect_order(refs, lang);
    }

    organize_import_refs(refs, lang)
}

fn organize_import_refs(
    imports: &[&ImportStatement],
    lang: LangId,
) -> (Vec<(ImportGroup, Vec<OrganizedImport>)>, usize) {
    // Group imports
    let mut groups: BTreeMap<ImportGroup, Vec<&ImportStatement>> = BTreeMap::new();
    for imp in imports {
        groups.entry(imp.group).or_default().push(*imp);
    }

    let mut result: Vec<(ImportGroup, Vec<OrganizedImport>)> = Vec::new();
    let mut total_removed = 0;

    for (group, imps) in &groups {
        let (organized, removed) = if matches!(lang, LangId::Rust) {
            organize_rust_group(imps)
        } else if should_preserve_raw_on_organize(lang) {
            organize_raw_preserving_group(imps)
        } else {
            organize_generic_group(imps, lang)
        };
        total_removed += removed;
        if !organized.is_empty() {
            result.push((*group, organized));
        }
    }

    (result, total_removed)
}

fn preserves_side_effect_order(lang: LangId) -> bool {
    matches!(
        lang,
        LangId::TypeScript | LangId::Tsx | LangId::JavaScript | LangId::Vue | LangId::Lua
    )
}

fn organize_preserving_side_effect_order(
    imports: &[&ImportStatement],
    lang: LangId,
) -> (Vec<(ImportGroup, Vec<OrganizedImport>)>, usize) {
    let mut result = Vec::new();
    let mut total_removed = 0;
    let mut segment: Vec<&ImportStatement> = Vec::new();

    for imp in imports {
        if imp.kind == ImportKind::SideEffect {
            let (mut grouped, removed) = organize_import_refs(&segment, lang);
            result.append(&mut grouped);
            total_removed += removed;
            segment.clear();

            result.push((imp.group, vec![organized_from_statement(imp, lang)]));
        } else {
            segment.push(*imp);
        }
    }

    let (mut grouped, removed) = organize_import_refs(&segment, lang);
    result.append(&mut grouped);
    total_removed += removed;

    (result, total_removed)
}

fn organized_from_statement(imp: &ImportStatement, lang: LangId) -> OrganizedImport {
    let mut names = imp.names.clone();
    sort_named_specifiers(&mut names);
    let raw_override = should_preserve_raw_on_organize(lang)
        .then(|| imp.raw_text.trim().to_string())
        .filter(|raw| !raw.is_empty());

    OrganizedImport {
        module_path: imp.module_path.clone(),
        names,
        default_import: imp.default_import.clone(),
        namespace_import: imp.namespace_import.clone(),
        kind: imp.kind,
        raw_override,
    }
}

fn should_preserve_raw_on_organize(lang: LangId) -> bool {
    matches!(
        lang,
        LangId::Scala
            | LangId::Java
            | LangId::CSharp
            | LangId::Php
            | LangId::Kotlin
            | LangId::Solidity
            | LangId::Swift
            | LangId::Ruby
            | LangId::Lua
            | LangId::Perl
            | LangId::C
            | LangId::Cpp
            | LangId::Vue
    )
}

/// An organized import ready for code generation.
#[derive(Debug, Clone)]
struct OrganizedImport {
    module_path: String,
    names: Vec<String>,
    default_import: Option<String>,
    namespace_import: Option<String>,
    kind: ImportKind,
    /// When set, the import is rendered verbatim from this string instead of
    /// being regenerated from the structured fields. Used by dialect-sensitive
    /// languages (e.g. Scala) where re-rendering would normalize across
    /// incompatible syntax variants and corrupt the source.
    raw_override: Option<String>,
}

/// Organize a group of non-Rust imports: sort by module path, deduplicate.
fn organize_generic_group(
    imps: &[&ImportStatement],
    _lang: LangId,
) -> (Vec<OrganizedImport>, usize) {
    use std::collections::HashSet;

    let mut seen: HashSet<String> = HashSet::new();
    let mut organized: Vec<OrganizedImport> = Vec::new();
    let mut removed = 0;

    let mut side_effects: Vec<&&ImportStatement> = imps
        .iter()
        .filter(|imp| imp.kind == ImportKind::SideEffect)
        .collect();
    let mut sorted: Vec<&&ImportStatement> = imps
        .iter()
        .filter(|imp| imp.kind != ImportKind::SideEffect)
        .collect();
    sorted.sort_by(|a, b| a.module_path.cmp(&b.module_path));

    // Side-effect imports are evaluation-order sensitive. Keep their original
    // relative source order as a pinned subgroup before value/type imports.
    side_effects.extend(sorted);

    for imp in side_effects {
        // Build dedup key: module_path + kind + sorted names + default + namespace.
        // Namespace imports introduce local bindings, so different aliases are
        // distinct and side-effect imports are not duplicates of namespace
        // imports from the same module.
        let names_key = {
            let mut n = imp.names.clone();
            sort_named_specifiers(&mut n);
            n.join(",")
        };
        let dedup_key = format!(
            "{}|{:?}|{}|{}|{}",
            imp.module_path,
            imp.kind,
            names_key,
            imp.default_import.as_deref().unwrap_or(""),
            imp.namespace_import.as_deref().unwrap_or("")
        );

        if seen.contains(&dedup_key) {
            removed += 1;
            continue;
        }
        seen.insert(dedup_key);

        let mut names = imp.names.clone();
        sort_named_specifiers(&mut names);

        organized.push(OrganizedImport {
            module_path: imp.module_path.clone(),
            names,
            default_import: imp.default_import.clone(),
            namespace_import: imp.namespace_import.clone(),
            kind: imp.kind,
            raw_override: None,
        });
    }

    (organized, removed)
}

fn organize_raw_preserving_group(imps: &[&ImportStatement]) -> (Vec<OrganizedImport>, usize) {
    use std::collections::HashSet;

    let mut seen: HashSet<String> = HashSet::new();
    let mut side_effects: Vec<&ImportStatement> = Vec::new();
    let mut sorted: Vec<&ImportStatement> = Vec::new();
    let mut removed = 0;

    for imp in imps {
        let raw = imp.raw_text.trim();
        if raw.is_empty() {
            continue;
        }

        let key = raw_preserving_dedup_key(imp);
        if !seen.insert(key) {
            removed += 1;
            continue;
        }

        if imp.kind == ImportKind::SideEffect {
            side_effects.push(*imp);
        } else {
            sorted.push(*imp);
        }
    }

    sorted.sort_by(|a, b| a.raw_text.trim().cmp(b.raw_text.trim()));
    side_effects.extend(sorted);

    let organized = side_effects
        .into_iter()
        .map(|imp| OrganizedImport {
            module_path: imp.module_path.clone(),
            names: imp.names.clone(),
            default_import: imp.default_import.clone(),
            namespace_import: imp.namespace_import.clone(),
            kind: imp.kind,
            raw_override: Some(imp.raw_text.trim().to_string()),
        })
        .collect();

    (organized, removed)
}

fn raw_preserving_dedup_key(imp: &ImportStatement) -> String {
    let mut form = imp.form.clone();
    match &mut form {
        ImportForm::Structured { named, .. }
        | ImportForm::Solidity { named, .. }
        | ImportForm::Es { named, .. }
        | ImportForm::Python { named, .. }
        | ImportForm::RustUse { named, .. } => sort_named_specifiers(named),
        ImportForm::Go { .. } => {}
    }

    format!("{}|{:?}|{:?}", imp.module_path, imp.kind, form)
}

fn sort_named_specifiers(names: &mut [String]) {
    names.sort_by(|a, b| {
        imports::specifier_imported_name(a)
            .cmp(imports::specifier_imported_name(b))
            .then_with(|| a.cmp(b))
    });
}

/// Organize Rust use declarations: sort, deduplicate, and merge common prefixes.
fn organize_rust_group(imps: &[&ImportStatement]) -> (Vec<OrganizedImport>, usize) {
    use std::collections::BTreeMap as BMap;

    // First pass: collect all use paths. For items like `use std::path::Path;`,
    // extract prefix `std::path` and item `Path`. For items like `use serde::{Deserialize, Serialize}`,
    // keep as-is (already a tree).
    #[derive(Debug)]
    struct UsePath {
        /// Full original module_path (e.g. "std::path::Path" or "serde::{Deserialize, Serialize}")
        full_path: String,
        /// Prefix for merging (e.g. "std::path")
        prefix: Option<String>,
        /// Leaf item(s) for merging (e.g. ["Path"])
        items: Vec<String>,
        kind: ImportKind,
        is_pub: bool,
    }

    let mut paths: Vec<UsePath> = Vec::new();
    let mut removed = 0;

    for imp in imps {
        let is_pub = imp.default_import.as_deref() == Some("pub");
        let mp = &imp.module_path;

        // Check if this already has a use list (contains '{')
        if mp.contains('{') {
            // Already a tree like "serde::{Deserialize, Serialize}"
            // Extract prefix and items
            if let Some(brace_pos) = mp.find("::{") {
                let prefix = mp[..brace_pos].to_string();
                let items_str = &mp[brace_pos + 3..mp.len() - 1]; // strip ::{ and }
                                                                  // Split on TOP-LEVEL commas only. A naive split(',') corrupts
                                                                  // nested use trees like `hash_map::{Entry, HashMap}, BTreeMap`
                                                                  // into `hash_map::{Entry` / `HashMap}` / `BTreeMap`, which then
                                                                  // sort and regroup into invalid Rust. Brace-aware splitting keeps
                                                                  // each nested subtree intact as one opaque item, so re-emitting
                                                                  // `prefix::{items}` stays syntactically valid.
                let items: Vec<String> = split_top_level_commas(items_str)
                    .into_iter()
                    .map(|s| s.trim().to_string())
                    .filter(|s| !s.is_empty())
                    .collect();
                paths.push(UsePath {
                    full_path: mp.clone(),
                    prefix: Some(prefix),
                    items,
                    kind: imp.kind,
                    is_pub,
                });
            } else {
                paths.push(UsePath {
                    full_path: mp.clone(),
                    prefix: None,
                    items: vec![],
                    kind: imp.kind,
                    is_pub,
                });
            }
        } else if let Some(last_sep) = mp.rfind("::") {
            // Simple path like "std::path::Path" → prefix "std::path", item "Path"
            let prefix = mp[..last_sep].to_string();
            let item = mp[last_sep + 2..].to_string();
            paths.push(UsePath {
                full_path: mp.clone(),
                prefix: Some(prefix),
                items: vec![item],
                kind: imp.kind,
                is_pub,
            });
        } else {
            // Single-segment like "serde" — no prefix to merge on
            paths.push(UsePath {
                full_path: mp.clone(),
                prefix: None,
                items: vec![],
                kind: imp.kind,
                is_pub,
            });
        }
    }

    // Group by (prefix, kind, is_pub) for merging
    // key: (prefix, kind_discriminant, is_pub)
    let mut merge_groups: BMap<(String, u8, bool), Vec<String>> = BMap::new();
    let mut no_prefix: Vec<OrganizedImport> = Vec::new();

    for up in &paths {
        if let Some(ref prefix) = up.prefix {
            let kind_d = match up.kind {
                ImportKind::Value => 0,
                ImportKind::Type => 1,
                ImportKind::SideEffect => 2,
            };
            let key = (prefix.clone(), kind_d, up.is_pub);
            let entry = merge_groups.entry(key).or_default();
            for item in &up.items {
                if !entry.contains(item) {
                    entry.push(item.clone());
                } else {
                    removed += 1;
                }
            }
        } else {
            // Check for duplicate
            let already = no_prefix.iter().any(|o| {
                o.module_path == up.full_path
                    && o.kind == up.kind
                    && (o.default_import.as_deref() == Some("pub")) == up.is_pub
            });
            if already {
                removed += 1;
            } else {
                no_prefix.push(OrganizedImport {
                    module_path: up.full_path.clone(),
                    names: vec![],
                    default_import: if up.is_pub {
                        Some("pub".to_string())
                    } else {
                        None
                    },
                    namespace_import: None,
                    kind: up.kind,
                    raw_override: None,
                });
            }
        }
    }

    // Convert merge groups into OrganizedImport entries
    let mut organized: Vec<OrganizedImport> = Vec::new();

    for ((prefix, kind_d, is_pub), mut items) in merge_groups {
        items.sort();
        let kind = match kind_d {
            1 => ImportKind::Type,
            2 => ImportKind::SideEffect,
            _ => ImportKind::Value,
        };

        let module_path = if items.len() == 1 {
            // Single item — no braces needed
            format!("{}::{}", prefix, items[0])
        } else {
            // Multiple items — use tree
            format!("{}::{{{}}}", prefix, items.join(", "))
        };

        organized.push(OrganizedImport {
            module_path,
            names: vec![],
            default_import: if is_pub {
                Some("pub".to_string())
            } else {
                None
            },
            namespace_import: None,
            kind,
            raw_override: None,
        });
    }

    // Add no-prefix items and sort everything by module_path
    organized.extend(no_prefix);
    organized.sort_by(|a, b| a.module_path.cmp(&b.module_path));

    // Track how many original imports were merged away
    let final_count = organized.len();
    let original_count = imps.len();
    if original_count > final_count + removed {
        removed = original_count - final_count;
    }

    (organized, removed)
}

/// Split a Rust use-list body on TOP-LEVEL commas only, treating nested
/// `{...}` (and defensively `[...]`/`(...)`) as opaque so commas inside a
/// nested subtree do not split it.
///
/// `"hash_map::{Entry, HashMap}, BTreeMap"` -> `["hash_map::{Entry, HashMap}", "BTreeMap"]`
/// `"Deserialize, Serialize"`               -> `["Deserialize", "Serialize"]`
fn split_top_level_commas(s: &str) -> Vec<String> {
    let mut items = Vec::new();
    let mut depth: i32 = 0;
    let mut start = 0usize;
    for (i, ch) in s.char_indices() {
        match ch {
            '{' | '[' | '(' => depth += 1,
            '}' | ']' | ')' => depth -= 1,
            ',' if depth == 0 => {
                items.push(s[start..i].to_string());
                start = i + 1;
            }
            _ => {}
        }
    }
    items.push(s[start..].to_string());
    items
}

/// Generate the full organized import block text.
fn generate_organized_block(
    grouped: &[(ImportGroup, Vec<OrganizedImport>)],
    lang: LangId,
) -> String {
    let mut output = String::new();
    let mut previous_group: Option<ImportGroup> = None;

    for (group, imps) in grouped {
        let mut lines: Vec<String> = Vec::new();
        for imp in imps {
            let line = generate_organized_line(imp, lang);
            lines.push(line);
        }
        if lines.is_empty() {
            continue;
        }

        if !output.is_empty() {
            if previous_group == Some(*group) {
                output.push('\n');
            } else {
                output.push_str("\n\n");
            }
        }
        output.push_str(&lines.join("\n"));
        previous_group = Some(*group);
    }

    output
}

fn generate_go_grouped_block(grouped: &[(ImportGroup, Vec<OrganizedImport>)]) -> String {
    let mut lines = Vec::new();
    lines.push("import (".to_string());
    for (group_idx, (_, imps)) in grouped.iter().enumerate() {
        if group_idx > 0 {
            lines.push(String::new());
        }
        for imp in imps {
            if let Some(ref alias) = imp.default_import {
                lines.push(format!("\t{} \"{}\"", alias, imp.module_path));
            } else {
                lines.push(format!("\t\"{}\"", imp.module_path));
            }
        }
    }
    lines.push(")".to_string());
    lines.join("\n")
}

/// Generate a single import line from an OrganizedImport.
fn generate_organized_line(imp: &OrganizedImport, lang: LangId) -> String {
    if let Some(ref raw) = imp.raw_override {
        return raw.clone();
    }
    match lang {
        LangId::Rust => {
            let prefix = if imp.default_import.as_deref() == Some("pub") {
                "pub "
            } else {
                ""
            };
            format!("{}use {};", prefix, imp.module_path)
        }
        LangId::Go => {
            // Go organize: regenerate as standalone imports
            // (organize_imports for Go would need grouped import rewrite — keep simple for now)
            if let Some(ref alias) = imp.default_import {
                format!("import {} \"{}\"", alias, imp.module_path)
            } else {
                format!("import \"{}\"", imp.module_path)
            }
        }
        LangId::TypeScript | LangId::Tsx | LangId::JavaScript
            if imp.names.is_empty()
                && imp.default_import.is_none()
                && imp.namespace_import.is_some() =>
        {
            let namespace = imp.namespace_import.as_deref().unwrap_or_default();
            format!("import * as {} from '{}';", namespace, imp.module_path)
        }
        _ => {
            // TS/JS/TSX/Python — use the standard generator
            imports::generate_import_line_with_namespace(
                lang,
                &imp.module_path,
                &imp.names,
                imp.default_import.as_deref(),
                imp.namespace_import.as_deref(),
                imp.kind == ImportKind::Type,
            )
        }
    }
}