diff --git a/haskell-utils.el b/haskell-utils.el index bd919cafd..6f4f9a53d 100644 --- a/haskell-utils.el +++ b/haskell-utils.el @@ -100,12 +100,13 @@ execusion." (remove-hook 'post-command-hook #'haskell-utils-async-update-post-command-flag t))) -(defun haskell-utils-reduce-string (s) - "Remove newlines and extra whitespace from S. -Removes all extra whitespace at the beginning of each line leaving -only a single space. Then removes all newlines." - (let ((s_ (replace-regexp-in-string "^\s+" " " s))) - (replace-regexp-in-string "\n" "" s_))) +(defun haskell-utils-reduce-string (str) + "Remove newlines and extra whitespace from string STR. +If line starts with a sequence of whitespaces, substitutes this +sequence with a single whitespace. Removes all newline +characters." + (let ((s (replace-regexp-in-string "^\s+" " " str))) + (replace-regexp-in-string "\r?\n" "" s))) (defun haskell-utils-repl-response-error-status (response) "Parse response REPL's RESPONSE for errors. diff --git a/tests/haskell-utils-tests.el b/tests/haskell-utils-tests.el index 8738d84f5..0bbb02355 100644 --- a/tests/haskell-utils-tests.el +++ b/tests/haskell-utils-tests.el @@ -201,4 +201,28 @@ strings will change in future." (should (equal r6 'no-error)) (should (equal r7 'no-error)))) +(ert-deftest reduce-strign () + "Test `haskell-utils-reduce-strign' command. +Whitespace sequences at beginning of lines should be replaced +with single whitespace, all newline characters should be +removed." + (should (string-equal "" (haskell-utils-reduce-string "\n"))) + (should (string-equal "" (haskell-utils-reduce-string "\r\n"))) + (should (string-equal " " (haskell-utils-reduce-string " \n"))) + (should (string-equal + "TestTest" + (haskell-utils-reduce-string "Test\nTest"))) + (should (string-equal + "Test Test" + (haskell-utils-reduce-string "Test\n Test"))) + (should (string-equal + " Test Test" + (haskell-utils-reduce-string " Test\r\n Test"))) + (should (string-equal + " TestTest" + (haskell-utils-reduce-string " Test\r\nTest"))) + (should (string-equal + " TestTest Test test" + (haskell-utils-reduce-string " Test\r\nTest\n Test test")))) + ;;; haskell-utils-tests.el ends here