Extract the common part of ruby-flymake-simple and ruby-flymake-rubocop

* lisp/progmodes/ruby-mode.el (ruby-flymake-simple)
(ruby-flymake-rubocop):
Extract the common part as ruby-flymake--helper.
(ruby--rubocop-flymake-proc): Remove.  Use the first proc
variable instead.
This commit is contained in:
Dmitry Gutov 2017-11-22 01:44:52 +02:00
parent 09944d499a
commit 2d203ffb7e

View file

@ -2261,24 +2261,10 @@ See `font-lock-syntax-table'.")
(unless (executable-find "ruby") (unless (executable-find "ruby")
(error "Cannot find the ruby executable")) (error "Cannot find the ruby executable"))
(when (process-live-p ruby--flymake-proc) (ruby-flymake--helper
(kill-process ruby--flymake-proc)) "ruby-flymake"
'("ruby" "-w" "-c")
(let ((source (current-buffer))) (lambda (_proc source)
(save-restriction
(widen)
(setq
ruby--flymake-proc
(make-process
:name "ruby-flymake-simple" :noquery t :connection-type 'pipe
:buffer (generate-new-buffer " *ruby-flymake*")
:command '("ruby" "-w" "-c")
:sentinel
(lambda (proc _event)
(when (eq 'exit (process-status proc))
(unwind-protect
(if (with-current-buffer source (eq proc ruby--flymake-proc))
(with-current-buffer (process-buffer proc)
(goto-char (point-min)) (goto-char (point-min))
(cl-loop (cl-loop
while (search-forward-regexp while (search-forward-regexp
@ -2297,7 +2283,28 @@ See `font-lock-syntax-table'.")
type type
msg) msg)
into diags into diags
finally (funcall report-fn diags))) finally (funcall report-fn diags)))))
(defun ruby-flymake--helper (process-name command parser-fn)
(when (process-live-p ruby--flymake-proc)
(kill-process ruby--flymake-proc))
(let ((source (current-buffer)))
(save-restriction
(widen)
(setq
ruby--flymake-proc
(make-process
:name process-name :noquery t :connection-type 'pipe
:buffer (generate-new-buffer (format " *%s*" process-name))
:command command
:sentinel
(lambda (proc _event)
(when (eq 'exit (process-status proc))
(unwind-protect
(if (with-current-buffer source (eq proc ruby--flymake-proc))
(with-current-buffer (process-buffer proc)
(funcall parser-fn proc source))
(flymake-log :debug "Canceling obsolete check %s" (flymake-log :debug "Canceling obsolete check %s"
proc)) proc))
(kill-buffer (process-buffer proc))))))) (kill-buffer (process-buffer proc)))))))
@ -2311,8 +2318,6 @@ Only takes effect if Rubocop is installed."
:group 'ruby :group 'ruby
:safe 'booleanp) :safe 'booleanp)
(defvar-local ruby--rubocop-flymake-proc nil)
(defcustom ruby-rubocop-config ".rubocop.yml" (defcustom ruby-rubocop-config ".rubocop.yml"
"Configuration file for `ruby-flymake-rubocop'." "Configuration file for `ruby-flymake-rubocop'."
:type 'string :type 'string
@ -2324,11 +2329,7 @@ Only takes effect if Rubocop is installed."
(unless (executable-find "rubocop") (unless (executable-find "rubocop")
(error "Cannot find the rubocop executable")) (error "Cannot find the rubocop executable"))
(when (process-live-p ruby--rubocop-flymake-proc) (let ((command (list "rubocop" "--stdin" buffer-file-name "--format" "emacs"
(kill-process ruby--rubocop-flymake-proc))
(let ((source (current-buffer))
(command (list "rubocop" "--stdin" buffer-file-name "--format" "emacs"
"--cache" "false" ; Work around a bug in old version. "--cache" "false" ; Work around a bug in old version.
"--display-cop-names")) "--display-cop-names"))
config-dir) config-dir)
@ -2339,20 +2340,11 @@ Only takes effect if Rubocop is installed."
(setq command (append command (list "--config" (setq command (append command (list "--config"
(expand-file-name ruby-rubocop-config (expand-file-name ruby-rubocop-config
config-dir))))) config-dir)))))
(save-restriction
(widen) (ruby-flymake--helper
(setq "rubocop-flymake"
ruby--rubocop-flymake-proc command
(make-process (lambda (proc source)
:name "rubocop-flymake" :noquery t :connection-type 'pipe
:buffer (generate-new-buffer " *rubocop-flymake*")
:command command
:sentinel
(lambda (proc _event)
(when (eq 'exit (process-status proc))
(unwind-protect
(if (with-current-buffer source (eq proc ruby--rubocop-flymake-proc))
(with-current-buffer (process-buffer proc)
;; Finding the executable is no guarantee of ;; Finding the executable is no guarantee of
;; rubocop working, especially in the presence ;; rubocop working, especially in the presence
;; of rbenv shims (which cross ruby versions). ;; of rbenv shims (which cross ruby versions).
@ -2362,7 +2354,7 @@ Only takes effect if Rubocop is installed."
(goto-char (point-min)) (goto-char (point-min))
(cl-loop (cl-loop
while (search-forward-regexp while (search-forward-regexp
"^\\(?:.*.rb\\|-\\):\\([0-9]+\\):\\([0-9]<<+\\): \\(.*\\)$" "^\\(?:.*.rb\\|-\\):\\([0-9]+\\):\\([0-9]+\\): \\(.*\\)$"
nil t) nil t)
for msg = (match-string 3) for msg = (match-string 3)
for (beg . end) = (flymake-diag-region for (beg . end) = (flymake-diag-region
@ -2381,12 +2373,7 @@ Only takes effect if Rubocop is installed."
type type
(substring msg 3)) (substring msg 3))
into diags into diags
finally (funcall report-fn diags))) finally (funcall report-fn diags)))))))
(flymake-log :debug "Canceling obsolete check %s"
proc))
(kill-buffer (process-buffer proc)))))))
(process-send-region ruby--rubocop-flymake-proc (point-min) (point-max))
(process-send-eof ruby--rubocop-flymake-proc)))))
(defun ruby-flymake-auto (report-fn &rest args) (defun ruby-flymake-auto (report-fn &rest args)
(apply (apply