Skip to content

Commit

Permalink
Simplify flycheck-prepend-with-option
Browse files Browse the repository at this point in the history
Wrap a list around a string result from prepend-fn, and replace -flatten
with a simply apply/append.
  • Loading branch information
swsnr committed Jul 18, 2014
1 parent 8c44c5f commit d599187
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
22 changes: 16 additions & 6 deletions flycheck.el
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion test/flycheck-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit d599187

Please sign in to comment.