Skip to content

Commit

Permalink
Fix clojure-sort-ns with comments in the end (#646)
Browse files Browse the repository at this point in the history
Closes #645
  • Loading branch information
p4v4n authored Jun 24, 2023
1 parent 47ce793 commit f5b85ca
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

### Bugs fixed

* [#645](https://github.com/clojure-emacs/clojure-mode/issues/645): Fix infinite loop when sorting a ns with comments in the end.
* [#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`.

## 5.16.0 (2022-12-14)
Expand Down
8 changes: 7 additions & 1 deletion clojure-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -2030,7 +2030,13 @@ content) are considered part of the preceding sexp."
(save-restriction
(narrow-to-region (point) (save-excursion
(up-list)
(1- (point))))
;; Ignore any comments in the end before sorting
(backward-char)
(forward-sexp -1)
(clojure-forward-logical-sexp)
(unless (looking-at-p ")")
(search-forward-regexp "$"))
(point)))
(skip-chars-forward "\r\n[:blank:]")
(sort-subr nil
(lambda () (skip-chars-forward "\r\n[:blank:]"))
Expand Down
44 changes: 43 additions & 1 deletion test/clojure-mode-util-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,49 @@
(expect (buffer-string) :to-equal
"(ns my-app.core
(:require [my-app.views [user-page :as user-page]]
[rum.core :as rum] ;comment\n))")))
[rum.core :as rum] ;comment
))")))

(it "should sort requires in a basic ns with comments in the end"
(with-clojure-buffer "(ns my-app.core
(:require [rum.core :as rum] ;comment
[my-app.views [user-page :as user-page]]
;;[comment2]
))"
(clojure-sort-ns)
(expect (buffer-string) :to-equal
"(ns my-app.core
(:require [my-app.views [user-page :as user-page]]
[rum.core :as rum] ;comment
;;[comment2]
))")))
(it "should sort requires in ns with copyright disclamer and comments"
(with-clojure-buffer ";; Copyright (c) John Doe. All rights reserved.
;; The use and distribution terms for this software are covered by the
;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
(ns clojure.core
(:require
;; The first comment
[foo] ;; foo comment
;; Middle comment
[bar] ;; bar comment
;; A last comment
))"
(clojure-sort-ns)
(expect (buffer-string) :to-equal
";; Copyright (c) John Doe. All rights reserved.
;; The use and distribution terms for this software are covered by the
;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
(ns clojure.core
(:require
;; Middle comment
[bar] ;; bar comment
;; The first comment
[foo] ;; foo comment
;; A last comment
))")))

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

0 comments on commit f5b85ca

Please sign in to comment.