splitrs 0.3.1

AST-based Rust refactoring tool with trait separation, config files, and intelligent module generation
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
//! File analysis module for SplitRS
//!
//! Contains the core file analyzer that processes Rust source files and
//! determines how to split them into modules.

// These types are used by the binary (main.rs) but the library target
// does not construct or call them externally, so the compiler emits dead_code
// warnings on the lib target. The items are intentionally part of the
// internal API shared between the lib and bin compilation units.
#![allow(dead_code)]

use crate::field_access_tracker::FieldAccessTracker;
use crate::helper_dependency_tracker::HelperDependencyTracker;
use crate::macro_analyzer::MacroAnalyzer;
use crate::method_analyzer::{ImplBlockAnalyzer, MethodGroup};
use crate::module_generator::Module;
use crate::scope_analyzer::{self, ScopeAnalyzer};
use crate::trait_method_tracker::TraitMethodTracker;
use std::collections::{HashMap, HashSet};
use std::fs;
use std::path::Path;
use syn::{File, ImplItem, Item, ItemImpl};

/// Information about a Rust type (struct or enum) and its associated impl blocks
///
/// This structure tracks all information needed to properly organize a type
/// when splitting it into modules, including the type definition itself,
/// its impl blocks, and any large impl blocks that need to be split.
#[derive(Clone)]
pub(crate) struct TypeInfo {
    /// Name of the type (struct or enum name)
    pub(crate) name: String,

    /// The type definition item (struct or enum)
    pub(crate) item: Item,

    /// Regular inherent impl blocks for this type (`impl Type { ... }`)
    pub(crate) impls: Vec<Item>,

    /// Trait implementation blocks (`impl Trait for Type { ... }`)
    pub(crate) trait_impls: Vec<TraitImplInfo>,

    /// Documentation comments associated with the type
    pub(crate) doc_comments: Vec<String>,

    /// Large impl blocks that should be split into separate modules
    ///
    /// Each tuple contains the original impl block and the groups of methods
    /// it should be split into, as determined by dependency analysis.
    pub(crate) large_impls: Vec<(ItemImpl, Vec<MethodGroup>)>,
}

/// Information about a trait implementation
#[derive(Clone)]
pub(crate) struct TraitImplInfo {
    /// Name of the trait being implemented
    pub(crate) trait_name: String,

    /// The trait impl block
    pub(crate) impl_item: Item,

    /// Whether this is an unsafe impl
    #[allow(dead_code)]
    pub(crate) is_unsafe: bool,
}

/// Core analyzer that processes a Rust file and determines how to split it
///
/// The `FileAnalyzer` is responsible for:
/// - Identifying types (structs, enums) and their impl blocks
/// - Determining which impl blocks are large enough to split
/// - Tracking standalone items (functions, constants, etc.)
/// - Coordinating with the scope analyzer for proper module placement
/// - Tracking helper function dependencies for cross-module visibility
pub(crate) struct FileAnalyzer {
    /// Map of type names to their information
    pub(crate) types: HashMap<String, TypeInfo>,

    /// Items that aren't type definitions (functions, constants, etc.)
    pub(crate) standalone_items: Vec<Item>,

    /// Use statements from the original file
    pub(crate) use_statements: Vec<Item>,

    /// Whether to enable impl block splitting
    split_impl_blocks: bool,

    /// Maximum lines per impl block before splitting
    max_impl_lines: usize,

    /// Analyzer for determining proper module scope and placement
    scope_analyzer: ScopeAnalyzer,

    /// Tracker for helper function dependencies
    helper_tracker: HelperDependencyTracker,

    /// Tracker for field access patterns
    field_tracker: FieldAccessTracker,

    /// Tracker for trait method calls
    pub(crate) trait_tracker: TraitMethodTracker,

    /// Analyzer for macro rules definitions and derive usage
    pub(crate) macro_analyzer: MacroAnalyzer,
}

impl FileAnalyzer {
    /// Creates a new FileAnalyzer with the specified configuration
    ///
    /// # Arguments
    ///
    /// * `split_impl_blocks` - Whether to enable experimental impl block splitting
    /// * `max_impl_lines` - Maximum lines per impl block before splitting
    pub(crate) fn new(split_impl_blocks: bool, max_impl_lines: usize) -> Self {
        Self {
            types: HashMap::new(),
            standalone_items: Vec::new(),
            use_statements: Vec::new(),
            split_impl_blocks,
            max_impl_lines,
            scope_analyzer: ScopeAnalyzer::new(),
            helper_tracker: HelperDependencyTracker::new(),
            field_tracker: FieldAccessTracker::new(),
            trait_tracker: TraitMethodTracker::new(),
            macro_analyzer: MacroAnalyzer::new(),
        }
    }

    /// Analyzes a parsed Rust file and extracts type information
    ///
    /// This method performs two passes:
    /// 1. Analyzes all types to build scope information
    /// 2. Processes each item to extract types, impls, and determine splitting strategy
    pub(crate) fn analyze(&mut self, file: &File) {
        // Analyze macros (macro_rules! definitions and #[derive] attributes)
        self.macro_analyzer.analyze_file(file);

        // Analyze helper function dependencies for cross-module visibility
        self.helper_tracker.analyze_file(file);

        // Analyze field access patterns for cross-module visibility
        self.field_tracker.analyze_file(file);

        // Analyze trait definitions for trait method imports
        self.trait_tracker.analyze_file(file);

        // First pass: analyze all types with scope analyzer
        self.scope_analyzer.analyze_types(&file.items);

        // Process items
        for item in &file.items {
            match item {
                Item::Struct(s) => {
                    let name = s.ident.to_string();
                    self.types.insert(
                        name.clone(),
                        TypeInfo {
                            name,
                            item: item.clone(),
                            impls: Vec::new(),
                            trait_impls: Vec::new(),
                            doc_comments: Vec::new(),
                            large_impls: Vec::new(),
                        },
                    );
                }
                Item::Enum(e) => {
                    let name = e.ident.to_string();
                    self.types.insert(
                        name.clone(),
                        TypeInfo {
                            name,
                            item: item.clone(),
                            impls: Vec::new(),
                            trait_impls: Vec::new(),
                            doc_comments: Vec::new(),
                            large_impls: Vec::new(),
                        },
                    );
                }
                Item::Impl(i) => {
                    if let Some(type_name) = Self::get_impl_type_name(i) {
                        if let Some(type_info) = self.types.get_mut(&type_name) {
                            // Check if this is a trait implementation
                            if let Some(trait_name) = Self::get_trait_name(i) {
                                // This is a trait impl: `impl Trait for Type`
                                type_info.trait_impls.push(TraitImplInfo {
                                    trait_name,
                                    impl_item: item.clone(),
                                    is_unsafe: i.unsafety.is_some(),
                                });
                                continue;
                            }

                            // This is an inherent impl: `impl Type`
                            // Check if impl block is large and should be split
                            if self.split_impl_blocks {
                                // Analyze impl block to get accurate line count from methods
                                let mut analyzer = ImplBlockAnalyzer::new();
                                analyzer.analyze(i);
                                let impl_lines = analyzer.get_total_lines();

                                if impl_lines > self.max_impl_lines
                                    && analyzer.get_total_methods() > 1
                                {
                                    // Split this impl block
                                    let groups = analyzer.group_methods(self.max_impl_lines);

                                    if !groups.is_empty() {
                                        // Register each group as an impl block with scope analyzer
                                        for group in &groups {
                                            let module_name = format!(
                                                "{}_{}",
                                                type_name.to_lowercase(),
                                                group.suggest_name()
                                            );
                                            self.scope_analyzer.register_impl_block(
                                                type_name.clone(),
                                                i.clone(),
                                                module_name,
                                                group.methods.len(),
                                            );
                                        }
                                        // Mark this type as needing an impl module
                                        self.scope_analyzer.mark_needs_impl_module(&type_name);
                                        type_info.large_impls.push((i.clone(), groups));
                                    } else {
                                        type_info.impls.push(item.clone());
                                    }
                                } else {
                                    type_info.impls.push(item.clone());
                                }
                            } else {
                                type_info.impls.push(item.clone());
                            }
                        } else {
                            // Impl for unknown type - keep as standalone
                            self.standalone_items.push(item.clone());
                        }
                    } else {
                        self.standalone_items.push(item.clone());
                    }
                }
                Item::Use(_) => {
                    // Collect use statements for later distribution to modules
                    self.use_statements.push(item.clone());
                }
                Item::Fn(_) | Item::Const(_) | Item::Static(_) | Item::Macro(_) => {
                    self.standalone_items.push(item.clone());
                }
                Item::Mod(mod_item) => {
                    // Skip test modules with #[path = "..."] attribute - they're handled separately
                    let is_test_module = Self::is_test_module_with_path(mod_item);
                    if !is_test_module {
                        self.standalone_items.push(item.clone());
                    }
                }
                _ => {
                    // Other items (type aliases, etc.) go to standalone
                    self.standalone_items.push(item.clone());
                }
            }
        }
    }

