reference-query 0.55.0

Reference Query — find the code you're looking for.
Documentation
#!/usr/bin/env bash
#
# rq-open: search with rq and open the chosen result in your editor.
#
#   rq-open <query>
#
# Picking: uses fzf if available (auto-selects a lone match); otherwise opens
# the top-ranked result. Opening: VS Code (`code --goto`) if present, else
# $EDITOR, else just prints the location.
#
# `rq -o/--open` does this natively (top match → open). This wrapper stays as
# the reference for an interactive fzf picker or a custom flow — it's just `rq`,
# so any editor or launcher can do the same. See docs/EDITORS.md.

set -euo pipefail

query="$*"
if [ -z "$query" ]; then
  echo "usage: rq-open <query>" >&2
  exit 1
fi

results="$(rq "$query" || true)"
if [ -z "$results" ]; then
  echo "no matches for: $query" >&2
  exit 1
fi

if command -v fzf >/dev/null 2>&1; then
  choice="$(printf '%s\n' "$results" | fzf --select-1 --exit-0 --prompt='rq> ')" || exit 0
else
  choice="$(printf '%s\n' "$results" | head -1)"
fi
[ -z "$choice" ] && exit 0

# A result line looks like:  path/to/file.rb:42  kind Name · Parent
loc="${choice%% *}"          # first token: path:line
file="${loc%:*}"             # repo-root-relative path (matches the index)
line="${loc##*:}"

# Open at the line. rq prints repo-root-relative paths, so resolve against the
# work-tree root — the bare path won't open from a subdirectory otherwise.
root="$(git rev-parse --show-toplevel 2>/dev/null || echo .)"
target="$root/$file"

if command -v code >/dev/null 2>&1; then
  exec code --goto "$target:$line"
elif [ -n "${EDITOR:-}" ]; then
  case "$EDITOR" in
    *vim*|*nvim*) exec "$EDITOR" "+$line" "$target" ;;
    *)            exec "$EDITOR" "$target" ;;
  esac
else
  echo "$target:$line"
fi