aboutsummaryrefslogtreecommitdiff
path: root/src/cli/add_completions.zig
blob: 2e7d6b4ea76677263d2b6ef9e08c2cc4520fda77 (plain) (blame)
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
pub const add_completions: []const u8 = @embedFile("add_completions.txt");
const std = @import("std");
const Environment = @import("../env.zig");

pub const FirstLetter = enum(u8) {
    a = 'a',
    b = 'b',
    c = 'c',
    d = 'd',
    e = 'e',
    f = 'f',
    g = 'g',
    h = 'h',
    i = 'i',
    j = 'j',
    k = 'k',
    l = 'l',
    m = 'm',
    n = 'n',
    o = 'o',
    p = 'p',
    q = 'q',
    r = 'r',
    s = 's',
    t = 't',
    u = 'u',
    v = 'v',
    w = 'w',
    x = 'x',
    y = 'y',
    z = 'z',
};

pub const Index = std.EnumArray(FirstLetter, []const []const u8);
pub const index: Index = if (Environment.isDebug) Index.initFill(&.{"OOMWorkAround"}) else brk: {
    var array: Index = Index.initFill(&[_][]const u8{});

    var i: u8 = 'a';
    var tokenizer = std.mem.tokenize(u8, add_completions, "\n");

    while (i <= 'z') {
        var init_tokenizer = tokenizer;
        var count: usize = 0;
        @setEvalBranchQuota(9999999);
        while (init_tokenizer.next()) |pkg| {
            if (pkg.len == 0) continue;
            if (pkg[0] == i) {
                count += 1;
            } else {
                break;
            }
        }

        var record: [count][]const u8 = undefined;
        var record_i: usize = 0;
        var next_i = i + 1;

        while (tokenizer.next()) |pkg| {
            if (pkg.len == 0) continue;

            if (pkg[0] == i) {
                record[record_i] = pkg;
                record_i += 1;
            } else {
                next_i = pkg[0];
                break;
            }
        }
        array.set(@enumFromInt(FirstLetter, i), &record);

        @setEvalBranchQuota(999999);
        i = next_i;
    }
    break :brk array;
};
pub const biggest_list: usize = brk: {
    var a = index;
    var iter = a.iterator();
    var max: usize = 0;
    while (iter.next()) |list| {
        max = @max(list.value.len, max);
    }
    break :brk max;
};

const index_blob = "add_completions.index.blob";

test "entries" {
    const items = index.get(FirstLetter.r);
    var found_react = false;
    for (items) |item| {
        try std.testing.expectEqual(item[0], 'r');
        if (std.mem.eql(u8, item, "react")) {
            found_react = true;
        }
    }

    try std.testing.expect(found_react);
}