Skip to content

Commit f3fe881

Browse files
committed
[Fix #608] Fix alignment issue involving margin comments.
Alignment wasn't working properly when nested, multi-line sexps were followed by margin comments.
1 parent e311868 commit f3fe881

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
* [#588](https://github.com/clojure-emacs/clojure-mode/pull/588): Fix font-lock for character literals.
2626
* Stop `clojure-sort-ns` from calling `redisplay`.
27+
* [#608](https://github.com/clojure-emacs/clojure-mode/issues/608) Fix alignment issue involving margin comments at the end of nested forms.
2728

2829
### Changes
2930

clojure-mode.el

+5-1
Original file line numberDiff line numberDiff line change
@@ -1252,6 +1252,9 @@ Place point as in `clojure--position-for-alignment'."
12521252

12531253
(defun clojure--search-whitespace-after-next-sexp (&optional bound _noerror)
12541254
"Move point after all whitespace after the next sexp.
1255+
Additionally, move past a comment if one exists (this is only
1256+
possible when the end of the sexp coincides with the end of a
1257+
line).
12551258
12561259
Set the match data group 1 to be this region of whitespace and
12571260
return point.
@@ -1260,7 +1263,8 @@ BOUND is bounds the whitespace search."
12601263
(unwind-protect
12611264
(ignore-errors
12621265
(clojure-forward-logical-sexp 1)
1263-
(search-forward-regexp "\\([,\s\t]*\\)" bound)
1266+
;; Move past any whitespace or comment.
1267+
(search-forward-regexp "\\([,\s\t]*\\)\\(;+.*\\)?" bound)
12641268
(pcase (syntax-after (point))
12651269
;; End-of-line, try again on next line.
12661270
(`(12) (clojure--search-whitespace-after-next-sexp bound))

test/clojure-mode-indentation-test.el

+37
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,43 @@ x
705705
"#?@(:clj [2]
706706
:cljs [2])")
707707

708+
(when-aligning-it "should handle sexps broken up by line comments"
709+
"
710+
(let [x 1
711+
;; comment
712+
xx 1]
713+
xx)"
714+
715+
"
716+
{:x 1
717+
;; comment
718+
:xxx 2}"
719+
720+
"
721+
(case x
722+
:aa 1
723+
;; comment
724+
:a 2)")
725+
726+
(when-aligning-it "should work correctly when margin comments appear after nested, multi-line, non-terminal sexps"
727+
"
728+
(let [x {:a 1
729+
:b 2} ; comment
730+
xx 3]
731+
x)"
732+
733+
"
734+
{:aa {:b 1
735+
:cc 2} ;; comment
736+
:a 1}}"
737+
738+
"
739+
(case x
740+
:a (let [a 1
741+
aa (+ a 1)]
742+
aa); comment
743+
:aa 2)")
744+
708745
(it "should handle improperly indented content"
709746
(let ((content "(let [a-long-name 10\nb 20])")
710747
(aligned-content "(let [a-long-name 10\n b 20])"))

0 commit comments

Comments
 (0)