context-creator 1.5.0

High-performance CLI tool to convert codebases to Markdown for LLM context
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
#![cfg(test)]

//! Error case tests for semantic analysis - handling malformed code and edge cases

use std::fs;
use tempfile::TempDir;

/// Test with syntax errors in files
#[test]
fn test_files_with_syntax_errors() {
    let temp_dir = TempDir::new().unwrap();
    let src_dir = temp_dir.path().join("src");
    fs::create_dir_all(&src_dir).unwrap();

    // Create files with various syntax errors
    fs::write(
        src_dir.join("broken.rs"),
        r#"
// Missing closing brace
fn broken_function() {
    println!("This function never closes");
    
use some::module; // use statement in wrong place
"#,
    )
    .unwrap();

    fs::write(
        src_dir.join("incomplete.rs"),
        r#"
mod utils;
use utils::{helper,}; // trailing comma

fn main() {
    helper(; // syntax error
}
"#,
    )
    .unwrap();

    let output = std::process::Command::new(env!("CARGO_BIN_EXE_context-creator"))
        .arg(&src_dir)
        .arg("--trace-imports")
        .arg("--include-callers")
        .output()
        .expect("Failed to execute context-creator");

    // Should not crash on syntax errors
    assert!(
        output.status.success(),
        "Should handle syntax errors gracefully"
    );

    let stdout = String::from_utf8_lossy(&output.stdout);
    // Files should still be included even with syntax errors
    assert!(stdout.contains("broken.rs"));
    assert!(stdout.contains("incomplete.rs"));
}

/// Test with files containing only comments
#[test]
fn test_comment_only_files() {
    let temp_dir = TempDir::new().unwrap();
    let src_dir = temp_dir.path().join("src");
    fs::create_dir_all(&src_dir).unwrap();

    fs::write(
        src_dir.join("comments.rs"),
        r#"
// This file contains only comments
// No actual code here
/* 
   Multi-line comment
   Still no code
*/

/// Documentation comment
/// But no actual items

// The end
"#,
    )
    .unwrap();

    fs::write(
        src_dir.join("todo.rs"),
        r#"
// TODO: Implement this module
// FIXME: Add proper error handling
// NOTE: This is a placeholder file
"#,
    )
    .unwrap();

    let output = std::process::Command::new(env!("CARGO_BIN_EXE_context-creator"))
        .arg(&src_dir)
        .arg("--trace-imports")
        .output()
        .expect("Failed to execute context-creator");

    let stdout = String::from_utf8_lossy(&output.stdout);

    // Should handle comment-only files
    assert!(output.status.success());
    assert!(stdout.contains("comments.rs"));
    assert!(stdout.contains("todo.rs"));

    // Should not show any semantic info for comment-only files
    let comments_section = stdout
        .split("## comments.rs")
        .nth(1)
        .unwrap_or("")
        .split("##")
        .next()
        .unwrap_or("");

    assert!(!comments_section.contains("Imports:"));
    assert!(!comments_section.contains("Function calls:"));
}

/// Test with macro-heavy code
#[test]
fn test_macro_heavy_code() {
    let temp_dir = TempDir::new().unwrap();
    let src_dir = temp_dir.path().join("src");
    fs::create_dir_all(&src_dir).unwrap();

    fs::write(
        src_dir.join("macros.rs"),
        r#"
macro_rules! create_functions {
    ($($name:ident),*) => {
        $(
            pub fn $name() {
                println!(stringify!($name));
            }
        )*
    };
}

create_functions!(foo, bar, baz);

#[derive(Debug, Clone)]
pub struct Generated;

// Procedural macro usage
#[cfg(feature = "serde")]
#[derive(Serialize, Deserialize)]
pub struct Config {
    value: String,
}

fn main() {
    foo(); // This is generated by macro
    println!("Hello"); // Regular macro call
    vec![1, 2, 3]; // Another macro
}
"#,
    )
    .unwrap();

    let output = std::process::Command::new(env!("CARGO_BIN_EXE_context-creator"))
        .arg(&src_dir)
        .arg("--trace-imports")
        .arg("--include-callers")
        .output()
        .expect("Failed to execute context-creator");

    // Should handle macro-heavy code
    assert!(output.status.success());

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("macros.rs"));
}

/// Test with extremely long lines
#[test]
fn test_very_long_lines() {
    let temp_dir = TempDir::new().unwrap();
    let src_dir = temp_dir.path().join("src");
    fs::create_dir_all(&src_dir).unwrap();

    // Create a file with very long lines
    let long_import_list = (0..100)
        .map(|i| format!("item_{i}"))
        .collect::<Vec<_>>()
        .join(", ");

    fs::write(
        src_dir.join("long_lines.rs"),
        format!(
            r#"
use some::module::{{{}}};

fn function_with_many_params({}) {{
    println!("Many parameters");
}}

const VERY_LONG_STRING: &str = "{}";
"#,
            long_import_list,
            (0..50)
                .map(|i| format!("param_{i}: i32"))
                .collect::<Vec<_>>()
                .join(", "),
            "A".repeat(1000)
        ),
    )
    .unwrap();

    let output = std::process::Command::new(env!("CARGO_BIN_EXE_context-creator"))
        .arg(&src_dir)
        .arg("--trace-imports")
        .output()
        .expect("Failed to execute context-creator");

    // Should handle very long lines
    assert!(output.status.success());
}

/// Test with Unicode identifiers and strings
#[test]
fn test_unicode_content() {
    let temp_dir = TempDir::new().unwrap();
    let src_dir = temp_dir.path().join("src");
    fs::create_dir_all(&src_dir).unwrap();

    fs::write(
        src_dir.join("unicode.rs"),
        r#"
// Unicode identifiers (allowed in Rust)
pub fn 你好() {
    println!("Hello in Chinese");
}

pub fn здравствуйте() {
    println!("Hello in Russian");
}

fn main() {
    let π = 3.14159;
    let 文字列 = "Japanese string";
    
    你好();
    здравствуйте();
    
    // Emoji in strings
    println!("🦀 Rust is awesome! 🎉");
    
    // Unicode in comments
    // → ← ↑ ↓ ⇒ ⇐ ⇑ ⇓
}
"#,
    )
    .unwrap();

    let output = std::process::Command::new(env!("CARGO_BIN_EXE_context-creator"))
        .arg(&src_dir)
        .arg("--trace-imports")
        .arg("--include-callers")
        .output()
        .expect("Failed to execute context-creator");

    // Should handle Unicode content
    assert!(output.status.success());

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("unicode.rs"));

    // Should track Unicode function calls
    if stdout.contains("Function calls:") {
        // The function names should be preserved
        assert!(stdout.contains("你好()") || stdout.contains("здравствуйте()"));
    }
}

