From 2d35ed3405d08277d3d62db4109edc2ff2ee2076 Mon Sep 17 00:00:00 2001 From: Jeremy Dormitzer Date: Mon, 29 Apr 2024 10:11:10 -0400 Subject: [PATCH] Add function to jump between Ruby source and spec file --- emacs/.emacs.d/config/init-ruby.el | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/emacs/.emacs.d/config/init-ruby.el b/emacs/.emacs.d/config/init-ruby.el index bdd0a4b..06486af 100644 --- a/emacs/.emacs.d/config/init-ruby.el +++ b/emacs/.emacs.d/config/init-ruby.el @@ -12,7 +12,32 @@ ("Guardfile\\'" . ruby-mode) ("Capfile\\'" . ruby-mode) ("Vagrantfile\\'" . ruby-mode)) - :hook (ruby-mode . highlight-indent-guides-mode)) + :hook (ruby-mode . highlight-indent-guides-mode) + :config + (defun toggle-spec-file () + "Find the spec file for the current non-spec buffer or the file under test for the current spec buffer." + (interactive) + (let* ((dir (if-let ((proj (project-current))) + (project-root proj) + default-directory)) + (ext (file-name-extension (buffer-file-name))) + (filename-base (file-name-base (buffer-file-name))) + (spec? (s-ends-with? "_spec" filename-base)) + (rx (if spec? + (format "%s.%s$" (s-chop-suffix "_spec" filename-base) ext) + (format "%s_spec.%s$" filename-base ext))) + (candidates (directory-files-recursively dir rx))) + (cond ((= (length candidates) 0) + (message "No matching file found")) + ((= (length candidates) 1) + (find-file (car candidates))) + (t + (let ((choice (consult--read candidates + :prompt "Find file: " + :category 'file))) + (find-file choice)))))) + :general + (ruby-mode-map "C-c C-t" #'toggle-spec-file)) (use-package inf-ruby :commands (inf-ruby inf-ruby-console-auto)