Skip to content

Commit 23f7742

Browse files
committed
[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.
1 parent 876ef22 commit 23f7742

3 files changed

Lines changed: 31 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
* [#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.
2222
* [#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)`).
2323
* [#365](https://github.com/clojure-emacs/clojure-mode/issues/365): Font-lock function names in `letfn` bindings with `font-lock-function-name-face`.
24+
* [#527](https://github.com/clojure-emacs/clojure-mode/issues/527): Fix `clojure-sort-ns` mangling `:gen-class` and other non-sortable ns forms.
2425
* Fix typos in `clojure-mode-extra-font-locking`: `halt-when?` -> `halt-when`, `simple-indent?` -> `simple-ident?`.
2526
* Fix `doc` and `find-doc` misplaced under `clojure.core` instead of `clojure.repl` in extra font-locking.
2627

clojure-mode.el

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2262,12 +2262,17 @@ content) are considered part of the preceding sexp."
22622262
(forward-sexp 1)
22632263
(setq ns (buffer-substring beg (point)))
22642264
(forward-char -1)
2265+
;; Walk backwards through keyword forms. The `while' loop
2266+
;; uses a broad match so it steps over ALL keyword forms
2267+
;; (including non-sortable ones like :gen-class), while
2268+
;; `when' only sorts forms with known sortable contents.
22652269
(while (progn (forward-sexp -1)
22662270
(looking-at "(:[a-z]"))
2267-
(save-excursion
2268-
(forward-char 1)
2269-
(forward-sexp 1)
2270-
(clojure--sort-following-sexps)))
2271+
(when (looking-at "(:\\(?:require\\|import\\|use\\|refer-clojure\\|require-macros\\)\\>")
2272+
(save-excursion
2273+
(forward-char 1)
2274+
(forward-sexp 1)
2275+
(clojure--sort-following-sexps))))
22712276
(goto-char beg)
22722277
(if (looking-at (regexp-quote ns))
22732278
(message "ns form is already sorted")

test/clojure-mode-util-test.el

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,27 @@
279279
;; Some comments.
280280
[rum.core :as rum])
281281
(:import [clojure.lang AFunction Atom MultiFn Namespace]
282-
java.io.Writer))"))))
282+
java.io.Writer))")))
283+
284+
(it "should not mangle :gen-class"
285+
(with-clojure-buffer "(ns foo
286+
(:require [c]
287+
[a]
288+
[b])
289+
(:gen-class
290+
:methods [[methodOne [] String]
291+
[methodTwo [] String]]
292+
:init init))"
293+
(clojure-sort-ns)
294+
(expect (buffer-string) :to-equal
295+
"(ns foo
296+
(:require [a]
297+
[b]
298+
[c])
299+
(:gen-class
300+
:methods [[methodOne [] String]
301+
[methodTwo [] String]]
302+
:init init))"))))
283303

284304
(describe "clojure-toggle-ignore"
285305
(when-refactoring-with-point-it "should add #_ to literals"

0 commit comments

Comments
 (0)