dotfiles/emacs/.emacs.d/config/init-ruby.el

53 lines
1.9 KiB
EmacsLisp

;; -*- lexical-binding: t; -*-
(use-package ruby-mode
:straight (:type built-in)
:mode (("\\.rb\\'" . ruby-mode)
("\\.rake\\'" . ruby-mode)
("\\.gemspec\\'" . ruby-mode)
("\\.ru\\'" . ruby-mode)
("\\.arb\\'" . ruby-mode)
("\\.rbi\\'" . ruby-mode)
("Rakefile\\'" . ruby-mode)
("Gemfile\\'" . ruby-mode)
("Guardfile\\'" . ruby-mode)
("Capfile\\'" . ruby-mode)
("Vagrantfile\\'" . ruby-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)
:general
([remap inf-ruby] #'inf-ruby-console-auto))
(use-package rbenv
:hook (after-init . global-rbenv-mode)
:custom
(rbenv-show-active-ruby-in-modeline nil))
(provide 'init-ruby)