Skip to content

Instantly share code, notes, and snippets.

@codeasone
Created October 31, 2023 08:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codeasone/eb7824489038e8e5e4f237709fe4f781 to your computer and use it in GitHub Desktop.
Save codeasone/eb7824489038e8e5e4f237709fe4f781 to your computer and use it in GitHub Desktop.
(defun jump-to-hugsql ()
"Go to the SQL behind a HugSQL sqlvec var"
(interactive)
;; Remember where we are so we can get back with M-,
(xref-push-marker-stack)
(let* ((symbol-at-point-str (symbol-name (symbol-at-point)))
(sql-func-name (replace-regexp-in-string "-sqlvec$" "" symbol-at-point-str))
(current-file-without-src (substring (buffer-file-name) (length (concat (projectile-project-root) "src/"))))
(sql-file-name (replace-regexp-in-string "\\.clj$" ".sql" current-file-without-src))
(sql-file-path (concat (projectile-project-root) "resources/sql/" sql-file-name)))
(find-file sql-file-path)
(goto-char (point-min))
(search-forward (format "-- :name %s" sql-func-name) nil t)))
(defun handle-sqlvec (orig-fun &rest args)
"Handle any symbol ending -sqlvec as a HugSQL target"
(let ((symbol-at-point-str (symbol-name (symbol-at-point))))
(if (string-suffix-p "-sqlvec" symbol-at-point-str)
(jump-to-hugsql)
(apply orig-fun args))))
;; Most people are using clojure-lsp and M-. to lsp-find-definition
(advice-add 'lsp-find-definition :around #'handle-sqlvec)
@codeasone
Copy link
Author

codeasone commented Oct 31, 2023

Assumptions:

  • You are using projectile, if not then you'll need to replace (projectile-project-root) with a function that returns the path to the root of your project i.e. the home of the .git/ folder
  • You are using clojure-lsp and lsp-mode

The default key-bindings and workflow for jumping to a definition and back again are M-. to jump to something, and M-, to return from it to where you started.

The gist assumes you've bound M-. to lsp-find-definition. If you haven't done this and use another function, then you'll want to adapt the advice-add :around accordingly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment