Klimi's new dotfiles with stow.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

254 lignes
10 KiB

il y a 5 ans
  1. ;;; ess-r-flymake.el --- A ess-r Flymake backend -*- lexical-binding: t; -*-
  2. ;;
  3. ;; Copyright (C) 2018 J. Alexander Branham (alex DOT branham AT gmail DOT com)
  4. ;; Copyright (C) 2018 ESS-core team
  5. ;; Maintainer: ESS-core <ESS-core@r-project.org>
  6. ;;
  7. ;; This file is NOT part of GNU Emacs.
  8. ;;
  9. ;; This is free software; you can redistribute it and/or modify it under
  10. ;; the terms of the GNU General Public License as published by the Free
  11. ;; Software Foundation; either version 3, or (at your option) any later
  12. ;; version.
  13. ;;
  14. ;; This is distributed in the hope that it will be useful, but WITHOUT
  15. ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  16. ;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  17. ;; for more details.
  18. ;;
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  21. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  22. ;; MA 02110-1301 USA.
  23. ;;
  24. ;;; Commentary:
  25. ;;
  26. ;; Flymake is the built-in Emacs package that supports on-the-fly
  27. ;; syntax checking. This file adds support for this in ess-r-mode by
  28. ;; relying on the lintr package, available on CRAN and currently
  29. ;; hosted at https://github.com/jimhester/lintr.
  30. ;;
  31. ;; It is enabled by default.
  32. ;;
  33. ;;; Code:
  34. (eval-when-compile (require 'cl-lib))
  35. (require 'ess-inf)
  36. (require 'flymake)
  37. ;; Appease the byte compiler for Emacs 25. Remove after dropping
  38. ;; support for Emacs 25.
  39. (declare-function flymake-diag-region "flymake")
  40. (declare-function flymake-make-diagnostic "flymake")
  41. (declare-function flymake--overlays "flymake")
  42. (defcustom ess-r-flymake-linters
  43. '("closed_curly_linter = NULL"
  44. "commas_linter = NULL"
  45. "commented_code_linter = NULL"
  46. "infix_spaces_linter = NULL"
  47. "line_length_linter = NULL"
  48. "object_length_linter = NULL"
  49. "object_name_linter = NULL"
  50. "object_usage_linter = NULL"
  51. "open_curly_linter = NULL"
  52. "pipe_continuation_linter = NULL"
  53. "single_quotes_linter = NULL"
  54. "spaces_inside_linter = NULL"
  55. "spaces_left_parentheses_linter = NULL"
  56. "trailing_blank_lines_linter = NULL"
  57. "trailing_whitespace_linter = NULL")
  58. "Default linters to use.
  59. Can be either a string with R expression to be used as
  60. is (e.g. 'lintr::default_linters'). Or a list of strings where
  61. each element is passed as argument to 'lintr::with_defaults'."
  62. :group 'ess-R
  63. :type '(choice string (repeat string))
  64. :package-version '(ess . "18.10"))
  65. (defcustom ess-r-flymake-lintr-cache t
  66. "If non-nil, cache lintr results."
  67. :group 'ess-R
  68. :type 'boolean
  69. :package-version '(ess . "18.10"))
  70. (defvar-local ess-r--flymake-proc nil)
  71. (defvar-local ess-r--lintr-file nil
  72. "Location of the .lintr file for this buffer.")
  73. (defvar ess-r--flymake-def-linter
  74. (replace-regexp-in-string
  75. "[\n\t ]+" " "
  76. "esslint <- function(str, ...) {
  77. if (!suppressWarnings(require(lintr, quietly=T))) {
  78. cat('@@error: @@`lintr` package not installed')
  79. } else {
  80. if (packageVersion('lintr') <= '1.0.3') {
  81. cat('@@error: @@Need `lintr` version > v1.0.3')
  82. } else {
  83. tryCatch(lintr::lint(commandArgs(TRUE), ...),
  84. error = function(e) {
  85. cat('@@warning: @@', e)
  86. })
  87. }
  88. }
  89. };"))
  90. (defun ess-r--find-lintr-file ()
  91. "Return the absolute path to the .lintr file.
  92. Check first the current directory, then the project root, then
  93. the user's home directory. Return nil if we couldn't find a .lintr file."
  94. (let ((cur-dir-file (expand-file-name ".lintr" default-directory))
  95. (ess-proj-file (and (fboundp 'ess-r-package-project)
  96. (ess-r-package-project)
  97. (expand-file-name ".lintr" (cdr (ess-r-package-project)))))
  98. (proj-file (and (project-current)
  99. (project-roots (project-current))
  100. (expand-file-name ".lintr" (car (project-roots (project-current))))))
  101. (home-file (expand-file-name ".lintr" (getenv "HOME"))))
  102. (cond (;; current directory
  103. (file-readable-p cur-dir-file)
  104. cur-dir-file)
  105. ;; Project root according to `ess-r-package-project'
  106. ((and ess-proj-file
  107. (file-readable-p ess-proj-file))
  108. ess-proj-file)
  109. ;; Project root according to `project-roots'
  110. ((and proj-file
  111. (file-readable-p proj-file)))
  112. ;; Home directory
  113. ((file-readable-p home-file)
  114. home-file))))
  115. (defun ess-r--flymake-linters ()
  116. "If `ess-r-flymake-linters' is a string, use that.
  117. Otherwise, construct a string to pass to lintr::with_defaults."
  118. (replace-regexp-in-string
  119. "[\n\t ]+" " "
  120. (if (stringp ess-r-flymake-linters)
  121. ess-r-flymake-linters
  122. (concat "lintr::with_defaults("
  123. (mapconcat #'identity
  124. ess-r-flymake-linters
  125. ", ")
  126. ")"))))
  127. (defun ess-r--flymake-msg-type (str)
  128. "Transform STR into log level."
  129. (cond ((string= str "error: ") :error)
  130. ((string= str "warning: ") :warning)
  131. ((string= str "style: ") :note)
  132. (t (error "Invalid msg type %s" str))))
  133. (defun ess-r--flymake-check-errors ()
  134. "Check for critical errors and return non-nil if such occurred."
  135. (goto-char (point-min))
  136. (when (re-search-forward "@@\\(\\(error\\|warning\\): \\)@@" nil t)
  137. (let ((type (ess-r--flymake-msg-type (match-string 1)))
  138. (msg (buffer-substring-no-properties (match-end 0) (point-max))))
  139. (flymake-log type msg)
  140. (eq type :error))))
  141. (defun ess-r--flymake-parse-output (msg-buffer src-buffer report-fn)
  142. "Parse the content of MSG-BUFFER for lint locations.
  143. SRC-BUFFER is the original source buffer. Collect all messages
  144. into a list and call REPORT-FN on it."
  145. (with-current-buffer msg-buffer
  146. (if (ess-r--flymake-check-errors)
  147. (with-current-buffer src-buffer
  148. ;; we are in the sentinel here; don't throw but remove our hook instead
  149. (remove-hook 'flymake-diagnostic-functions 'ess-r-flymake t))
  150. (goto-char (point-min))
  151. (cl-loop
  152. while (search-forward-regexp
  153. ;; Regex to match the output lint() gives us.
  154. (rx line-start "<text>:"
  155. ;; row
  156. (group-n 1 (one-or-more num)) ":"
  157. ;; column
  158. (group-n 2 (one-or-more num)) ": "
  159. ;; type
  160. (group-n 3 (| "style: " "warning: " "error: "))
  161. ;; msg
  162. (group-n 4 (one-or-more not-newline)) line-end)
  163. nil t)
  164. for msg = (match-string 4)
  165. for (beg . end) = (let ((line (string-to-number (match-string 1)))
  166. (col (string-to-number (match-string 2))))
  167. (flymake-diag-region src-buffer line col))
  168. for type = (ess-r--flymake-msg-type (match-string 3))
  169. collect (flymake-make-diagnostic src-buffer beg end type msg)
  170. into diags
  171. finally (funcall report-fn diags)))))
  172. (defun ess-r-flymake (report-fn &rest _args)
  173. "A Flymake backend for ESS-R modes. Relies on the lintr package.
  174. REPORT-FN is flymake's callback function."
  175. (unless (executable-find inferior-ess-r-program)
  176. (error "Cannot find program '%s'" inferior-ess-r-program))
  177. ;; Kill the process if earlier check was found. The sentinel of the earlier
  178. ;; check will detect this.
  179. (when (process-live-p ess-r--flymake-proc)
  180. (kill-process ess-r--flymake-proc))
  181. (if (and (eql ess-use-flymake 'process)
  182. (not (ess-process-live-p)))
  183. (progn
  184. (funcall report-fn nil)
  185. (mapc #'delete-overlay (flymake--overlays)))
  186. (let ((src-buffer (current-buffer)))
  187. (setq ess-r--flymake-proc
  188. (make-process
  189. :name "ess-r-flymake" :noquery t :connection-type 'pipe
  190. :buffer (generate-new-buffer " *ess-r-flymake*")
  191. :command (list inferior-R-program
  192. "--no-save" "--no-restore" "--no-site-file" "--no-init-file" "--slave"
  193. "-e" (concat
  194. (when ess-r--lintr-file
  195. (concat "options(lintr.linter_file = \"" ess-r--lintr-file "\");"))
  196. ess-r--flymake-def-linter
  197. ;; commandArgs(TRUE) returns everything after
  198. ;; --args as a character vector
  199. "esslint(commandArgs(TRUE)"
  200. (unless ess-r--lintr-file
  201. (concat ", linters = " (ess-r--flymake-linters)))
  202. (when ess-r-flymake-lintr-cache
  203. ", cache = TRUE")
  204. ")")
  205. "--args" (buffer-substring-no-properties
  206. (point-min) (point-max)))
  207. :sentinel
  208. (lambda (proc _event)
  209. (cond
  210. ((eq 'exit (process-status proc))
  211. (unwind-protect
  212. (if (eq proc (buffer-local-value 'ess-r--flymake-proc src-buffer))
  213. (ess-r--flymake-parse-output (process-buffer proc) src-buffer report-fn)
  214. (flymake-log :warning "Canceling obsolete check %s" proc))
  215. (kill-buffer (process-buffer proc))))
  216. ((not (eq 'run (process-status proc)))
  217. (kill-buffer (process-buffer proc))))))))))
  218. (defun ess-r-setup-flymake ()
  219. "Setup flymake for ESS.
  220. Activate flymake only if `ess-use-flymake' is non-nil."
  221. (when ess-use-flymake
  222. (when (< emacs-major-version 26)
  223. (error "ESS-flymake requires Emacs version 26 or later"))
  224. (when (string= "R" ess-dialect)
  225. (setq ess-r--lintr-file (ess-r--find-lintr-file))
  226. (add-hook 'flymake-diagnostic-functions #'ess-r-flymake nil t)
  227. ;; Try not to enable flymake if flycheck is already running:
  228. (unless (bound-and-true-p flycheck-mode)
  229. (flymake-mode)))))
  230. ;; Enable flymake in Emacs 26+
  231. (when (<= 26 emacs-major-version)
  232. (if (eval-when-compile (<= 26 emacs-major-version))
  233. (add-hook 'ess-r-mode-hook #'ess-r-setup-flymake)
  234. (when ess-use-flymake
  235. (display-warning 'ess "ESS was compiled with older version of Emacs;\n `ess-r-flymake' won't be available"))))
  236. (provide 'ess-r-flymake)
  237. ;;; ess-r-flymake.el ends here