Skip to content

Commit eb2ec99

Browse files
committed
haskell-sort-imports: do not modify buffer if imports are already sorted
1 parent 64bd44f commit eb2ec99

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

haskell-sort-imports.el

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,19 @@ within that region."
5353
(progn (goto-char (region-beginning))
5454
(haskell-sort-imports-goto-import-start))
5555
(haskell-sort-imports-goto-group-start))
56-
(let ((start (point))
57-
(imports (haskell-sort-imports-collect-imports)))
58-
(delete-region start (point))
59-
(mapc (lambda (import)
60-
(insert import "\n"))
61-
(sort imports (lambda (a b)
62-
(string< (haskell-sort-imports-normalize a)
63-
(haskell-sort-imports-normalize b)))))
64-
(goto-char start)
65-
(when (search-forward current-string nil t 1)
66-
(forward-char (- (length current-string)))
67-
(forward-char current-offset))))))
56+
(let* ((start (point))
57+
(imports (haskell-sort-imports-collect-imports))
58+
(sorted (sort (copy-list imports)
59+
(lambda (a b)
60+
(string< (haskell-sort-imports-normalize a)
61+
(haskell-sort-imports-normalize b))))))
62+
(when (not (equal imports sorted))
63+
(delete-region start (point))
64+
(mapc (lambda (import) (insert import "\n")) sorted))
65+
(goto-char start)
66+
(when (search-forward current-string nil t 1)
67+
(forward-char (- (length current-string)))
68+
(forward-char current-offset))))))
6869

6970
(defun haskell-sort-imports-normalize (i)
7071
"Normalize an import, if possible, so that it can be sorted."

0 commit comments

Comments
 (0)