Skip to content

Commit

Permalink
fix up tests, simplify predicate code a bit.
Browse files Browse the repository at this point in the history
  • Loading branch information
lewang committed Sep 13, 2016
1 parent 7d57ac7 commit 1131153
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
21 changes: 12 additions & 9 deletions tests/ws-butler-tests.el
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
(when (get-buffer test-buffer-name)
(kill-buffer test-buffer-name))
(switch-to-buffer (get-buffer-create test-buffer-name))
(ws-butler-mode t)
,@body)))

(defmacro ws-butler-test-with-common-setup (&rest body)
Expand All @@ -33,12 +34,14 @@
(ert-deftest ws-butler-test-trim-predicate ()
"Tests `ws-butler-trim-predicate'."
(ws-butler-test-with-common-setup
(let ((ws-butler-trim-predicate (lambda (_beg _end) false)))
(insert "a b c. \n")
(ws-butler-before-save)
(should (string-equal (buffer-string) "a b c. \n")))
(let (ws-butler-trim-predicate)
(erase-buffer)
(insert "a b c. \n")
(ws-butler-before-save)
(should (string-equal (buffer-string) "a b c.\n")))))
(setq-local ws-butler-trim-predicate (lambda (_beg _end) nil))
(insert "a b c. \n")
(ws-butler-before-save)
(should (string-equal (buffer-string) "a b c. \n"))))

(ert-deftest ws-butler-test-trim-predicate-nil ()
"Tests `ws-butler-trim-predicate' is nil."
(ws-butler-test-with-common-setup
(insert "a b c. \n")
(ws-butler-before-save)
(should (string-equal (buffer-string) "a b c.\n"))))
9 changes: 5 additions & 4 deletions ws-butler.el
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ i.e. only the \"virtual\" space is preserved in the buffer."
:group 'ws-butler)

(defcustom ws-butler-trim-predicate
nil
(lambda (_beg _end) t)
"Return true for regions that should be trimmed.
Expects 2 arguments - beginning and end of a region.
Expand Down Expand Up @@ -135,7 +135,8 @@ Also see `require-final-newline'."
(goto-char (point-max))
(skip-chars-backward " \t\n\v")
(let ((printable-point-max (point)))
(when (>= last-modified-pos printable-point-max)
(when (and (funcall ws-butler-trim-predicate printable-point-max (point-max))
(>= last-modified-pos printable-point-max))
(ws-butler-trim-eob-lines))))))
;; clean return code for hooks
nil)
Expand Down Expand Up @@ -193,6 +194,7 @@ in place."
Setting `ws-butler-keep-whitespace-before-point' will also
ensure point doesn't jump due to white space trimming."

;; save data to restore later
(when ws-butler-keep-whitespace-before-point
(ws-butler-with-save
Expand All @@ -210,8 +212,7 @@ ensure point doesn't jump due to white space trimming."
;; always expand to end of line anyway, this should be OK.
end (progn (goto-char (1- end))
(point-at-eol))))
(unless (and (functionp ws-butler-trim-predicate)
(not (funcall ws-butler-trim-predicate beg end)))
(when (funcall ws-butler-trim-predicate beg end)
(ws-butler-clean-region beg end))
(setq last-end end)))
(ws-butler-maybe-trim-eob-lines last-end)))
Expand Down

0 comments on commit 1131153

Please sign in to comment.