diff --git a/CHANGELOG.md b/CHANGELOG.md index 30988889..1758cb4f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/clojure-mode.el b/clojure-mode.el index 320eca1c..aac6c8b2 100644 --- a/clojure-mode.el +++ b/clojure-mode.el @@ -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:]")) diff --git a/test/clojure-mode-util-test.el b/test/clojure-mode-util-test.el index bf17d02a..353f0e5e 100644 --- a/test/clojure-mode-util-test.el +++ b/test/clojure-mode-util-test.el @@ -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