gobby-code 0.9.6

Fast Rust CLI for Gobby's code index — AST-aware search, symbol navigation, and dependency graph
Documentation
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
//! PostgreSQL pg_search BM25 query sanitization and execution.
//!
//! The module name stays `fts` to keep command wiring stable; runtime keyword
//! search is pg_search BM25 against Gobby's PostgreSQL hub.

use std::collections::HashSet;

use postgres::Client;
use postgres::types::ToSql;

use crate::db;
use crate::models::{ContentSearchHit, SearchResult, Symbol};

type PgParam = Box<dyn ToSql + Sync>;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ResolvedGraphSymbol {
    pub id: String,
    pub display_name: String,
}

#[derive(Debug, Clone, Copy, Default)]
struct SymbolFilters<'a> {
    kind: Option<&'a str>,
    language: Option<&'a str>,
    paths: &'a [String],
}

pub const FILTERED_FETCH_CAP: usize = 10_000;

fn push_param<T>(params: &mut Vec<PgParam>, value: T) -> String
where
    T: ToSql + Sync + 'static,
{
    params.push(Box::new(value));
    format!("${}", params.len())
}

fn param_refs(params: &[PgParam]) -> Vec<&(dyn ToSql + Sync)> {
    params
        .iter()
        .map(|param| param.as_ref() as &(dyn ToSql + Sync))
        .collect()
}

/// Escape LIKE wildcards (`%`, `_`) and the backslash escape char itself.
fn escape_like(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    for c in s.chars() {
        if matches!(c, '\\' | '%' | '_') {
            out.push('\\');
        }
        out.push(c);
    }
    out
}

/// Extract a SQL LIKE prefix from a glob pattern for index-assisted pre-filtering.
fn glob_to_like_prefix(pattern: &str) -> Option<String> {
    let prefix: String = pattern
        .chars()
        .take_while(|c| !matches!(c, '*' | '?' | '['))
        .collect();
    if prefix.is_empty() {
        None
    } else {
        Some(format!("{}%", escape_like(&prefix)))
    }
}

fn has_glob_meta(path: &str) -> bool {
    path.chars().any(|c| matches!(c, '*' | '?' | '['))
}

pub fn expand_paths(paths: &[String]) -> Vec<String> {
    let mut expanded = Vec::new();
    let mut seen = HashSet::new();
    for path in paths {
        let trimmed = path.trim().trim_end_matches('/');
        if trimmed.is_empty() {
            continue;
        }

        let patterns = if has_glob_meta(trimmed) {
            vec![trimmed.to_string()]
        } else {
            vec![trimmed.to_string(), format!("{trimmed}/**")]
        };
        for pattern in patterns {
            if seen.insert(pattern.clone()) {
                expanded.push(pattern);
            }
        }
    }
    expanded
}

pub fn compile_patterns(paths: &[String]) -> anyhow::Result<Vec<glob::Pattern>> {
    paths
        .iter()
        .map(|path| {
            glob::Pattern::new(path).map_err(|e| anyhow::anyhow!("invalid path glob `{path}`: {e}"))
        })
        .collect()
}

fn path_like_prefixes(paths: &[String]) -> Option<Vec<String>> {
    if paths.is_empty() {
        return Some(Vec::new());
    }

    let mut prefixes = Vec::with_capacity(paths.len());
    for path in paths {
        prefixes.push(glob_to_like_prefix(path)?);
    }
    Some(prefixes)
}

pub fn path_filter_falls_back(paths: &[String]) -> bool {
    !paths.is_empty() && path_like_prefixes(paths).is_none()
}

