gobby-code 0.7.0

Fast Rust CLI for Gobby's code index — AST-aware search, symbol navigation, and dependency graph
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
use crate::commands::scope;
use crate::config::Context;
use crate::db;
use crate::models::Symbol;
use crate::output::{self, Format};
use crate::savings;

pub fn outline(ctx: &Context, file: &str, format: Format, verbose: bool) -> anyhow::Result<()> {
    let conn = db::open_readwrite(&ctx.db_path)?;
    let file = scope::normalize_file_arg(ctx, file);
    let mut stmt = conn.prepare(
        "SELECT * FROM code_symbols WHERE project_id = ?1 AND file_path = ?2 ORDER BY line_start",
    )?;
    let symbols: Vec<Symbol> = stmt
        .query_map(rusqlite::params![&ctx.project_id, &file], Symbol::from_row)?
        .filter_map(|r| r.ok())
        .collect();

    if symbols.is_empty() && !ctx.quiet {
        eprintln!("{}", outline_missing_diagnostic(&conn, ctx, &file));
    }

    // Record savings: outline bytes vs full file bytes
    let file_path = ctx.project_root.join(&file);
    if let Ok(meta) = file_path.metadata() {
        let file_bytes = meta.len() as usize;
        let outline_bytes: usize = symbols
            .iter()
            .map(|s| {
                // Approximate outline size: name + kind + line numbers + signature
                s.qualified_name.len()
                    + s.kind.len()
                    + s.signature.as_ref().map_or(0, |sig| sig.len())
                    + 20 // line numbers, separators
            })
            .sum();
        if outline_bytes > 0 && file_bytes > outline_bytes {
            savings::print_savings(&format!("outline {file}"), file_bytes, outline_bytes);
            if let Some(url) = savings::resolve_daemon_url(None) {
                savings::report_savings(&url, file_bytes, outline_bytes);
            }
        }
    }

    match format {
        Format::Json => {
            if verbose {
                output::print_json(&symbols)
            } else {
                let slim: Vec<_> = symbols.iter().map(|s| s.to_outline()).collect();
                output::print_json(&slim)
            }
        }
        Format::Text => {
            for s in &symbols {
                let indent = if s.parent_symbol_id.is_some() {
                    "  "
                } else {
                    ""
                };
                println!("{indent}{}", format_outline_text_line(s));
            }
            Ok(())
        }
    }
}

fn outline_missing_diagnostic(conn: &rusqlite::Connection, ctx: &Context, file: &str) -> String {
    if scope::path_exists_in_current_project(ctx, file) {
        if scope::indexed_file_exists(conn, &ctx.project_id, file) {
            return format!("file has no indexed symbols in current project: {file}");
        }
        return format!("file not indexed in current project: {file}");
    }

    if let Some(owner) = scope::other_project_for_path(conn, ctx, file) {
        return format!(
            "path belongs to indexed project {} ({}); use --project {}",
            owner.root_path,
            short_id(&owner.id),
            owner.root_path
        );
    }

    if scope::indexed_file_exists(conn, &ctx.project_id, file)
        || scope::content_chunks_exist(conn, &ctx.project_id, file)
    {
        return format!("indexed path missing from current checkout: {file}; run gcode index");
    }

    format!("file not indexed in current project: {file}")
}

fn format_outline_text_line(symbol: &Symbol) -> String {
    let mut line = format!(
        "{}:{}-{} [{}] {} id={}",
        symbol.file_path,
        symbol.line_start,
        symbol.line_end,
        symbol.kind,
        symbol.qualified_name,
        symbol.id
    );
    if let Some(sig) = symbol.signature.as_deref().filter(|sig| !sig.is_empty()) {
        line.push_str(" sig=");
        line.push_str(sig);
    }
    line
}

fn short_id(id: &str) -> &str {
    id.get(..8).unwrap_or(id)
}

