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
loc="${choice%% *}" file="${loc%:*}" line="${loc##*:}"
rq --record --file "$file" --line "$line" "$query" || true
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