Skip to content

Commit fbdc679

Browse files
committed
Sort on just module names (#270)
1 parent 8f2a7e0 commit fbdc679

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

haskell-sort-imports.el

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@
3030

3131
;;; Code:
3232

33+
(defvar haskell-sort-imports-regexp
34+
(concat "^import[ ]+"
35+
"\\(qualified \\)?"
36+
"[ ]*\\(\"[^\"]*\" \\)?"
37+
"[ ]*\\([A-Za-z0-9_.']*.*\\)"))
38+
3339
;;;###autoload
3440
(defun haskell-sort-imports ()
3541
(interactive)
@@ -52,12 +58,21 @@ within that region."
5258
(delete-region start (point))
5359
(mapc (lambda (import)
5460
(insert import "\n"))
55-
(sort imports #'string<))
61+
(sort imports (lambda (a b)
62+
(string< (haskell-sort-imports-normalize a)
63+
(haskell-sort-imports-normalize b)))))
5664
(goto-char start)
5765
(when (search-forward current-string nil t 1)
5866
(forward-char (- (length current-string)))
5967
(forward-char current-offset))))))
6068

69+
(defun haskell-sort-imports-normalize (i)
70+
"Normalize an import, if possible, so that it can be sorted."
71+
(if (string-match haskell-sort-imports-regexp
72+
i)
73+
(match-string 3 i)
74+
i))
75+
6176
(defun haskell-sort-imports-collect-imports ()
6277
(let ((imports (list)))
6378
(while (looking-at "import")

0 commit comments

Comments
 (0)