2016-06-07 02:19:14 +00:00
|
|
|
|
;;; funcs.el --- Ruby Layer functions File
|
|
|
|
|
;;
|
2020-09-16 21:34:40 +00:00
|
|
|
|
;; Copyright (c) 2012-2020 Sylvain Benner & Contributors
|
2016-06-07 02:19:14 +00:00
|
|
|
|
;;
|
|
|
|
|
;; Author: Sylvain Benner <sylvain.benner@gmail.com>
|
|
|
|
|
;; URL: https://github.com/syl20bnr/spacemacs
|
|
|
|
|
;;
|
|
|
|
|
;; This file is not part of GNU Emacs.
|
|
|
|
|
;;
|
|
|
|
|
;;; License: GPLv3
|
|
|
|
|
|
2019-04-18 18:16:01 +00:00
|
|
|
|
|
|
|
|
|
;; backend
|
|
|
|
|
|
2019-09-30 04:49:44 +00:00
|
|
|
|
(defun spacemacs//ruby-backend ()
|
|
|
|
|
"Returns selected backend."
|
|
|
|
|
(if ruby-backend
|
|
|
|
|
ruby-backend
|
|
|
|
|
(cond
|
|
|
|
|
((configuration-layer/layer-used-p 'lsp) 'lsp)
|
|
|
|
|
(t 'robe))))
|
|
|
|
|
|
2019-04-18 18:16:01 +00:00
|
|
|
|
(defun spacemacs//ruby-setup-backend ()
|
|
|
|
|
"Conditionally configure Ruby backend"
|
|
|
|
|
(spacemacs//ruby-setup-version-manager)
|
2019-09-30 04:49:44 +00:00
|
|
|
|
(pcase (spacemacs//ruby-backend)
|
2019-04-18 18:16:01 +00:00
|
|
|
|
(`lsp (spacemacs//ruby-setup-lsp))
|
|
|
|
|
(`robe (spacemacs//ruby-setup-robe))))
|
|
|
|
|
|
|
|
|
|
(defun spacemacs//ruby-setup-company ()
|
|
|
|
|
"Configure backend company"
|
2019-09-30 04:49:44 +00:00
|
|
|
|
(pcase (spacemacs//ruby-backend)
|
2019-04-18 18:16:01 +00:00
|
|
|
|
(`robe (spacemacs//ruby-setup-robe-company))
|
|
|
|
|
(`lsp nil))) ;; Company is automatically set up by lsp
|
|
|
|
|
|
2019-09-30 01:30:53 +00:00
|
|
|
|
(defun spacemacs//ruby-setup-dap ()
|
|
|
|
|
"Conditionally setup elixir DAP integration."
|
|
|
|
|
;; currently DAP is only available using LSP
|
2019-09-30 04:49:44 +00:00
|
|
|
|
(pcase (spacemacs//ruby-backend)
|
2019-09-30 01:30:53 +00:00
|
|
|
|
(`lsp (spacemacs//ruby-setup-lsp-dap))))
|
|
|
|
|
|
2019-04-18 18:16:01 +00:00
|
|
|
|
|
|
|
|
|
;; lsp
|
|
|
|
|
|
|
|
|
|
(defun spacemacs//ruby-setup-lsp ()
|
|
|
|
|
"Setup Ruby lsp."
|
|
|
|
|
(if (configuration-layer/layer-used-p 'lsp)
|
|
|
|
|
(lsp)
|
2019-09-30 01:30:53 +00:00
|
|
|
|
(message "`lsp' layer is not installed, please add `lsp' layer to your dotfile.")))
|
|
|
|
|
|
|
|
|
|
(defun spacemacs//ruby-setup-lsp-dap ()
|
|
|
|
|
"Setup DAP integration."
|
|
|
|
|
(require 'dap-ruby))
|
2019-04-18 18:16:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; robe
|
|
|
|
|
|
|
|
|
|
(defun spacemacs//ruby-setup-robe ()
|
|
|
|
|
(robe-mode))
|
|
|
|
|
|
|
|
|
|
(defun spacemacs//ruby-setup-robe-company ()
|
|
|
|
|
"Setup robe auto-completion."
|
|
|
|
|
(when (configuration-layer/package-used-p 'robe)
|
|
|
|
|
(spacemacs|add-company-backends
|
|
|
|
|
:backends company-robe
|
|
|
|
|
:modes ruby-mode enh-ruby-mode))
|
|
|
|
|
(with-eval-after-load 'company-dabbrev-code
|
|
|
|
|
(dolist (mode '(ruby-mode enh-ruby-mode))
|
|
|
|
|
(add-to-list 'company-dabbrev-code-modes mode))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; version manager
|
|
|
|
|
|
|
|
|
|
(defun spacemacs//ruby-setup-version-manager ()
|
|
|
|
|
"Setup ruby version manager."
|
|
|
|
|
(pcase ruby-version-manager
|
|
|
|
|
(`rbenv (spacemacs//enable-rbenv))))
|
|
|
|
|
|
2016-06-07 02:24:47 +00:00
|
|
|
|
|
|
|
|
|
;; rbenv
|
2016-06-07 02:52:19 +00:00
|
|
|
|
|
2016-06-07 02:24:47 +00:00
|
|
|
|
(defun spacemacs//enable-rbenv ()
|
|
|
|
|
"Enable rbenv, use .ruby-version if exists."
|
|
|
|
|
(require 'rbenv)
|
|
|
|
|
(let ((version-file-path (rbenv--locate-file ".ruby-version")))
|
|
|
|
|
(global-rbenv-mode)
|
|
|
|
|
;; try to use the ruby defined in .ruby-version
|
|
|
|
|
(if version-file-path
|
|
|
|
|
(progn
|
|
|
|
|
(rbenv-use (rbenv--read-version-from-file
|
|
|
|
|
version-file-path))
|
|
|
|
|
(message (concat "[rbenv] Using ruby version "
|
|
|
|
|
"from .ruby-version file.")))
|
|
|
|
|
(message "[rbenv] Using the currently activated ruby."))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; rspec
|
|
|
|
|
|
2016-06-07 02:52:19 +00:00
|
|
|
|
(defun spacemacs//ruby-enable-rspec-mode ()
|
|
|
|
|
"Conditionally enable `rspec-mode'"
|
|
|
|
|
(when (eq 'rspec ruby-test-runner)
|
|
|
|
|
(rspec-enable-appropriate-mode)))
|
|
|
|
|
|
2016-06-07 02:19:14 +00:00
|
|
|
|
(defun ruby/rspec-verify-directory (dir)
|
|
|
|
|
"Launch tests in DIR directory.
|
|
|
|
|
Called interactively it prompts for a directory."
|
|
|
|
|
(interactive "Drspec directory: ")
|
|
|
|
|
(rspec-run-single-file dir (rspec-core-options)))
|
2016-06-07 02:24:47 +00:00
|
|
|
|
|
2016-06-07 02:28:38 +00:00
|
|
|
|
(defun spacemacs//inf-ruby-auto-enter ()
|
|
|
|
|
"Automatically enters inf-ruby-mode in ruby modes' debugger breakpoints."
|
|
|
|
|
(add-hook 'compilation-filter-hook 'inf-ruby-auto-enter nil t))
|
|
|
|
|
|
2016-06-07 02:24:47 +00:00
|
|
|
|
|
|
|
|
|
;; ruby-test
|
|
|
|
|
|
|
|
|
|
(defun spacemacs//ruby-enable-ruby-test-mode ()
|
|
|
|
|
"Conditionally enable `ruby-test-mode'"
|
|
|
|
|
(when (eq 'ruby-test ruby-test-runner)
|
|
|
|
|
(ruby-test-mode)))
|
2016-09-07 21:33:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; minitest
|
|
|
|
|
|
|
|
|
|
(defun spacemacs//ruby-enable-minitest-mode ()
|
|
|
|
|
"Conditionally enable `minitest-mode'"
|
|
|
|
|
(when (eq 'minitest ruby-test-runner)
|
|
|
|
|
(minitest-enable-appropriate-mode)))
|
2017-01-04 23:14:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; highlight debugger keywords
|
2017-05-24 07:43:06 +00:00
|
|
|
|
|
|
|
|
|
(defun spacemacs/ruby-maybe-highlight-debugger-keywords ()
|
2017-01-04 23:14:10 +00:00
|
|
|
|
"Highlight break point lines."
|
|
|
|
|
(interactive)
|
2017-05-24 07:43:06 +00:00
|
|
|
|
(when ruby-highlight-debugger-keywords
|
|
|
|
|
(highlight-lines-matching-regexp "byebug")
|
|
|
|
|
(highlight-lines-matching-regexp "binding.irb")
|
|
|
|
|
(highlight-lines-matching-regexp "binding.pry")))
|
2020-07-14 17:32:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; Insert text
|
|
|
|
|
|
|
|
|
|
(defun spacemacs/ruby-insert-frozen-string-literal-comment ()
|
|
|
|
|
(interactive)
|
|
|
|
|
(save-excursion
|
|
|
|
|
(goto-char (point-min))
|
|
|
|
|
(insert "# frozen_string_literal: true\n")))
|
|
|
|
|
|
|
|
|
|
(defun spacemacs/ruby-insert-shebang ()
|
|
|
|
|
(interactive)
|
|
|
|
|
(save-excursion
|
|
|
|
|
(goto-char (point-min))
|
|
|
|
|
(insert "#!/usr/bin/env ruby\n")))
|
2021-01-09 07:34:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; Prettier
|
|
|
|
|
|
|
|
|
|
(defun spacemacs/ruby-format ()
|
|
|
|
|
(interactive)
|
|
|
|
|
(call-interactively 'prettier-js))
|
|
|
|
|
|
|
|
|
|
(defun spacemacs/ruby-fmt-before-save-hook ()
|
|
|
|
|
(add-hook 'before-save-hook 'spacemacs/ruby-format t t))
|