-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompletion.ts
More file actions
121 lines (113 loc) · 3.25 KB
/
Copy pathcompletion.ts
File metadata and controls
121 lines (113 loc) · 3.25 KB
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
// Shell completion scripts for `deepcode completion <bash|zsh|fish>` (Codex
// parity: `codex completion`). Emits a script to stdout that the user installs,
// e.g. `deepcode completion zsh > ~/.zfunc/_deepcode` or
// `eval "$(deepcode completion bash)"`.
//
// The flag/subcommand lists are kept here deliberately (not derived from the
// parser) so the script is a self-contained string; keep them in sync with
// parse-args.ts when the flag set changes.
import type { Writable } from 'node:stream';
export type CompletionShell = 'bash' | 'zsh' | 'fish';
export const COMPLETION_SHELLS: CompletionShell[] = ['bash', 'zsh', 'fish'];
/** Top-level flags `deepcode` accepts (see parse-args.ts). */
const FLAGS = [
'--help',
'--version',
'--print',
'--resume',
'--continue',
'--fork-session',
'--mode',
'--permission-mode',
'--model',
'--effort',
'--max-turns',
'--bare',
'-C',
'--cd',
'--system-prompt',
'--append-system-prompt',
'--append-system-prompt-file',
'--allowedTools',
'--disallowedTools',
'--output-format',
'--json-schema',
'--include-partial-messages',
'--verbose',
'--json',
'--settings',
'--agents',
'--mcp-config',
'--plugin-dir',
'--plugin-url',
'--no-plugins',
'--strict',
];
/** Positional subcommands (see cli.ts dispatch). */
const SUBCOMMANDS = [
'doctor',
'upgrade',
'mcp',
'app-server',
'trust',
'plugins',
'skills',
'cron',
'scheduler',
'setup-token',
'completion',
];
export function isCompletionShell(value: string | undefined): value is CompletionShell {
return value === 'bash' || value === 'zsh' || value === 'fish';
}
/** Return the completion script for `shell`. */
export function completionScript(shell: CompletionShell): string {
const words = [...FLAGS, ...SUBCOMMANDS].join(' ');
if (shell === 'bash') {
return `# deepcode bash completion — eval "$(deepcode completion bash)"
_deepcode() {
local cur="\${COMP_WORDS[COMP_CWORD]}"
COMPREPLY=( $(compgen -W "${words}" -- "\${cur}") )
}
complete -F _deepcode deepcode
`;
}
if (shell === 'zsh') {
return `#compdef deepcode
# deepcode zsh completion — deepcode completion zsh > "\${fpath[1]}/_deepcode"
_deepcode() {
local -a words
words=(${[...FLAGS, ...SUBCOMMANDS].map((w) => `'${w}'`).join(' ')})
compadd -- $words
}
compdef _deepcode deepcode
`;
}
// fish
const lines = [
'# deepcode fish completion — deepcode completion fish > ~/.config/fish/completions/deepcode.fish',
];
lines.push('complete -c deepcode -f');
for (const f of FLAGS) {
if (f.startsWith('--')) lines.push(`complete -c deepcode -l ${f.slice(2)}`);
else if (f.startsWith('-')) lines.push(`complete -c deepcode -o ${f.slice(1)}`);
}
lines.push(`complete -c deepcode -a '${SUBCOMMANDS.join(' ')}'`);
return lines.join('\n') + '\n';
}
/** `deepcode completion <shell>` handler. Returns the process exit code. */
export function runCompletion(
args: string[],
io: { output: Writable; errOutput: Writable },
): number {
const shell = args[0];
if (!isCompletionShell(shell)) {
io.errOutput.write(
`Usage: deepcode completion <bash|zsh|fish>\n` +
(shell ? `Unknown shell "${shell}".\n` : 'Missing shell argument.\n'),
);
return 2;
}
io.output.write(completionScript(shell));
return 0;
}