yek 0.25.5

A tool to serialize a repository into chunks of text files
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
use serde::{Deserialize, Serialize};
use std::path::Path;

/// File categories for sorting and prioritization
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
pub enum FileCategory {
    /// Main application or library source code
    Source,
    /// Test files and testing related code
    Test,
    /// Configuration files (yaml, toml, json, etc.)
    Configuration,
    /// Documentation files (markdown, rst, docs folder)
    Documentation,
    /// Other files that don't fit into above categories
    #[default]
    Other,
}

impl FileCategory {
    /// Get the default priority offset for this category
    pub fn default_priority_offset(self) -> i32 {
        match self {
            FileCategory::Configuration => 5,
            FileCategory::Test => 10,
            FileCategory::Documentation => 15,
            FileCategory::Source => 20,
            FileCategory::Other => 1,
        }
    }

    /// Get the category name as a string for display/debug purposes
    pub fn name(self) -> &'static str {
        match self {
            FileCategory::Source => "source",
            FileCategory::Test => "test",
            FileCategory::Configuration => "configuration",
            FileCategory::Documentation => "documentation",
            FileCategory::Other => "other",
        }
    }
}

/// Configuration for category-based priority weights
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CategoryWeights {
    /// Priority offset for source files
    pub source: i32,
    /// Priority offset for test files
    pub test: i32,
    /// Priority offset for configuration files
    pub configuration: i32,
    /// Priority offset for documentation files
    pub documentation: i32,
    /// Priority offset for other files
    pub other: i32,
}

impl Default for CategoryWeights {
    fn default() -> Self {
        Self {
            source: FileCategory::Source.default_priority_offset(),
            test: FileCategory::Test.default_priority_offset(),
            configuration: FileCategory::Configuration.default_priority_offset(),
            documentation: FileCategory::Documentation.default_priority_offset(),
            other: FileCategory::Other.default_priority_offset(),
        }
    }
}

impl CategoryWeights {
    /// Get the priority offset for a given category
    pub fn get_offset(&self, category: FileCategory) -> i32 {
        match category {
            FileCategory::Source => self.source,
            FileCategory::Test => self.test,
            FileCategory::Configuration => self.configuration,
            FileCategory::Documentation => self.documentation,
            FileCategory::Other => self.other,
        }
    }
}

/// Categorize a file based on its path and extension using heuristics
pub fn categorize_file(file_path: &str) -> FileCategory {
    let path = Path::new(file_path);

    // Get file extension (lowercase)
    let extension = path
        .extension()
        .and_then(|ext| ext.to_str())
        .map(|s| s.to_lowercase());

    // Get file name (lowercase)
    let file_name = path
        .file_name()
        .and_then(|name| name.to_str())
        .map(|s| s.to_lowercase())
        .unwrap_or_default();

    // Convert path to lowercase string for pattern matching
    let path_lower = file_path.to_lowercase();

    // Check for test files first (most specific)
    if is_test_file(&path_lower, &file_name, &extension) {
        return FileCategory::Test;
    }

    // Check for configuration files
    if is_configuration_file(&path_lower, &file_name, &extension) {
        return FileCategory::Configuration;
    }

    // Check for documentation files
    if is_documentation_file(&path_lower, &file_name, &extension) {
        return FileCategory::Documentation;
    }

    // Check for source files
    if is_source_file(&path_lower, &extension) {
        return FileCategory::Source;
    }

    // Default to Other
    FileCategory::Other
}

