Skip to content

Commit

Permalink
Add rubocop and standardrb formatters for Ruby
Browse files Browse the repository at this point in the history
  • Loading branch information
knu committed Nov 22, 2020
1 parent 7ab93d3 commit a6f1914
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Supported languages
* **Python** ([*black*](https://github.com/ambv/black), [*yapf*](https://github.com/google/yapf))
* **R** ([*styler*](https://github.com/r-lib/styler))
* **Reason** ([*bsrefmt*](https://github.com/glennsl/bs-refmt))
* **Ruby** ([*rufo*](https://github.com/ruby-formatter/rufo))
* **Ruby** ([*rubocop*](https://github.com/rubocop-hq/rubocop), [*rufo*](https://github.com/ruby-formatter/rufo), [*standardrb*](https://github.com/testdouble/standard))
* **Rust** ([*rustfmt*](https://github.com/rust-lang-nursery/rustfmt))
* **Scala** ([*scalafmt*](https://github.com/scalameta/scalafmt))
* **Shell script** ([*beautysh*](https://github.com/lovesegfault/beautysh), [*shfmt*](https://github.com/mvdan/sh))
Expand Down
77 changes: 74 additions & 3 deletions format-all.el
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
;; - Python (black, yapf)
;; - R (styler)
;; - Reason (bsrefmt)
;; - Ruby (rufo)
;; - Ruby (rubocop, rufo, standardrb)
;; - Rust (rustfmt)
;; - Scala (scalafmt)
;; - Shell script (beautysh, shfmt)
Expand Down Expand Up @@ -347,6 +347,53 @@ If ARGS are given, those are arguments to EXECUTABLE. They don't
need to be shell-quoted."
(apply 'format-all--buffer-hard nil nil nil executable args))

(defun format-all--ruby-gem-bundled-p (gem-name)
"Internal helper function to check if GEM-NAME is listed in the current project's Gemfile.lock."
(let* ((lockfile "Gemfile.lock")
(dir (locate-dominating-file (buffer-file-name) lockfile)))
(and dir
(not
(string=
""
(shell-command-to-string
(format "grep -e %s -- %s 2>/dev/null"
(shell-quote-argument
(format "^ %s " gem-name))
(shell-quote-argument
(expand-file-name lockfile dir)))))))))

(defun format-all--buffer-hard-ruby
(gem-name ok-statuses error-regexp root-files executable &rest args)
"Internal helper function to implement ruby based formatters.
GEM-NAME is the name of a Ruby gem required to run EXECUTABLE.
For OK-STATUSES, ERROR-REGEXP, ROOT-FILES, EXECUTABLE and ARGS, see `format-all--buffer-hard'."
(let* ((error-regexp
(apply #'regexp-or
"Bundler::GemNotFound"
(concat (regexp-or "bundle" (regexp-quote executable))
": command not found")
(if error-regexp (list error-regexp))))
(command-args
(append (if (format-all--ruby-gem-bundled-p gem-name)
'("bundle" "exec"))
(cons executable args)))
(command-args
;; This tweak will eventually be removed after rubocop with
;; --stderr has been released and a sufficient amount of
;; time has passed.
(if (member executable '("rubocop" "standardrb"))
(list
"bash" "-eo" "pipefail" "-c"
(format "%s | ruby -ne 'print unless 1../^={20}$/'"
(mapconcat #'shell-quote-argument command-args " ")))
command-args)))
(format-all--buffer-hard
ok-statuses error-regexp root-files
(car command-args)
(cdr command-args))))

(defvar format-all--executable-table (make-hash-table)
"Internal table of formatter executable names for format-all.")

Expand Down Expand Up @@ -693,12 +740,36 @@ Consult the existing formatters for examples of BODY."
(:install "gem install rufo")
(:languages "Ruby")
(:format
(format-all--buffer-easy
executable
(format-all--buffer-hard-ruby
"rufo" nil nil nil
"rufo"
"--simple-exit"
(when (buffer-file-name)
(list "--filename" (buffer-file-name))))))

(define-format-all-formatter rubocop
(:executable "rubocop")
(:install "gem install rubocop")
(:languages "Ruby")
(:format
(format-all--buffer-hard-ruby
"rubocop" '(0 1) nil nil
"rubocop"
"--auto-format"
"--format" "quiet"
"--stdin" (or (buffer-file-name) (buffer-name)))))

(define-format-all-formatter standardrb
(:executable "standardrb")
(:install "gem install standard")
(:languages "Ruby")
(:format
(format-all--buffer-hard-ruby
"standard" '(0 1) nil nil
"standardrb"
"--auto-format"
"--stdin" (or (buffer-file-name) (buffer-name)))))

(define-format-all-formatter rustfmt
(:executable "rustfmt")
(:install "rustup component add rustfmt")
Expand Down

0 comments on commit a6f1914

Please sign in to comment.