diff options
| author | Dmitry Gutov | 2017-11-22 01:44:52 +0200 |
|---|---|---|
| committer | Dmitry Gutov | 2017-11-22 01:56:13 +0200 |
| commit | 2d203ffb7eb1e9541e80f4dc9d91150e9d12be52 (patch) | |
| tree | fc938773bfdd2b53833255e0e8aa454a88bc9da9 | |
| parent | 09944d499a361044d81eb6afaa29ffda885b2076 (diff) | |
| download | emacs-2d203ffb7eb1e9541e80f4dc9d91150e9d12be52.tar.gz emacs-2d203ffb7eb1e9541e80f4dc9d91150e9d12be52.zip | |
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.
| -rw-r--r-- | lisp/progmodes/ruby-mode.el | 141 |
1 files changed, 64 insertions, 77 deletions
diff --git a/lisp/progmodes/ruby-mode.el b/lisp/progmodes/ruby-mode.el index 18ed6ee5e79..1dce49e80c3 100644 --- a/lisp/progmodes/ruby-mode.el +++ b/lisp/progmodes/ruby-mode.el | |||
| @@ -2261,6 +2261,31 @@ See `font-lock-syntax-table'.") | |||
| 2261 | (unless (executable-find "ruby") | 2261 | (unless (executable-find "ruby") |
| 2262 | (error "Cannot find the ruby executable")) | 2262 | (error "Cannot find the ruby executable")) |
| 2263 | 2263 | ||
| 2264 | (ruby-flymake--helper | ||
| 2265 | "ruby-flymake" | ||
| 2266 | '("ruby" "-w" "-c") | ||
| 2267 | (lambda (_proc source) | ||
| 2268 | (goto-char (point-min)) | ||
| 2269 | (cl-loop | ||
| 2270 | while (search-forward-regexp | ||
| 2271 | "^\\(?:.*.rb\\|-\\):\\([0-9]+\\): \\(.*\\)$" | ||
| 2272 | nil t) | ||
| 2273 | for msg = (match-string 2) | ||
| 2274 | for (beg . end) = (flymake-diag-region | ||
| 2275 | source | ||
| 2276 | (string-to-number (match-string 1))) | ||
| 2277 | for type = (if (string-match "^warning" msg) | ||
| 2278 | :warning | ||
| 2279 | :error) | ||
| 2280 | collect (flymake-make-diagnostic source | ||
| 2281 | beg | ||
| 2282 | end | ||
| 2283 | type | ||
| 2284 | msg) | ||
| 2285 | into diags | ||
| 2286 | finally (funcall report-fn diags))))) | ||
| 2287 | |||
| 2288 | (defun ruby-flymake--helper (process-name command parser-fn) | ||
| 2264 | (when (process-live-p ruby--flymake-proc) | 2289 | (when (process-live-p ruby--flymake-proc) |
| 2265 | (kill-process ruby--flymake-proc)) | 2290 | (kill-process ruby--flymake-proc)) |
| 2266 | 2291 | ||
| @@ -2270,34 +2295,16 @@ See `font-lock-syntax-table'.") | |||
| 2270 | (setq | 2295 | (setq |
| 2271 | ruby--flymake-proc | 2296 | ruby--flymake-proc |
| 2272 | (make-process | 2297 | (make-process |
| 2273 | :name "ruby-flymake-simple" :noquery t :connection-type 'pipe | 2298 | :name process-name :noquery t :connection-type 'pipe |
| 2274 | :buffer (generate-new-buffer " *ruby-flymake*") | 2299 | :buffer (generate-new-buffer (format " *%s*" process-name)) |
| 2275 | :command '("ruby" "-w" "-c") | 2300 | :command command |
| 2276 | :sentinel | 2301 | :sentinel |
| 2277 | (lambda (proc _event) | 2302 | (lambda (proc _event) |
| 2278 | (when (eq 'exit (process-status proc)) | 2303 | (when (eq 'exit (process-status proc)) |
| 2279 | (unwind-protect | 2304 | (unwind-protect |
| 2280 | (if (with-current-buffer source (eq proc ruby--flymake-proc)) | 2305 | (if (with-current-buffer source (eq proc ruby--flymake-proc)) |
| 2281 | (with-current-buffer (process-buffer proc) | 2306 | (with-current-buffer (process-buffer proc) |
| 2282 | (goto-char (point-min)) | 2307 | (funcall parser-fn proc source)) |
| 2283 | (cl-loop | ||
| 2284 | while (search-forward-regexp | ||
| 2285 | "^\\(?:.*.rb\\|-\\):\\([0-9]+\\): \\(.*\\)$" | ||
| 2286 | nil t) | ||
| 2287 | for msg = (match-string 2) | ||
| 2288 | for (beg . end) = (flymake-diag-region | ||
| 2289 | source | ||
| 2290 | (string-to-number (match-string 1))) | ||
| 2291 | for type = (if (string-match "^warning" msg) | ||
| 2292 | :warning | ||
| 2293 | :error) | ||
| 2294 | collect (flymake-make-diagnostic source | ||
| 2295 | beg | ||
| 2296 | end | ||
| 2297 | type | ||
| 2298 | msg) | ||
| 2299 | into diags | ||
| 2300 | finally (funcall report-fn diags))) | ||
| 2301 | (flymake-log :debug "Canceling obsolete check %s" | 2308 | (flymake-log :debug "Canceling obsolete check %s" |
| 2302 | proc)) | 2309 | proc)) |
| 2303 | (kill-buffer (process-buffer proc))))))) | 2310 | (kill-buffer (process-buffer proc))))))) |
| @@ -2311,8 +2318,6 @@ Only takes effect if Rubocop is installed." | |||
| 2311 | :group 'ruby | 2318 | :group 'ruby |
| 2312 | :safe 'booleanp) | 2319 | :safe 'booleanp) |
| 2313 | 2320 | ||
| 2314 | (defvar-local ruby--rubocop-flymake-proc nil) | ||
| 2315 | |||
| 2316 | (defcustom ruby-rubocop-config ".rubocop.yml" | 2321 | (defcustom ruby-rubocop-config ".rubocop.yml" |
| 2317 | "Configuration file for `ruby-flymake-rubocop'." | 2322 | "Configuration file for `ruby-flymake-rubocop'." |
| 2318 | :type 'string | 2323 | :type 'string |
| @@ -2324,11 +2329,7 @@ Only takes effect if Rubocop is installed." | |||
| 2324 | (unless (executable-find "rubocop") | 2329 | (unless (executable-find "rubocop") |
| 2325 | (error "Cannot find the rubocop executable")) | 2330 | (error "Cannot find the rubocop executable")) |
| 2326 | 2331 | ||
| 2327 | (when (process-live-p ruby--rubocop-flymake-proc) | 2332 | (let ((command (list "rubocop" "--stdin" buffer-file-name "--format" "emacs" |
| 2328 | (kill-process ruby--rubocop-flymake-proc)) | ||
| 2329 | |||
| 2330 | (let ((source (current-buffer)) | ||
| 2331 | (command (list "rubocop" "--stdin" buffer-file-name "--format" "emacs" | ||
| 2332 | "--cache" "false" ; Work around a bug in old version. | 2333 | "--cache" "false" ; Work around a bug in old version. |
| 2333 | "--display-cop-names")) | 2334 | "--display-cop-names")) |
| 2334 | config-dir) | 2335 | config-dir) |
| @@ -2339,54 +2340,40 @@ Only takes effect if Rubocop is installed." | |||
| 2339 | (setq command (append command (list "--config" | 2340 | (setq command (append command (list "--config" |
| 2340 | (expand-file-name ruby-rubocop-config | 2341 | (expand-file-name ruby-rubocop-config |
| 2341 | config-dir))))) | 2342 | config-dir))))) |
| 2342 | (save-restriction | 2343 | |
| 2343 | (widen) | 2344 | (ruby-flymake--helper |
| 2344 | (setq | 2345 | "rubocop-flymake" |
| 2345 | ruby--rubocop-flymake-proc | 2346 | command |
| 2346 | (make-process | 2347 | (lambda (proc source) |
| 2347 | :name "rubocop-flymake" :noquery t :connection-type 'pipe | 2348 | ;; Finding the executable is no guarantee of |
| 2348 | :buffer (generate-new-buffer " *rubocop-flymake*") | 2349 | ;; rubocop working, especially in the presence |
| 2349 | :command command | 2350 | ;; of rbenv shims (which cross ruby versions). |
| 2350 | :sentinel | 2351 | (unless (zerop (process-exit-status proc)) |
| 2351 | (lambda (proc _event) | 2352 | (flymake-log :warning "Rubocop returned non-zero status: %s" |
| 2352 | (when (eq 'exit (process-status proc)) | 2353 | (buffer-string))) |
| 2353 | (unwind-protect | 2354 | (goto-char (point-min)) |
| 2354 | (if (with-current-buffer source (eq proc ruby--rubocop-flymake-proc)) | 2355 | (cl-loop |
| 2355 | (with-current-buffer (process-buffer proc) | 2356 | while (search-forward-regexp |
| 2356 | ;; Finding the executable is no guarantee of | 2357 | "^\\(?:.*.rb\\|-\\):\\([0-9]+\\):\\([0-9]+\\): \\(.*\\)$" |
| 2357 | ;; rubocop working, especially in the presence | 2358 | nil t) |
| 2358 | ;; of rbenv shims (which cross ruby versions). | 2359 | for msg = (match-string 3) |
| 2359 | (unless (zerop (process-exit-status proc)) | 2360 | for (beg . end) = (flymake-diag-region |
| 2360 | (flymake-log :warning "Rubocop returned non-zero status: %s" | 2361 | source |
| 2361 | (buffer-string))) | 2362 | (string-to-number (match-string 1)) |
| 2362 | (goto-char (point-min)) | 2363 | (string-to-number (match-string 2))) |
| 2363 | (cl-loop | 2364 | for type = (cond |
| 2364 | while (search-forward-regexp | 2365 | ((string-match "^[EF]: " msg) |
| 2365 | "^\\(?:.*.rb\\|-\\):\\([0-9]+\\):\\([0-9]<<+\\): \\(.*\\)$" | 2366 | :error) |
| 2366 | nil t) | 2367 | ((string-match "^W: " msg) |
| 2367 | for msg = (match-string 3) | 2368 | :warning) |
| 2368 | for (beg . end) = (flymake-diag-region | 2369 | (t :note)) |
| 2369 | source | 2370 | collect (flymake-make-diagnostic source |
| 2370 | (string-to-number (match-string 1)) | 2371 | beg |
| 2371 | (string-to-number (match-string 2))) | 2372 | end |
| 2372 | for type = (cond | 2373 | type |
| 2373 | ((string-match "^[EF]: " msg) | 2374 | (substring msg 3)) |
| 2374 | :error) | 2375 | into diags |
| 2375 | ((string-match "^W: " msg) | 2376 | finally (funcall report-fn diags))))))) |
| 2376 | :warning) | ||
| 2377 | (t :note)) | ||
| 2378 | collect (flymake-make-diagnostic source | ||
| 2379 | beg | ||
| 2380 | end | ||
| 2381 | type | ||
| 2382 | (substring msg 3)) | ||
| 2383 | into diags | ||
| 2384 | finally (funcall report-fn diags))) | ||
| 2385 | (flymake-log :debug "Canceling obsolete check %s" | ||
| 2386 | proc)) | ||
| 2387 | (kill-buffer (process-buffer proc))))))) | ||
| 2388 | (process-send-region ruby--rubocop-flymake-proc (point-min) (point-max)) | ||
| 2389 | (process-send-eof ruby--rubocop-flymake-proc))))) | ||
| 2390 | 2377 | ||
| 2391 | (defun ruby-flymake-auto (report-fn &rest args) | 2378 | (defun ruby-flymake-auto (report-fn &rest args) |
| 2392 | (apply | 2379 | (apply |