branch: elpa/clojure-mode
commit 23f774271d389beec5bf82342f64a400c710802b
Author: Bozhidar Batsov <[email protected]>
Commit: Bozhidar Batsov <[email protected]>

    [Fix #527] Fix clojure-sort-ns mangling :gen-class
    
    Only sort ns sub-forms that have sortable content (:require, :import,
    :use, :refer-clojure, :require-macros). Other keyword forms like
    :gen-class are skipped rather than having their contents rearranged.
---
 CHANGELOG.md                   |  1 +
 clojure-mode.el                | 13 +++++++++----
 test/clojure-mode-util-test.el | 22 +++++++++++++++++++++-
 3 files changed, 31 insertions(+), 5 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index fcf83c31f0..4d7f0f872d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -21,6 +21,7 @@
 * [#649](https://github.com/clojure-emacs/clojure-mode/issues/649): Fix 
`clojure-add-arity` severing arglist metadata (`^String`, `^:keyword`, 
`^{...}`) when converting single-arity to multi-arity.
 * [#600](https://github.com/clojure-emacs/clojure-mode/issues/600): Fix 
`clojure--valid-put-clojure-indent-call-p` rejecting valid indent specs with 
nested lists (e.g. `letfn`'s `(1 ((:defn)) nil)`).
 * [#365](https://github.com/clojure-emacs/clojure-mode/issues/365): Font-lock 
function names in `letfn` bindings with `font-lock-function-name-face`.
+* [#527](https://github.com/clojure-emacs/clojure-mode/issues/527): Fix 
`clojure-sort-ns` mangling `:gen-class` and other non-sortable ns forms.
 * Fix typos in `clojure-mode-extra-font-locking`: `halt-when?` -> `halt-when`, 
`simple-indent?` -> `simple-ident?`.
 * Fix `doc` and `find-doc` misplaced under `clojure.core` instead of 
`clojure.repl` in extra font-locking.
 
diff --git a/clojure-mode.el b/clojure-mode.el
index b04cb770b0..02ed25063e 100644
--- a/clojure-mode.el
+++ b/clojure-mode.el
@@ -2262,12 +2262,17 @@ content) are considered part of the preceding sexp."
           (forward-sexp 1)
           (setq ns (buffer-substring beg (point)))
           (forward-char -1)
+          ;; Walk backwards through keyword forms.  The `while' loop
+          ;; uses a broad match so it steps over ALL keyword forms
+          ;; (including non-sortable ones like :gen-class), while
+          ;; `when' only sorts forms with known sortable contents.
           (while (progn (forward-sexp -1)
                         (looking-at "(:[a-z]"))
-            (save-excursion
-              (forward-char 1)
-              (forward-sexp 1)
-              (clojure--sort-following-sexps)))
+            (when (looking-at 
"(:\\(?:require\\|import\\|use\\|refer-clojure\\|require-macros\\)\\>")
+              (save-excursion
+                (forward-char 1)
+                (forward-sexp 1)
+                (clojure--sort-following-sexps))))
           (goto-char beg)
           (if (looking-at (regexp-quote ns))
               (message "ns form is already sorted")
diff --git a/test/clojure-mode-util-test.el b/test/clojure-mode-util-test.el
index b0f7099d1c..bd5033506a 100644
--- a/test/clojure-mode-util-test.el
+++ b/test/clojure-mode-util-test.el
@@ -279,7 +279,27 @@
               ;; Some comments.
               [rum.core :as rum])
     (:import [clojure.lang AFunction Atom MultiFn Namespace]
-             java.io.Writer))"))))
+             java.io.Writer))")))
+
+  (it "should not mangle :gen-class"
+    (with-clojure-buffer "(ns foo
+  (:require [c]
+            [a]
+            [b])
+  (:gen-class
+   :methods [[methodOne [] String]
+             [methodTwo [] String]]
+   :init init))"
+      (clojure-sort-ns)
+      (expect (buffer-string) :to-equal
+              "(ns foo
+  (:require [a]
+            [b]
+            [c])
+  (:gen-class
+   :methods [[methodOne [] String]
+             [methodTwo [] String]]
+   :init init))"))))
 
 (describe "clojure-toggle-ignore"
   (when-refactoring-with-point-it "should add #_ to literals"

Reply via email to