2010-03-12 04:39:38 +00:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;;;
|
|
|
|
;;; ghc-flymake.el
|
|
|
|
;;;
|
|
|
|
|
|
|
|
;; Author: Kazu Yamamoto <Kazu@Mew.org>
|
|
|
|
;; Created: Mar 12, 2010
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
2010-03-11 10:03:17 +00:00
|
|
|
(require 'flymake)
|
2011-11-15 11:40:25 +00:00
|
|
|
(require 'ghc-func)
|
2010-03-11 10:03:17 +00:00
|
|
|
|
2010-06-14 02:56:35 +00:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
(defvar ghc-hlint-options nil "*Hlint options")
|
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2011-08-02 18:18:07 +00:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2010-06-14 03:03:14 +00:00
|
|
|
(defconst ghc-error-buffer-name "*GHC Errors*")
|
2010-04-01 02:58:44 +00:00
|
|
|
|
2010-06-14 03:03:14 +00:00
|
|
|
(defconst ghc-flymake-allowed-file-name-masks
|
2010-03-11 10:03:17 +00:00
|
|
|
'("\\.l?hs$" ghc-flymake-init flymake-simple-cleanup flymake-get-real-file-name))
|
|
|
|
|
2010-06-14 03:03:14 +00:00
|
|
|
(defconst ghc-flymake-err-line-patterns
|
2010-04-01 02:58:44 +00:00
|
|
|
'("^\\(.*\\.l?hs\\):\\([0-9]+\\):\\([0-9]+\\):[ ]*\\(.+\\)" 1 2 3 4))
|
2010-03-11 10:03:17 +00:00
|
|
|
|
|
|
|
(add-to-list 'flymake-allowed-file-name-masks
|
|
|
|
ghc-flymake-allowed-file-name-masks)
|
|
|
|
|
|
|
|
(add-to-list 'flymake-err-line-patterns
|
|
|
|
ghc-flymake-err-line-patterns)
|
|
|
|
|
2010-04-01 02:58:44 +00:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2010-03-11 10:03:17 +00:00
|
|
|
(defun ghc-flymake-init ()
|
2010-03-12 02:55:11 +00:00
|
|
|
(let ((after-save-hook nil))
|
|
|
|
(save-buffer))
|
2010-03-11 15:20:02 +00:00
|
|
|
(let ((file (file-name-nondirectory (buffer-file-name))))
|
2011-10-19 10:09:36 +00:00
|
|
|
(list ghc-module-command (ghc-flymake-command file))))
|
2010-05-06 06:29:55 +00:00
|
|
|
|
|
|
|
(defvar ghc-flymake-command nil) ;; nil: check, t: lint
|
|
|
|
|
2011-10-19 10:09:36 +00:00
|
|
|
(defun ghc-flymake-command (file)
|
2010-05-06 06:29:55 +00:00
|
|
|
(if ghc-flymake-command
|
2011-10-19 10:09:36 +00:00
|
|
|
(let ((hopts (ghc-mapconcat (lambda (x) (list "-h" x)) ghc-hlint-options)))
|
2010-06-14 02:56:35 +00:00
|
|
|
`(,@hopts "lint" ,file))
|
2011-11-15 11:40:25 +00:00
|
|
|
`(,@(ghc-make-ghc-options) "check" ,file)))
|
2011-08-03 02:18:35 +00:00
|
|
|
|
2010-05-06 06:29:55 +00:00
|
|
|
(defun ghc-flymake-toggle-command ()
|
|
|
|
(interactive)
|
2010-06-08 02:23:58 +00:00
|
|
|
(setq ghc-flymake-command (not ghc-flymake-command))
|
|
|
|
(if ghc-flymake-command
|
|
|
|
(message "Syntax check with hlint")
|
|
|
|
(message "Syntax check with GHC")))
|
2010-03-11 15:20:02 +00:00
|
|
|
|
2010-04-01 02:58:44 +00:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2010-05-04 03:14:33 +00:00
|
|
|
(defun ghc-flymake-display-errors ()
|
2010-04-01 02:58:44 +00:00
|
|
|
(interactive)
|
2010-05-04 03:14:33 +00:00
|
|
|
(if (not (ghc-flymake-have-errs-p))
|
|
|
|
(message "No errors or warnings")
|
|
|
|
(let ((buf (get-buffer-create ghc-error-buffer-name))
|
|
|
|
(title (ghc-flymake-err-title))
|
|
|
|
(errs (ghc-flymake-err-list)))
|
|
|
|
(with-current-buffer buf
|
|
|
|
(erase-buffer)
|
|
|
|
(ghc-flymake-insert-errors title errs))
|
|
|
|
(display-buffer buf))))
|
|
|
|
|
|
|
|
(defun ghc-flymake-insert-errors (title errs)
|
|
|
|
(save-excursion
|
2010-06-14 05:42:17 +00:00
|
|
|
(insert title "\n\n")
|
2010-06-25 12:45:32 +00:00
|
|
|
(mapc (lambda (x) (insert (ghc-replace-character x ghc-null ghc-newline) "\n")) errs)))
|
2010-06-14 05:27:35 +00:00
|
|
|
|
2010-04-01 02:58:44 +00:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2012-03-02 12:36:58 +00:00
|
|
|
(defun ghc-flymake-jump ()
|
|
|
|
(interactive)
|
|
|
|
(if (not (ghc-flymake-have-errs-p))
|
|
|
|
(message "No errors or warnings")
|
|
|
|
(let* ((acts (ghc-flymake-act-list))
|
|
|
|
(act (car acts)))
|
|
|
|
(if (not act)
|
|
|
|
(message "No destination")
|
|
|
|
(eval act)))))
|
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2011-01-14 04:38:12 +00:00
|
|
|
(defun ghc-extract-type (str)
|
|
|
|
(with-temp-buffer
|
|
|
|
(insert str)
|
|
|
|
(goto-char (point-min))
|
2011-03-25 04:00:08 +00:00
|
|
|
(when (re-search-forward "Inferred type: \\|no type signature:\\( \\|\0 +\\)?" nil t)
|
2011-01-14 04:38:12 +00:00
|
|
|
(delete-region (point-min) (point)))
|
2011-01-14 04:52:41 +00:00
|
|
|
(when (re-search-forward " forall [^.]+\\." nil t)
|
2011-01-14 04:38:12 +00:00
|
|
|
(replace-match ""))
|
|
|
|
(while (re-search-forward "\0 +" nil t)
|
|
|
|
(replace-match " "))
|
|
|
|
(goto-char (point-min))
|
|
|
|
(while (re-search-forward "\\[Char\\]" nil t)
|
|
|
|
(replace-match "String"))
|
|
|
|
(re-search-forward "\0" nil t)
|
|
|
|
(buffer-substring-no-properties (point-min) (1- (point)))))
|
|
|
|
|
2010-05-04 03:14:33 +00:00
|
|
|
(defun ghc-flymake-insert-from-warning ()
|
2010-03-14 13:39:45 +00:00
|
|
|
(interactive)
|
2010-05-04 03:14:33 +00:00
|
|
|
(dolist (data (ghc-flymake-err-list))
|
|
|
|
(save-excursion
|
|
|
|
(cond
|
2011-03-25 04:00:08 +00:00
|
|
|
((string-match "Inferred type: \\|no type signature:" data)
|
2010-03-14 13:39:45 +00:00
|
|
|
(beginning-of-line)
|
2011-01-14 04:38:12 +00:00
|
|
|
(insert (ghc-extract-type data) "\n"))
|
2010-06-25 11:31:46 +00:00
|
|
|
((string-match "lacks an accompanying binding" data)
|
|
|
|
(beginning-of-line)
|
|
|
|
(when (looking-at "^\\([^ ]+\\) *::")
|
|
|
|
(save-match-data
|
|
|
|
(forward-line)
|
2011-03-07 02:03:48 +00:00
|
|
|
(if (not (bolp)) (insert "\n")))
|
2010-06-25 11:31:46 +00:00
|
|
|
(insert (match-string 1) " = undefined\n")))
|
2010-05-04 03:14:33 +00:00
|
|
|
((string-match "Not in scope: `\\([^']+\\)'" data)
|
|
|
|
(save-match-data
|
|
|
|
(unless (re-search-forward "^$" nil t)
|
|
|
|
(goto-char (point-max))
|
|
|
|
(insert "\n")))
|
2010-06-14 05:26:14 +00:00
|
|
|
(insert "\n" (match-string 1 data) " = undefined\n"))
|
2012-02-27 02:01:18 +00:00
|
|
|
((string-match "Pattern match(es) are non-exhaustive" data)
|
|
|
|
(let* ((fn (ghc-get-function-name))
|
|
|
|
(arity (ghc-get-function-arity fn)))
|
|
|
|
(ghc-insert-underscore fn arity)))
|
2010-06-14 05:26:14 +00:00
|
|
|
((string-match "Found:\0[ ]*\\([^\0]+\\)\0Why not:\0[ ]*\\([^\0]+\\)" data)
|
|
|
|
(let ((old (match-string 1 data))
|
|
|
|
(new (match-string 2 data)))
|
|
|
|
(beginning-of-line)
|
|
|
|
(when (search-forward old nil t)
|
|
|
|
(let ((end (point)))
|
|
|
|
(search-backward old nil t)
|
|
|
|
(delete-region (point) end))
|
|
|
|
(insert new))))))))
|
2010-03-14 13:39:45 +00:00
|
|
|
|
2012-02-27 02:01:18 +00:00
|
|
|
(defun ghc-get-function-name ()
|
|
|
|
(save-excursion
|
|
|
|
(beginning-of-line)
|
|
|
|
(when (looking-at "\\([^ ]+\\) ")
|
|
|
|
(match-string 1))))
|
|
|
|
|
|
|
|
(defun ghc-get-function-arity (fn)
|
|
|
|
(when fn
|
|
|
|
(save-excursion
|
|
|
|
(let ((regex (format "^%s *::" (regexp-quote fn))))
|
|
|
|
(when (re-search-backward regex nil t)
|
|
|
|
(ghc-get-function-arity0))))))
|
|
|
|
|
|
|
|
(defun ghc-get-function-arity0 ()
|
|
|
|
(let ((end (save-excursion (end-of-line) (point)))
|
|
|
|
(arity 0))
|
|
|
|
(while (search-forward "->" end t)
|
|
|
|
(setq arity (1+ arity)))
|
|
|
|
arity))
|
|
|
|
|
|
|
|
(defun ghc-insert-underscore (fn ar)
|
|
|
|
(when fn
|
|
|
|
(let ((arity (or ar 1)))
|
|
|
|
(save-excursion
|
|
|
|
(goto-char (point-max))
|
|
|
|
(re-search-backward (format "^%s *::" (regexp-quote fn)))
|
|
|
|
(forward-line)
|
|
|
|
(re-search-forward "^$" nil t)
|
|
|
|
(insert fn)
|
|
|
|
(dotimes (i arity)
|
|
|
|
(insert " _"))
|
2012-02-27 02:03:27 +00:00
|
|
|
(insert " = error \"" fn "\"")))))
|
2012-02-27 02:01:18 +00:00
|
|
|
|
2010-04-01 02:58:44 +00:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2010-05-04 03:14:33 +00:00
|
|
|
(defun ghc-flymake-err-get-title (x) (nth 0 x))
|
|
|
|
(defun ghc-flymake-err-get-errs (x) (nth 1 x))
|
|
|
|
|
2012-03-02 12:36:58 +00:00
|
|
|
(defun ghc-flymake-err-get-err-msg (x) (nth 0 x))
|
|
|
|
(defun ghc-flymake-err-get-err-act (x) (nth 1 x))
|
|
|
|
|
2010-05-04 03:14:33 +00:00
|
|
|
(defalias 'ghc-flymake-have-errs-p 'ghc-flymake-data)
|
|
|
|
|
2010-03-14 13:39:45 +00:00
|
|
|
(defun ghc-flymake-data ()
|
|
|
|
(let* ((line-no (flymake-current-line-no))
|
2010-04-01 02:58:44 +00:00
|
|
|
(info (nth 0 (flymake-find-err-info flymake-err-info line-no))))
|
|
|
|
(flymake-make-err-menu-data line-no info)))
|
|
|
|
|
2010-05-04 03:14:33 +00:00
|
|
|
(defun ghc-flymake-err-title ()
|
|
|
|
(ghc-flymake-err-get-title (ghc-flymake-data)))
|
|
|
|
|
|
|
|
(defun ghc-flymake-err-list ()
|
2012-03-02 12:36:58 +00:00
|
|
|
(mapcar 'ghc-flymake-err-get-err-msg (ghc-flymake-err-get-errs (ghc-flymake-data))))
|
|
|
|
|
|
|
|
(defun ghc-flymake-act-list ()
|
|
|
|
(mapcar 'ghc-flymake-err-get-err-act (ghc-flymake-err-get-errs (ghc-flymake-data))))
|
2010-03-14 13:39:45 +00:00
|
|
|
|
2010-05-04 03:14:33 +00:00
|
|
|
(provide 'ghc-flymake)
|