branch: elpa/flymake-collection
commit 6df3ac725d3b6543de059ef827cd26c1ea0b3fa8
Author: Mohsin Kaleem <mohk...@kisara.moe>
Commit: Mohsin Kaleem <mohk...@kisara.moe>

    (flymake-rest-hook): Add mode -> checker configuration
    
    Now you can specify which checkers you want enabled in which modes and
    then have them automatically added to flymake-diagnostic-functions at
    startup.
---
 flymake-rest-hook-use-package.el | 24 +++++++++++++++++
 flymake-rest-hook.el             | 58 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 82 insertions(+)

diff --git a/flymake-rest-hook-use-package.el b/flymake-rest-hook-use-package.el
new file mode 100644
index 0000000000..e05f843cce
--- /dev/null
+++ b/flymake-rest-hook-use-package.el
@@ -0,0 +1,24 @@
+;;; flymake-rest-use-package.el --- use-package extensions for flymake-rest 
-*- lexical-binding: t -*-
+
+(require 'flymake-rest)
+(require 'flymake-rest-hook)
+
+(require 'use-package-core)
+
+;; Add to use-package-keywords, just after :custom.
+(unless (member :flymake-hook use-package-keywords)
+  (let ((tail (nthcdr (cl-position :custom use-package-keywords)
+                      use-package-keywords)))
+    (setcdr tail (cons :flymake-hook (cdr tail)))))
+
+(defun use-package-normalize/:flymake-hook (_name _keyword args)
+  args)
+
+(defun use-package-handler/:flymake-hook (name-symbol _ hooks rest state)
+  (let ((body (use-package-process-keywords name-symbol rest state)))
+    (use-package-concat
+     (cl-loop for it in hooks
+              collect `(push (quote ,it) flymake-rest-config))
+     body)))
+
+(provide 'flymake-rest-hook-use-package)
diff --git a/flymake-rest-hook.el b/flymake-rest-hook.el
new file mode 100644
index 0000000000..2793df3fcf
--- /dev/null
+++ b/flymake-rest-hook.el
@@ -0,0 +1,58 @@
+;;; flymake-rest-hook.el --- Support for binding flymake backends to specific 
modes -*- lexical-binding: t -*-
+
+;; Copyright (C) 2021 Mohsin Kaleem
+
+;;;###autoload
+(defcustom flymake-rest-config nil
+  "Configuration mapping major-modes to flymake-backends."
+  :type 'list)
+
+(defcustom flymake-rest-config-inherit nil
+  "When true any configured checkers for a parent major-mode are
+also added to `flymake-diagnostic-functions'."
+  :type 'boolean)
+
+(defun flymake-rest-configured-checkers (mode)
+  (let (checkers
+        (modes (list mode)))
+    ;; Consider all the parent modes as well.
+    (when flymake-rest-config-inherit
+      (while (setq mode (get mode 'derived-mode-parent))
+        (push mode modes)))
+    ;; For each mode populate the checkers alist with (checker . depth).
+    (dolist (mode modes)
+      (dolist (conf (alist-get mode flymake-rest-config))
+        (cond ((symbolp conf)
+               (push (cons conf nil) checkers))
+              ((consp conf)
+               (cl-destructuring-bind (checker &optional &key depth predicate 
disabled &allow-other-keys)
+                   (if (numberp conf)
+                       `(,(car conf) :depth ,(cdr conf))
+                     conf)
+                 (when (and (not disabled)
+                            (or (not predicate)
+                                (funcall predicate)))
+                   (push (cons checker depth) checkers))))
+              (t
+               (warn "Unknown checker config in `flymake-rest-config': %s" 
conf)))))
+    (nreverse checkers)))
+
+(defun flymake-rest-hook-set-backends ()
+  "Function to add all the diagnostic for the current-major mode
+from `flymake-rest-config' to `flymake-diagnostic-functions'."
+  (dolist (it flymake-rest-configured-checkers)
+    (add-hook 'flymake-diagnostic-functions (car it) (cdr it) t)))
+
+(defun flymake-rest-hook-setup ()
+  "Setup flymake-hook."
+  (add-hook 'after-change-major-mode-hook #'flymake-rest-hook-set-backends))
+
+(defun flymake-rest-hook-teardown ()
+  "Tear down flymake-hook."
+  (remove-hook 'after-change-major-mode-hook #'flymake-rest-hook-set-backends))
+
+;;;###autoload
+(with-eval-after-load 'use-package
+  (require 'flymake-rest-hook-use-package))
+
+(provide 'flymake-rest-hook)

Reply via email to