Skip to main content

tessera_codegraph/
completions.rs

1use std::fmt::{self, Display};
2
3use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
6#[serde(rename_all = "snake_case")]
7pub enum CompletionShell {
8    Bash,
9    Zsh,
10    Fish,
11    Powershell,
12}
13
14#[derive(Debug, Clone, Serialize, Deserialize)]
15pub struct CompletionScript {
16    pub shell: CompletionShell,
17    pub script: String,
18}
19
20impl Display for CompletionScript {
21    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22        f.write_str(&self.script)
23    }
24}
25
26pub fn generate(shell: CompletionShell) -> CompletionScript {
27    let commands = [
28        "index",
29        "watch",
30        "doctor",
31        "init",
32        "completions",
33        "find-definition",
34        "find-references",
35        "get-outline",
36        "expand-symbol",
37        "impact",
38        "validate",
39        "validate-snippet",
40        "stats",
41        "search",
42        "unused",
43        "context-pack",
44        "plan-query",
45        "edit-prep",
46        "diff-impact",
47        "imports",
48        "imported-by",
49        "signature",
50        "siblings",
51        "tests-for",
52        "connect",
53        "export",
54        "bench",
55        "snapshot",
56        "mcp",
57        "mcp-http",
58        "shell",
59    ];
60    let joined = commands.join(" ");
61    let script = match shell {
62        CompletionShell::Bash => format!(
63            r#"_tessera()
64{{
65  local cur="${{COMP_WORDS[COMP_CWORD]}}"
66  if [[ $COMP_CWORD -eq 1 ]]; then
67    COMPREPLY=( $(compgen -W "{joined}" -- "$cur") )
68  fi
69}}
70complete -F _tessera tessera
71"#
72        ),
73        CompletionShell::Zsh => format!(
74            r#"#compdef tessera
75_arguments '1:command:({joined})' '*::arg:->args'
76"#
77        ),
78        CompletionShell::Fish => {
79            commands
80                .iter()
81                .map(|command| {
82                    format!("complete -c tessera -f -n '__fish_use_subcommand' -a {command}")
83                })
84                .collect::<Vec<_>>()
85                .join("\n")
86                + "\n"
87        }
88        CompletionShell::Powershell => format!(
89            r#"Register-ArgumentCompleter -Native -CommandName tessera -ScriptBlock {{
90  param($wordToComplete, $commandAst, $cursorPosition)
91  "{joined}".Split(" ") | Where-Object {{ $_ -like "$wordToComplete*" }} | ForEach-Object {{ [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_) }}
92}}
93"#
94        ),
95    };
96    CompletionScript { shell, script }
97}