From 74550a9413b8c6c7d8916748ad2569ba88ff75bf Mon Sep 17 00:00:00 2001 From: p4v4n Date: Mon, 6 Mar 2023 23:28:49 +0530 Subject: [PATCH 1/5] Fix clojure-sort-ns with comments in the end - closes #645 --- clojure-mode.el | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/clojure-mode.el b/clojure-mode.el index 86ff81c5..d5185715 100644 --- a/clojure-mode.el +++ b/clojure-mode.el @@ -2029,7 +2029,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:]")) From b53c714931821f41e04554e5f4cb863f9db9e7e8 Mon Sep 17 00:00:00 2001 From: p4v4n Date: Tue, 7 Mar 2023 00:52:52 +0530 Subject: [PATCH 2/5] Add another test for clojure-sort-ns --- test/clojure-mode-util-test.el | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/clojure-mode-util-test.el b/test/clojure-mode-util-test.el index bf17d02a..5a0d8f3a 100644 --- a/test/clojure-mode-util-test.el +++ b/test/clojure-mode-util-test.el @@ -138,6 +138,18 @@ (:require [my-app.views [user-page :as user-page]] [rum.core :as rum] ;comment\n))"))) + (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]\n))" + (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\n + ;;[comment2]\n))"))) + (it "should also sort imports in a ns" (with-clojure-buffer "\n(ns my-app.core (:require [my-app.views [front-page :as front-page]] From 4c81348628d9089b47fe870ab2efb9c777582b01 Mon Sep 17 00:00:00 2001 From: p4v4n Date: Sat, 24 Jun 2023 23:05:11 +0530 Subject: [PATCH 3/5] Add more tests for clojure-sort-ns --- test/clojure-mode-util-test.el | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/test/clojure-mode-util-test.el b/test/clojure-mode-util-test.el index 5a0d8f3a..81933a2f 100644 --- a/test/clojure-mode-util-test.el +++ b/test/clojure-mode-util-test.el @@ -149,6 +149,31 @@ (:require [my-app.views [user-page :as user-page]] [rum.core :as rum] ;comment\n ;;[comment2]\n))"))) + (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\n + ;; A last comment + ))"))) (it "should also sort imports in a ns" (with-clojure-buffer "\n(ns my-app.core From 4e5cc6bf287301aabc86f6f631cff2adc1ff1303 Mon Sep 17 00:00:00 2001 From: p4v4n Date: Sat, 24 Jun 2023 23:12:30 +0530 Subject: [PATCH 4/5] Replace \n with linebreaks --- test/clojure-mode-util-test.el | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/test/clojure-mode-util-test.el b/test/clojure-mode-util-test.el index 81933a2f..353f0e5e 100644 --- a/test/clojure-mode-util-test.el +++ b/test/clojure-mode-util-test.el @@ -136,19 +136,23 @@ (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]\n))" + ;;[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\n - ;;[comment2]\n))"))) + [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 @@ -171,7 +175,8 @@ ;; Middle comment [bar] ;; bar comment ;; The first comment - [foo] ;; foo comment\n + [foo] ;; foo comment + ;; A last comment ))"))) From 3d4fc8e832240864945698ec89557ea0c2cdb574 Mon Sep 17 00:00:00 2001 From: p4v4n Date: Sat, 24 Jun 2023 23:48:47 +0530 Subject: [PATCH 5/5] Update Changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) 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)