fn push_path_filter(
    conditions: &mut Vec<String>,
    params: &mut Vec<PgParam>,
    alias: &str,
    paths: &[String],
) -> bool {
    let Some(prefixes) = path_like_prefixes(paths) else {
        for path in paths
            .iter()
            .filter(|path| glob_to_like_prefix(path).is_none())
        {
            log::warn!(
                "omitting SQL path filter for alias `{alias}` because path filter `{path}` cannot be converted to a LIKE prefix; relying on post-query glob matching",
            );
        }
        return true;
    };
    if prefixes.is_empty() {
        return false;
    }

    let predicates = prefixes
        .into_iter()
        .map(|prefix| {
            let placeholder = push_param(params, prefix);
            format!("{alias}.file_path LIKE {placeholder} ESCAPE '\\'")
        })
        .collect::<Vec<_>>();
    conditions.push(format!("({})", predicates.join(" OR ")));
    false
}

fn push_symbol_filters(
    conditions: &mut Vec<String>,
    params: &mut Vec<PgParam>,
    alias: &str,
    filters: SymbolFilters<'_>,
) {
    if let Some(kind) = filters.kind {
        let placeholder = push_param(params, kind.to_string());
        conditions.push(format!("{alias}.kind = {placeholder}"));
    }
    if let Some(language) = filters.language {
        let placeholder = push_param(params, language.to_string());
        conditions.push(format!("{alias}.language = {placeholder}"));
    }
    push_path_filter(conditions, params, alias, filters.paths);
}

fn append_unique_symbols(
    out: &mut Vec<Symbol>,
    seen: &mut HashSet<String>,
    symbols: Vec<Symbol>,
    limit: usize,
) {
    for symbol in symbols {
        if seen.insert(symbol.id.clone()) {
            out.push(symbol);
            if out.len() >= limit {
                return;
            }
        }
    }
}

fn query_symbols_by_conditions(
    conn: &mut Client,
    mut conditions: Vec<String>,
    mut params: Vec<PgParam>,
    filters: SymbolFilters<'_>,
    limit: usize,
    order_by: &str,
) -> Vec<Symbol> {
    push_symbol_filters(&mut conditions, &mut params, "cs", filters);
    let limit_placeholder = push_param(&mut params, limit as i64);
    let where_clause = conditions.join(" AND ");
    let columns = db::symbol_select_columns("cs");
    let sql = format!(
        "SELECT {columns}
         FROM code_symbols cs
         JOIN code_indexed_files cf
           ON cf.project_id = cs.project_id AND cf.file_path = cs.file_path
         WHERE {where_clause}
         ORDER BY {order_by}
         LIMIT {limit_placeholder}"
    );
    let refs = param_refs(&params);
    conn.query(&sql, &refs)
        .ok()
        .map(|rows| {
            rows.iter()
                .filter_map(|row| Symbol::from_row(row).ok())
                .collect()
        })
        .unwrap_or_default()
}

/// Sanitize user input for pg_search's BM25 query DSL.
pub fn sanitize_pg_search_query(query: &str) -> String {
    let cleaned: String = query
        .chars()
        .map(|ch| {
            if ch.is_alphanumeric() || matches!(ch, ' ' | '_' | '-') {
                ch
            } else {
                ' '
            }
        })
        .collect();
    cleaned
        .split_whitespace()
        .filter(|token| !token.is_empty())
        .collect::<Vec<_>>()
        .join(" ")
}

/// BM25 search across symbol names, qualified names, signatures, docstrings, and summaries.
pub fn search_symbols_fts(
    conn: &mut Client,
    query: &str,
    project_id: &str,
    kind: Option<&str>,
    language: Option<&str>,
    paths: &[String],
    limit: usize,
) -> Vec<Symbol> {
    let bm25_query = sanitize_pg_search_query(query);
    if bm25_query.is_empty() || limit == 0 {
        return Vec::new();
    }

    let mut params = Vec::new();
    let query_placeholder = push_param(&mut params, bm25_query);
    let project_placeholder = push_param(&mut params, project_id.to_string());
    let conditions = vec![
        format!(
            "(cs.name @@@ {q} OR cs.qualified_name @@@ {q} OR cs.signature @@@ {q} OR cs.docstring @@@ {q} OR cs.summary @@@ {q})",
            q = query_placeholder
        ),
        format!("cs.project_id = {project_placeholder}"),
    ];
    let filters = SymbolFilters {
        kind,
        language,
        paths,
    };
    query_symbols_by_conditions(
        conn,
        conditions,
        params,
        filters,
        limit,
        "pdb.score(cs.id) DESC, cs.id ASC",
    )
}

/// Fallback LIKE search on symbol names.
pub fn search_symbols_by_name(
    conn: &mut Client,
    query: &str,
    project_id: &str,
    kind: Option<&str>,
    language: Option<&str>,
    paths: &[String],
    limit: usize,
) -> Vec<Symbol> {
    if query.trim().is_empty() || limit == 0 {
        return Vec::new();
    }
    let escaped_query = escape_like(query);
    let pattern = format!("%{escaped_query}%");
    let mut params = Vec::new();
    let project_placeholder = push_param(&mut params, project_id.to_string());
    let name_placeholder = push_param(&mut params, pattern.clone());
    let qualified_placeholder = push_param(&mut params, pattern);
    let conditions = vec![
        format!("cs.project_id = {project_placeholder}"),
        format!(
            "(cs.name LIKE {name_placeholder} ESCAPE '\\' OR cs.qualified_name LIKE {qualified_placeholder} ESCAPE '\\')"
        ),
    ];
    query_symbols_by_conditions(
        conn,
        conditions,
        params,
        SymbolFilters {
            kind,
            language,
            paths,
        },
        limit,
        "cs.name ASC, cs.file_path ASC, cs.line_start ASC",
    )
}

pub fn search_symbols_exact_first(
    conn: &mut Client,
    query: &str,
    project_id: &str,
    kind: Option<&str>,
    language: Option<&str>,
    paths: &[String],
    limit: usize,
) -> Vec<Symbol> {
    if query.trim().is_empty() || limit == 0 {
        return Vec::new();
    }

    let mut results = Vec::new();
    let mut seen = HashSet::new();
    let filters = SymbolFilters {
        kind,
        language,
        paths,
    };

    let mut params = Vec::new();
    let project = push_param(&mut params, project_id.to_string());
    let name = push_param(&mut params, query.to_string());
    let qualified = push_param(&mut params, query.to_string());
    let exact = query_symbols_by_conditions(
        conn,
        vec![
            format!("cs.project_id = {project}"),
            format!("(cs.name = {name} OR cs.qualified_name = {qualified})"),
        ],
        params,
        filters,
        limit,
        "cs.file_path ASC, cs.line_start ASC",
    );
    append_unique_symbols(&mut results, &mut seen, exact, limit);
    if results.len() >= limit {
        return results;
    }

    let mut params = Vec::new();
    let project = push_param(&mut params, project_id.to_string());
    let name = push_param(&mut params, query.to_string());
    let qualified = push_param(&mut params, query.to_string());
    let ci_exact = query_symbols_by_conditions(
        conn,
        vec![
            format!("cs.project_id = {project}"),
            format!(
                "(lower(cs.name) = lower({name}) OR lower(cs.qualified_name) = lower({qualified}))"
            ),
        ],
        params,
        filters,
        limit,
        "cs.file_path ASC, cs.line_start ASC",
    );
    append_unique_symbols(&mut results, &mut seen, ci_exact, limit);
    if results.len() >= limit {
        return results;
    }

    let prefix_pattern = format!("{}%", escape_like(query));
    let mut params = Vec::new();
    let project = push_param(&mut params, project_id.to_string());
    let name = push_param(&mut params, prefix_pattern.clone());
    let qualified = push_param(&mut params, prefix_pattern);
    let prefix_matches = query_symbols_by_conditions(
        conn,
        vec![
            format!("cs.project_id = {project}"),
            format!(
                "(cs.name LIKE {name} ESCAPE '\\' OR cs.qualified_name LIKE {qualified} ESCAPE '\\')"
            ),
        ],
        params,
        filters,
        limit,
        "cs.name ASC, cs.file_path ASC, cs.line_start ASC",
    );
    append_unique_symbols(&mut results, &mut seen, prefix_matches, limit);
    if results.len() >= limit {
        return results;
    }

    let contains = search_symbols_by_name(conn, query, project_id, kind, language, paths, limit);
    append_unique_symbols(&mut results, &mut seen, contains, limit);
    if results.len() >= limit {
        return results;
    }

    let fts = search_symbols_fts(conn, query, project_id, kind, language, paths, limit);
    append_unique_symbols(&mut results, &mut seen, fts, limit);

    results
}

