From 12eeddb90a702da752c9dc2bbd9ed9dcbf916960 Mon Sep 17 00:00:00 2001 From: David Morgan Date: Mon, 6 Sep 2021 21:14:32 +0100 Subject: [PATCH] Add function to switch back and forth between clojure src and test files --- .emacs.d/lisp/init-clojure.el | 4 +++- .emacs.d/lisp/init-project.el | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) 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)