    /// Get the macro analyzer results
    pub(crate) fn macro_analyzer(&self) -> &MacroAnalyzer {
        &self.macro_analyzer
    }

    /// Analyze with referenced test files
    ///
    /// Detects `#[cfg(test)] #[path = "..."] mod tests;` patterns
    /// and analyzes those files for field accesses to ensure proper visibility.
    pub(crate) fn analyze_with_test_files(&mut self, file: &File, input_path: &Path) {
        // First do the regular analysis
        self.analyze(file);

        // Then analyze referenced test files
        for item in &file.items {
            if let Item::Mod(mod_item) = item {
                // Check for #[path = "..."] attribute
                let mut path_attr: Option<String> = None;
                let mut is_test = false;

                for attr in &mod_item.attrs {
                    let meta_path = attr.path();
                    if let Some(ident) = meta_path.get_ident() {
                        if ident == "cfg" {
                            // Check if this is #[cfg(test)]
                            if let syn::Meta::List(meta_list) = &attr.meta {
                                let tokens = meta_list.tokens.to_string();
                                if tokens.contains("test") {
                                    is_test = true;
                                }
                            }
                        } else if ident == "path" {
                            // Extract the path value
                            if let syn::Meta::NameValue(nv) = &attr.meta {
                                if let syn::Expr::Lit(syn::ExprLit {
                                    lit: syn::Lit::Str(lit_str),
                                    ..
                                }) = &nv.value
                                {
                                    path_attr = Some(lit_str.value());
                                }
                            }
                        }
                    }
                }

                // If we found a test module with a path, analyze that file
                if is_test {
                    if let Some(test_path_str) = path_attr {
                        // Resolve path relative to input file's directory
                        if let Some(parent) = input_path.parent() {
                            let test_file_path = parent.join(&test_path_str);
                            if test_file_path.exists() {
                                if let Ok(test_source) = fs::read_to_string(&test_file_path) {
                                    if let Ok(test_file) = syn::parse_file(&test_source) {
                                        // Analyze field accesses in the test file
                                        self.field_tracker.analyze_test_file(&test_file);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    /// Extracts the type name from an impl block
    ///
    /// # Returns
    ///
    /// The name of the type being implemented, or `None` if it cannot be determined.
    fn get_impl_type_name(impl_item: &syn::ItemImpl) -> Option<String> {
        if let syn::Type::Path(type_path) = &*impl_item.self_ty {
            if let Some(segment) = type_path.path.segments.last() {
                return Some(segment.ident.to_string());
            }
        }
        None
    }

    /// Extracts the trait name from a trait implementation
    ///
    /// # Returns
    ///
    /// The name of the trait being implemented, or `None` if this is an inherent impl.
    fn get_trait_name(impl_item: &syn::ItemImpl) -> Option<String> {
        impl_item
            .trait_
            .as_ref()
            .and_then(|(_, path, _)| path.segments.last().map(|s| s.ident.to_string()))
    }

    /// Check if a module item is a test module with a #[path = "..."] attribute
    ///
    /// These modules are handled specially and shouldn't be included in standalone items.
    fn is_test_module_with_path(mod_item: &syn::ItemMod) -> bool {
        let mut has_path = false;
        let mut is_test = false;

        for attr in &mod_item.attrs {
            let meta_path = attr.path();
            if let Some(ident) = meta_path.get_ident() {
                if ident == "cfg" {
                    if let syn::Meta::List(meta_list) = &attr.meta {
                        let tokens = meta_list.tokens.to_string();
                        if tokens.contains("test") {
                            is_test = true;
                        }
                    }
                } else if ident == "path" {
                    has_path = true;
                }
            }
        }

        is_test && has_path
    }

    /// Get recommended visibility for a type's fields based on impl organization
    ///
    /// When impl blocks are split into separate modules, fields may need to be
    /// made `pub(super)` to allow access from those modules.
    fn get_field_visibility(&self, type_name: &str) -> scope_analyzer::FieldVisibility {
        self.scope_analyzer.infer_field_visibility(type_name)
    }

    /// Get organization strategy for a type's impl blocks
    ///
    /// Determines whether impl blocks should be kept inline, placed in submodules,
    /// or organized using a wrapper pattern.
    fn get_organization_strategy(
        &self,
        type_name: &str,
    ) -> scope_analyzer::ImplOrganizationStrategy {
        self.scope_analyzer.determine_strategy(type_name)
    }

    /// Groups types and items into modules respecting size constraints
    ///
    /// # Arguments
    ///
    /// * `max_lines` - Target maximum lines per module
    ///
    /// # Returns
    ///
    /// A vector of modules, each containing related types and items.
    pub(crate) fn group_by_module(&self, max_lines: usize) -> Vec<Module> {
        let mut modules = Vec::new();
        let mut module_name_counts: HashMap<String, usize> = HashMap::new();

        // Process types with trait implementations.
        //
        // Enhancement: instead of one module per type, pack multiple types' trait impls
        // together into shared modules up to `max_lines`, so that files with many small
        // trait impls don't explode into dozens of tiny 1-impl modules.
        {
            // Collect all (type_name, trait_impls) pairs that have trait impls
            let mut trait_groups: Vec<(String, Vec<TraitImplInfo>)> = self
                .types
                .values()
                .filter(|t| !t.trait_impls.is_empty())
                .map(|t| (t.name.clone(), t.trait_impls.clone()))
                .collect();
            // Sort by type name for deterministic output
            trait_groups.sort_by(|a, b| a.0.cmp(&b.0));

            // Pack into batched modules
            let mut current_trait_module_impls: Vec<TraitImplInfo> = Vec::new();
            let mut current_trait_module_types: Vec<String> = Vec::new();
            let mut current_trait_lines: usize = 0;
            let mut trait_batch_index: usize = 0;

            for (type_name, trait_impls) in trait_groups {
                // Estimate lines for all trait impls of this type
                let group_lines: usize = trait_impls
                    .iter()
                    .map(|ti| {
                        prettyplease::unparse(&syn::File {
                            shebang: None,
                            attrs: Vec::new(),
                            items: vec![ti.impl_item.clone()],
                        })
                        .lines()
                        .count()
                    })
                    .sum();

                // If adding this group would exceed max_lines and we have content, flush
                if current_trait_lines + group_lines > max_lines
                    && !current_trait_module_impls.is_empty()
                {
                    let module_name = if trait_batch_index == 0 {
                        "trait_impls".to_string()
                    } else {
                        format!("trait_impls_{}", trait_batch_index)
                    };
                    let mut trait_module = Module::new(module_name);
                    // Use the first type name as the primary label for doc comments
                    trait_module.type_name_for_traits = current_trait_module_types.first().cloned();
                    trait_module.trait_impls = current_trait_module_impls.clone();
                    modules.push(trait_module);

                    trait_batch_index += 1;
                    current_trait_module_impls.clear();
                    current_trait_module_types.clear();
                    current_trait_lines = 0;
                }

                current_trait_module_impls.extend(trait_impls);
                current_trait_module_types.push(type_name);
                current_trait_lines += group_lines;
            }

            // Flush remaining
            if !current_trait_module_impls.is_empty() {
                let module_name = if trait_batch_index == 0 {
                    "trait_impls".to_string()
                } else {
                    format!("trait_impls_{}", trait_batch_index)
                };
                let mut trait_module = Module::new(module_name);
                trait_module.type_name_for_traits = current_trait_module_types.first().cloned();
                trait_module.trait_impls = current_trait_module_impls;
                modules.push(trait_module);
            }
        }

        // Process types with large impl blocks separately
        for type_info in self.types.values() {
            if !type_info.large_impls.is_empty() {
                // Determine organization strategy and visibility for this type
                let _strategy = self.get_organization_strategy(&type_info.name);
                let visibility = self.get_field_visibility(&type_info.name);

                // Create modules for this type with split impl blocks.
                // Batch multiple MethodGroups into a single module file when
                // their combined line count fits under max_lines, so we don't
                // produce hundreds of tiny files for types with many small methods.
                for (impl_block, method_groups) in &type_info.large_impls {
                    // Estimate accurate line count for each group using prettyplease.
                    // The heuristic in MethodInfo.line_count (token_lines * 15) wildly
                    // overestimates. Instead, build a synthetic impl block from the
                    // group's methods and measure the formatted output.
                    let groups_with_sizes: Vec<(usize, &MethodGroup)> = method_groups
                        .iter()
                        .map(|g| {
                            let impl_items: Vec<ImplItem> = g
                                .methods
                                .iter()
                                .map(|m| ImplItem::Fn(m.item.clone()))
                                .collect();
                            let synthetic = Item::Impl(ItemImpl {
                                attrs: impl_block.attrs.clone(),
                                defaultness: impl_block.defaultness,
                                unsafety: impl_block.unsafety,
                                impl_token: impl_block.impl_token,
                                generics: impl_block.generics.clone(),
                                trait_: impl_block.trait_.clone(),
                                self_ty: impl_block.self_ty.clone(),
                                brace_token: impl_block.brace_token,
                                items: impl_items,
                            });
                            let lines = prettyplease::unparse(&File {
                                shebang: None,
                                attrs: Vec::new(),
                                items: vec![synthetic],
                            })
                            .lines()
                            .count();
                            (lines, g)
                        })
                        .collect();

                    // Batch groups so each batch stays under max_lines
                    let mut batch: Vec<&MethodGroup> = Vec::new();
                    let mut batch_lines: usize = 0;
                    let base_impl_name = format!("{}_impl", type_info.name.to_lowercase());

                    // Helper to emit one batched module
                    let emit_batch =
                        |batch: &[&MethodGroup],
                         module_name_counts: &mut HashMap<String, usize>,
                         modules: &mut Vec<Module>| {
                            if batch.is_empty() {
                                return;
                            }
                            // Merge all groups in the batch into one combined MethodGroup
                            let mut combined = (*batch[0]).clone();
                            for g in &batch[1..] {
                                combined.methods.extend(g.methods.iter().cloned());
                            }

                            let module_name =
                                if let Some(count) = module_name_counts.get(&base_impl_name) {
                                    let unique_name = format!("{}_{}", base_impl_name, count + 1);
                                    module_name_counts.insert(base_impl_name.clone(), count + 1);
                                    unique_name
                                } else {
                                    module_name_counts.insert(base_impl_name.clone(), 0);
                                    base_impl_name.clone()
                                };

                            let mut module = Module::new(module_name);
                            module.impl_type_name = Some(type_info.name.clone());
                            module.impl_self_ty = Some(impl_block.self_ty.clone());
                            module.impl_generics = Some(impl_block.generics.clone());
                            module.impl_attrs = impl_block.attrs.clone();
                            module.method_group = Some(combined);
                            modules.push(module);
                        };

                    for (group_lines, group) in &groups_with_sizes {
                        if batch_lines + group_lines > max_lines && !batch.is_empty() {
                            emit_batch(&batch, &mut module_name_counts, &mut modules);
                            batch = Vec::new();
                            batch_lines = 0;
                        }
                        batch.push(group);
                        batch_lines += group_lines;
                    }
                    // Flush remaining batch
                    emit_batch(&batch, &mut module_name_counts, &mut modules);
                }

                // Create main module for the type definition
                let mut type_module =
                    Module::new(format!("{}_type", type_info.name.to_lowercase()));
                type_module.field_visibility = Some(visibility.clone());
                type_module.types.push(TypeInfo {
                    name: type_info.name.clone(),
                    item: type_info.item.clone(),
                    impls: type_info.impls.clone(),
                    trait_impls: vec![], // Trait impls go in separate module
                    doc_comments: type_info.doc_comments.clone(),
                    large_impls: vec![],
                });
                modules.push(type_module);
            }
        }

        // Process regular types
        let mut current_module = Module::new("types".to_string());
        let mut current_lines = 0;

        let regular_types: Vec<_> = self
            .types
            .values()
            .filter(|t| t.large_impls.is_empty())
            .collect();

        for type_info in regular_types {
            let type_lines = type_info.estimate_lines();

            if current_lines + type_lines > max_lines && !current_module.types.is_empty() {
                modules.push(current_module);
                current_module = Module::new(format!("types_{}", modules.len() + 1));
                current_lines = 0;
            }

            current_module.types.push(type_info.clone());
            current_lines += type_lines;
        }

        if !current_module.types.is_empty() {
            modules.push(current_module);
        }

        // Add standalone items to modules, splitting by line count
        if !self.standalone_items.is_empty() {
            let mut current_fn_module = Module::new("functions".to_string());
            let mut current_fn_lines = 0;
            let mut fn_module_count = 0;

            for item in &self.standalone_items {
                // Estimate lines for this item
                let item_lines = estimate_item_lines(item);

                // If adding this item would exceed max_lines and we have items, start a new module
                if current_fn_lines + item_lines > max_lines
                    && !current_fn_module.standalone_items.is_empty()
                {
                    modules.push(current_fn_module);
                    fn_module_count += 1;
                    current_fn_module = Module::new(format!("functions_{}", fn_module_count + 1));
                    current_fn_lines = 0;
                }

                current_fn_module.standalone_items.push(item.clone());
                current_fn_lines += item_lines;
            }

            if !current_fn_module.standalone_items.is_empty() {
                modules.push(current_fn_module);
            }
        }

        modules
    }

    /// Compute which private functions need to be made pub(super) for cross-module access
    ///
    /// Returns:
    /// - A set of function names that should have their visibility upgraded
    /// - A map of (module_name -> HashMap<source_module, Vec<function_names>>) for imports
    /// - A map of (struct_name -> Vec<field_name>) for fields that need visibility upgrade
    #[allow(clippy::type_complexity)]
    pub(crate) fn compute_cross_module_visibility(
        &self,
        modules: &[Module],
    ) -> (
        HashSet<String>,
        HashMap<String, HashMap<String, Vec<String>>>,
        HashMap<String, HashSet<String>>,
    ) {
        let mut needs_pub_super = HashSet::new();
        // module_name -> (source_module -> function_names)
        let mut cross_module_imports: HashMap<String, HashMap<String, Vec<String>>> =
            HashMap::new();
        // struct_name -> field_names that need pub(super)
        let mut fields_need_pub_super: HashMap<String, HashSet<String>> = HashMap::new();

        // Build a map of function name -> module name
        let mut fn_to_module: HashMap<String, String> = HashMap::new();
        for module in modules {
            for item in &module.standalone_items {
                if let Item::Fn(f) = item {
                    fn_to_module.insert(f.sig.ident.to_string(), module.name.clone());
                }
            }
        }

        // Build a map of struct name -> module name
        let mut struct_to_module: HashMap<String, String> = HashMap::new();
        for module in modules {
            for type_info in &module.types {
                struct_to_module.insert(type_info.name.clone(), module.name.clone());
            }
        }

        // For each module, check if any of its items call private functions in other modules
        for module in modules {
            // Collect all function names called by items in this module
            let mut called_functions: HashSet<String> = HashSet::new();

            for item in &module.standalone_items {
                match item {
                    Item::Fn(f) => {
                        let fn_name = f.sig.ident.to_string();
                        // Get helpers called by this function
                        let helpers = self.helper_tracker.get_required_helpers(&fn_name);
                        called_functions.extend(helpers);
                    }
                    Item::Impl(impl_item) => {
                        // Also check impl blocks in standalone items (e.g., impl Trait for f32)
                        for item in &impl_item.items {
                            if let syn::ImplItem::Fn(method) = item {
                                let method_name = method.sig.ident.to_string();
                                let helpers =
                                    self.helper_tracker.get_required_helpers(&method_name);
                                called_functions.extend(helpers);
                            }
                        }
                    }
                    _ => {}
                }
            }

            // Check trait impls from TraitImplInfo
            for trait_impl in &module.trait_impls {
                if let Item::Impl(impl_item) = &trait_impl.impl_item {
                    for item in &impl_item.items {
                        if let syn::ImplItem::Fn(method) = item {
                            let method_name = method.sig.ident.to_string();
                            let helpers = self.helper_tracker.get_required_helpers(&method_name);
                            called_functions.extend(helpers);
                        }
                    }
                }
            }

            // For each called function, check if it's in a different module
            for called_fn in &called_functions {
                if let Some(source_module) = fn_to_module.get(called_fn) {
                    if source_module != &module.name {
                        // This function is called from a different module
                        // Check if it's a private function
                        if self.helper_tracker.is_private_helper(called_fn) {
                            needs_pub_super.insert(called_fn.clone());

                            // Track the import needed for this module
                            cross_module_imports
                                .entry(module.name.clone())
                                .or_default()
                                .entry(source_module.clone())
                                .or_default()
                                .push(called_fn.clone());
                        }
                    }
                }
            }
        }

        // Check for cross-module field access
        // Build accessor module map (function/method name -> module)
        let mut accessor_to_module: HashMap<String, String> = HashMap::new();
        for module in modules {
            for item in &module.standalone_items {
                if let Item::Fn(f) = item {
                    accessor_to_module.insert(f.sig.ident.to_string(), module.name.clone());
                }
            }
            // Also add methods from impl blocks
            for type_info in &module.types {
                for impl_item in &type_info.impls {
                    if let Item::Impl(impl_block) = impl_item {
                        for item in &impl_block.items {
                            if let syn::ImplItem::Fn(method) = item {
                                accessor_to_module
                                    .insert(method.sig.ident.to_string(), module.name.clone());
                            }
                        }
                    }
                }
            }
            // Add trait impl methods
            for trait_impl in &module.trait_impls {
                if let Item::Impl(impl_block) = &trait_impl.impl_item {
                    for item in &impl_block.items {
                        if let syn::ImplItem::Fn(method) = item {
                            accessor_to_module
                                .insert(method.sig.ident.to_string(), module.name.clone());
                        }
                    }
                }
            }
        }

        // Check each struct's fields for cross-module access
        for (struct_name, struct_module) in &struct_to_module {
            let fields = self.field_tracker.get_fields_needing_upgrade(
                struct_name,
                struct_module,
                &accessor_to_module,
            );

            if !fields.is_empty() {
                fields_need_pub_super
                    .entry(struct_name.clone())
                    .or_default()
                    .extend(fields);
            }
        }

        (needs_pub_super, cross_module_imports, fields_need_pub_super)
    }
}

impl TypeInfo {
    /// Estimates the total number of lines for this type and its impl blocks
    ///
    /// Uses prettyplease to format each item for an accurate line count that matches
    /// the final output, since the compressed token stream representation significantly
    /// underestimates actual formatted code size.
    pub(crate) fn estimate_lines(&self) -> usize {
        let item_lines = prettyplease::unparse(&syn::File {
            shebang: None,
            attrs: Vec::new(),
            items: vec![self.item.clone()],
        })
        .lines()
        .count();
        let impl_lines: usize = self
            .impls
            .iter()
            .map(|i| {
                prettyplease::unparse(&syn::File {
                    shebang: None,
                    attrs: Vec::new(),
                    items: vec![i.clone()],
                })
                .lines()
                .count()
            })
            .sum();
        item_lines + impl_lines
    }
}

/// Estimate the number of lines for a standalone item (function, const, etc.)
///
/// Uses prettyplease to format the item and count lines for accurate estimation.
fn estimate_item_lines(item: &Item) -> usize {
    // Use prettyplease for accurate line count (matches final output)
    let formatted = prettyplease::unparse(&syn::File {
        shebang: None,
        attrs: Vec::new(),
        items: vec![item.clone()],
    });
    formatted.lines().count()
}