Skip to main content

coding_tools/
completion.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 Jonathan Shook
3
4//! Dynamic shell completion for the `ct` umbrella, via `veks-completion`.
5//!
6//! The command tree is derived from the lib-hosted clap grammar
7//! ([`crate::cli`]) so it can never drift from the actual CLI: every leaf tool
8//! becomes a `ct <name>` subcommand whose value/boolean flags come straight
9//! from the introspected grammar, and a value_enum flag's variants become its
10//! completion set the same way. On top of that, a few flags get **runtime**
11//! value providers that read the live rule store — ids, tags, and def names —
12//! which a static `clap_complete` script cannot do. The `ct` binary drives this
13//! tree through [`veks_completion::handle_complete_env`].
14
15use std::sync::Arc;
16
17use veks_completion::{CommandTree, Node, ValueProvider};
18
19use crate::{cli, rules};
20
21/// Which rule-store field a dynamic provider serves.
22#[derive(Clone, Copy)]
23enum Field {
24    Id,
25    Tag,
26    Def,
27}
28
29/// Read the nearest rule store (`.ct/rules.jsonc`, discovered upward from the
30/// cwd) and return its ids / tags / def names. Empty on any failure —
31/// completion offers nothing rather than erroring.
32fn store_values(field: Field) -> Vec<String> {
33    let Ok(cwd) = std::env::current_dir() else {
34        return Vec::new();
35    };
36    let Some(root) = rules::discover_root(&cwd) else {
37        return Vec::new();
38    };
39    let Ok(text) = std::fs::read_to_string(rules::store_path(&root)) else {
40        return Vec::new();
41    };
42    let Ok(store) = rules::parse_store(&text) else {
43        return Vec::new();
44    };
45    match field {
46        Field::Id => store.rules.iter().map(|r| r.id.clone()).collect(),
47        Field::Tag => {
48            let mut tags: Vec<String> =
49                store.rules.iter().flat_map(|r| r.tags.iter().cloned()).collect();
50            tags.sort();
51            tags.dedup();
52            tags
53        }
54        Field::Def => store.defs.keys().cloned().collect(),
55    }
56}
57
58/// A provider over a live store field, filtered by the partial word.
59fn store_provider(field: Field) -> ValueProvider {
60    Arc::new(move |partial: &str, _: &[&str]| {
61        store_values(field).into_iter().filter(|v| v.starts_with(partial)).collect()
62    })
63}
64
65/// A provider over a fixed value set (a value_enum's variants).
66fn set_provider(values: Vec<String>) -> ValueProvider {
67    Arc::new(move |partial: &str, _: &[&str]| {
68        values.iter().filter(|v| v.starts_with(partial)).cloned().collect()
69    })
70}
71
72/// Attach the store-backed dynamic providers for a subcommand that selects by
73/// id/tag/def (`ct check`, `ct rules`).
74fn with_store_providers(sub: &str, node: Node) -> Node {
75    match sub {
76        "check" => node
77            .with_value_provider("--id", store_provider(Field::Id))
78            .with_value_provider("--tag", store_provider(Field::Tag)),
79        "rules" => node
80            .with_value_provider("--promote", store_provider(Field::Id))
81            .with_value_provider("--remove", store_provider(Field::Id))
82            .with_value_provider("--tag", store_provider(Field::Tag))
83            .with_value_provider("--def", store_provider(Field::Def)),
84        _ => node,
85    }
86}
87
88/// Build the `ct` completion tree from the live clap grammar: one subcommand
89/// per leaf tool (its `ct-` prefix dropped), flags split into value/boolean by
90/// the introspected kind, enum value-sets and store providers attached.
91pub fn command_tree() -> CommandTree {
92    let mut tree = CommandTree::new("ct");
93    for (name, grammar) in cli::grammars() {
94        let sub = name.strip_prefix("ct-").unwrap_or(name);
95        let value_flags: Vec<String> = grammar
96            .flags
97            .iter()
98            .filter(|f| f.kind != "boolean")
99            .map(|f| format!("--{}", f.name))
100            .collect();
101        let bool_flags: Vec<String> = grammar
102            .flags
103            .iter()
104            .filter(|f| f.kind == "boolean")
105            .map(|f| format!("--{}", f.name))
106            .collect();
107        let vf: Vec<&str> = value_flags.iter().map(String::as_str).collect();
108        let bf: Vec<&str> = bool_flags.iter().map(String::as_str).collect();
109
110        let mut node = Node::leaf_with_flags(&vf, &bf);
111        for f in grammar.flags.iter().filter(|f| f.kind != "boolean" && !f.values.is_empty()) {
112            node = node.with_value_provider(&format!("--{}", f.name), set_provider(f.values.clone()));
113        }
114        node = with_store_providers(sub, node);
115        tree = tree.command(sub, node);
116    }
117    // The meta-command that prints the shell registration script; its single
118    // positional is the shell name.
119    let shells = ["bash", "zsh", "fish"].iter().map(|s| s.to_string()).collect();
120    tree = tree.command(
121        "completions",
122        Node::leaf(&[]).with_positional_provider(set_provider(shells)),
123    );
124    tree
125}