Created
October 31, 2023 08:21
-
-
Save codeasone/eb7824489038e8e5e4f237709fe4f781 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Assumptions:
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/
folderclojure-lsp
andlsp-mode
The default key-bindings and workflow for jumping to a definition and back again are
M-.
to jump to something, andM-,
to return from it to where you started.The gist assumes you've bound
M-.
tolsp-find-definition
. If you haven't done this and use another function, then you'll want to adapt theadvice-add :around
accordingly.