fn exact_symbol_matches(
    conn: &mut Client,
    project_id: &str,
    column: &str,
    input: &str,
    limit: usize,
) -> Vec<Symbol> {
    if !matches!(column, "id" | "qualified_name" | "name") {
        return Vec::new();
    }
    let columns = db::symbol_select_columns("");
    let sql = format!(
        "SELECT {columns}
         FROM code_symbols
         WHERE project_id = $1 AND {column} = $2
         ORDER BY file_path ASC, line_start ASC
         LIMIT $3"
    );
    conn.query(&sql, &[&project_id, &input, &(limit as i64)])
        .ok()
        .map(|rows| {
            rows.iter()
                .filter_map(|row| Symbol::from_row(row).ok())
                .collect()
        })
        .unwrap_or_default()
}

fn suggestion_label(symbol: &Symbol) -> String {
    format!(
        "{} ({}:{})",
        symbol.qualified_name, symbol.file_path, symbol.line_start
    )
}

fn resolved_symbol(symbol: &Symbol) -> ResolvedGraphSymbol {
    ResolvedGraphSymbol {
        id: symbol.id.clone(),
        display_name: symbol.name.clone(),
    }
}

fn resolve_from_candidates(candidates: Vec<Symbol>) -> (Option<ResolvedGraphSymbol>, Vec<String>) {
    match candidates.len() {
        0 => (None, vec![]),
        1 => (Some(resolved_symbol(&candidates[0])), vec![]),
        _ => {
            let mut suggestions = Vec::new();
            let mut seen = HashSet::new();
            for symbol in &candidates {
                let label = suggestion_label(symbol);
                if seen.insert(label.clone()) {
                    suggestions.push(label);
                }
            }
            (None, suggestions)
        }
    }
}

/// Resolve user input to a canonical symbol id for graph queries.
///
/// Resolution is fail-closed: ambiguous matches return `None` with suggestions.
pub fn resolve_graph_symbol(
    conn: &mut Client,
    input: &str,
    project_id: &str,
) -> (Option<ResolvedGraphSymbol>, Vec<String>) {
    let ids = exact_symbol_matches(conn, project_id, "id", input, 2);
    let (resolved, suggestions) = resolve_from_candidates(ids);
    if resolved.is_some() || !suggestions.is_empty() {
        return (resolved, suggestions);
    }

    let qualified = exact_symbol_matches(conn, project_id, "qualified_name", input, 6);
    let (resolved, suggestions) = resolve_from_candidates(qualified);
    if resolved.is_some() || !suggestions.is_empty() {
        return (resolved, suggestions);
    }

    let exact = exact_symbol_matches(conn, project_id, "name", input, 6);
    let (resolved, suggestions) = resolve_from_candidates(exact);
    if resolved.is_some() || !suggestions.is_empty() {
        return (resolved, suggestions);
    }

    let like_matches = search_symbols_by_name(conn, input, project_id, None, None, &[], 6);
    let (resolved, suggestions) = resolve_from_candidates(like_matches);
    if resolved.is_some() || !suggestions.is_empty() {
        return (resolved, suggestions);
    }

    let fts_results = search_symbols_fts(conn, input, project_id, None, None, &[], 6);
    resolve_from_candidates(fts_results)
}

