aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoão Távora2017-11-03 12:43:11 +0000
committerJoão Távora2017-11-05 12:16:28 +0000
commit9dee764165f54bf93039b8301e0ef6dd143d6cf1 (patch)
treef973ca93b7f841d162caad878df2b9bd59a0f4ce
parent3ad712ebc91438838df29b0e12b3103d409ee3a4 (diff)
downloademacs-9dee764165f54bf93039b8301e0ef6dd143d6cf1.tar.gz
emacs-9dee764165f54bf93039b8301e0ef6dd143d6cf1.zip
Add a Flymake backend for Ruby
* lisp/progmodes/ruby-mode.el (ruby-flymake-command): New defcustom. (ruby--flymake-proc): New buffer-local variable. (ruby-flymake): New function. (ruby-mode): Add flymake-diagnostic-functions.
-rw-r--r--lisp/progmodes/ruby-mode.el63
1 files changed, 63 insertions, 0 deletions
diff --git a/lisp/progmodes/ruby-mode.el b/lisp/progmodes/ruby-mode.el
index 0024957c39b..1f4aa6d9fbd 100644
--- a/lisp/progmodes/ruby-mode.el
+++ b/lisp/progmodes/ruby-mode.el
@@ -2253,6 +2253,68 @@ See `font-lock-syntax-table'.")
2253 (progn (set-match-data value) t)) 2253 (progn (set-match-data value) t))
2254 (ruby-match-expression-expansion limit))))) 2254 (ruby-match-expression-expansion limit)))))
2255 2255
2256;;; Flymake support
2257(defcustom ruby-flymake-command '("ruby" "-w" "-c")
2258 "External tool used to check Ruby source code.
2259This is a non empty list of strings, the checker tool possibly
2260followed by required arguments. Once launched it will receive
2261the Ruby source to be checked as its standard input."
2262 :group 'ruby
2263 :type '(repeat string))
2264
2265(defvar-local ruby--flymake-proc nil)
2266
2267(defun ruby-flymake (report-fn &rest _args)
2268 "Ruby backend for Flymake. Launches
2269`ruby-flymake-command' (which see) and passes to its standard
2270input the contents of the current buffer. The output of this
2271command is analysed for error and warning messages."
2272 (unless (executable-find (car ruby-flymake-command))
2273 (error "Cannot find a suitable checker"))
2274
2275 (when (process-live-p ruby--flymake-proc)
2276 (kill-process ruby--flymake-proc))
2277
2278 (let ((source (current-buffer)))
2279 (save-restriction
2280 (widen)
2281 (setq
2282 ruby--flymake-proc
2283 (make-process
2284 :name "ruby-flymake" :noquery t :connection-type 'pipe
2285 :buffer (generate-new-buffer " *ruby-flymake*")
2286 :command ruby-flymake-command
2287 :sentinel
2288 (lambda (proc _event)
2289 (when (eq 'exit (process-status proc))
2290 (unwind-protect
2291 (if (with-current-buffer source (eq proc ruby--flymake-proc))
2292 (with-current-buffer (process-buffer proc)
2293 (goto-char (point-min))
2294 (cl-loop
2295 while (search-forward-regexp
2296 "^\\(?:.*.rb\\|-\\):\\([0-9]+\\): \\(.*\\)$"
2297 nil t)
2298 for msg = (match-string 2)
2299 for (beg . end) = (flymake-diag-region
2300 source
2301 (string-to-number (match-string 1)))
2302 for type = (if (string-match "^warning" msg)
2303 :warning
2304 :error)
2305 collect (flymake-make-diagnostic source
2306 beg
2307 end
2308 type
2309 msg)
2310 into diags
2311 finally (funcall report-fn diags)))
2312 (flymake-log :debug "Canceling obsolete check %s"
2313 proc))
2314 (kill-buffer (process-buffer proc)))))))
2315 (process-send-region ruby--flymake-proc (point-min) (point-max))
2316 (process-send-eof ruby--flymake-proc))))
2317
2256;;;###autoload 2318;;;###autoload
2257(define-derived-mode ruby-mode prog-mode "Ruby" 2319(define-derived-mode ruby-mode prog-mode "Ruby"
2258 "Major mode for editing Ruby code." 2320 "Major mode for editing Ruby code."
@@ -2265,6 +2327,7 @@ See `font-lock-syntax-table'.")
2265 2327
2266 (add-hook 'after-save-hook 'ruby-mode-set-encoding nil 'local) 2328 (add-hook 'after-save-hook 'ruby-mode-set-encoding nil 'local)
2267 (add-hook 'electric-indent-functions 'ruby--electric-indent-p nil 'local) 2329 (add-hook 'electric-indent-functions 'ruby--electric-indent-p nil 'local)
2330 (add-hook 'flymake-diagnostic-functions 'ruby-flymake nil 'local)
2268 2331
2269 (setq-local font-lock-defaults '((ruby-font-lock-keywords) nil nil)) 2332 (setq-local font-lock-defaults '((ruby-font-lock-keywords) nil nil))
2270 (setq-local font-lock-keywords ruby-font-lock-keywords) 2333 (setq-local font-lock-keywords ruby-font-lock-keywords)