/// Check if a file is a test file based on path patterns and naming conventions
#[allow(clippy::collapsible_match)]
fn is_test_file(path_lower: &str, file_name: &str, extension: &Option<String>) -> bool {
    // Test directory patterns - check both absolute and relative paths
    let test_directories = [
        "/test/",
        "/tests/",
        "/__test__/",
        "/__tests__/",
        "/spec/",
        "/specs/",
        "\\test\\",
        "\\tests\\",
        "\\__test__\\",
        "\\__tests__\\",
        "\\spec\\",
        "\\specs\\",
        "/e2e/",
        "/integration/",
        "/unit/",
        "\\e2e\\",
        "\\integration\\",
        "\\unit\\",
        // Also check for patterns that start at the beginning of the path
        "test/",
        "tests/",
        "__test__/",
        "__tests__/",
        "spec/",
        "specs/",
        "e2e/",
        "integration/",
        "unit/",
    ];

    // Check if path contains test directories
    for test_dir in &test_directories {
        if path_lower.contains(test_dir) {
            return true;
        }
    }

    // Check for test file naming patterns
    let test_patterns = [
        "_test.", ".test.", "_spec.", ".spec.", "_e2e.", ".e2e.", "test_", "spec_", "e2e_",
    ];

    for pattern in &test_patterns {
        if file_name.contains(pattern) {
            return true;
        }
    }

    // Special cases for specific languages/frameworks
    match extension.as_deref() {
        Some("test") | Some("spec") => return true,
        Some("js") | Some("ts") | Some("jsx") | Some("tsx") => {
            if file_name.ends_with(".test.js")
                || file_name.ends_with(".test.ts")
                || file_name.ends_with(".spec.js")
                || file_name.ends_with(".spec.ts")
                || file_name.ends_with(".test.jsx")
                || file_name.ends_with(".test.tsx")
                || file_name.ends_with(".spec.jsx")
                || file_name.ends_with(".spec.tsx")
            {
                return true;
            }
        }
        Some("py") => {
            if file_name.starts_with("test_") || file_name.ends_with("_test.py") {
                return true;
            }
        }
        Some("rs") => {
            // Rust integration tests
            if path_lower.contains("/tests/")
                || path_lower.contains("\\tests\\")
                || path_lower.starts_with("tests/")
            {
                return true;
            }
        }
        Some("java") => {
            if file_name.ends_with("test.java") || file_name.ends_with("tests.java") {
                return true;
            }
        }
        _ => {}
    }

    false
}

/// Check if a file is a configuration file
fn is_configuration_file(path_lower: &str, file_name: &str, extension: &Option<String>) -> bool {
    // Configuration file extensions
    let config_extensions = [
        "toml",
        "yaml",
        "yml",
        "json",
        "ini",
        "cfg",
        "conf",
        "config",
        "properties",
        "env",
        "rc",
        "lock",
        "sum",
        "mod",
        "makefile",
    ];

    if let Some(ext) = extension {
        if config_extensions.contains(&ext.as_str()) {
            return true;
        }
    }

    // Specific configuration file names
    let config_files = [
        "makefile",
        "dockerfile",
        "containerfile",
        "rakefile",
        "gemfile",
        "podfile",
        "vagrantfile",
        "brewfile",
        "procfile",
        "nixfile",
        "package.json",
        "composer.json",
        "project.json",
        "cargo.toml",
        "pyproject.toml",
        "poetry.toml",
        "setup.cfg",
        "tox.ini",
        "docker-compose.yml",
        "docker-compose.yaml",
        "docker-stack.yml",
        "docker-stack.yaml",
        ".gitignore",
        ".gitattributes",
        ".gitmodules",
        ".dockerignore",
        ".eslintrc",
        ".eslintrc.json",
        ".eslintrc.yml",
        ".eslintrc.yaml",
        ".prettierrc",
        ".babelrc",
        ".editorconfig",
        ".travis.yml",
        ".github",
        "appveyor.yml",
        "azure-pipelines.yml",
        "tsconfig.json",
        "jsconfig.json",
        "webpack.config.js",
        "rollup.config.js",
        "vite.config.js",
        "vite.config.ts",
        "next.config.js",
        "nuxt.config.js",
        "tailwind.config.js",
        "postcss.config.js",
        "requirements.txt",
        "setup.py",
        "setup.cfg",
        "manifest.in",
        "pipfile",
        "build.gradle",
        "pom.xml",
        "build.xml",
        "ivy.xml",
        "cmake",
        "cmakelist.txt",
        "meson.build",
    ];

    for config_name in &config_files {
        if file_name == *config_name {
            return true;
        }
    }

    // Check for dotfiles (usually configuration)
    if file_name.starts_with('.') && !file_name.starts_with("..") {
        // Exclude some common non-config dotfiles
        let non_config_dotfiles = [".git", ".gitkeep", ".keep", ".empty", ".placeholder"];
        if !non_config_dotfiles.contains(&file_name) {
            return true;
        }
    }

    // Configuration directories
    let config_dirs = [
        "/config/",
        "/configs/",
        "/.config/",
        "/configuration/",
        "/settings/",
        "\\config\\",
        "\\configs\\",
        "\\.config\\",
        "\\configuration\\",
        "\\settings\\",
        // Also check for patterns that start at the beginning of the path
        "config/",
        "configs/",
        ".config/",
        "configuration/",
        "settings/",
    ];

    for config_dir in &config_dirs {
        if path_lower.contains(config_dir) {
            return true;
        }
    }

    false
}