/// Count matching symbols using pg_search BM25, with LIKE fallback.
pub fn count_text(
    conn: &mut Client,
    query: &str,
    project_id: &str,
    language: Option<&str>,
    paths: &[String],
) -> usize {
    if query.trim().is_empty() {
        return 0;
    }

    let bm25_query = sanitize_pg_search_query(query);
    // Intentional fallback: when BM25 sanitization empties the query, use
    // count_symbols_by_name_like, which may count LIKE matches BM25 filtered out.
    if bm25_query.is_empty() {
        return count_symbols_by_name_like(conn, query, project_id, language, paths);
    }

    let mut params = Vec::new();
    let query_placeholder = push_param(&mut params, bm25_query);
    let project_placeholder = push_param(&mut params, project_id.to_string());
    let mut conditions = vec![
        format!(
            "(cs.name @@@ {q} OR cs.qualified_name @@@ {q} OR cs.signature @@@ {q} OR cs.docstring @@@ {q} OR cs.summary @@@ {q})",
            q = query_placeholder
        ),
        format!("cs.project_id = {project_placeholder}"),
    ];
    push_symbol_filters(
        &mut conditions,
        &mut params,
        "cs",
        SymbolFilters {
            kind: None,
            language,
            paths,
        },
    );
    let refs = param_refs(&params);
    let sql = format!(
        "SELECT COUNT(*)::BIGINT AS count
         FROM code_symbols cs
         JOIN code_indexed_files cf
           ON cf.project_id = cs.project_id AND cf.file_path = cs.file_path
         WHERE {}",
        conditions.join(" AND ")
    );
    let count = conn
        .query_one(&sql, &refs)
        .ok()
        .and_then(|row| row.try_get::<_, i64>("count").ok())
        .unwrap_or(0);
    if count > 0 {
        return count as usize;
    }

    count_symbols_by_name_like(conn, query, project_id, language, paths)
}

fn count_symbols_by_name_like(
    conn: &mut Client,
    query: &str,
    project_id: &str,
    language: Option<&str>,
    paths: &[String],
) -> usize {
    let escaped_query = escape_like(query);
    let pattern = format!("%{escaped_query}%");
    let mut params = Vec::new();
    let project_placeholder = push_param(&mut params, project_id.to_string());
    let name_placeholder = push_param(&mut params, pattern.clone());
    let qualified_placeholder = push_param(&mut params, pattern);
    let mut conditions = vec![
        format!("cs.project_id = {project_placeholder}"),
        format!(
            "(cs.name LIKE {name_placeholder} ESCAPE '\\' OR cs.qualified_name LIKE {qualified_placeholder} ESCAPE '\\')"
        ),
    ];
    push_symbol_filters(
        &mut conditions,
        &mut params,
        "cs",
        SymbolFilters {
            kind: None,
            language,
            paths,
        },
    );
    let refs = param_refs(&params);
    let sql = format!(
        "SELECT COUNT(*)::BIGINT AS count
         FROM code_symbols cs
         JOIN code_indexed_files cf
           ON cf.project_id = cs.project_id AND cf.file_path = cs.file_path
         WHERE {}",
        conditions.join(" AND ")
    );
    conn.query_one(&sql, &refs)
        .ok()
        .and_then(|row| row.try_get::<_, i64>("count").ok())
        .unwrap_or(0) as usize
}

