Skip to content

Commit 2d5456d

Browse files
authored
[Fix #551] Indent clojure-align region before aligning (#552)
This prevents issues when aligning improperly indented code. Before, the region would be aligned while retaining the improper indentation. The follow-up indentation step would then break the alignment that was just performed. With this commit, we switch the order by indenting first then aligning. This retains compatibility with the test cases in #360.
1 parent 64ed043 commit 2d5456d

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### Bugs fixed
66

7+
* [#551](https://github.com/clojure-emacs/clojure-mode/issues/551): Indent `clojure-align` region before aligning.
78
* [#520](https://github.com/clojure-emacs/clojure-mode/issues/508): Fix allow `clojure-align-cond-forms` to recognize qualified forms.
89
* Enhance add arity refactoring to support a defn inside a reader conditional.
910
* [#404](https://github.com/clojure-emacs/clojure-mode/issues/404)/[#528]((https://github.com/clojure-emacs/clojure-mode/issues/528)) Fix syntax highlighting for multiple consecutive comment reader macros (`#_#_`)

clojure-mode.el

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,15 +1268,16 @@ When called from lisp code align everything between BEG and END."
12681268
(save-excursion
12691269
(while (search-forward-regexp "^ *\n" sexp-end 'noerror)
12701270
(cl-incf count)))
1271+
;; Pre-indent the region to avoid aligning to improperly indented
1272+
;; contents (#551). Also fixes #360.
1273+
(indent-region (point) sexp-end)
12711274
(dotimes (_ count)
12721275
(align-region (point) sexp-end nil
12731276
`((clojure-align (regexp . clojure--search-whitespace-after-next-sexp)
12741277
(group . 1)
12751278
(separate . ,clojure-align-separator)
12761279
(repeat . t)))
1277-
nil))
1278-
;; Reindent after aligning because of #360.
1279-
(indent-region (point) sexp-end)))))
1280+
nil))))))
12801281

12811282
;;; Indentation
12821283
(defun clojure-indent-region (beg end)

test/clojure-mode-indentation-test.el

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,13 @@ x
659659
"#?@(:clj [2]
660660
:cljs [2])")
661661

662+
(it "should handle improperly indented content"
663+
(let ((content "(let [a-long-name 10\nb 20])")
664+
(aligned-content "(let [a-long-name 10\n b 20])"))
665+
(with-clojure-buffer content
666+
(call-interactively #'clojure-align)
667+
(expect (buffer-string) :to-equal aligned-content))))
668+
662669
(it "should not align reader conditionals by default"
663670
(let ((content "#?(:clj 2\n :cljs 2)"))
664671
(with-clojure-buffer content

0 commit comments

Comments
 (0)