pub fn symbol(ctx: &Context, id: &str, format: Format) -> anyhow::Result<()> {
    let conn = db::open_readwrite(&ctx.db_path)?;
    let sym: Option<Symbol> = conn
        .query_row(
            "SELECT * FROM code_symbols WHERE id = ?1 AND project_id = ?2",
            rusqlite::params![id, &ctx.project_id],
            Symbol::from_row,
        )
        .ok();

    match sym {
        Some(s) => {
            let file_path = ctx.project_root.join(&s.file_path);
            if file_path.exists() {
                let source = std::fs::read(&file_path)?;
                let file_bytes = source.len();
                let end = s.byte_end.min(source.len());
                let start = s.byte_start.min(end);
                let symbol_bytes = end - start;
                let snippet = String::from_utf8_lossy(&source[start..end]);

                // Record savings: symbol bytes vs full file bytes
                if symbol_bytes > 0 && file_bytes > symbol_bytes {
                    savings::print_savings(
                        &format!("symbol {}", s.qualified_name),
                        file_bytes,
                        symbol_bytes,
                    );
                    if let Some(url) = savings::resolve_daemon_url(None) {
                        savings::report_savings(&url, file_bytes, symbol_bytes);
                    }
                }

                match format {
                    Format::Json => {
                        let mut result = serde_json::to_value(&s)?;
                        result["source"] = serde_json::Value::String(snippet.to_string());
                        output::print_json(&result)
                    }
                    Format::Text => {
                        println!("{snippet}");
                        Ok(())
                    }
                }
            } else {
                match format {
                    Format::Json => output::print_json(&s),
                    Format::Text => {
                        println!("{}: file not found on disk", s.file_path);
                        Ok(())
                    }
                }
            }
        }
        None => anyhow::bail!("Symbol not found in current project: {id}"),
    }
}

pub fn symbols(ctx: &Context, ids: &[String], format: Format) -> anyhow::Result<()> {
    let conn = db::open_readwrite(&ctx.db_path)?;
    let placeholders: Vec<String> = (1..=ids.len()).map(|i| format!("?{i}")).collect();
    let sql = format!(
        "SELECT * FROM code_symbols WHERE project_id = ?{} AND id IN ({})",
        ids.len() + 1,
        placeholders.join(",")
    );
    let mut stmt = conn.prepare(&sql)?;
    let mut params: Vec<&dyn rusqlite::types::ToSql> = ids
        .iter()
        .map(|s| s as &dyn rusqlite::types::ToSql)
        .collect();
    params.push(&ctx.project_id);
    let results: Vec<Symbol> = stmt
        .query_map(&*params, Symbol::from_row)?
        .filter_map(|r| r.ok())
        .collect();

    // Aggregate savings across batch
    let mut total_file_bytes = 0usize;
    let mut total_symbol_bytes = 0usize;
    for s in &results {
        let file_path = ctx.project_root.join(&s.file_path);
        if let Ok(meta) = file_path.metadata() {
            total_file_bytes += meta.len() as usize;
            total_symbol_bytes += s.byte_end - s.byte_start;
        }
    }
    if total_symbol_bytes > 0 && total_file_bytes > total_symbol_bytes {
        savings::print_savings(
            &format!("symbols ({})", results.len()),
            total_file_bytes,
            total_symbol_bytes,
        );
        if let Some(url) = savings::resolve_daemon_url(None) {
            savings::report_savings(&url, total_file_bytes, total_symbol_bytes);
        }
    }

    match format {
        Format::Json => output::print_json(&results),
        Format::Text => {
            for s in &results {
                println!(
                    "{}:{} [{}] {}",
                    s.file_path, s.line_start, s.kind, s.qualified_name
                );
            }
            Ok(())
        }
    }
}

pub fn kinds(ctx: &Context, format: Format) -> anyhow::Result<()> {
    let conn = db::open_readonly(&ctx.db_path)?;
    let mut stmt =
        conn.prepare("SELECT DISTINCT kind FROM code_symbols WHERE project_id = ?1 ORDER BY kind")?;
    let kinds: Vec<String> = stmt
        .query_map(rusqlite::params![&ctx.project_id], |row| row.get(0))?
        .filter_map(|r| r.ok())
        .collect();

    match format {
        Format::Json => output::print_json(&kinds),
        Format::Text => {
            for k in &kinds {
                println!("{k}");
            }
            Ok(())
        }
    }
}