/// Count matching content chunks using pg_search BM25, with LIKE fallback.
pub fn count_content(
    conn: &mut Client,
    query: &str,
    project_id: &str,
    language: Option<&str>,
    paths: &[String],
) -> usize {
    if query.trim().is_empty() {
        return 0;
    }

    let bm25_query = sanitize_pg_search_query(query);
    if bm25_query.is_empty() {
        return count_content_like(conn, query, project_id, language, paths);
    }
    let mut params = Vec::new();
    let query_placeholder = push_param(&mut params, bm25_query);
    let project_placeholder = push_param(&mut params, project_id.to_string());
    let mut conditions = vec![
        format!("c.content @@@ {query_placeholder}"),
        format!("c.project_id = {project_placeholder}"),
    ];
    if let Some(lang) = language {
        let placeholder = push_param(&mut params, lang.to_string());
        conditions.push(format!("c.language = {placeholder}"));
    }
    push_path_filter(&mut conditions, &mut params, "c", paths);
    let refs = param_refs(&params);
    let sql = format!(
        "SELECT COUNT(*)::BIGINT AS count
         FROM code_content_chunks c
         JOIN code_indexed_files cf
           ON cf.project_id = c.project_id AND cf.file_path = c.file_path
         WHERE {}",
        conditions.join(" AND ")
    );
    let count = conn
        .query_one(&sql, &refs)
        .ok()
        .and_then(|row| row.try_get::<_, i64>("count").ok())
        .unwrap_or(0);
    if count > 0 {
        return count as usize;
    }

    count_content_like(conn, query, project_id, language, paths)
}

fn count_content_like(
    conn: &mut Client,
    query: &str,
    project_id: &str,
    language: Option<&str>,
    paths: &[String],
) -> usize {
    let escaped_query = escape_like(query);
    let like_query = format!("%{escaped_query}%");
    let mut params = Vec::new();
    let project_placeholder = push_param(&mut params, project_id.to_string());
    let like_placeholder = push_param(&mut params, like_query);
    let mut conditions = vec![
        format!("c.project_id = {project_placeholder}"),
        format!("c.content LIKE {like_placeholder} ESCAPE '\\'"),
    ];
    if let Some(lang) = language {
        let placeholder = push_param(&mut params, lang.to_string());
        conditions.push(format!("c.language = {placeholder}"));
    }
    push_path_filter(&mut conditions, &mut params, "c", paths);
    let refs = param_refs(&params);
    let sql = format!(
        "SELECT COUNT(*)::BIGINT AS count
         FROM code_content_chunks c
         JOIN code_indexed_files cf
           ON cf.project_id = c.project_id AND cf.file_path = c.file_path
         WHERE {}",
        conditions.join(" AND ")
    );
    conn.query_one(&sql, &refs)
        .ok()
        .and_then(|row| row.try_get::<_, i64>("count").ok())
        .unwrap_or(0) as usize
}

/// Full-text search for symbols: BM25 with LIKE fallback.
pub fn search_text(
    conn: &mut Client,
    query: &str,
    project_id: &str,
    language: Option<&str>,
    paths: &[String],
    limit: usize,
) -> Vec<SearchResult> {
    let mut results = search_symbols_fts(conn, query, project_id, None, language, paths, limit);
    if results.is_empty() {
        results = search_symbols_by_name(conn, query, project_id, None, language, paths, limit);
    }
    results.into_iter().map(|s| s.to_brief()).collect()
}