/// Check if a file is a documentation file
fn is_documentation_file(path_lower: &str, file_name: &str, extension: &Option<String>) -> bool {
    // Documentation file extensions
    let doc_extensions = ["md", "rst", "txt", "adoc", "asciidoc", "org", "wiki"];

    if let Some(ext) = extension {
        if doc_extensions.contains(&ext.as_str()) {
            return true;
        }
    }

    // Documentation file names
    let doc_files = [
        "readme",
        "readme.txt",
        "readme.md",
        "readme.rst",
        "changelog",
        "changelog.md",
        "changelog.txt",
        "changelog.rst",
        "license",
        "license.txt",
        "license.md",
        "contributing",
        "contributing.md",
        "contributing.txt",
        "authors",
        "authors.md",
        "authors.txt",
        "todo",
        "todo.md",
        "todo.txt",
        "notes",
        "notes.md",
        "notes.txt",
        "manual",
        "manual.md",
        "manual.txt",
        "guide",
        "guide.md",
        "guide.txt",
        "help",
        "help.md",
        "help.txt",
        "faq",
        "faq.md",
        "faq.txt",
        "install",
        "install.md",
        "install.txt",
        "usage",
        "usage.md",
        "usage.txt",
        "quickstart",
        "getting-started",
    ];

    for doc_name in &doc_files {
        if file_name == *doc_name {
            return true;
        }
    }

    // Documentation directories
    let doc_dirs = [
        "/doc/",
        "/docs/",
        "/documentation/",
        "/manual/",
        "/guide/",
        "/guides/",
        "\\doc\\",
        "\\docs\\",
        "\\documentation\\",
        "\\manual\\",
        "\\guide\\",
        "\\guides\\",
        // Also check for patterns that start at the beginning of the path
        "doc/",
        "docs/",
        "documentation/",
        "manual/",
        "guide/",
        "guides/",
    ];

    for doc_dir in &doc_dirs {
        if path_lower.contains(doc_dir) {
            return true;
        }
    }

    false
}

