From e9763514241583be864cbc2caaa6f6dee8c9907e Mon Sep 17 00:00:00 2001 From: Jeetaditya Chatterjee Date: Mon, 7 Aug 2023 23:38:46 +0530 Subject: [PATCH] feat(emacs-lisp): add reduced-flymake-byte-compile This is a flymake backend that gives a reduced set of byte compiler warnings in accordance too `+emacs-lisp-linter-warnings` --- modules/lang/emacs-lisp/autoload.el | 83 +++++++++++++++++++++++++---- 1 file changed, 74 insertions(+), 9 deletions(-) diff --git a/modules/lang/emacs-lisp/autoload.el b/modules/lang/emacs-lisp/autoload.el index 616fd1676..d7e838ff7 100644 --- a/modules/lang/emacs-lisp/autoload.el +++ b/modules/lang/emacs-lisp/autoload.el @@ -277,9 +277,71 @@ https://emacs.stackexchange.com/questions/10230/how-to-indent-keywords-aligned" file-base))))))) (not (locate-dominating-file default-directory ".doommodule"))))) +(defvar-local +emacs-lisp-reduced-flymake-byte-compile--process nil) + +(defun +emacs-lisp-reduced-flymake-byte-compile (report-fn &rest _args) + "A Flymake backend for warnings from the elisp byte compiler + +This checker reduces the amount of false positives the byte compiler throws off +compared too `elisp-flymake-byte-compile'. The linter warnings that are enabled +are set by `+emacs-lisp-linter-warnings' + +This backend does not need to be added directly +as `+emacs-lisp-non-package-mode' will enable it and disable the other checkers." + ;; if a process already exists. kill it. + (when (and +emacs-lisp-reduced-flymake-byte-compile--process + (process-live-p +emacs-lisp-reduced-flymake-byte-compile--process)) + (kill-process +emacs-lisp-reduced-flymake-byte-compile--process)) + (let ((source (current-buffer)) + (tmp-file (make-temp-file "+emacs-lisp-byte-compile-src")) + (out-buf (generate-new-buffer "+emacs-lisp-byte-compile-out"))) + ;; write the content to a temp file + (save-restriction + (widen) + (write-region nil nil tmp-file nil 'nomessage)) + ;; make the process + (setq +emacs-lisp-reduced-flymake-byte-compile--process + (make-process + :name "+emacs-reduced-flymake" + :noquery t + :connetion-type 'pipe + :buffer out-buf + :command `(,(expand-file-name invocation-name invocation-directory) + "-Q" + "--batch" + ,@(mapcan (fn! (list "-L" %)) elisp-flymake-byte-compile-load-path) + ;; this is what silences the byte compiler + "--eval" ,(prin1-to-string `(setq doom-modules ',doom-modules + doom-disabled-packages ',doom-disabled-packages + byte-compile-warnings ',+emacs-lisp-linter-warnings) +) + "-f" "elisp-flymake--batch-compile-for-flymake" + ,tmp-file) + :stderr "*stderr of +elisp-flymake-byte-compile-out*" + :sentinel + ;; deal with the process when it exits + (lambda (proc _event) + (when (memq (process-status proc) '(exit signal)) + (unwind-protect + (cond + ;; if the buffer is dead or the process is not the same, log the process as old. + ((or (not (buffer-live-p source)) + (not (with-current-buffer source (eq proc +emacs-lisp-reduced-flymake-byte-compile--process)))) + (flymake-log :warning "byte compile process %s is old" proc)) + ;; if the process exited without problem process the buffer + ((zerop (process-exit-status proc)) + (elisp-flymake--byte-compile-done report-fn source out-buf)) + ;; otherwise something else horrid has gone wrong and we panic + (t (funcall report-fn :panic + :explanation + (format "byte compile process %s died" proc)))) + ;; cleanup + (ignore-errors (delete-file tmp-file)) + (kill-buffer out-buf)))))))) + ;;;###autoload (define-minor-mode +emacs-lisp-non-package-mode - "Reduce flycheck verbosity where it is appropriate. + "Reduce flycheck/flymake verbosity where it is appropriate. Essentially, this means in any elisp file that either: - Is not a theme in `custom-theme-load-path', @@ -292,7 +354,7 @@ This generally applies to your private config (`doom-user-dir') or Doom's source :since "3.0.0" (unless (and (or (bound-and-true-p flycheck-mode) (bound-and-true-p flymake-mode)) - (not (+emacs-lisp--in-package-buffer-p))) + (not (+emacs-lisp--in-package-buffer-p))) (setq +emacs-lisp-non-package-mode nil)) (when (derived-mode-p 'emacs-lisp-mode) (add-hook 'after-save-hook #'+emacs-lisp-non-package-mode nil t)) @@ -300,14 +362,17 @@ This generally applies to your private config (`doom-user-dir') or Doom's source (if (modulep! :checkers syntax +flymake) ;; flymake (progn - (add-hook 'flymake-diagnostic-functions #'elisp-flymake-checkdoc nil t)) - ;; flycheck - (when (get 'flycheck-disabled-checkers 'initial-value) - (setq-local flycheck-disabled-checkers (get 'flycheck-disabled-checkers 'initial-value)) - (kill-local-variable 'flycheck-emacs-lisp-check-form))) + (add-hook! 'flymake-diagnostic-functions :local #'elisp-flymake-checkdoc #'elisp-flymake-byte-compile) + (remove-hook 'flymake-diagnostic-functions #'+emacs-lisp-reduced-flymake-byte-compile)) + ;; flycheck + (when (get 'flycheck-disabled-checkers 'initial-value) + (setq-local flycheck-disabled-checkers (get 'flycheck-disabled-checkers 'initial-value)) + (kill-local-variable 'flycheck-emacs-lisp-check-form))) (if (modulep! :checkers syntax +flymake) - ;; flymake - (remove-hook 'flymake-diagnostic-functions #'elisp-flymake-checkdoc t) + ;; flymake + (progn + (remove-hook! 'flymake-diagnostic-functions :local #'elisp-flymake-checkdoc #'elisp-flymake-byte-compile) + (add-hook 'flymake-diagnostic-functions #'+emacs-lisp-reduced-flymake-byte-compile)) ;; flycheck (with-memoization (get 'flycheck-disabled-checkers 'initial-value) flycheck-disabled-checkers)