Skip to content

Commit ae7b21c

Browse files
authored
Merge pull request #33 from Hi-Angel/fix-sorting-import
Improve algorithm for imports sorting and remove an autoload
2 parents f310fbe + 4c2d188 commit ae7b21c

File tree

2 files changed

+56
-59
lines changed

2 files changed

+56
-59
lines changed

purescript-sort-imports.el

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,9 @@
3232

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

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

6967
(defun purescript-sort-imports-normalize (i)
7068
"Normalize an import, if possible, so that it can be sorted."
71-
(if (string-match purescript-sort-imports-regexp
72-
i)
73-
(match-string 3 i)
69+
(if (string-match purescript-sort-imports-regexp i)
70+
(match-string 2 i)
7471
i))
7572

7673
(defun purescript-sort-imports-collect-imports ()

tests/purescript-sort-imports-tests.el

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -21,114 +21,114 @@
2121
(require 'purescript-sort-imports)
2222

2323
(ert-deftest empty-buffer ()
24-
(should (with-temp-buffer
25-
(purescript-sort-imports)
26-
t)))
24+
(with-temp-buffer
25+
(purescript-sort-imports)
26+
t))
2727

2828
(ert-deftest single-line ()
29-
(should (with-temp-buffer
30-
(insert "import A\n")
31-
(goto-char (point-min))
32-
(purescript-sort-imports)
33-
(string= (buffer-string)
29+
(with-temp-buffer
30+
(insert "import A\n")
31+
(goto-char (point-min))
32+
(purescript-sort-imports)
33+
(should (string= (buffer-string)
3434
"import A\n"))))
3535

3636
(ert-deftest two-idem ()
37-
(should (with-temp-buffer
38-
(insert "import A
37+
(with-temp-buffer
38+
(insert "import A
3939
import B
4040
")
41-
(goto-char (point-min))
42-
(purescript-sort-imports)
43-
(string= (buffer-string)
41+
(goto-char (point-min))
42+
(purescript-sort-imports)
43+
(should (string= (buffer-string)
4444
"import A
4545
import B
4646
")))
47-
(should (with-temp-buffer
48-
(insert "import qualified A
47+
(with-temp-buffer
48+
(insert "import A (A, B, C)
4949
import B
5050
")
51-
(goto-char (point-min))
52-
(purescript-sort-imports)
53-
(string= (buffer-string)
54-
"import qualified A
51+
(goto-char (point-min))
52+
(purescript-sort-imports)
53+
(should (string= (buffer-string)
54+
"import A (A, B, C)
5555
import B
5656
")))
57-
(should (with-temp-buffer
58-
(insert "import qualified \"mtl\" A
57+
(with-temp-buffer
58+
(insert "import A (mtl)
5959
import B
6060
")
61-
(goto-char (point-min))
62-
(purescript-sort-imports)
63-
(string= (buffer-string)
64-
"import qualified \"mtl\" A
61+
(goto-char (point-min))
62+
(purescript-sort-imports)
63+
(should (string= (buffer-string)
64+
"import A (mtl)
6565
import B
6666
"))))
6767

6868
(ert-deftest two-rev ()
69-
(should (with-temp-buffer
70-
(insert "import B
69+
(with-temp-buffer
70+
(insert "import B
7171
import A
7272
")
73-
(goto-char (point-min))
74-
(purescript-sort-imports)
75-
(string= (buffer-string)
73+
(goto-char (point-min))
74+
(purescript-sort-imports)
75+
(should (string= (buffer-string)
7676
"import A
7777
import B
7878
"))))
7979

8080
(ert-deftest file-structure ()
81-
(should (with-temp-buffer
82-
(insert "module A where
81+
(with-temp-buffer
82+
(insert "module A where
8383
import B
8484
import A
8585
")
86-
;; test at line 2
87-
(goto-char (point-min))
88-
(forward-line 1)
89-
(purescript-sort-imports)
90-
(string= (buffer-string)
86+
;; test at line 2
87+
(goto-char (point-min))
88+
(forward-line 1)
89+
(purescript-sort-imports)
90+
(should (string= (buffer-string)
9191
"module A where
9292
import A
9393
import B
9494
")))
95-
(should (with-temp-buffer
96-
(insert "module C where
95+
(with-temp-buffer
96+
(insert "module C where
9797
9898
import B
9999
import A
100100
")
101-
;; test at line 3
102-
(goto-char (point-min))
103-
(forward-line 2)
104-
(purescript-sort-imports)
105-
(string= (buffer-string)
101+
;; test at line 3
102+
(goto-char (point-min))
103+
(forward-line 2)
104+
(purescript-sort-imports)
105+
(should (string= (buffer-string)
106106
"module C where
107107
108108
import A
109109
import B
110110
"))))
111111

112112
(ert-deftest bos-270 ()
113-
(should (with-temp-buffer
114-
(insert "import Data.Aeson.Encode (encode)
113+
(with-temp-buffer
114+
(insert "import Data.Aeson.Encode (encode)
115115
import Data.Aeson.Types
116116
import Data.Aeson.Parser.Internal (decodeWith, decodeStrictWith,
117117
eitherDecodeWith, eitherDecodeStrictWith,
118118
jsonEOF, json, jsonEOF', json')
119-
import qualified Data.ByteString as B
120-
import qualified Data.ByteString.Lazy as L
119+
import Data.ByteString as B
120+
import Data.ByteString.Lazy as L
121121
")
122-
(goto-char (point-min))
123-
(purescript-sort-imports)
124-
(string= (buffer-string)
122+
(goto-char (point-min))
123+
(purescript-sort-imports)
124+
(should (string= (buffer-string)
125125
"import Data.Aeson.Encode (encode)
126126
import Data.Aeson.Parser.Internal (decodeWith, decodeStrictWith,
127127
eitherDecodeWith, eitherDecodeStrictWith,
128128
jsonEOF, json, jsonEOF', json')
129129
import Data.Aeson.Types
130-
import qualified Data.ByteString as B
131-
import qualified Data.ByteString.Lazy as L
130+
import Data.ByteString as B
131+
import Data.ByteString.Lazy as L
132132
"))))
133133

134134
(provide 'purescript-sort-imports-tests)

0 commit comments

Comments
 (0)