1use std::collections::HashSet;
4use std::io::{IsTerminal, Write};
5use std::path::PathBuf;
6use std::process::ExitCode;
7use std::time::Duration;
8
9use clap::{CommandFactory, Parser};
10use clap_complete::Shell;
11
12use crate::store::Store;
13
14#[derive(Parser)]
18#[command(
19 name = "rq",
20 version,
21 about = "Ranked definition lookup — the one place a symbol is defined, first.",
22 long_about = "rq finds where a symbol is defined and ranks the one you most \
23likely meant to the top — not every match.\n\n\
24Search is the default action; operations are flags, not subcommands, so every \
25word (including \"index\", \"status\", \"record\") stays searchable. Ranking favors \
26your current repo and recently-active files, and learns from the results you open \
27(see RECORDING below). Run `rq <query> --explain` to see the score behind each result.",
28 after_help = "EXAMPLES:\n \
29rq thing search for a definition named or like \"thing\"\n \
30rq wibble --explain same, plus the score behind each result\n \
31rq thing --json machine-readable results (for editors/agents)\n \
32rq thing --no-record search without recording it (speculative/agent queries)\n \
33rq thing --no-wait answer now from the committed index; don't block on a rebuild\n \
34rq thing --wait 2s ...or wait up to a bounded time for the index to warm\n \
35rq thing app/web restrict to a directory (rg-style)\n \
36rq perform -k method restrict to a symbol kind (c/mod/m/f/s/e/t)\n \
37rq class Widget a leading kind keyword is shorthand for -k\n \
38rq --symbols FILE outline a file's definitions, in line order\n \
39rq thing -x rust restrict to a language (ruby/rust/go/python/ts/js)\n \
40rq -o thing open the best match in your editor (and record it)\n \
41rq --index index the current repository\n \
42rq --status show indexing coverage\n \
43rq --drop remove this repo's index (opposite of --index)\n\n\
44SHORT FLAGS (easy to misread):\n \
45-j = --json (not jobs; --jobs is long-only) -l = --limit (not lang) -x = --lang\n\n\
46RECORDING (editor/shell hook):\n \
47rq --record --file <path> --line <n> <query>\n \
48Tells rq which result you opened for a query, so ranking learns. Pass --no-record \
49to a search to skip this. Editors and the script/rq-open wrapper call --record for you.\n\n\
50The index is a SQLite file at $RQ_DB (default ~/.local/share/rq/rq.db); it warms \
51automatically on the first search in a git repo. On a large, cold repo a search \
52keeps indexing until it can answer rather than reporting a premature \"no \
53matches\" (an interactive run shows progress and stops on Ctrl-C). Exit codes: 0 \
54= matched, 1 = no match, 2 = no match yet (index still warming — try again)."
55)]
56struct Cli {
57 #[arg(value_name = "TARGET", value_hint = clap::ValueHint::Other)]
64 target: Option<String>,
65
66 #[arg(value_name = "PATH")]
68 dirs: Vec<String>,
69
70 #[arg(short = 'e', long)]
72 explain: bool,
73
74 #[arg(long)]
76 no_record: bool,
77
78 #[arg(long = "no-wait")]
84 no_wait: bool,
85
86 #[arg(long, value_name = "DUR", value_parser = parse_wait, conflicts_with = "no_wait")]
91 wait: Option<Duration>,
92
93 #[arg(short = 'o', long, conflicts_with_all = ["index", "status", "record", "json", "ndjson"])]
98 open: bool,
99
100 #[arg(long, conflicts_with_all = ["open", "index", "status", "record", "symbols", "drop"])]
104 show: bool,
105
106 #[arg(short = 'j', long)]
108 json: bool,
109
110 #[arg(short = 'J', long, conflicts_with = "json")]
112 ndjson: bool,
113
114 #[arg(short = 'p', long, value_name = "DIR")]
116 path: Vec<String>,
117
118 #[arg(short = 'l', long, value_name = "N", default_value_t = 10)]
120 limit: usize,
121
122 #[arg(short = 'k', long, value_name = "KIND", value_delimiter = ',')]
126 kind: Vec<String>,
127
128 #[arg(short = 'x', long = "lang", value_name = "LANG", value_delimiter = ',')]
132 lang: Vec<String>,
133
134 #[arg(long = "all-repos")]
137 all_repos: bool,
138
139 #[arg(long, value_name = "PATH", num_args = 0..=1, value_hint = clap::ValueHint::AnyPath, conflicts_with_all = ["status", "record"])]
141 index: Option<Option<String>>,
142
143 #[arg(long, conflicts_with_all = ["index", "record"])]
145 status: bool,
146
147 #[arg(long, value_name = "FILE", value_hint = clap::ValueHint::FilePath, conflicts_with_all = ["index", "status", "record", "drop", "open"])]
150 symbols: Option<String>,
151
152 #[arg(long, conflicts_with_all = ["index", "status", "record", "open"])]
156 drop: bool,
157
158 #[arg(long, requires = "file", conflicts_with_all = ["index", "status"])]
161 record: bool,
162
163 #[arg(long)]
165 file: Option<String>,
166
167 #[arg(long)]
169 line: Option<i64>,
170
171 #[arg(long, default_value = "select")]
173 event: String,
174
175 #[arg(long, hide = true, value_name = "PATH", num_args = 0..=1, value_hint = clap::ValueHint::AnyPath, conflicts_with_all = ["index", "status", "record", "drop", "symbols", "open", "show"])]
179 warm: Option<Option<String>>,
180
181 #[arg(long, value_name = "SHELL")]
183 completions: Option<Shell>,
184
185 #[arg(short = 'v', long)]
188 verbose: bool,
189
190 #[arg(long)]
194 profile: bool,
195
196 #[arg(long, value_name = "N", default_value_t = 0)]
199 jobs: usize,
200}
201
202pub fn run() -> ExitCode {
204 let cli = Cli::parse();
205 crate::trace::enable_from(cli.verbose);
206 crate::profile::enable_from(cli.profile);
207 crate::index::set_parse_jobs(cli.jobs);
208
209 if let Some(shell) = cli.completions {
210 clap_complete::generate(shell, &mut Cli::command(), "rq", &mut std::io::stdout());
211 return ExitCode::SUCCESS;
212 }
213 if let Some(path) = &cli.index {
214 let out = output_format(&cli);
216 return cmd_index(path.as_deref().map(PathBuf::from), &cli.path, out);
217 }
218 if let Some(path) = &cli.warm {
219 return cmd_warm(path.as_deref());
220 }
221 if cli.status {
222 return cmd_status(output_format(&cli));
223 }
224 if cli.drop {
225 let out = output_format(&cli);
226 return cmd_drop(cli.target, out);
227 }
228 if cli.record {
229 if !matches!(cli.event.as_str(), "select" | "open") {
231 return fail(format_args!(
232 "rq --record: unknown --event {:?} (expected select or open)",
233 cli.event
234 ));
235 }
236 let file = cli.file.expect("--record requires --file");
238 return cmd_record(&cli.event, cli.target.as_deref(), &file, cli.line);
239 }
240 let out = output_format(&cli);
241 let mut kinds: Vec<String> = cli.kind.iter().map(|k| canonical_kind(k)).collect();
242 let langs: Vec<String> = cli.lang.iter().flat_map(|x| canonical_langs(x)).collect();
244 if let Some(file) = &cli.symbols {
245 return cmd_symbols(file, &kinds, &langs, out);
246 }
247 let mut paths = cli.path.clone();
249 match cli.target {
250 Some(target) => {
251 let query = if cli.kind.is_empty() {
254 let (kw, query, dirs) = split_kind_keyword(target, cli.dirs.clone());
255 if let Some(k) = kw {
256 kinds.push(k.to_string());
257 }
258 paths.extend(dirs);
259 query
260 } else {
261 paths.extend(cli.dirs.clone());
262 target
263 };
264 let mut session = match Session::open() {
265 Ok(s) => s,
266 Err(code) => return code,
267 };
268 cmd_search(
269 &mut session,
270 &SearchArgs {
271 query: &query,
272 explain: cli.explain,
273 out,
274 paths: &paths,
275 kinds: &kinds,
276 langs: &langs,
277 want: cli.limit,
278 no_record: cli.no_record,
279 no_wait: cli.no_wait,
280 wait: cli.wait,
281 open: cli.open,
282 all_repos: cli.all_repos,
283 show: cli.show,
284 batch: false,
285 },
286 )
287 }
288 None if !std::io::stdin().is_terminal() => cmd_batch(&cli, out, &paths, &kinds, &langs),
291 None => {
293 let _ = Cli::command().print_long_help();
294 ExitCode::SUCCESS
295 }
296 }
297}
298
299#[derive(Clone, Copy, PartialEq)]
301enum Output {
302 Text,
303 Json,
304 Ndjson,
305}
306
307fn output_format(cli: &Cli) -> Output {
308 if cli.ndjson {
309 Output::Ndjson
310 } else if cli.json {
311 Output::Json
312 } else {
313 Output::Text
314 }
315}
316
317const PATH_HEADROOM: usize = 200;
320
321const POLL_INTERVAL: Duration = Duration::from_millis(100);
328
329const HEADS_UP_DELAY: Duration = Duration::from_millis(500);
333
334const PROGRESS_REDRAW: Duration = Duration::from_millis(120);
338
339struct SearchArgs<'a> {
341 query: &'a str,
342 explain: bool,
343 out: Output,
344 paths: &'a [String],
345 kinds: &'a [String],
346 langs: &'a [String],
347 want: usize,
349 no_record: bool,
350 no_wait: bool,
352 wait: Option<Duration>,
355 open: bool,
356 all_repos: bool,
357 batch: bool,
361 show: bool,
362}
363
364struct Session {
375 store: Store,
376 cwd: Option<PathBuf>,
377 cwd_is_git: bool,
378 root: Option<PathBuf>,
379 active_paths: Vec<String>,
380 branch_refresh: Option<BranchRefresh>,
381 identity: Option<String>,
382 coverage: Option<String>,
383}
384
385impl Session {
386 fn open() -> std::result::Result<Session, ExitCode> {
388 let open_span = crate::profile::span("store open");
389 let store = match open_store() {
390 Ok(s) => s,
391 Err(e) => return Err(fail(format_args!("rq: cannot open database: {e}"))),
392 };
393 drop(open_span);
394 let git_span = crate::profile::span("setup: git root");
395 let cwd = std::env::current_dir().ok();
396 let cwd_is_git = cwd.as_deref().is_some_and(crate::index::is_git_repo);
397
398 let root = cwd
404 .as_deref()
405 .map(|c| crate::index::repo_root(c).unwrap_or_else(|| c.to_path_buf()));
406 drop(git_span);
407
408 let mut branch_span = crate::profile::span("setup: branch files");
411 let (active_paths, branch_refresh) = match &root {
412 Some(c) if cwd_is_git => cached_branch_files(&store, c),
413 _ => (Vec::new(), None),
414 };
415 branch_span.note(|| {
416 let how = if branch_refresh.is_some() {
417 "cached, refreshing alongside"
418 } else {
419 "cached"
420 };
421 format!("{} changed, {how}", active_paths.len())
422 });
423 drop(branch_span);
424
425 let mut identity_span = crate::profile::span("setup: identity");
430 let identity = root.as_deref().map(|c| resolve_identity(&store, c));
431 let coverage = identity
432 .as_deref()
433 .and_then(|id| store.coverage_status(id).ok())
434 .flatten();
435 identity_span.note(|| coverage.as_deref().unwrap_or("unknown").to_string());
436 drop(identity_span);
437 Ok(Session {
438 store,
439 cwd,
440 cwd_is_git,
441 root,
442 active_paths,
443 branch_refresh,
444 identity,
445 coverage,
446 })
447 }
448}
449
450fn cmd_batch(
465 cli: &Cli,
466 out: Output,
467 paths: &[String],
468 kinds: &[String],
469 langs: &[String],
470) -> ExitCode {
471 if out == Output::Json {
472 return fail(format_args!(
473 "rq: --json can't frame a stream of queries — use --ndjson (-J), \
474 where each line carries the query it answers"
475 ));
476 }
477 if cli.open || cli.show {
478 return fail(format_args!(
479 "rq: --open and --show act on a single result, not a stream of queries"
480 ));
481 }
482
483 use std::io::BufRead;
484 let queries: Vec<String> = std::io::stdin()
485 .lock()
486 .lines()
487 .map_while(std::result::Result::ok)
488 .map(|l| l.trim().to_string())
489 .filter(|l| !l.is_empty())
490 .collect();
491 if queries.is_empty() {
495 let _ = Cli::command().print_long_help();
496 return ExitCode::SUCCESS;
497 }
498
499 let mut session = match Session::open() {
500 Ok(s) => s,
501 Err(code) => return code,
502 };
503
504 if !cli.no_wait
507 && session.coverage.as_deref() != Some("complete")
508 && let Some(root) = session.root.clone()
509 {
510 {
511 let budget = cli.wait.unwrap_or_else(wait_budget);
512 crate::trace!(
513 "batch: warming {} queries' worth of index first",
514 queries.len()
515 );
516 let active = session.active_paths.clone();
517 let _ = crate::index::index_budgeted(&mut session.store, &root, &active, budget, None);
518 session.coverage = session
519 .identity
520 .as_deref()
521 .and_then(|id| session.store.coverage_status(id).ok())
522 .flatten();
523 }
524 }
525
526 let mut worst = ExitCode::SUCCESS;
527 let mut any_hit = false;
528 for query in &queries {
529 let code = cmd_search(
530 &mut session,
531 &SearchArgs {
532 query,
533 explain: cli.explain,
534 out,
535 paths,
536 kinds,
537 langs,
538 want: cli.limit,
539 no_record: cli.no_record,
540 no_wait: true,
544 wait: cli.wait,
545 open: false,
546 all_repos: cli.all_repos,
547 show: false,
548 batch: true,
549 },
550 );
551 if code == ExitCode::SUCCESS {
552 any_hit = true;
553 } else {
554 worst = code;
555 }
556 }
557 if any_hit { ExitCode::SUCCESS } else { worst }
560}
561
562fn cmd_search(session: &mut Session, args: &SearchArgs) -> ExitCode {
563 let &SearchArgs {
564 query,
565 out,
566 want,
567 no_record,
568 no_wait,
569 wait,
570 open,
571 all_repos,
572 show,
573 ..
574 } = args;
575 let wait_budget = wait.unwrap_or_else(wait_budget);
578 let no_wait = no_wait || wait_budget.is_zero();
579 let limit = if args.paths.is_empty() && args.kinds.is_empty() && args.langs.is_empty() {
582 want
583 } else {
584 (want * 20).max(PATH_HEADROOM)
585 };
586 let _timer = crate::trace::Timer::start("search done");
587 let profile_started = std::time::Instant::now();
588 let t_setup = std::time::Instant::now();
589 let setup_span = crate::profile::span("setup");
591 let Session {
594 store,
595 cwd,
596 cwd_is_git,
597 root,
598 active_paths,
599 branch_refresh,
600 identity,
601 coverage,
602 } = session;
603 let cwd_is_git = *cwd_is_git;
604
605 let known = coverage.is_some();
613 let warming_ok = cwd_is_git || known;
614 if crate::trace::enabled() {
615 crate::trace!(
616 "query {query:?}: root={} identity={} coverage={} warming_ok={warming_ok} active={}",
617 root.as_deref().map_or("?".into(), crate::trace::abbrev),
618 identity.as_deref().unwrap_or("none"),
619 coverage.as_deref().unwrap_or("none"),
620 active_paths.len(),
621 );
622 }
623 let repo_span = crate::profile::span("setup: repo state");
624 let current = identity
625 .as_deref()
626 .and_then(|id| store.repository_id(id).ok().flatten());
627 let only_repo = if all_repos { None } else { current };
630 let active = crate::search::ActiveFiles::new(active_paths.clone());
631
632 if !no_record && let Some(repo) = current {
636 let qn = query.to_ascii_lowercase();
637 if store.is_repeat_search(repo, &qn).unwrap_or(false) {
638 let _ = store.decay_selections(repo, &qn);
639 }
640 }
641
642 drop(repo_span);
643 let warm_span = crate::profile::span("setup: warm decision");
644
645 let warm_budget = if warm_detach_enabled() {
652 answer_warm_budget()
653 } else {
654 answer_warm_budget() + deferred_warm_budget()
655 };
656 let was_warming = coverage.as_deref() != Some("complete");
657
658 let indexed_head = (!was_warming)
669 .then(|| current.and_then(|id| store.indexed_head(id).ok().flatten()))
670 .flatten();
671 let staleness = (!was_warming && warming_ok && !args.batch)
674 .then(|| root.clone())
675 .flatten()
676 .map(|c| std::thread::spawn(move || worktree_changed(&c, indexed_head.as_deref())));
677 let want_warm = warming_ok && was_warming && root.is_some();
680
681 let block = want_warm && was_warming && !no_wait;
695 let progress_ui = block && show_progress(out, stderr_interactive());
700 let indexer_budget = if block { wait_budget } else { warm_budget };
701 if progress_ui {
702 install_interrupt_handler();
703 }
704
705 let warm_done = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
708 let indexer = (want_warm && root.is_some() && !no_wait).then(|| {
709 crate::trace!(
710 "background warm ({indexer_budget:?}, block={block}, progress_ui={progress_ui}, {} jobs)",
711 crate::index::parse_jobs()
712 );
713 let root = root.clone().expect("checked");
714 let active = active_paths.clone();
715 let q = query.to_string();
716 let warm_done = std::sync::Arc::clone(&warm_done);
717 std::thread::spawn(move || {
718 if let Ok(mut idx) = open_store() {
719 let _ = if block {
721 crate::index::index_budgeted_cancellable(
724 &mut idx,
725 &root,
726 &active,
727 indexer_budget,
728 Some(&q),
729 &INTERRUPTED,
730 )
731 } else {
732 crate::index::index_budgeted(&mut idx, &root, &active, indexer_budget, Some(&q))
733 };
734 }
735 warm_done.store(true, std::sync::atomic::Ordering::Relaxed);
736 })
737 });
738
739 crate::trace!(
746 "setup (open + repo detect + warm decision): {} ms",
747 t_setup.elapsed().as_millis()
748 );
749 let poll_start = std::time::Instant::now();
750 let deadline = if progress_ui {
754 None
755 } else if block {
756 Some(poll_start + wait_budget)
757 } else {
758 Some(poll_start + answer_warm_budget())
759 };
760 drop(warm_span);
761 let polling = indexer.is_some() && was_warming;
762 drop(setup_span);
766 let mut query_span = crate::profile::span("query");
767 let label = repo_label(root.as_deref());
768 let mut drew_progress = false;
769 let mut last_draw = poll_start;
770 let mut hits = loop {
771 match crate::search::search(store, query, current, only_repo, &active, limit) {
772 Ok(h) => {
773 let confident = h.first().is_some_and(|hit| {
774 hit.features
775 .iter()
776 .any(|f| matches!(f.name, "exact" | "prefix"))
777 });
778 let warm_finished = warm_done.load(std::sync::atomic::Ordering::Relaxed);
779 let stopped = INTERRUPTED.load(std::sync::atomic::Ordering::Relaxed);
780 let timed_out = deadline.is_some_and(|d| std::time::Instant::now() >= d);
781 if !polling || confident || warm_finished || stopped || timed_out {
782 break h;
783 }
784 if progress_ui
785 && poll_start.elapsed() >= HEADS_UP_DELAY
786 && last_draw.elapsed() >= PROGRESS_REDRAW
787 {
788 draw_progress(store, identity.as_deref(), &label);
789 drew_progress = true;
790 last_draw = std::time::Instant::now();
791 }
792 }
793 Err(e) => {
794 if let Some(h) = indexer {
795 let _ = h.join();
796 }
797 return fail(format_args!("rq: {e}"));
798 }
799 }
800 std::thread::sleep(POLL_INTERVAL);
801 };
802 query_span.note(|| {
803 if polling {
804 "polled a warming index".to_string()
805 } else {
806 String::new()
807 }
808 });
809 drop(query_span);
810 if drew_progress {
811 clear_progress();
812 }
813 let interrupted = INTERRUPTED.load(std::sync::atomic::Ordering::Relaxed);
815
816 if !hits.is_empty() && revalidate_top(store, &hits) {
818 hits = crate::search::search(store, query, current, only_repo, &active, limit)
819 .unwrap_or_default();
820 }
821
822 if !hits.iter().any(strong)
826 && indexer.is_none()
827 && coverage.is_none()
828 && let Some(root) = &root
829 {
830 let tail = live_fallback(root, query, limit);
831 hits = crate::search::merge(hits, tail, limit);
832 }
833
834 apply_gates(query, &mut hits);
835 apply_post_filters(args, cwd.as_deref(), root.as_deref(), &mut hits);
836
837 if hits.is_empty() {
838 if block {
840 INTERRUPTED.store(true, std::sync::atomic::Ordering::Relaxed);
841 }
842 if let Some(h) = indexer {
843 let _ = h.join();
844 }
845 let mut incomplete = (block || no_wait)
852 && identity
853 .as_deref()
854 .and_then(|id| store.coverage_status(id).ok().flatten())
855 .as_deref()
856 != Some("complete");
857 incomplete |= settle_warm(
867 store,
868 staleness,
869 was_warming,
870 warming_ok,
871 root.as_deref(),
872 active_paths,
873 query,
874 warm_budget,
875 no_wait,
876 identity.as_deref(),
877 );
878 return no_match_code(out, query, interrupted, incomplete);
879 }
880
881 for hit in &mut hits {
884 hit.signature = read_signature(
885 store,
886 &hit.repo_identity,
887 &hit.file,
888 hit.line,
889 cwd.as_deref(),
890 );
891 }
892 attach_confidence(&mut hits);
893
894 if show && let Some(code) = show_top_definition(store, &mut hits, query, out, cwd.as_deref()) {
897 return code;
898 }
899
900 if open {
904 return finish_open(store, &hits, query, current, root.as_deref(), no_record);
905 }
906
907 if let Some(code) = render_hits(args, &hits) {
908 return code;
909 }
910
911 if crate::profile::enabled() {
914 let total = profile_started.elapsed();
915 if args.out == Output::Text {
916 for line in crate::profile::report(total) {
917 eprintln!("{line}");
918 }
919 } else {
920 eprintln!("{}", crate::profile::json(total));
923 }
924 }
925
926 if let Some(refresh) = branch_refresh.take() {
933 refresh.store(store);
934 }
935
936 if !no_record {
941 let _ = store.record_event(
942 "search",
943 Some(&query.to_ascii_lowercase()),
944 current,
945 None,
946 None,
947 None,
948 );
949 }
950 deferred_maintenance(store);
951
952 if block {
957 INTERRUPTED.store(true, std::sync::atomic::Ordering::Relaxed);
958 }
959 if let Some(h) = indexer {
960 let _ = h.join();
961 }
962 let _ = settle_warm(
963 store,
964 staleness,
965 was_warming,
966 warming_ok,
967 root.as_deref(),
968 active_paths,
969 query,
970 warm_budget,
971 no_wait,
972 identity.as_deref(),
973 );
974
975 ExitCode::SUCCESS
976}
977
978fn maybe_detach_warm(
981 store: &Store,
982 want_warm: bool,
983 changed: bool,
984 root: Option<&std::path::Path>,
985 identity: Option<&str>,
986) {
987 if !warm_detach_enabled() || !want_warm {
988 return;
989 }
990 let (Some(root), Some(id)) = (root, identity) else {
991 return;
992 };
993 if !changed && store.coverage_status(id).ok().flatten().as_deref() == Some("complete") {
997 return; }
999 spawn_detached_warm(root);
1000}
1001
1002fn spawn_detached_warm(root: &std::path::Path) {
1006 use std::os::unix::process::CommandExt;
1007 let Ok(exe) = std::env::current_exe() else {
1008 return;
1009 };
1010 let mut cmd = std::process::Command::new(exe);
1011 cmd.arg("--warm")
1012 .arg(root)
1013 .stdin(std::process::Stdio::null())
1014 .stdout(std::process::Stdio::null())
1015 .stderr(std::process::Stdio::null())
1016 .process_group(0);
1017 match cmd.spawn() {
1018 Ok(child) => crate::trace!(
1019 "background warm (detached): pid {} for {}",
1020 child.id(),
1021 crate::trace::abbrev(root)
1022 ),
1023 Err(e) => crate::trace!("detached warm failed to spawn: {e}"),
1024 }
1025}
1026
1027const WARM_LOCK_TTL_SECS: i64 = 600;
1030
1031fn cmd_warm(path: Option<&str>) -> ExitCode {
1036 #[cfg(target_os = "macos")]
1039 unsafe extern "C" {
1040 fn setiopolicy_np(
1043 iotype: libc::c_int,
1044 scope: libc::c_int,
1045 policy: libc::c_int,
1046 ) -> libc::c_int;
1047 }
1048 unsafe {
1049 libc::nice(10);
1050 #[cfg(target_os = "macos")]
1051 setiopolicy_np(0, 0, 3);
1052 }
1053 let mut store = match open_store() {
1054 Ok(s) => s,
1055 Err(_) => return ExitCode::FAILURE,
1056 };
1057 let start = path
1058 .map(PathBuf::from)
1059 .or_else(|| std::env::current_dir().ok())
1060 .unwrap_or_else(|| PathBuf::from("."));
1061 let root = crate::index::repo_root(&start).unwrap_or(start);
1062 let identity = resolve_identity(&store, &root);
1063
1064 if let Ok(Some((pid, ts))) = store.warm_lock(&identity)
1067 && pid != std::process::id()
1068 && unsafe { libc::kill(pid as libc::pid_t, 0) } == 0
1069 && now_secs() - ts < WARM_LOCK_TTL_SECS
1070 {
1071 return ExitCode::SUCCESS;
1072 }
1073 let _ = store.set_warm_lock(&identity, std::process::id());
1074
1075 let deadline = std::time::Instant::now() + warm_bg_budget();
1078 let active = crate::index::branch_changed_files(&root);
1079 loop {
1080 let remaining = deadline.saturating_duration_since(std::time::Instant::now());
1081 if remaining.is_zero() {
1082 break;
1083 }
1084 let stats = match crate::index::index_budgeted(&mut store, &root, &active, remaining, None)
1085 {
1086 Ok(s) => s,
1087 Err(_) => break,
1088 };
1089 if store.coverage_status(&identity).ok().flatten().as_deref() == Some("complete")
1090 || stats.files_indexed == 0
1091 {
1092 break;
1093 }
1094 }
1095 let _ = store.clear_warm_lock(&identity);
1096 ExitCode::SUCCESS
1097}
1098
1099fn now_secs() -> i64 {
1100 std::time::SystemTime::now()
1101 .duration_since(std::time::UNIX_EPOCH)
1102 .map(|d| d.as_secs() as i64)
1103 .unwrap_or(0)
1104}
1105
1106fn live_fallback(root: &std::path::Path, query: &str, limit: usize) -> Vec<crate::search::Hit> {
1109 crate::trace!("empty → live (in-memory) scan of an untracked dir");
1110 let deadline = std::time::Instant::now() + live_fallback_budget();
1111 let h = crate::search::live_search(root, query, limit, &HashSet::new(), Some(deadline), true);
1112 if !h.is_empty() {
1113 return h;
1114 }
1115 crate::search::live_search(root, query, limit, &HashSet::new(), Some(deadline), false)
1116}
1117
1118fn strong(h: &crate::search::Hit) -> bool {
1120 h.features
1121 .iter()
1122 .any(|f| matches!(f.name, "exact" | "prefix"))
1123}
1124
1125fn apply_gates(query: &str, hits: &mut Vec<crate::search::Hit>) {
1134 if hits.iter().any(strong) {
1135 hits.retain(strong);
1136 }
1137 crate::search::apply_scope_gate(query, hits);
1138}
1139
1140fn apply_post_filters(
1143 args: &SearchArgs,
1144 cwd: Option<&std::path::Path>,
1145 root: Option<&std::path::Path>,
1146 hits: &mut Vec<crate::search::Hit>,
1147) {
1148 if !args.paths.is_empty() {
1149 let here = cwd.map_or_else(|| PathBuf::from("."), PathBuf::from);
1153 let base = root.map_or_else(|| here.clone(), PathBuf::from);
1154 let norm: Vec<String> = args
1155 .paths
1156 .iter()
1157 .map(|p| repo_relative(&base, &here, p))
1158 .collect();
1159 hits.retain(|h| under_any(&h.file, &norm));
1160 }
1161 if !args.kinds.is_empty() {
1162 hits.retain(|h| args.kinds.iter().any(|k| k == &h.kind));
1163 }
1164 if !args.langs.is_empty() {
1165 hits.retain(|h| args.langs.iter().any(|l| l == &h.language));
1166 }
1167 if !args.paths.is_empty() || !args.kinds.is_empty() || !args.langs.is_empty() {
1168 hits.truncate(args.want);
1169 }
1170}
1171
1172fn no_match_code(out: Output, query: &str, interrupted: bool, incomplete: bool) -> ExitCode {
1178 let status = if interrupted {
1179 "interrupted"
1180 } else if incomplete {
1181 "warming"
1182 } else {
1183 "no_match"
1184 };
1185 match out {
1186 Output::Json | Output::Ndjson => {
1187 let obj = serde_json::json!({ "status": status, "query": query });
1188 let _ = emit_json(out, &obj); }
1190 Output::Text if interrupted => {
1191 eprintln!("rq: indexing interrupted — run again to finish")
1192 }
1193 Output::Text if incomplete => eprintln!(
1194 "rq: still indexing — no match for {query:?} yet (run again, or `rq --index` to finish)"
1195 ),
1196 Output::Text => eprintln!("no matches for {query:?}"),
1197 }
1198 if incomplete {
1199 ExitCode::from(2)
1200 } else {
1201 ExitCode::FAILURE
1202 }
1203}
1204
1205fn attach_confidence(hits: &mut [crate::search::Hit]) {
1209 let (top, second) = hits.iter().fold((None::<f64>, None::<f64>), |(t, s), h| {
1210 if t.is_none_or(|t| h.score > t) {
1211 (Some(h.score), t)
1212 } else if s.is_none_or(|s| h.score > s) {
1213 (t, Some(h.score))
1214 } else {
1215 (t, s)
1216 }
1217 });
1218 for hit in hits.iter_mut() {
1219 let best_other = if Some(hit.score) == top { second } else { top };
1220 hit.confidence = crate::search::confidence(
1221 hit.score,
1222 crate::search::match_quality(&hit.features),
1223 best_other,
1224 );
1225 }
1226}
1227
1228fn render_hits(args: &SearchArgs, hits: &[crate::search::Hit]) -> Option<ExitCode> {
1231 let render_span = crate::profile::span("render");
1235 if args.batch {
1236 #[derive(serde::Serialize)]
1239 struct Tagged<'a> {
1240 query: &'a str,
1241 #[serde(flatten)]
1242 hit: &'a crate::search::Hit,
1243 }
1244 let rows: Vec<Tagged> = hits
1245 .iter()
1246 .map(|hit| Tagged {
1247 query: args.query,
1248 hit,
1249 })
1250 .collect();
1251 if let Some(code) = emit_rows(args.out, &rows) {
1252 return Some(code);
1253 }
1254 } else if let Some(code) = emit_rows(args.out, hits) {
1255 return Some(code);
1256 }
1257 if args.out != Output::Text {
1258 return None;
1259 }
1260 drop(render_span);
1261 let color = match_color();
1262 let c = color.as_deref();
1263 let query = args.query;
1264 if args.show {
1265 eprintln!(
1267 "rq: no single confident match for {query:?} — {} candidates below; narrow the query to --show one",
1268 hits.len()
1269 );
1270 }
1271 for hit in hits {
1272 let name = hl(&hit.name, query, c);
1275 let qualified = match &hit.parent {
1276 Some(p) => format!("{name} · {p}"),
1277 None => name,
1278 };
1279 println!(
1280 "{}:{} {} {}",
1281 hl_path(&hit.file, query, c),
1282 hit.line,
1283 hit.kind,
1284 qualified
1285 );
1286 if let Some(sig) = &hit.signature {
1287 println!(" {}", hl(sig, query, c));
1288 }
1289 if args.explain {
1290 let parts: Vec<String> = hit
1291 .features
1292 .iter()
1293 .map(|f| format!("{} {:.0}", f.name, f.value))
1294 .collect();
1295 println!(
1296 " confidence {:.2} · score {:.0} = {}",
1297 hit.confidence,
1298 hit.score,
1299 parts.join(" + ")
1300 );
1301 }
1302 }
1303 None
1304}
1305
1306fn choose_hit(hits: &[crate::search::Hit]) -> Option<&crate::search::Hit> {
1310 use std::io::{IsTerminal, Write};
1311 if hits.len() == 1 || !std::io::stdin().is_terminal() || !std::io::stderr().is_terminal() {
1312 return hits.first();
1313 }
1314 let mut err = std::io::stderr();
1315 let _ = writeln!(err, "rq: {} matches — pick one (enter = 1):", hits.len());
1316 for (i, h) in hits.iter().enumerate() {
1317 let _ = writeln!(
1318 err,
1319 " {}. {}:{} {} {}",
1320 i + 1,
1321 h.file,
1322 h.line,
1323 h.kind,
1324 h.name
1325 );
1326 }
1327 let _ = write!(err, "rq> ");
1328 let _ = err.flush();
1329 let mut line = String::new();
1330 if std::io::stdin().read_line(&mut line).unwrap_or(0) == 0 {
1331 return None; }
1333 parse_choice(&line, hits.len()).and_then(|i| hits.get(i))
1334}
1335
1336fn parse_choice(input: &str, n: usize) -> Option<usize> {
1339 let s = input.trim();
1340 if s.is_empty() {
1341 return Some(0);
1342 }
1343 let i = s.parse::<usize>().ok()?.checked_sub(1)?;
1344 (i < n).then_some(i)
1345}
1346
1347fn finish_open(
1351 store: &mut Store,
1352 hits: &[crate::search::Hit],
1353 query: &str,
1354 current: Option<i64>,
1355 root: Option<&std::path::Path>,
1356 no_record: bool,
1357) -> ExitCode {
1358 let Some(hit) = choose_hit(hits) else {
1359 return ExitCode::SUCCESS; };
1361
1362 if !no_record {
1365 let _ = store.record_event(
1366 "select",
1367 Some(&query.to_ascii_lowercase()),
1368 current,
1369 Some(&hit.file),
1370 Some(hit.line),
1371 None,
1372 );
1373 deferred_maintenance(store);
1374 }
1375
1376 let target = match root {
1379 Some(r) => r.join(&hit.file),
1380 None => PathBuf::from(&hit.file),
1381 };
1382 launch_editor(&target, hit.line)
1383}
1384
1385fn launch_editor(file: &std::path::Path, line: i64) -> ExitCode {
1389 use std::os::unix::process::CommandExt;
1390 let loc = format!("{}:{}", file.display(), line);
1391 match open_command(file, line, &loc) {
1392 Some((prog, args)) => {
1393 let err = std::process::Command::new(&prog).args(&args).exec();
1395 fail(format_args!("rq --open: cannot run {prog}: {err}"))
1396 }
1397 None => {
1398 println!("{loc}");
1399 ExitCode::SUCCESS
1400 }
1401 }
1402}
1403
1404fn open_command(file: &std::path::Path, line: i64, loc: &str) -> Option<(String, Vec<String>)> {
1408 let fstr = file.to_string_lossy().into_owned();
1409
1410 if let Some(t) = std::env::var_os("RQ_OPEN") {
1411 let t = t.to_string_lossy();
1412 let mut parts = t.split_whitespace().map(|p| {
1413 p.replace("{file}", &fstr)
1414 .replace("{line}", &line.to_string())
1415 .replace("{}", loc)
1416 });
1417 if let Some(prog) = parts.next() {
1418 return Some((prog, parts.collect()));
1419 }
1420 }
1421
1422 if on_path("code") {
1423 return Some(("code".into(), vec!["--goto".into(), loc.into()]));
1424 }
1425
1426 if let Some(ed) = std::env::var_os("VISUAL").or_else(|| std::env::var_os("EDITOR")) {
1427 let ed = ed.to_string_lossy().into_owned();
1428 let l = ed.to_ascii_lowercase();
1429 if ["vim", "nvim", "vi", "nano", "emacs", "kak", "micro"]
1431 .iter()
1432 .any(|e| l.contains(e))
1433 {
1434 return Some((ed, vec![format!("+{line}"), fstr]));
1435 }
1436 return Some((ed, vec![fstr]));
1437 }
1438
1439 None
1440}
1441
1442fn on_path(prog: &str) -> bool {
1444 std::env::var_os("PATH")
1445 .is_some_and(|paths| std::env::split_paths(&paths).any(|dir| dir.join(prog).is_file()))
1446}
1447
1448fn unix_now() -> i64 {
1458 std::time::SystemTime::now()
1459 .duration_since(std::time::UNIX_EPOCH)
1460 .map(|d| d.as_secs() as i64)
1461 .unwrap_or(0)
1462}
1463
1464const BRANCH_FILES_TTL_SECS: i64 = 15;
1470
1471struct BranchRefresh {
1475 handle: std::thread::JoinHandle<Vec<String>>,
1476 identity: String,
1477 stamp: String,
1478}
1479
1480impl BranchRefresh {
1481 fn store(self, store: &Store) {
1483 let Ok(files) = self.handle.join() else {
1484 return;
1485 };
1486 let _ = store.branch_files_set(&self.identity, &self.stamp, unix_now(), &files);
1487 }
1488}
1489
1490fn cached_branch_files(
1504 store: &Store,
1505 root: &std::path::Path,
1506) -> (Vec<String>, Option<BranchRefresh>) {
1507 let identity = resolve_identity(store, root);
1508 let stamp = crate::index::branch_files_stamp(root);
1509 let cached = store.branch_files_get(&identity).ok().flatten();
1510 let now = unix_now();
1511
1512 if let (Some((cached_stamp, at, files)), Some(stamp)) = (&cached, &stamp) {
1513 if cached_stamp == stamp && now.saturating_sub(*at) < BRANCH_FILES_TTL_SECS {
1514 return (files.clone(), None);
1515 }
1516 let owned_root = root.to_path_buf();
1517 let refresh = BranchRefresh {
1518 handle: std::thread::spawn(move || crate::index::branch_changed_files(&owned_root)),
1519 identity,
1520 stamp: stamp.clone(),
1521 };
1522 return (files.clone(), Some(refresh));
1523 }
1524
1525 let files = crate::index::branch_changed_files(root);
1527 if let Some(stamp) = stamp {
1528 let _ = store.branch_files_set(&identity, &stamp, now, &files);
1529 }
1530 (files, None)
1531}
1532
1533fn worktree_changed(cwd: &std::path::Path, indexed_head: Option<&str>) -> bool {
1542 let Some(head) = indexed_head else {
1543 return true;
1544 };
1545 crate::index::git_head(cwd).as_deref() != Some(head) || crate::index::is_dirty(cwd)
1546}
1547
1548#[allow(clippy::too_many_arguments)]
1556fn settle_warm(
1557 store: &Store,
1558 staleness: Option<std::thread::JoinHandle<bool>>,
1559 was_warming: bool,
1560 warming_ok: bool,
1561 root: Option<&std::path::Path>,
1562 active: &[String],
1563 query: &str,
1564 budget: Duration,
1565 no_wait: bool,
1566 identity: Option<&str>,
1567) -> bool {
1568 let changed = staleness.is_some_and(|h| h.join().unwrap_or(true));
1571 if changed
1581 && !no_wait
1582 && !warm_detach_enabled()
1583 && let Some(r) = root
1584 && let Ok(mut idx) = open_store()
1585 {
1586 crate::trace!("background warm (deferred, {budget:?}): worktree changed since index");
1587 let _ = crate::index::index_budgeted(&mut idx, r, active, budget, Some(query));
1588 }
1589 maybe_detach_warm(
1590 store,
1591 warming_ok && (was_warming || changed),
1592 changed,
1593 root,
1594 identity,
1595 );
1596 changed && warm_detach_enabled()
1600}
1601
1602fn answer_warm_budget() -> Duration {
1611 env_budget("RQ_ANSWER_BUDGET_MS", 500)
1612}
1613
1614fn deferred_warm_budget() -> Duration {
1617 env_budget("RQ_DEFERRED_BUDGET_MS", 250)
1618}
1619
1620fn live_fallback_budget() -> Duration {
1623 env_budget("RQ_FALLBACK_BUDGET_MS", 250)
1624}
1625
1626fn warm_bg_budget() -> Duration {
1629 env_budget("RQ_WARM_BUDGET_MS", 20_000)
1630}
1631
1632fn warm_detach_enabled() -> bool {
1636 std::env::var("RQ_WARM_DETACH").map_or(true, |v| v != "0")
1637}
1638
1639fn wait_budget() -> Duration {
1648 env_budget("RQ_WAIT_BUDGET_MS", 60_000)
1649}
1650
1651fn parse_wait(s: &str) -> std::result::Result<Duration, String> {
1656 let s = s.trim();
1657 let bad = || format!("invalid duration {s:?} — use e.g. 50ms, 2s, 1m, or 0");
1658 let (num, unit_ms) = if let Some(n) = s.strip_suffix("ms") {
1660 (n, 1.0)
1661 } else if let Some(n) = s.strip_suffix('s') {
1662 (n, 1_000.0)
1663 } else if let Some(n) = s.strip_suffix('m') {
1664 (n, 60_000.0)
1665 } else {
1666 (s, 1_000.0)
1668 };
1669 let val: f64 = num.trim().parse().map_err(|_| bad())?;
1670 if !val.is_finite() || val < 0.0 {
1671 return Err(bad());
1672 }
1673 Ok(Duration::from_millis((val * unit_ms).round() as u64))
1674}
1675
1676static INTERRUPTED: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
1680
1681extern "C" fn on_sigint(_: libc::c_int) {
1682 INTERRUPTED.store(true, std::sync::atomic::Ordering::Relaxed);
1684}
1685
1686fn install_interrupt_handler() {
1689 static ONCE: std::sync::Once = std::sync::Once::new();
1690 ONCE.call_once(|| unsafe {
1691 let mut action: libc::sigaction = std::mem::zeroed();
1692 action.sa_sigaction = on_sigint as *const () as usize;
1693 libc::sigemptyset(&mut action.sa_mask);
1694 libc::sigaction(libc::SIGINT, &action, std::ptr::null_mut());
1695 });
1696}
1697
1698fn stderr_interactive() -> bool {
1702 std::io::stderr().is_terminal() || std::env::var_os("RQ_ASSUME_INTERACTIVE").is_some()
1703}
1704
1705fn show_progress(out: Output, interactive: bool) -> bool {
1710 interactive && matches!(out, Output::Text)
1711}
1712
1713fn repo_label(root: Option<&std::path::Path>) -> String {
1716 root.and_then(|r| r.file_name())
1717 .map(|n| n.to_string_lossy().into_owned())
1718 .unwrap_or_else(|| "repo".into())
1719}
1720
1721fn draw_progress(store: &Store, identity: Option<&str>, label: &str) {
1725 let files = identity
1726 .and_then(|id| store.repository_id(id).ok().flatten())
1727 .and_then(|rid| store.repo_totals(rid).ok())
1728 .map_or(0, |(f, _)| f);
1729 eprint!("\r\x1b[Krq: indexing {label}… {files} files");
1730 let _ = std::io::stderr().flush();
1731}
1732
1733fn clear_progress() {
1735 eprint!("\r\x1b[K");
1736 let _ = std::io::stderr().flush();
1737}
1738
1739fn env_budget(var: &str, default_ms: u64) -> Duration {
1743 let ms = std::env::var(var)
1744 .ok()
1745 .and_then(|v| v.parse().ok())
1746 .unwrap_or(default_ms);
1747 Duration::from_millis(ms)
1748}
1749
1750const AGGREGATE_BATCH: usize = 256;
1753
1754const KEEP_RECENT_EVENTS: i64 = 200;
1757
1758fn deferred_maintenance(store: &mut Store) {
1761 let _ = store.aggregate_events(AGGREGATE_BATCH);
1762 let _ = store.prune_events(KEEP_RECENT_EVENTS);
1763}
1764
1765fn cmd_record(kind: &str, query: Option<&str>, file: &str, line: Option<i64>) -> ExitCode {
1768 let mut store = match open_store() {
1769 Ok(s) => s,
1770 Err(e) => return fail(format_args!("rq: cannot open database: {e}")),
1771 };
1772 let cwd = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."));
1773 let identity = crate::index::detect_identity(&cwd).to_string();
1774 let repo_id = store.repository_id(&identity).ok().flatten();
1775
1776 let rel = match repo_id.and_then(|id| store.checkout_root(id).ok().flatten()) {
1779 Some(root) => repo_relative(std::path::Path::new(&root), &cwd, file),
1780 None => file.to_string(),
1781 };
1782 let query_norm = query.map(|q| q.to_ascii_lowercase());
1783
1784 if let Err(e) = store.record_event(kind, query_norm.as_deref(), repo_id, Some(&rel), line, None)
1785 {
1786 return fail(format_args!("rq record: {e}"));
1787 }
1788 deferred_maintenance(&mut store);
1789 ExitCode::SUCCESS
1790}
1791
1792fn hit_file_roots(
1798 store: &Store,
1799 repo_identity: &str,
1800 cwd: Option<&std::path::Path>,
1801) -> Vec<PathBuf> {
1802 let mut roots: Vec<PathBuf> = store
1803 .repository_id(repo_identity)
1804 .ok()
1805 .flatten()
1806 .map(|id| store.checkout_roots(id).unwrap_or_default())
1807 .unwrap_or_default()
1808 .into_iter()
1809 .map(PathBuf::from)
1810 .collect();
1811 if let Some(c) = cwd {
1812 let c = c.to_path_buf();
1813 if !roots.contains(&c) {
1814 roots.push(c);
1815 }
1816 }
1817 roots
1818}
1819
1820fn read_signature(
1823 store: &Store,
1824 repo_identity: &str,
1825 file: &str,
1826 line: i64,
1827 cwd: Option<&std::path::Path>,
1828) -> Option<String> {
1829 hit_file_roots(store, repo_identity, cwd)
1830 .into_iter()
1831 .find_map(|root| signature_in(&std::fs::read_to_string(root.join(file)).ok()?, line))
1832}
1833
1834const SHOW_CONFIDENCE: f64 = 0.85;
1838
1839fn show_top_definition(
1843 store: &Store,
1844 hits: &mut [crate::search::Hit],
1845 query: &str,
1846 out: Output,
1847 cwd: Option<&std::path::Path>,
1848) -> Option<ExitCode> {
1849 let top = hits.first()?;
1850 if top.confidence < SHOW_CONFIDENCE {
1851 return None; }
1853 let end = top.end_line.unwrap_or(top.line);
1854 let body = read_span(store, &top.repo_identity, &top.file, top.line, end, cwd);
1855 hits[0].body = body;
1856 let top = &hits[0];
1857 match out {
1858 Output::Json | Output::Ndjson => {
1859 return Some(emit_json(out, top));
1861 }
1862 Output::Text => {
1863 let color = match_color();
1864 let c = color.as_deref();
1865 let name = hl(&top.name, query, c);
1866 let qualified = match &top.parent {
1867 Some(p) => format!("{name} · {p}"),
1868 None => name,
1869 };
1870 println!(
1871 "{}:{} {} {}",
1872 hl_path(&top.file, query, c),
1873 top.line,
1874 top.kind,
1875 qualified
1876 );
1877 match (&top.body, &top.signature) {
1878 (Some(body), _) => println!("{body}"),
1879 (None, Some(sig)) => println!("{sig}"),
1881 (None, None) => {}
1882 }
1883 }
1884 }
1885 Some(ExitCode::SUCCESS)
1886}
1887
1888fn read_span(
1891 store: &Store,
1892 repo_identity: &str,
1893 file: &str,
1894 start: i64,
1895 end: i64,
1896 cwd: Option<&std::path::Path>,
1897) -> Option<String> {
1898 hit_file_roots(store, repo_identity, cwd)
1899 .into_iter()
1900 .find_map(|root| span_in(&std::fs::read_to_string(root.join(file)).ok()?, start, end))
1901}
1902
1903fn span_in(content: &str, start: i64, end: i64) -> Option<String> {
1906 let s = usize::try_from(start).ok()?.checked_sub(1)?;
1907 let lines: Vec<&str> = content.lines().collect();
1908 if s >= lines.len() {
1909 return None;
1910 }
1911 let e = usize::try_from(end).ok()?.clamp(s + 1, lines.len());
1912 Some(lines[s..e].join("\n"))
1913}
1914
1915fn signature_in(content: &str, line: i64) -> Option<String> {
1919 let idx = usize::try_from(line).ok()?.checked_sub(1)?;
1920 let l = content.lines().nth(idx)?.trim();
1921 (!l.is_empty()).then(|| l.to_string())
1922}
1923
1924#[derive(serde::Serialize)]
1928struct SymbolOut {
1929 name: String,
1930 kind: String,
1931 language: String,
1932 file: String,
1933 line: i64,
1934 #[serde(skip_serializing_if = "Option::is_none")]
1935 end_line: Option<i64>,
1936 #[serde(skip_serializing_if = "Option::is_none")]
1937 parent: Option<String>,
1938 #[serde(skip_serializing_if = "Option::is_none")]
1939 visibility: Option<String>,
1940 repo: String,
1941 #[serde(skip_serializing_if = "Option::is_none")]
1942 signature: Option<String>,
1943}
1944
1945fn cmd_symbols(file_arg: &str, kinds: &[String], langs: &[String], out: Output) -> ExitCode {
1950 let mut store = match open_store() {
1951 Ok(s) => s,
1952 Err(e) => return fail(format_args!("rq: cannot open database: {e}")),
1953 };
1954 let cwd = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."));
1955 let root = crate::index::repo_root(&cwd).unwrap_or_else(|| cwd.clone());
1956 let rel = repo_relative(&root, &cwd, file_arg);
1957
1958 let identity = resolve_identity(&store, &root);
1959 let coverage = store.coverage_status(&identity).ok().flatten();
1960 let warming_ok = crate::index::is_git_repo(&root) || coverage.is_some();
1961 let current = store.repository_id(&identity).ok().flatten();
1962 let indexed_head = current.and_then(|id| store.indexed_head(id).ok().flatten());
1965 let needs_warm = warming_ok
1966 && (coverage.as_deref() != Some("complete")
1967 || worktree_changed(&root, indexed_head.as_deref()));
1968 if needs_warm {
1969 let budget = answer_warm_budget() + deferred_warm_budget();
1971 let _ = crate::index::index_budgeted(&mut store, &root, &[], budget, Some(&rel));
1972 }
1973
1974 let Some(repo_id) = store.repository_id(&identity).ok().flatten() else {
1975 return emit_symbols(out, &[]); };
1977 let mut rows = match store.symbols_in_file(repo_id, &rel) {
1978 Ok(r) => r,
1979 Err(e) => return fail(format_args!("rq: {e}")),
1980 };
1981 if !kinds.is_empty() {
1982 rows.retain(|r| kinds.iter().any(|k| k == &r.kind));
1983 }
1984 if !langs.is_empty() {
1985 rows.retain(|r| langs.iter().any(|l| l == &r.language));
1986 }
1987
1988 let content = hit_file_roots(&store, &identity, Some(&root))
1992 .iter()
1993 .find_map(|r| std::fs::read_to_string(r.join(&rel)).ok());
1994 let syms: Vec<SymbolOut> = rows
1995 .into_iter()
1996 .map(|r| SymbolOut {
1997 signature: content.as_deref().and_then(|c| signature_in(c, r.line)),
1998 name: r.name,
1999 kind: r.kind,
2000 language: r.language,
2001 file: r.file,
2002 line: r.line,
2003 end_line: r.end_line,
2004 parent: r.parent,
2005 visibility: r.visibility,
2006 repo: r.repo_identity,
2007 })
2008 .collect();
2009 emit_symbols(out, &syms)
2010}
2011
2012fn emit_symbols(out: Output, syms: &[SymbolOut]) -> ExitCode {
2015 if syms.is_empty() {
2016 match out {
2017 Output::Json | Output::Ndjson => {
2018 let obj = serde_json::json!({ "status": "no_match" });
2019 let _ = emit_json(out, &obj); }
2021 Output::Text => eprintln!("no symbols"),
2022 }
2023 return ExitCode::FAILURE;
2024 }
2025 if let Some(code) = emit_rows(out, syms) {
2026 return code;
2027 }
2028 match out {
2029 Output::Json | Output::Ndjson => {}
2030 Output::Text => {
2031 for s in syms {
2032 let qualified = match &s.parent {
2033 Some(p) => format!("{} · {p}", s.name),
2034 None => s.name.clone(),
2035 };
2036 println!("{}:{} {} {}", s.file, s.line, s.kind, qualified);
2037 if let Some(sig) = &s.signature {
2038 println!(" {sig}");
2039 }
2040 }
2041 }
2042 }
2043 ExitCode::SUCCESS
2044}
2045
2046fn keyword_kind(token: &str) -> Option<&'static str> {
2051 match token.to_ascii_lowercase().as_str() {
2052 "class" => Some("class"),
2053 "module" => Some("module"),
2054 "method" => Some("method"),
2055 "function" | "fn" => Some("function"),
2056 "struct" | "type" => Some("struct"),
2057 "enum" => Some("enum"),
2058 "trait" | "interface" => Some("trait"),
2059 _ => None,
2060 }
2061}
2062
2063fn split_kind_keyword(
2069 target: String,
2070 dirs: Vec<String>,
2071) -> (Option<&'static str>, String, Vec<String>) {
2072 if let Some((head, rest)) = target.split_once(char::is_whitespace) {
2075 let rest = rest.trim();
2076 if let Some(k) = keyword_kind(head)
2077 && !rest.is_empty()
2078 {
2079 return (Some(k), rest.to_string(), dirs);
2080 }
2081 } else if let Some(k) = keyword_kind(&target)
2082 && let Some((query, extra)) = dirs.split_first()
2083 {
2084 return (Some(k), query.clone(), extra.to_vec());
2086 }
2087 (None, target, dirs)
2088}
2089
2090fn canonical_kind(s: &str) -> String {
2093 match s.to_ascii_lowercase().as_str() {
2094 "c" | "class" => "class",
2095 "m" | "method" => "method",
2096 "f" | "fn" | "func" | "function" => "function",
2097 "mod" | "module" => "module",
2098 "s" | "struct" | "type" => "struct",
2099 "e" | "enum" => "enum",
2100 "t" | "trait" | "interface" => "trait",
2101 other => return other.to_string(),
2102 }
2103 .to_string()
2104}
2105
2106fn canonical_langs(s: &str) -> Vec<String> {
2113 let t = s.to_ascii_lowercase();
2114 let alias = match t.as_str() {
2115 "rb" => Some("ruby"),
2116 "rs" => Some("rust"),
2117 "golang" => Some("go"),
2118 "ts" | "tsx" => Some("typescript"),
2119 "js" | "jsx" => Some("javascript"),
2120 _ => None,
2121 };
2122 let matched: Vec<String> = crate::lang::languages()
2123 .into_iter()
2124 .filter(|lang| alias == Some(*lang) || lang.starts_with(&t))
2125 .map(str::to_string)
2126 .collect();
2127 if matched.is_empty() { vec![t] } else { matched }
2128}
2129
2130fn match_color() -> Option<String> {
2134 if std::env::var_os("NO_COLOR").is_some() || !std::io::stdout().is_terminal() {
2135 return None;
2136 }
2137 let style = std::env::var("GREP_COLORS").ok().and_then(|gc| {
2138 gc.split(':').find_map(|e| {
2139 e.strip_prefix("mt=")
2140 .or_else(|| e.strip_prefix("ms="))
2141 .filter(|v| !v.is_empty())
2142 .map(str::to_string)
2143 })
2144 });
2145 Some(style.unwrap_or_else(|| "1;31".to_string()))
2146}
2147
2148fn hl(text: &str, query: &str, color: Option<&str>) -> String {
2151 match color {
2152 Some(c) => highlight(text, &crate::search::match_positions(query, text), c),
2153 None => text.to_string(),
2154 }
2155}
2156
2157fn hl_path(path: &str, query: &str, color: Option<&str>) -> String {
2160 let Some(c) = color else {
2161 return path.to_string();
2162 };
2163 let base_byte = path.rfind('/').map(|b| b + 1).unwrap_or(0);
2164 let base_start = path[..base_byte].chars().count();
2165 let stem = crate::search::path_stem(path);
2169 let positions: Vec<usize> = crate::search::match_positions(query, stem)
2170 .into_iter()
2171 .map(|p| p + base_start)
2172 .collect();
2173 highlight(path, &positions, c)
2174}
2175
2176fn highlight(text: &str, positions: &[usize], color: &str) -> String {
2179 if positions.is_empty() {
2180 return text.to_string();
2181 }
2182 let matched: std::collections::HashSet<usize> = positions.iter().copied().collect();
2183 let mut out = String::new();
2184 let mut on = false;
2185 for (i, c) in text.chars().enumerate() {
2186 match (matched.contains(&i), on) {
2187 (true, false) => {
2188 out.push_str("\x1b[");
2189 out.push_str(color);
2190 out.push('m');
2191 on = true;
2192 }
2193 (false, true) => {
2194 out.push_str("\x1b[0m");
2195 on = false;
2196 }
2197 _ => {}
2198 }
2199 out.push(c);
2200 }
2201 if on {
2202 out.push_str("\x1b[0m");
2203 }
2204 out
2205}
2206
2207fn under_any(file: &str, paths: &[String]) -> bool {
2211 paths.iter().any(|p| {
2212 let p = p.trim_start_matches("./").trim_end_matches('/');
2213 p.is_empty() || file == p || file.starts_with(&format!("{p}/"))
2214 })
2215}
2216
2217fn repo_relative(root: &std::path::Path, cwd: &std::path::Path, file: &str) -> String {
2219 let p = std::path::Path::new(file);
2220 let abs = if p.is_absolute() {
2221 p.to_path_buf()
2222 } else {
2223 cwd.join(p)
2224 };
2225 let abs = abs.canonicalize().unwrap_or(abs);
2226 abs.strip_prefix(root)
2227 .map(|r| r.to_string_lossy().into_owned())
2228 .unwrap_or_else(|_| file.to_string())
2229}
2230
2231fn revalidate_top(store: &mut Store, hits: &[crate::search::Hit]) -> bool {
2235 use std::collections::HashSet;
2236 let mut seen = HashSet::new();
2237 let mut changed = false;
2238 for hit in hits {
2239 if !seen.insert((hit.repo_identity.clone(), hit.file.clone())) {
2240 continue;
2241 }
2242 let Some(repo_id) = store.repository_id(&hit.repo_identity).ok().flatten() else {
2243 continue;
2244 };
2245 let Some(root) = store.checkout_root(repo_id).ok().flatten() else {
2246 continue;
2247 };
2248 if let Ok(crate::index::Refresh::Updated) =
2249 crate::index::refresh_file(store, repo_id, std::path::Path::new(&root), &hit.file)
2250 {
2251 changed = true;
2252 }
2253 }
2254 changed
2255}
2256
2257fn resolve_identity(store: &Store, cwd: &std::path::Path) -> String {
2263 if let Ok(canon) = cwd.canonicalize() {
2264 if let Ok(Some(identity)) = store.identity_for_root(&canon.to_string_lossy()) {
2265 return identity;
2266 }
2267 if crate::index::repo_root(cwd).is_none() {
2268 return crate::core::RepoIdentity::local(&canon.to_string_lossy()).to_string();
2269 }
2270 }
2271 crate::index::detect_identity(cwd).to_string()
2272}
2273
2274fn cmd_index(path: Option<PathBuf>, subdirs: &[String], out: Output) -> ExitCode {
2275 let explicit = path.is_some();
2276 let target = path.unwrap_or_else(|| PathBuf::from("."));
2277 let root = crate::index::repo_root(&target).unwrap_or_else(|| target.clone());
2282 let mut subdirs = subdirs.to_vec();
2287 if explicit
2288 && let (Ok(t), Ok(r)) = (target.canonicalize(), root.canonicalize())
2289 && t != r
2290 && let Ok(rel) = t.strip_prefix(&r)
2291 && !rel.as_os_str().is_empty()
2292 {
2293 subdirs.push(rel.to_string_lossy().into_owned());
2294 }
2295 let mut store = match open_store() {
2296 Ok(s) => s,
2297 Err(e) => return fail(format_args!("rq: cannot open database: {e}")),
2298 };
2299 let identity = crate::index::detect_identity(&root).to_string();
2300 match crate::index::index_under(&mut store, &root, &subdirs) {
2301 Ok(stats) => {
2302 let subtree = !subdirs.is_empty();
2303 let totals = store
2305 .repository_id(&identity)
2306 .ok()
2307 .flatten()
2308 .and_then(|id| store.repo_totals(id).ok());
2309 match out {
2310 Output::Json | Output::Ndjson => {
2311 let (files, symbols) = match totals {
2312 Some((f, s)) => (Some(f), Some(s)),
2313 None => (None, None),
2314 };
2315 return emit_json(
2316 out,
2317 &serde_json::json!({
2318 "repo": identity,
2319 "scope": if subtree { "subtree" } else { "full" },
2320 "files_added": stats.files_indexed,
2321 "symbols_added": stats.symbols,
2322 "files": files,
2323 "symbols": symbols,
2324 }),
2325 );
2326 }
2327 Output::Text => {
2328 let scope = if subtree { " (subtree seed)" } else { "" };
2329 match totals {
2330 Some((files, symbols)) => println!(
2331 "{} file(s)/{} symbol(s) added this run; index{scope} now {files} files, {symbols} symbols",
2332 stats.files_indexed, stats.symbols
2333 ),
2334 None => println!(
2335 "{} file(s)/{} symbol(s) added this run{scope}",
2336 stats.files_indexed, stats.symbols
2337 ),
2338 }
2339 }
2340 }
2341 ExitCode::SUCCESS
2342 }
2343 Err(e) => fail(format_args!("rq --index: {e}")),
2344 }
2345}
2346
2347fn cmd_drop(target: Option<String>, out: Output) -> ExitCode {
2348 let mut store = match open_store() {
2349 Ok(s) => s,
2350 Err(e) => return fail(format_args!("rq: cannot open database: {e}")),
2351 };
2352
2353 let path = PathBuf::from(target.clone().unwrap_or_else(|| ".".to_string()));
2357 let root = crate::index::repo_root(&path).unwrap_or(path);
2358 let from_path = crate::index::detect_identity(&root).to_string();
2359 let resolved = match store.repository_id(&from_path) {
2360 Ok(Some(id)) => Some((from_path.clone(), id)),
2361 Ok(None) => target.as_deref().and_then(|s| {
2362 store
2363 .repository_id(s)
2364 .ok()
2365 .flatten()
2366 .map(|id| (s.to_string(), id))
2367 }),
2368 Err(e) => return fail(format_args!("rq --drop: {e}")),
2369 };
2370
2371 let Some((identity, repo_id)) = resolved else {
2372 return match out {
2374 Output::Text => {
2375 println!("not indexed: {from_path}");
2376 ExitCode::SUCCESS
2377 }
2378 _ => emit_json(
2379 out,
2380 &serde_json::json!({"repo": from_path, "files": 0, "symbols": 0, "dropped": false}),
2381 ),
2382 };
2383 };
2384
2385 let (files, symbols) = store.repo_totals(repo_id).unwrap_or((0, 0));
2386 match store.drop_repository(repo_id) {
2387 Ok(()) => match out {
2388 Output::Text => {
2389 println!("dropped {identity} ({files} file(s), {symbols} symbol(s))");
2390 ExitCode::SUCCESS
2391 }
2392 _ => emit_json(
2393 out,
2394 &serde_json::json!({"repo": identity, "files": files, "symbols": symbols, "dropped": true}),
2395 ),
2396 },
2397 Err(e) => fail(format_args!("rq --drop: {e}")),
2398 }
2399}
2400
2401fn emit_json<T: serde::Serialize>(out: Output, value: &T) -> ExitCode {
2405 let rendered = if out == Output::Json {
2406 serde_json::to_string_pretty(value)
2407 } else {
2408 serde_json::to_string(value)
2409 };
2410 match rendered {
2411 Ok(s) => {
2412 println!("{s}");
2413 ExitCode::SUCCESS
2414 }
2415 Err(e) => fail(format_args!("rq: {e}")),
2416 }
2417}
2418
2419fn emit_rows<T: serde::Serialize>(out: Output, rows: &[T]) -> Option<ExitCode> {
2423 match out {
2424 Output::Json => match serde_json::to_string_pretty(rows) {
2425 Ok(s) => println!("{s}"),
2426 Err(e) => return Some(fail(format_args!("rq: {e}"))),
2427 },
2428 Output::Ndjson => {
2429 for r in rows {
2430 match serde_json::to_string(r) {
2431 Ok(line) => println!("{line}"),
2432 Err(e) => return Some(fail(format_args!("rq: {e}"))),
2433 }
2434 }
2435 }
2436 Output::Text => {}
2437 }
2438 None
2439}
2440
2441fn cmd_status(out: Output) -> ExitCode {
2442 let store = match open_store() {
2443 Ok(s) => s,
2444 Err(e) => return fail(format_args!("rq: cannot open database: {e}")),
2445 };
2446 let rows = match store.coverage_overview() {
2447 Ok(rows) => rows,
2448 Err(e) => return fail(format_args!("rq --status: {e}")),
2449 };
2450 if let Some(code) = emit_rows(out, &rows) {
2451 return code;
2452 }
2453 match out {
2454 Output::Json | Output::Ndjson => {}
2455 Output::Text if rows.is_empty() => {
2456 println!("no repositories indexed yet (try `rq --index`)");
2457 }
2458 Output::Text => {
2459 for r in &rows {
2460 println!(
2461 "{:<10} {:>6} files {:>7} symbols {}",
2462 r.status, r.files, r.symbols, r.identity
2463 );
2464 }
2465 }
2466 }
2467 ExitCode::SUCCESS
2468}
2469
2470fn open_store() -> Result<Store, Box<dyn std::error::Error>> {
2472 let path = db_path()?;
2473 if let Some(parent) = path.parent() {
2474 std::fs::create_dir_all(parent)?;
2475 }
2476 Ok(Store::open(&path)?)
2477}
2478
2479fn db_path() -> Result<PathBuf, Box<dyn std::error::Error>> {
2481 if let Ok(p) = std::env::var("RQ_DB") {
2482 return Ok(PathBuf::from(p));
2483 }
2484 let home = std::env::var("HOME")?;
2485 Ok(PathBuf::from(home).join(".local/share/rq/rq.db"))
2486}
2487
2488fn fail(args: std::fmt::Arguments) -> ExitCode {
2489 eprintln!("{args}");
2490 ExitCode::FAILURE
2491}
2492
2493#[cfg(test)]
2494mod tests {
2495 use super::*;
2496
2497 #[test]
2498 fn open_menu_choice_parsing() {
2499 assert_eq!(parse_choice("\n", 5), Some(0));
2501 assert_eq!(parse_choice(" ", 5), Some(0));
2502 assert_eq!(parse_choice("3", 5), Some(2));
2503 assert_eq!(parse_choice("5", 5), Some(4));
2504 assert_eq!(parse_choice("6", 5), None);
2506 assert_eq!(parse_choice("0", 5), None);
2507 assert_eq!(parse_choice("q", 5), None);
2508 }
2509
2510 #[test]
2511 fn wait_duration_parsing() {
2512 use std::time::Duration;
2513 assert_eq!(parse_wait("50ms"), Ok(Duration::from_millis(50)));
2515 assert_eq!(parse_wait("2s"), Ok(Duration::from_secs(2)));
2516 assert_eq!(parse_wait("1m"), Ok(Duration::from_secs(60)));
2517 assert_eq!(parse_wait("250"), Ok(Duration::from_secs(250)));
2518 assert_eq!(parse_wait("1.5s"), Ok(Duration::from_millis(1500)));
2520 assert_eq!(parse_wait("0"), Ok(Duration::ZERO));
2521 assert!(parse_wait("0s").unwrap().is_zero());
2522 assert_eq!(parse_wait(" 2s "), Ok(Duration::from_secs(2)));
2524 assert!(parse_wait("2x").is_err());
2526 assert!(parse_wait("").is_err());
2527 assert!(parse_wait("s").is_err());
2528 assert!(parse_wait("-1s").is_err());
2529 }
2530
2531 #[test]
2532 fn leading_kind_keyword_becomes_a_kind_filter() {
2533 let d = |s: &[&str]| s.iter().map(|x| x.to_string()).collect::<Vec<_>>();
2534 assert_eq!(
2536 split_kind_keyword("class".into(), d(&["Widget"])),
2537 (Some("class"), "Widget".into(), vec![])
2538 );
2539 assert_eq!(
2541 split_kind_keyword("method zoom".into(), vec![]),
2542 (Some("method"), "zoom".into(), vec![])
2543 );
2544 assert_eq!(
2546 split_kind_keyword("fn".into(), d(&["Foo::run"])),
2547 (Some("function"), "Foo::run".into(), vec![])
2548 );
2549 assert_eq!(
2551 split_kind_keyword("struct".into(), d(&["Gadget", "src"])),
2552 (Some("struct"), "Gadget".into(), d(&["src"]))
2553 );
2554 }
2555
2556 #[test]
2557 fn a_bare_or_non_keyword_query_is_left_alone() {
2558 let d = |s: &[&str]| s.iter().map(|x| x.to_string()).collect::<Vec<_>>();
2559 assert_eq!(
2561 split_kind_keyword("class".into(), vec![]),
2562 (None, "class".into(), vec![])
2563 );
2564 assert_eq!(
2566 split_kind_keyword("Widget".into(), d(&["app"])),
2567 (None, "Widget".into(), d(&["app"]))
2568 );
2569 assert_eq!(
2571 split_kind_keyword("c".into(), d(&["Foo"])),
2572 (None, "c".into(), d(&["Foo"]))
2573 );
2574 }
2575
2576 #[test]
2577 fn a_language_selects_by_prefix_or_alias() {
2578 assert_eq!(canonical_langs("r"), ["ruby", "rust"]);
2580 assert_eq!(canonical_langs("t"), ["typescript"]);
2581 assert_eq!(canonical_langs("ts"), ["typescript"]);
2583 assert_eq!(canonical_langs("jsx"), ["javascript"]);
2584 assert_eq!(canonical_langs("rb"), ["ruby"]);
2585 assert_eq!(canonical_langs("COBOL"), ["cobol"]);
2587 }
2588
2589 #[test]
2590 fn a_kind_normalizes_language_specific_spellings() {
2591 assert_eq!(canonical_kind("f"), "function");
2592 assert_eq!(canonical_kind("interface"), "trait");
2594 assert_eq!(canonical_kind("type"), "struct");
2595 let d = |s: &[&str]| s.iter().map(|x| x.to_string()).collect::<Vec<_>>();
2597 assert_eq!(
2598 split_kind_keyword("interface".into(), d(&["Renderer"])),
2599 (Some("trait"), "Renderer".into(), vec![])
2600 );
2601 }
2602
2603 #[test]
2604 fn highlight_wraps_matched_runs() {
2605 assert_eq!(
2606 highlight("FooThing", &[0, 1, 2], "1;31"),
2607 "\u{1b}[1;31mFoo\u{1b}[0mThing"
2608 );
2609 assert_eq!(
2611 highlight("FooThing", &[0, 3], "1"),
2612 "\u{1b}[1mF\u{1b}[0moo\u{1b}[1mT\u{1b}[0mhing"
2613 );
2614 assert_eq!(highlight("FooThing", &[], "1;31"), "FooThing");
2616 }
2617
2618 #[test]
2619 fn progress_ui_only_for_an_interactive_text_terminal() {
2620 assert!(show_progress(Output::Text, true));
2622
2623 assert!(!show_progress(Output::Json, true));
2625 assert!(!show_progress(Output::Ndjson, true));
2626
2627 assert!(!show_progress(Output::Text, false));
2629 }
2630
2631 #[test]
2632 fn repo_label_uses_the_directory_name() {
2633 assert_eq!(
2634 repo_label(Some(std::path::Path::new("/src/widgets"))),
2635 "widgets"
2636 );
2637 assert_eq!(repo_label(None), "repo");
2638 }
2639
2640 #[test]
2641 fn hl_path_highlights_the_stem_not_the_extension() {
2642 let out = hl_path(
2645 "app/employees_controller.rb",
2646 "employeescontroller",
2647 Some("1;31"),
2648 );
2649 assert!(
2650 out.starts_with("app/\u{1b}[1;31memployees"),
2651 "stem highlighted: {out:?}"
2652 );
2653 assert!(
2654 out.ends_with("controller\u{1b}[0m.rb"),
2655 "`.rb` left un-highlighted: {out:?}"
2656 );
2657 }
2658}