about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorDavid Morgan <djm_uk@protonmail.com>2021-09-06 21:14:32 +0100
committerDavid Morgan <djm_uk@protonmail.com>2021-09-06 21:14:32 +0100
commit12eeddb90a702da752c9dc2bbd9ed9dcbf916960 (patch)
treef311b7ea77ddae2531d08caa990d4827ec63f061
parent72734a940344c8e1bb7c027c39aaf2484533c6a6 (diff)
downloaddotfiles-12eeddb90a702da752c9dc2bbd9ed9dcbf916960.tar.gz
Add function to switch back and forth between clojure src and test files
-rw-r--r--.emacs.d/lisp/init-clojure.el4
-rw-r--r--.emacs.d/lisp/init-project.el20
2 files changed, 23 insertions, 1 deletions
diff --git a/.emacs.d/lisp/init-clojure.el b/.emacs.d/lisp/init-clojure.el
index 719f2ca..56c3a02 100644
--- a/.emacs.d/lisp/init-clojure.el
+++ b/.emacs.d/lisp/init-clojure.el
@@ -44,7 +44,9 @@
         nrepl-log-messages t
         clojure-toplevel-inside-comment-form t)
   (unbind-key "C-c C-l" cider-mode-map)
-  :bind (:map cider-mode-map ("C-c M-l" . cider-load-file))
+  :bind
+  (:map cider-mode-map ("C-c M-l" . cider-load-file))
+  (:map clojure-mode-map ("C-x p q" . project-clojure-test-switch))
   :hook
   (cider-repl-mode . (lambda ()
                        (display-line-numbers-mode -1)
diff --git a/.emacs.d/lisp/init-project.el b/.emacs.d/lisp/init-project.el
index e74adf7..62065f0 100644
--- a/.emacs.d/lisp/init-project.el
+++ b/.emacs.d/lisp/init-project.el
@@ -2,9 +2,29 @@
 ;;; Commentary:
 ;;; Code:
 
+(require 'subr-x)
+
 (use-package project
   :ensure nil
   :config
+  (defun project--clojure-switch-to-test (filename project-root)
+    (let* ((project-src-file (string-remove-prefix project-root filename))
+           (project-test-file (replace-regexp-in-string "\.clj$" "_test.clj"
+                                                (replace-regexp-in-string "^src/" "test/" project-src-file))))
+      (find-file (expand-file-name project-test-file project-root))))
+  (defun project--clojure-switch-to-src (test-filename project-root)
+    (let* ((project-test-file (string-remove-prefix project-root test-filename))
+           (project-src-file (replace-regexp-in-string "_test\.clj$" ".clj"
+                                                        (replace-regexp-in-string "^test/" "src/" project-test-file))))
+      (find-file (expand-file-name project-src-file project-root))))
+  (defun project-clojure-test-switch ()
+    (interactive)
+    (let ((filename (buffer-file-name))
+          (project-root (consult--project-root))) ;; TODO don't depend on consult
+      (cond ((string-match (concat "^" project-root "test/.*_test\.clj") filename)
+             (project--clojure-switch-to-src filename project-root))
+            ((string-match (concat "^" project-root "src/.*\.clj") filename)
+             (project--clojure-switch-to-test filename project-root)))))
   (defun project-recentf ()
     "Show a list of recently visited files in a project."
     (interactive)