/// Test with conditional compilation
#[test]
fn test_conditional_compilation() {
    let temp_dir = TempDir::new().unwrap();
    let src_dir = temp_dir.path().join("src");
    fs::create_dir_all(&src_dir).unwrap();

    fs::write(
        src_dir.join("conditional.rs"),
        r#"
#[cfg(target_os = "windows")]
mod windows_specific;

#[cfg(target_os = "linux")]
mod linux_specific;

#[cfg(target_os = "macos")]
mod macos_specific;

#[cfg(feature = "advanced")]
use advanced::features::{Feature1, Feature2};

#[cfg(not(feature = "minimal"))]
pub fn full_version() {
    #[cfg(debug_assertions)]
    println!("Debug mode");
    
    #[cfg(not(debug_assertions))]
    println!("Release mode");
}

#[cfg(test)]
mod tests {
    use super::*;
    
    #[test]
    fn test_something() {
        // Test code
    }
}
"#,
    )
    .unwrap();

    let output = std::process::Command::new(env!("CARGO_BIN_EXE_context-creator"))
        .arg(&src_dir)
        .arg("--trace-imports")
        .output()
        .expect("Failed to execute context-creator");

    // Should handle conditional compilation
    assert!(output.status.success());
}

/// Test with async/await code
#[test]
fn test_async_await_code() {
    let temp_dir = TempDir::new().unwrap();
    let src_dir = temp_dir.path().join("src");
    fs::create_dir_all(&src_dir).unwrap();

    fs::write(
        src_dir.join("async_code.rs"),
        r#"
use std::future::Future;

mod utils;
use utils::async_helper;

pub async fn async_function() -> Result<(), Box<dyn std::error::Error>> {
    async_helper().await?;
    
    // Async block
    let result = async {
        println!("In async block");
        42
    }.await;
    
    Ok(())
}

async fn another_async() {
    async_function().await.unwrap();
}

// Async trait (with async-trait crate syntax)
#[async_trait]
trait AsyncTrait {
    async fn method(&self) -> i32;
}
"#,
    )
    .unwrap();

    fs::write(
        src_dir.join("utils.rs"),
        r#"
pub async fn async_helper() -> Result<(), Box<dyn std::error::Error>> {
    tokio::time::sleep(std::time::Duration::from_millis(100)).await;
    Ok(())
}
"#,
    )
    .unwrap();

    let output = std::process::Command::new(env!("CARGO_BIN_EXE_context-creator"))
        .arg(&src_dir)
        .arg("--trace-imports")
        .arg("--include-callers")
        .output()
        .expect("Failed to execute context-creator");

    let stdout = String::from_utf8_lossy(&output.stdout);

    // Should handle async/await syntax
    assert!(output.status.success());
    assert!(stdout.contains("async_code.rs"));

    // Should track imports in async code
    assert!(stdout.contains("Imports: utils"));
    assert!(stdout.contains("utils.rs") && stdout.contains("Imported by: async_code.rs"));
}

/// Test with generics and trait bounds
#[test]
fn test_complex_generics() {
    let temp_dir = TempDir::new().unwrap();
    let src_dir = temp_dir.path().join("src");
    fs::create_dir_all(&src_dir).unwrap();

    fs::write(
        src_dir.join("generics.rs"),
        r#"
use std::fmt::{Debug, Display};
use std::marker::PhantomData;

mod traits;
use traits::{CustomTrait, AnotherTrait};

pub struct Complex<T, U, V>
where
    T: Debug + Display + Send + Sync,
    U: CustomTrait<Item = T> + 'static,
    V: AnotherTrait<T, U>,
{
    t: T,
    u: U,
    v: V,
    _phantom: PhantomData<fn(T) -> U>,
}

impl<T, U, V> Complex<T, U, V>
where
    T: Debug + Display + Send + Sync,
    U: CustomTrait<Item = T> + 'static,
    V: AnotherTrait<T, U>,
{
    pub fn new(t: T, u: U, v: V) -> Self {
        Self {
            t,
            u,
            v,
            _phantom: PhantomData,
        }
    }
}

// Higher-ranked trait bounds
fn higher_ranked<F>(f: F)
where
    F: for<'a> Fn(&'a str) -> &'a str,
{
    let result = f("test");
    println!("{}", result);
}
"#,
    )
    .unwrap();

    fs::write(
        src_dir.join("traits.rs"),
        r#"
pub trait CustomTrait {
    type Item;
    fn process(&self) -> Self::Item;
}

pub trait AnotherTrait<T, U> {
    fn combine(&self, t: T, u: U);
}
"#,
    )
    .unwrap();

    let output = std::process::Command::new(env!("CARGO_BIN_EXE_context-creator"))
        .arg(&src_dir)
        .arg("--trace-imports")
        .arg("--include-types")
        .output()
        .expect("Failed to execute context-creator");

    let stdout = String::from_utf8_lossy(&output.stdout);

    // Should handle complex generics
    assert!(output.status.success());
    assert!(stdout.contains("generics.rs"));
    assert!(stdout.contains("Imports: traits"));
}

/// Test with inline modules
#[test]
fn test_inline_modules() {
    let temp_dir = TempDir::new().unwrap();
    let src_dir = temp_dir.path().join("src");
    fs::create_dir_all(&src_dir).unwrap();

    fs::write(
        src_dir.join("inline_mods.rs"),
        r#"
// Inline module definition
mod inner {
    pub fn inner_function() {
        println!("Inner");
    }
    
    pub mod deeper {
        pub fn deeper_function() {
            super::inner_function();
        }
    }
}

mod another {
    use super::inner::{self, deeper};
    
    pub fn use_inner() {
        inner::inner_function();
        deeper::deeper_function();
    }
}

fn main() {
    inner::inner_function();
    inner::deeper::deeper_function();
    another::use_inner();
}
"#,
    )
    .unwrap();

    let output = std::process::Command::new(env!("CARGO_BIN_EXE_context-creator"))
        .arg(&src_dir)
        .arg("--trace-imports")
        .arg("--include-callers")
        .output()
        .expect("Failed to execute context-creator");

    // Should handle inline modules
    assert!(output.status.success());
}

/// Test with re-exports and glob imports
#[test]
fn test_reexports_and_glob_imports() {
    let temp_dir = TempDir::new().unwrap();
    let src_dir = temp_dir.path().join("src");
    fs::create_dir_all(&src_dir).unwrap();

    fs::write(
        src_dir.join("lib.rs"),
        r#"
mod internal;
mod types;

// Re-export everything from internal
pub use internal::*;

// Selective re-exports
pub use types::{Type1, Type2 as RenamedType};

// Glob import in the module
use std::collections::*;

pub fn use_hashmap() -> HashMap<String, i32> {
    let mut map = HashMap::new();
    map.insert("key".to_string(), 42);
    map
}
"#,
    )
    .unwrap();

    fs::write(
        src_dir.join("internal.rs"),
        r#"
pub fn exported_function() {
    println!("This is re-exported");
}

pub struct ExportedStruct {
    pub field: i32,
}
"#,
    )
    .unwrap();

    fs::write(
        src_dir.join("types.rs"),
        r#"
pub struct Type1;
pub struct Type2;
pub struct Type3; // Not re-exported
"#,
    )
    .unwrap();

    let output = std::process::Command::new(env!("CARGO_BIN_EXE_context-creator"))
        .arg(&src_dir)
        .arg("--trace-imports")
        .output()
        .expect("Failed to execute context-creator");

    let stdout = String::from_utf8_lossy(&output.stdout);

    // Should handle re-exports and glob imports
    assert!(output.status.success());
    assert!(stdout.contains("lib.rs"));
    assert!(stdout.contains("Imports: internal, types"));
}

/// Test with empty modules and files
#[test]
fn test_empty_modules() {
    let temp_dir = TempDir::new().unwrap();
    let src_dir = temp_dir.path().join("src");
    fs::create_dir_all(&src_dir).unwrap();

    // Create empty file
    fs::write(src_dir.join("empty.rs"), "").unwrap();

    // Create file with just module declaration
    fs::write(src_dir.join("just_mod.rs"), "mod empty;\n").unwrap();

    let output = std::process::Command::new(env!("CARGO_BIN_EXE_context-creator"))
        .arg(&src_dir)
        .arg("--trace-imports")
        .output()
        .expect("Failed to execute context-creator");

    // Should handle empty files
    assert!(output.status.success());

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("empty.rs"));
    assert!(stdout.contains("just_mod.rs"));
}