Skip to content

Commit f5b85ca

Browse files
authored
Fix clojure-sort-ns with comments in the end (#646)
Closes #645
1 parent 47ce793 commit f5b85ca

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
### Bugs fixed
1010

11+
* [#645](https://github.com/clojure-emacs/clojure-mode/issues/645): Fix infinite loop when sorting a ns with comments in the end.
1112
* [#586](https://github.com/clojure-emacs/clojure-mode/issues/586): Fix infinite loop when opening file containing `comment` with `clojure-toplevel-inside-comment-form` set to `t`.
1213

1314
## 5.16.0 (2022-12-14)

clojure-mode.el

+7-1
Original file line numberDiff line numberDiff line change
@@ -2030,7 +2030,13 @@ content) are considered part of the preceding sexp."
20302030
(save-restriction
20312031
(narrow-to-region (point) (save-excursion
20322032
(up-list)
2033-
(1- (point))))
2033+
;; Ignore any comments in the end before sorting
2034+
(backward-char)
2035+
(forward-sexp -1)
2036+
(clojure-forward-logical-sexp)
2037+
(unless (looking-at-p ")")
2038+
(search-forward-regexp "$"))
2039+
(point)))
20342040
(skip-chars-forward "\r\n[:blank:]")
20352041
(sort-subr nil
20362042
(lambda () (skip-chars-forward "\r\n[:blank:]"))

test/clojure-mode-util-test.el

+43-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,49 @@
136136
(expect (buffer-string) :to-equal
137137
"(ns my-app.core
138138
(:require [my-app.views [user-page :as user-page]]
139-
[rum.core :as rum] ;comment\n))")))
139+
[rum.core :as rum] ;comment
140+
))")))
141+
142+
(it "should sort requires in a basic ns with comments in the end"
143+
(with-clojure-buffer "(ns my-app.core
144+
(:require [rum.core :as rum] ;comment
145+
[my-app.views [user-page :as user-page]]
146+
;;[comment2]
147+
))"
148+
(clojure-sort-ns)
149+
(expect (buffer-string) :to-equal
150+
"(ns my-app.core
151+
(:require [my-app.views [user-page :as user-page]]
152+
[rum.core :as rum] ;comment
153+
154+
;;[comment2]
155+
))")))
156+
(it "should sort requires in ns with copyright disclamer and comments"
157+
(with-clojure-buffer ";; Copyright (c) John Doe. All rights reserved.
158+
;; The use and distribution terms for this software are covered by the
159+
;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
160+
(ns clojure.core
161+
(:require
162+
;; The first comment
163+
[foo] ;; foo comment
164+
;; Middle comment
165+
[bar] ;; bar comment
166+
;; A last comment
167+
))"
168+
(clojure-sort-ns)
169+
(expect (buffer-string) :to-equal
170+
";; Copyright (c) John Doe. All rights reserved.
171+
;; The use and distribution terms for this software are covered by the
172+
;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
173+
(ns clojure.core
174+
(:require
175+
;; Middle comment
176+
[bar] ;; bar comment
177+
;; The first comment
178+
[foo] ;; foo comment
179+
180+
;; A last comment
181+
))")))
140182

141183
(it "should also sort imports in a ns"
142184
(with-clojure-buffer "\n(ns my-app.core

0 commit comments

Comments
 (0)