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

72 lines
2.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))
(use-package inf-ruby
:commands (inf-ruby inf-ruby-console-auto)
:general
([remap inf-ruby] #'inf-ruby-console-auto)
:hook (after-init . inf-ruby-switch-setup))
(use-package rbenv
:hook (after-init . global-rbenv-mode)
:custom
(rbenv-show-active-ruby-in-modeline nil))
(use-package rspec-mode
:hook (dired-mode . rspec-dired-mode)
:config
(defun rspec-compile-command-filter-return (output)
(if current-prefix-arg
(read-string "Command: " output)
output))
(advice-add 'rspec-compile-command :filter-return #'rspec-compile-command-filter-return)
(defun rspec--run-verify-changed (&optional related)
(let* ((repo (forge-get-repository :tracked))
(branch (or (when repo
(oref repo default-branch))
"master"))
(remote (or (when repo
(oref repo remote))
"origin"))
(target-branch (format "%s/%s" remote branch))
(files (->> (magit-git-lines "diff" "--name-only" (format "%s..." target-branch))
(seq-filter (lambda (file)
(string-match-p "\\.rb\\'" file)))
(mapcar (lambda (file) (expand-file-name file (magit-toplevel))))))
(specs (->> (if related
(seq-mapcat #'rspec-all-related-spec-files files)
(mapcar #'rspec-spec-file-for files))
(mapcar (lambda (file) (expand-file-name file (magit-toplevel))))
(seq-filter #'file-exists-p))))
(rspec-run-multiple-files specs (rspec-core-options))))
(defun rspec-verify-changed ()
"Runs RSpec on the specs for the Ruby files changed between the current branch and TARGET-BRANCH."
(interactive)
(rspec--run-verify-changed))
(defun rspec-verify-changed-related ()
"Runs RSpec on all related specs for the Ruby files changed between the current branch and TARGET-BRANCH."
(interactive)
(rspec--run-verify-changed t))
:general
(rspec-mode-map "C-c , g" #'rspec-verify-changed
"C-c , G" #'rspec-verify-changed-related)
(rspec-verifiable-mode-map "C-c , g" #'rspec-verify-changed
"C-c , G" #'rspec-verify-changed-related)
:custom
(rspec-use-opts-file-when-available nil))
(provide 'init-ruby)