|
| 1 | +//! `flowctl find` — Smart code search: auto-routes to the best search backend. |
| 2 | +//! |
| 3 | +//! Routing logic: |
| 4 | +//! - If query looks like regex (contains \s \d \w .* .+ [^ etc) -> index regex |
| 5 | +//! - If query matches a known symbol name (in graph) -> graph refs |
| 6 | +//! - If query is short (<3 chars) -> brute force fuzzy search |
| 7 | +//! - Otherwise -> index search (trigram literal), falling back to fuzzy search |
| 8 | +
|
| 9 | +use serde_json::json; |
| 10 | + |
| 11 | +use flowctl_core::frecency::FrecencyStore; |
| 12 | +use flowctl_core::fuzzy; |
| 13 | +use flowctl_core::graph_store::CodeGraph; |
| 14 | +use flowctl_core::ngram_index::NgramIndex; |
| 15 | + |
| 16 | +use crate::output::{json_output, pretty_output}; |
| 17 | + |
| 18 | +use super::helpers::get_flow_dir; |
| 19 | + |
| 20 | +/// Detect whether the query looks like a regex pattern. |
| 21 | +fn is_regex_pattern(query: &str) -> bool { |
| 22 | + query.contains("\\s") |
| 23 | + || query.contains("\\d") |
| 24 | + || query.contains("\\w") |
| 25 | + || query.contains(".*") |
| 26 | + || query.contains(".+") |
| 27 | + || query.contains("[^") |
| 28 | + || query.contains('(') |
| 29 | + || query.contains('|') |
| 30 | +} |
| 31 | + |
| 32 | +/// Run smart code search and display results. |
| 33 | +pub fn cmd_find(json: bool, query: &str, limit: usize) { |
| 34 | + let flow_dir = get_flow_dir(); |
| 35 | + |
| 36 | + // ── Strategy 1: Regex route ───────────────────────────────────── |
| 37 | + if is_regex_pattern(query) { |
| 38 | + let index_path = flow_dir.join("index").join("ngram.bin"); |
| 39 | + if index_path.exists() { |
| 40 | + if let Ok(idx) = NgramIndex::load(&index_path) { |
| 41 | + let results = idx.search_regex(query, limit); |
| 42 | + if !results.is_empty() { |
| 43 | + output_index_results(json, query, "index_regex", &results); |
| 44 | + return; |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + // Regex but no index — fall through to fuzzy |
| 49 | + output_fuzzy_fallback(json, query, limit); |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + // ── Strategy 2: Graph symbol lookup ───────────────────────────── |
| 54 | + let graph_path = flow_dir.join("graph.bin"); |
| 55 | + if graph_path.exists() { |
| 56 | + if let Ok(graph) = CodeGraph::load(&graph_path) { |
| 57 | + let refs = graph.find_refs(query); |
| 58 | + if !refs.is_empty() { |
| 59 | + if json { |
| 60 | + let items: Vec<_> = refs |
| 61 | + .iter() |
| 62 | + .map(|s| { |
| 63 | + json!({ |
| 64 | + "path": s.file, |
| 65 | + "line": s.line, |
| 66 | + "kind": s.kind, |
| 67 | + "context": s.signature, |
| 68 | + }) |
| 69 | + }) |
| 70 | + .collect(); |
| 71 | + json_output(json!({ |
| 72 | + "query": query, |
| 73 | + "backend": "graph_refs", |
| 74 | + "count": items.len(), |
| 75 | + "results": items, |
| 76 | + })); |
| 77 | + } else { |
| 78 | + let mut out = format!("{} symbol references for \"{}\":\n", refs.len(), query); |
| 79 | + for r in &refs { |
| 80 | + out.push_str(&format!( |
| 81 | + " {}:{} {} ({})\n", |
| 82 | + r.file, r.line, r.name, r.kind |
| 83 | + )); |
| 84 | + } |
| 85 | + pretty_output("find", &out); |
| 86 | + } |
| 87 | + return; |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + // ── Strategy 3: Short queries → skip trigram (needs 3+ chars) ─── |
| 93 | + if query.len() < 3 { |
| 94 | + output_fuzzy_fallback(json, query, limit); |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + // ── Strategy 4: Trigram index literal search ──────────────────── |
| 99 | + let index_path = flow_dir.join("index").join("ngram.bin"); |
| 100 | + if index_path.exists() { |
| 101 | + if let Ok(idx) = NgramIndex::load(&index_path) { |
| 102 | + let results = idx.search(query, limit); |
| 103 | + if !results.is_empty() { |
| 104 | + output_index_results(json, query, "index_literal", &results); |
| 105 | + return; |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + // ── Strategy 5: Fuzzy fallback ────────────────────────────────── |
| 111 | + output_fuzzy_fallback(json, query, limit); |
| 112 | +} |
| 113 | + |
| 114 | +/// Output results from the trigram index (literal or regex). |
| 115 | +fn output_index_results( |
| 116 | + json_flag: bool, |
| 117 | + query: &str, |
| 118 | + backend: &str, |
| 119 | + results: &[flowctl_core::ngram_index::NgramSearchResult], |
| 120 | +) { |
| 121 | + if json_flag { |
| 122 | + let items: Vec<_> = results |
| 123 | + .iter() |
| 124 | + .map(|r| { |
| 125 | + json!({ |
| 126 | + "path": r.path.to_string_lossy(), |
| 127 | + "line": null, |
| 128 | + "kind": "match", |
| 129 | + "context": format!("{} hits", r.match_count), |
| 130 | + }) |
| 131 | + }) |
| 132 | + .collect(); |
| 133 | + json_output(json!({ |
| 134 | + "query": query, |
| 135 | + "backend": backend, |
| 136 | + "count": items.len(), |
| 137 | + "results": items, |
| 138 | + })); |
| 139 | + } else { |
| 140 | + let mut out = format!( |
| 141 | + "{} matches for \"{}\" (backend: {}):\n", |
| 142 | + results.len(), |
| 143 | + query, |
| 144 | + backend |
| 145 | + ); |
| 146 | + for r in results { |
| 147 | + out.push_str(&format!(" {} ({} hits)\n", r.path.display(), r.match_count)); |
| 148 | + } |
| 149 | + pretty_output("find", &out); |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +/// Fuzzy file search fallback. |
| 154 | +fn output_fuzzy_fallback(json_flag: bool, query: &str, limit: usize) { |
| 155 | + let flow_dir = get_flow_dir(); |
| 156 | + let frecency = if flow_dir.exists() { |
| 157 | + Some(FrecencyStore::load(&flow_dir)) |
| 158 | + } else { |
| 159 | + None |
| 160 | + }; |
| 161 | + |
| 162 | + let root = std::env::current_dir().unwrap_or_else(|e| { |
| 163 | + crate::output::error_exit(&format!("Cannot determine current directory: {e}")); |
| 164 | + }); |
| 165 | + |
| 166 | + let results = fuzzy::search(&root, query, None, frecency.as_ref(), limit); |
| 167 | + |
| 168 | + if json_flag { |
| 169 | + let items: Vec<_> = results |
| 170 | + .iter() |
| 171 | + .map(|r| { |
| 172 | + json!({ |
| 173 | + "path": r.path, |
| 174 | + "line": null, |
| 175 | + "kind": "fuzzy", |
| 176 | + "context": format!("score: {:.1}", r.final_score), |
| 177 | + }) |
| 178 | + }) |
| 179 | + .collect(); |
| 180 | + json_output(json!({ |
| 181 | + "query": query, |
| 182 | + "backend": "fuzzy", |
| 183 | + "count": items.len(), |
| 184 | + "results": items, |
| 185 | + })); |
| 186 | + } else if results.is_empty() { |
| 187 | + pretty_output("find", &format!("No matches for \"{query}\"\n")); |
| 188 | + } else { |
| 189 | + let mut out = format!("{} fuzzy matches for \"{}\":\n", results.len(), query); |
| 190 | + for r in &results { |
| 191 | + out.push_str(&format!(" {:>8.1} {}\n", r.final_score, r.path)); |
| 192 | + } |
| 193 | + pretty_output("find", &out); |
| 194 | + } |
| 195 | +} |
0 commit comments