/// Full-text search across file content chunks.
pub fn search_content(
    conn: &mut Client,
    query: &str,
    project_id: &str,
    language: Option<&str>,
    paths: &[String],
    limit: usize,
) -> Vec<ContentSearchHit> {
    if query.trim().is_empty() || limit == 0 {
        return Vec::new();
    }

    let bm25_query = sanitize_pg_search_query(query);
    if bm25_query.is_empty() {
        return search_content_like(conn, query, project_id, language, paths, limit);
    }
    let mut params = Vec::new();
    let query_placeholder = push_param(&mut params, bm25_query);
    let project_placeholder = push_param(&mut params, project_id.to_string());
    let mut conditions = vec![
        format!("c.content @@@ {query_placeholder}"),
        format!("c.project_id = {project_placeholder}"),
    ];
    if let Some(lang) = language {
        let placeholder = push_param(&mut params, lang.to_string());
        conditions.push(format!("c.language = {placeholder}"));
    }
    push_path_filter(&mut conditions, &mut params, "c", paths);
    let limit_placeholder = push_param(&mut params, limit as i64);
    let refs = param_refs(&params);
    let sql = format!(
        "SELECT c.file_path,
                c.line_start::BIGINT AS line_start,
                c.line_end::BIGINT AS line_end,
                c.language,
                c.content
         FROM code_content_chunks c
         JOIN code_indexed_files cf
           ON cf.project_id = c.project_id AND cf.file_path = c.file_path
         WHERE {}
         ORDER BY pdb.score(c.id) DESC, c.id ASC
         LIMIT {limit_placeholder}",
        conditions.join(" AND ")
    );

    let hits: Vec<ContentSearchHit> = conn
        .query(&sql, &refs)
        .ok()
        .map(|rows| {
            rows.iter()
                .filter_map(|row| {
                    let content: String = row.try_get("content").ok()?;
                    Some(ContentSearchHit {
                        file_path: row.try_get("file_path").ok()?,
                        line_start: row.try_get::<_, i64>("line_start").ok()? as usize,
                        line_end: row.try_get::<_, i64>("line_end").ok()? as usize,
                        snippet: make_snippet(&content, query),
                        language: row.try_get("language").ok()?,
                    })
                })
                .collect()
        })
        .unwrap_or_default();

    if !hits.is_empty() {
        return hits;
    }

    search_content_like(conn, query, project_id, language, paths, limit)
}

fn search_content_like(
    conn: &mut Client,
    query: &str,
    project_id: &str,
    language: Option<&str>,
    paths: &[String],
    limit: usize,
) -> Vec<ContentSearchHit> {
    let escaped_query = escape_like(query);
    let like_query = format!("%{escaped_query}%");
    let mut params = Vec::new();
    let project_placeholder = push_param(&mut params, project_id.to_string());
    let like_placeholder = push_param(&mut params, like_query);
    let mut conditions = vec![
        format!("c.project_id = {project_placeholder}"),
        format!("c.content LIKE {like_placeholder} ESCAPE '\\'"),
    ];
    if let Some(lang) = language {
        let placeholder = push_param(&mut params, lang.to_string());
        conditions.push(format!("c.language = {placeholder}"));
    }
    push_path_filter(&mut conditions, &mut params, "c", paths);
    let limit_placeholder = push_param(&mut params, limit as i64);
    let refs = param_refs(&params);
    let sql = format!(
        "SELECT c.file_path,
                c.line_start::BIGINT AS line_start,
                c.line_end::BIGINT AS line_end,
                c.language,
                c.content
         FROM code_content_chunks c
         JOIN code_indexed_files cf
           ON cf.project_id = c.project_id AND cf.file_path = c.file_path
         WHERE {}
         ORDER BY c.file_path ASC, c.line_start ASC
         LIMIT {limit_placeholder}",
        conditions.join(" AND ")
    );

    conn.query(&sql, &refs)
        .ok()
        .map(|rows| {
            rows.iter()
                .filter_map(|row| {
                    let content: String = row.try_get("content").ok()?;
                    Some(ContentSearchHit {
                        file_path: row.try_get("file_path").ok()?,
                        line_start: row.try_get::<_, i64>("line_start").ok()? as usize,
                        line_end: row.try_get::<_, i64>("line_end").ok()? as usize,
                        snippet: make_snippet(&content, query),
                        language: row.try_get("language").ok()?,
                    })
                })
                .collect()
        })
        .unwrap_or_default()
}

