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
use std::path::PathBuf;
use clap::{Parser, Subcommand, ValueEnum};
#[derive(Parser, Debug)]
#[command(
name = "krait",
about = "Code intelligence CLI for AI agents",
version,
propagate_version = true
)]
pub struct Cli {
#[command(subcommand)]
pub command: Command,
/// Output format
#[arg(long, global = true, default_value = "compact")]
pub format: OutputFormat,
/// Enable verbose logging
#[arg(long, global = true)]
pub verbose: bool,
}
#[derive(Debug, Clone, Copy, ValueEnum)]
pub enum OutputFormat {
Compact,
Json,
Human,
}
#[derive(Subcommand, Debug)]
pub enum Command {
/// Generate krait.toml config from auto-detected workspaces
Init {
/// Overwrite existing krait.toml
#[arg(long)]
force: bool,
/// Print what would be generated without writing
#[arg(long)]
dry_run: bool,
},
/// Show daemon health, LSP state, cache stats
Status,
/// Show LSP diagnostics (errors/warnings)
Check {
/// File path to check (all files if omitted)
path: Option<PathBuf>,
/// Show only errors, suppress warnings/hints
#[arg(long)]
errors_only: bool,
},
/// Find symbols and references
#[command(subcommand)]
Find(FindCommand),
/// List symbols or other resources
#[command(subcommand)]
List(ListCommand),
/// Read files or symbol bodies
#[command(subcommand)]
Read(ReadCommand),
/// Semantic code editing via stdin
#[command(subcommand)]
Edit(EditCommand),
/// Manage the background daemon
#[command(subcommand)]
Daemon(DaemonCommand),
/// Show type info and documentation for a symbol
Hover {
/// Symbol name to hover over
name: String,
},
/// Format a file using the LSP formatter
Format {
/// File path to format
path: std::path::PathBuf,
},
/// Rename a symbol across all files
Rename {
/// Symbol name to rename
symbol: String,
/// New name for the symbol
new_name: String,
},
/// Apply LSP quick-fix code actions for current diagnostics
Fix {
/// File path to fix (all files with diagnostics if omitted)
path: Option<std::path::PathBuf>,
},
/// Poll for diagnostics and print timestamped results
Watch {
/// Path to check (all files if omitted)
path: Option<std::path::PathBuf>,
/// Polling interval in milliseconds
#[arg(long, default_value = "1500")]
interval: u64,
},
/// Manage LSP server installations
#[command(subcommand)]
Server(ServerCommand),
/// Search for a pattern in project files
Search {
/// Pattern to search for (regex by default)
pattern: String,
/// File or directory to search in (default: project root)
path: Option<PathBuf>,
/// Case-insensitive search
#[arg(short = 'i', long)]
ignore_case: bool,
/// Match whole words only
#[arg(short = 'w', long)]
word: bool,
/// Treat pattern as literal string (no regex)
#[arg(short = 'F', long)]
literal: bool,
/// Show N lines of context around each match
#[arg(short = 'C', long, default_value = "0")]
context: u32,
/// List only matching file paths
#[arg(short = 'l', long)]
files: bool,
/// Filter by language (ts, js, rs, go, py, java, cs, rb, lua)
#[arg(long, value_name = "LANG")]
r#type: Option<String>,
/// Max total matches (default: 200)
#[arg(long)]
max: Option<usize>,
},
}
#[derive(Subcommand, Debug)]
pub enum FindCommand {
/// Locate symbol definition
Symbol {
/// Symbol name to find
name: String,
/// Filter results to paths containing this substring (for disambiguation)
#[arg(long, value_name = "SUBSTR")]
path: Option<String>,
/// Exclude noise paths (www/, dist/, `node_modules`/, .d.ts, .mdx)
#[arg(long)]
src_only: bool,
/// Include full symbol body in results (like Serena's `include_body=True`)
#[arg(long)]
include_body: bool,
},
/// Find all references to a symbol
Refs {
/// Symbol name to search for
name: String,
/// Enrich each reference with its containing symbol (function/class name)
#[arg(long)]
with_symbol: bool,
},
/// Navigate from interface method to concrete implementations
Impl {
/// Symbol name (interface method) to find implementations of
name: String,
},
}
#[derive(Subcommand, Debug)]
pub enum ListCommand {
/// Semantic outline of a file
Symbols {
/// File path to list symbols for
path: PathBuf,
/// Depth of symbol tree (1=top-level, 2=methods, 3=full)
#[arg(long, default_value = "1")]
depth: u8,
},
}
#[derive(Subcommand, Debug)]
pub enum ReadCommand {
/// Read file contents with line numbers
File {
/// File path to read
path: PathBuf,
/// Start line (1-indexed)
#[arg(long)]
from: Option<u32>,
/// End line (inclusive)
#[arg(long)]
to: Option<u32>,
/// Max lines to show
#[arg(long)]
max_lines: Option<u32>,
},
/// Extract symbol body/code
Symbol {
/// Symbol name to read
name: String,
/// Show only the signature/declaration
#[arg(long)]
signature_only: bool,
/// Max lines to show
#[arg(long)]
max_lines: Option<u32>,
/// Select the definition whose path contains this substring (for disambiguation)
#[arg(long, value_name = "SUBSTR")]
path: Option<String>,
/// Skip overload stubs (single-line `;` declarations) and return the implementation
#[arg(long)]
has_body: bool,
},
}
#[derive(Subcommand, Debug)]
pub enum EditCommand {
/// Replace symbol body with stdin
Replace {
/// Symbol to replace
symbol: String,
},
/// Insert code after a symbol (stdin)
InsertAfter {
/// Symbol to insert after
symbol: String,
},
/// Insert code before a symbol (stdin)
InsertBefore {
/// Symbol to insert before
symbol: String,
},
}
#[derive(Subcommand, Debug)]
pub enum ServerCommand {
/// List all supported LSP servers and their install status
List,
/// Install an LSP server (omit lang to install all missing)
Install {
/// Language (rust, go, python, java, cpp, csharp, ruby, lua, ts, js)
lang: Option<String>,
/// Force reinstall even if already present
#[arg(long)]
reinstall: bool,
},
/// Remove all managed servers from ~/.krait/servers/
Clean,
/// Show running LSP server status from daemon
Status,
/// Restart a language server in the running daemon
Restart {
/// Language to restart (rust, go, python, etc.)
lang: String,
},
}
#[derive(Subcommand, Debug)]
pub enum DaemonCommand {
/// Start the daemon (foreground)
Start,
/// Stop the running daemon
Stop,
/// Show daemon status
Status,
}
#[cfg(test)]
mod tests {
use clap::CommandFactory;
use super::*;
#[test]
fn cli_help_does_not_panic() {
Cli::command().debug_assert();
}
#[test]
fn cli_parses_find_symbol() {
let cli = Cli::try_parse_from(["krait", "find", "symbol", "MyStruct"]).unwrap();
assert!(matches!(
cli.command,
Command::Find(FindCommand::Symbol { name, .. }) if name == "MyStruct"
));
}
#[test]
fn cli_parses_read_file_with_flags() {
let cli = Cli::try_parse_from([
"krait",
"read",
"file",
"src/lib.rs",
"--from",
"5",
"--to",
"10",
"--max-lines",
"20",
])
.unwrap();
assert!(matches!(
cli.command,
Command::Read(ReadCommand::File {
ref path,
from: Some(5),
to: Some(10),
max_lines: Some(20),
}) if path == &PathBuf::from("src/lib.rs")
));
}
#[test]
fn cli_parses_edit_replace() {
let cli = Cli::try_parse_from(["krait", "edit", "replace", "my_func"]).unwrap();
assert!(matches!(
cli.command,
Command::Edit(EditCommand::Replace { symbol }) if symbol == "my_func"
));
}
#[test]
fn cli_rejects_unknown_command() {
let result = Cli::try_parse_from(["krait", "explode"]);
assert!(result.is_err());
}
}