/// Check if a file is a source code file
fn is_source_file(path_lower: &str, extension: &Option<String>) -> bool {
    // Source code extensions
    let source_extensions = [
        // Popular languages
        "rs", "go", "py", "js", "ts", "jsx", "tsx", "java", "kt", "scala", "c", "cpp", "cc", "cxx",
        "c++", "h", "hpp", "hxx", "h++", "cs", "vb", "fs", "fsx", "fsi", "php", "rb", "pl", "pm",
        "r", "m", "mm", "swift", "dart", "lua", "sh", "bash", "zsh", "fish", "ps1", "bat", "cmd",
        "clj", "cljs", "cljc", "ex", "exs", "erl", "hrl", "hs", "lhs", "elm", "ml", "mli", "ocaml",
        // Web technologies
        "html", "htm", "css", "scss", "sass", "less", "vue", "svelte", // Mobile
        "m", "mm", "swift", "kt", "java", "dart", // System/Low-level
        "asm", "s", "nasm", "v", "vhd", "vhdl", // Functional
        "clj", "cljs", "hs", "elm", "ml", "fs", // Other
        "sql", "graphql", "proto", "thrift", "avro",
    ];

    if let Some(ext) = extension {
        if source_extensions.contains(&ext.as_str()) {
            return true;
        }
    }

    // Source directories (less specific than test directories)
    let source_dirs = [
        "/src/",
        "/source/",
        "/lib/",
        "/libs/",
        "/app/",
        "/application/",
        "\\src\\",
        "\\source\\",
        "\\lib\\",
        "\\libs\\",
        "\\app\\",
        "\\application\\",
        // Also check for patterns that start at the beginning of the path
        "src/",
        "source/",
        "lib/",
        "libs/",
        "app/",
        "application/",
    ];

    for source_dir in &source_dirs {
        if path_lower.contains(source_dir) {
            return true;
        }
    }

    false
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_categorize_source_files() {
        assert_eq!(categorize_file("src/main.rs"), FileCategory::Source);
        assert_eq!(categorize_file("lib/utils.py"), FileCategory::Source);
        assert_eq!(categorize_file("app/component.js"), FileCategory::Source);
        assert_eq!(categorize_file("main.go"), FileCategory::Source);
        assert_eq!(categorize_file("index.html"), FileCategory::Source);
    }

    #[test]
    fn test_categorize_test_files() {
        assert_eq!(categorize_file("tests/test_main.py"), FileCategory::Test);
        assert_eq!(categorize_file("test/utils_test.go"), FileCategory::Test);
        assert_eq!(categorize_file("src/component.test.js"), FileCategory::Test);
        assert_eq!(categorize_file("__tests__/unit.js"), FileCategory::Test);
        assert_eq!(categorize_file("spec/feature_spec.rb"), FileCategory::Test);
        assert_eq!(
            categorize_file("e2e/integration.test.ts"),
            FileCategory::Test
        );
    }

    #[test]
    fn test_categorize_configuration_files() {
        assert_eq!(categorize_file("package.json"), FileCategory::Configuration);
        assert_eq!(categorize_file("Cargo.toml"), FileCategory::Configuration);
        assert_eq!(
            categorize_file("docker-compose.yml"),
            FileCategory::Configuration
        );
        assert_eq!(
            categorize_file(".eslintrc.json"),
            FileCategory::Configuration
        );
        assert_eq!(
            categorize_file("config/database.yml"),
            FileCategory::Configuration
        );
        assert_eq!(categorize_file("Makefile"), FileCategory::Configuration);
        assert_eq!(categorize_file(".gitignore"), FileCategory::Configuration);
    }

    #[test]
    fn test_categorize_documentation_files() {
        assert_eq!(categorize_file("README.md"), FileCategory::Documentation);
        assert_eq!(
            categorize_file("docs/guide.rst"),
            FileCategory::Documentation
        );
        assert_eq!(
            categorize_file("CHANGELOG.txt"),
            FileCategory::Documentation
        );
        assert_eq!(categorize_file("LICENSE"), FileCategory::Documentation);
        assert_eq!(
            categorize_file("manual/install.md"),
            FileCategory::Documentation
        );
    }

    #[test]
    fn test_categorize_other_files() {
        assert_eq!(categorize_file("random.unknown"), FileCategory::Other);
        assert_eq!(categorize_file("data.bin"), FileCategory::Other);
        assert_eq!(categorize_file("image.png"), FileCategory::Other);
    }

    #[test]
    fn test_category_priority_offsets() {
        assert_eq!(FileCategory::Configuration.default_priority_offset(), 5);
        assert_eq!(FileCategory::Test.default_priority_offset(), 10);
        assert_eq!(FileCategory::Documentation.default_priority_offset(), 15);
        assert_eq!(FileCategory::Source.default_priority_offset(), 20);
        assert_eq!(FileCategory::Other.default_priority_offset(), 1);
    }

    #[test]
    fn test_category_weights() {
        let weights = CategoryWeights::default();
        assert_eq!(weights.get_offset(FileCategory::Source), 20);
        assert_eq!(weights.get_offset(FileCategory::Test), 10);

        let custom_weights = CategoryWeights {
            source: 100,
            test: 50,
            configuration: 25,
            documentation: 10,
            other: 5,
        };
        assert_eq!(custom_weights.get_offset(FileCategory::Source), 100);
        assert_eq!(custom_weights.get_offset(FileCategory::Test), 50);
    }
}