pub fn tree(ctx: &Context, format: Format) -> anyhow::Result<()> {
    let conn = db::open_readonly(&ctx.db_path)?;
    let mut stmt = conn.prepare(
        "SELECT file_path, language, symbol_count FROM code_indexed_files \
         WHERE project_id = ?1 ORDER BY file_path",
    )?;

    let files: Vec<serde_json::Value> = stmt
        .query_map(rusqlite::params![&ctx.project_id], |row| {
            Ok(serde_json::json!({
                "file_path": row.get::<_, String>(0)?,
                "language": row.get::<_, String>(1)?,
                "symbol_count": row.get::<_, i64>(2)?,
            }))
        })?
        .filter_map(|r| r.ok())
        .collect();

    match format {
        Format::Json => output::print_json(&files),
        Format::Text => {
            for f in &files {
                println!(
                    "{} [{}] ({} symbols)",
                    f["file_path"].as_str().unwrap_or(""),
                    f["language"].as_str().unwrap_or(""),
                    f["symbol_count"].as_i64().unwrap_or(0),
                );
            }
            Ok(())
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn context_for(root: &std::path::Path, db_path: std::path::PathBuf) -> Context {
        Context {
            db_path,
            project_root: root.to_path_buf(),
            project_id: "current-project".to_string(),
            quiet: false,
            neo4j: None,
            qdrant: None,
            embedding: None,
            daemon_url: None,
        }
    }

    fn setup_conn() -> rusqlite::Connection {
        let conn = rusqlite::Connection::open_in_memory().expect("open sqlite");
        conn.execute_batch(
            "CREATE TABLE code_indexed_projects (
                id TEXT PRIMARY KEY,
                root_path TEXT NOT NULL
            );
            CREATE TABLE code_indexed_files (
                project_id TEXT NOT NULL,
                file_path TEXT NOT NULL
            );
            CREATE TABLE code_content_chunks (
                project_id TEXT NOT NULL,
                file_path TEXT NOT NULL
            );",
        )
        .expect("create schema");
        conn
    }

    fn symbol() -> Symbol {
        Symbol {
            id: "12345678-1234-5678-1234-567812345678".to_string(),
            project_id: "current-project".to_string(),
            file_path: "src/commands.rs".to_string(),
            name: "outline".to_string(),
            qualified_name: "outline".to_string(),
            kind: "function".to_string(),
            language: "rust".to_string(),
            byte_start: 0,
            byte_end: 10,
            line_start: 7,
            line_end: 63,
            signature: Some("pub fn outline() -> anyhow::Result<()> {".to_string()),
            docstring: None,
            parent_symbol_id: None,
            content_hash: String::new(),
            summary: None,
            created_at: String::new(),
            updated_at: String::new(),
        }
    }

    #[test]
    fn outline_text_line_includes_id_range_and_signature() {
        let line = format_outline_text_line(&symbol());

        assert!(line.contains("src/commands.rs:7-63 [function] outline"));
        assert!(line.contains("id=12345678-1234-5678-1234-567812345678"));
        assert!(line.contains("sig=pub fn outline() -> anyhow::Result<()> {"));
    }

    #[test]
    fn outline_diagnostic_reports_unindexed_current_file() {
        let current = tempfile::tempdir().expect("current tempdir");
        let src = current.path().join("src");
        std::fs::create_dir_all(&src).expect("create src");
        std::fs::write(src.join("lib.rs"), "fn main() {}").expect("write file");
        let conn = setup_conn();
        let ctx = context_for(current.path(), current.path().join("index.db"));

        let diagnostic = outline_missing_diagnostic(&conn, &ctx, "src/lib.rs");

        assert_eq!(
            diagnostic,
            "file not indexed in current project: src/lib.rs"
        );
    }

    #[test]
    fn outline_diagnostic_reports_other_project_owner() {
        let conn = setup_conn();
        let current = tempfile::tempdir().expect("current tempdir");
        let other = tempfile::tempdir().expect("other tempdir");
        conn.execute(
            "INSERT INTO code_indexed_projects (id, root_path) VALUES (?1, ?2)",
            rusqlite::params!["other-project", other.path().to_string_lossy().to_string()],
        )
        .expect("insert project");
        conn.execute(
            "INSERT INTO code_indexed_files (project_id, file_path) VALUES ('other-project', 'src/lib.rs')",
            [],
        )
        .expect("insert file");
        let ctx = context_for(current.path(), current.path().join("index.db"));

        let diagnostic = outline_missing_diagnostic(&conn, &ctx, "src/lib.rs");

        assert!(diagnostic.contains("path belongs to indexed project"));
        assert!(diagnostic.contains("(other-pr)"));
    }

    #[test]
    fn outline_diagnostic_reports_stale_current_index() {
        let conn = setup_conn();
        let current = tempfile::tempdir().expect("current tempdir");
        conn.execute(
            "INSERT INTO code_indexed_files (project_id, file_path) VALUES ('current-project', 'src/missing.rs')",
            [],
        )
        .expect("insert file");
        let ctx = context_for(current.path(), current.path().join("index.db"));

        let diagnostic = outline_missing_diagnostic(&conn, &ctx, "src/missing.rs");

        assert_eq!(
            diagnostic,
            "indexed path missing from current checkout: src/missing.rs; run gcode index"
        );
    }
}