Skip to content
Browse files

Simplify flycheck-prepend-with-option

Wrap a list around a string result from prepend-fn, and replace -flatten
with a simply apply/append.
  • Loading branch information...
1 parent 8c44c5f commit d599187810ccad5fce19eb50c2c06bee978a037a @lunaryorn lunaryorn committed
Showing with 17 additions and 7 deletions.
  1. +16 −6 flycheck.el
  2. +1 −1 test/flycheck-test.el
View
22 flycheck.el
@@ -1228,16 +1228,26 @@ Prepend OPTION to each item in ITEMS.
ITEMS is a list of strings to pass to the syntax checker. OPTION
is the option, as string. PREPEND-FN is a function called to
-prepend OPTION to each item in ITEMS. If nil or omitted, use
-`list'.
-
-Return a flattened list where OPTION is prepended to each item in
-ITEMS."
+prepend OPTION to each item in ITEMS. It receives the option and
+a single item from ITEMS as argument, and must return a string or
+a list of strings with OPTION prepended to the item. If
+PREPEND-FN is nil or omitted, use `list'.
+
+Return a list of strings where OPTION is prepended to each item
+in ITEMS using PREPEND-FN. If PREPEND-FN returns a list, it is
+spliced into the resulting list."
(unless (stringp option)
(error "Option %S is not a string" option))
(unless prepend-fn
(setq prepend-fn #'list))
- (-flatten (mapcar (apply-partially #'funcall prepend-fn option) items)))
+ (let ((prepend
+ (lambda (item)
+ (let ((result (funcall prepend-fn option item)))
+ (cond
+ ((and (listp result) (-all? #'stringp result)) result)
+ ((stringp result) (list result))
+ (t (error "Invalid result type for option: %S" result)))))))
+ (apply #'append (mapcar prepend items))))
(defun flycheck-find-in-buffer (pattern)
"Find PATTERN in the current buffer.
View
2 test/flycheck-test.el
@@ -1134,7 +1134,7 @@ check with. ERRORS is the list of expected errors."
(ert-deftest flycheck-prepend-with-option/empty-list ()
:tags '(utility)
- (should (null (flycheck-prepend-with-option "-f" nil))))
+ (should-not (flycheck-prepend-with-option "-f" nil)))
(ert-deftest flycheck-prepend-with-option/default-prepend-function ()
:tags '(utility)

0 comments on commit d599187

Please sign in to comment.
Something went wrong with that request. Please try again.