Add function to jump between Ruby source and spec file

This commit is contained in:
Jeremy Dormitzer 2024-04-29 10:11:10 -04:00
parent b8a895808d
commit 2d35ed3405

View File

@ -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)