Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions purescript-sort-imports.el
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,9 @@

(defvar purescript-sort-imports-regexp
(concat "^import[ ]+"
"\\(qualified \\)?"
"[ ]*\\(\"[^\"]*\" \\)?"
"\\(\"[^\"]*\" \\)?"
"[ ]*\\([A-Za-z0-9_.']*.*\\)"))

;;;###autoload
(defun purescript-sort-imports ()
"Sort the import list at point. It sorts the current group
i.e. an import list separated by blank lines on either side.
Expand Down Expand Up @@ -68,9 +66,8 @@ within that region."

(defun purescript-sort-imports-normalize (i)
"Normalize an import, if possible, so that it can be sorted."
(if (string-match purescript-sort-imports-regexp
i)
(match-string 3 i)
(if (string-match purescript-sort-imports-regexp i)
(match-string 2 i)
i))

(defun purescript-sort-imports-collect-imports ()
Expand Down
106 changes: 53 additions & 53 deletions tests/purescript-sort-imports-tests.el
Original file line number Diff line number Diff line change
Expand Up @@ -21,114 +21,114 @@
(require 'purescript-sort-imports)

(ert-deftest empty-buffer ()
(should (with-temp-buffer
(purescript-sort-imports)
t)))
(with-temp-buffer
(purescript-sort-imports)
t))

(ert-deftest single-line ()
(should (with-temp-buffer
(insert "import A\n")
(goto-char (point-min))
(purescript-sort-imports)
(string= (buffer-string)
(with-temp-buffer
(insert "import A\n")
(goto-char (point-min))
(purescript-sort-imports)
(should (string= (buffer-string)
"import A\n"))))

(ert-deftest two-idem ()
(should (with-temp-buffer
(insert "import A
(with-temp-buffer
(insert "import A
import B
")
(goto-char (point-min))
(purescript-sort-imports)
(string= (buffer-string)
(goto-char (point-min))
(purescript-sort-imports)
(should (string= (buffer-string)
"import A
import B
")))
(should (with-temp-buffer
(insert "import qualified A
(with-temp-buffer
(insert "import A (A, B, C)
import B
")
(goto-char (point-min))
(purescript-sort-imports)
(string= (buffer-string)
"import qualified A
(goto-char (point-min))
(purescript-sort-imports)
(should (string= (buffer-string)
"import A (A, B, C)
import B
")))
(should (with-temp-buffer
(insert "import qualified \"mtl\" A
(with-temp-buffer
(insert "import A (mtl)
import B
")
(goto-char (point-min))
(purescript-sort-imports)
(string= (buffer-string)
"import qualified \"mtl\" A
(goto-char (point-min))
(purescript-sort-imports)
(should (string= (buffer-string)
"import A (mtl)
import B
"))))

(ert-deftest two-rev ()
(should (with-temp-buffer
(insert "import B
(with-temp-buffer
(insert "import B
import A
")
(goto-char (point-min))
(purescript-sort-imports)
(string= (buffer-string)
(goto-char (point-min))
(purescript-sort-imports)
(should (string= (buffer-string)
"import A
import B
"))))

(ert-deftest file-structure ()
(should (with-temp-buffer
(insert "module A where
(with-temp-buffer
(insert "module A where
import B
import A
")
;; test at line 2
(goto-char (point-min))
(forward-line 1)
(purescript-sort-imports)
(string= (buffer-string)
;; test at line 2
(goto-char (point-min))
(forward-line 1)
(purescript-sort-imports)
(should (string= (buffer-string)
"module A where
import A
import B
")))
(should (with-temp-buffer
(insert "module C where
(with-temp-buffer
(insert "module C where

import B
import A
")
;; test at line 3
(goto-char (point-min))
(forward-line 2)
(purescript-sort-imports)
(string= (buffer-string)
;; test at line 3
(goto-char (point-min))
(forward-line 2)
(purescript-sort-imports)
(should (string= (buffer-string)
"module C where

import A
import B
"))))

(ert-deftest bos-270 ()
(should (with-temp-buffer
(insert "import Data.Aeson.Encode (encode)
(with-temp-buffer
(insert "import Data.Aeson.Encode (encode)
import Data.Aeson.Types
import Data.Aeson.Parser.Internal (decodeWith, decodeStrictWith,
eitherDecodeWith, eitherDecodeStrictWith,
jsonEOF, json, jsonEOF', json')
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as L
import Data.ByteString as B
import Data.ByteString.Lazy as L
")
(goto-char (point-min))
(purescript-sort-imports)
(string= (buffer-string)
(goto-char (point-min))
(purescript-sort-imports)
(should (string= (buffer-string)
"import Data.Aeson.Encode (encode)
import Data.Aeson.Parser.Internal (decodeWith, decodeStrictWith,
eitherDecodeWith, eitherDecodeStrictWith,
jsonEOF, json, jsonEOF', json')
import Data.Aeson.Types
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as L
import Data.ByteString as B
import Data.ByteString.Lazy as L
"))))

(provide 'purescript-sort-imports-tests)