fn make_snippet(content: &str, query: &str) -> String {
    let tokens: Vec<String> = query
        .split_whitespace()
        .map(str::to_lowercase)
        .filter(|token| !token.is_empty())
        .collect();
    let (lower_content, lower_byte_to_original_char) = lowercase_with_original_char_map(content);
    let mut match_at = None;
    for token in tokens {
        if let Some(byte_index) = lower_content.find(&token) {
            match_at = lower_byte_to_original_char
                .get(byte_index)
                .copied()
                .or(Some(0));
            break;
        }
    }
    let match_at = match_at.unwrap_or(0);
    let start = match_at.saturating_sub(60);
    let end = (match_at + 120).min(content.chars().count());
    content.chars().skip(start).take(end - start).collect()
}

fn lowercase_with_original_char_map(content: &str) -> (String, Vec<usize>) {
    let mut lower = String::with_capacity(content.len());
    let mut lower_byte_to_original_char = Vec::with_capacity(content.len());
    for (original_char_index, ch) in content.chars().enumerate() {
        for lower_ch in ch.to_lowercase() {
            let mut buf = [0; 4];
            let encoded = lower_ch.encode_utf8(&mut buf);
            lower_byte_to_original_char
                .extend(std::iter::repeat_n(original_char_index, encoded.len()));
            lower.push(lower_ch);
        }
    }
    (lower, lower_byte_to_original_char)
}

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

    #[test]
    fn sanitize_pg_search_query_matches_gobby_rules() {
        assert_eq!(
            sanitize_pg_search_query("foo::bar baz-qux _id + \"drop\""),
            "foo bar baz-qux _id drop"
        );
    }

    #[test]
    fn sanitize_pg_search_query_drops_empty_queries() {
        assert_eq!(sanitize_pg_search_query(":: + ()"), "");
    }

    #[test]
    fn glob_to_like_prefix_escapes_like_wildcards() {
        assert_eq!(
            glob_to_like_prefix("src/foo_bar/*.rs").as_deref(),
            Some("src/foo\\_bar/%")
        );
    }

    #[test]
    fn expand_paths_trims_skips_empty_and_expands_bare_paths() {
        let paths = vec![
            " src/gobby ".to_string(),
            "".to_string(),
            "crates/**/*.rs".to_string(),
            "src/gobby/".to_string(),
        ];

        assert_eq!(
            expand_paths(&paths),
            vec!["src/gobby", "src/gobby/**", "crates/**/*.rs"]
        );
    }

    #[test]
    fn compile_patterns_reports_invalid_glob() {
        let err = compile_patterns(&["src/[".to_string()])
            .expect_err("invalid glob should fail")
            .to_string();

        assert!(err.contains("invalid path glob `src/[`"));
    }

    #[test]
    fn path_like_prefixes_escape_and_require_all_patterns() {
        let paths = vec![
            "src/foo_bar".to_string(),
            "src/foo_bar/**".to_string(),
            "src/100%/**".to_string(),
        ];
        assert_eq!(
            path_like_prefixes(&paths).expect("prefixes"),
            vec!["src/foo\\_bar%", "src/foo\\_bar/%", "src/100\\%/%"]
        );

        let mixed = vec!["src/**".to_string(), "*.rs".to_string()];
        assert!(path_like_prefixes(&mixed).is_none());
        assert!(path_filter_falls_back(&mixed));
        assert!(!path_filter_falls_back(&paths));
    }

    #[test]
    fn snippet_centers_first_matching_token() {
        let content = "before ".repeat(20) + "target call here";
        let snippet = make_snippet(&content, "target");

        assert!(snippet.contains("target call here"));
        assert!(snippet.len() <= 180);
    }

    #[test]
    fn snippet_handles_unicode_before_match() {
        let content = "é".repeat(80) + " target call here";
        let snippet = make_snippet(&content, "target");

        assert!(snippet.contains("target call here"));
        assert!(snippet.chars().count() <= 180);

        let content = "\u{0130}".repeat(80) + " target call here";
        let snippet = make_snippet(&content, "target");

        assert!(snippet.contains("target call here"));
        assert!(snippet.chars().count() <= 180);
    }
}