Skip to content

Commit 51016fa

Browse files
sulamibbatsov
authored andcommitted
Support multiple consecutive reader comments (#_#_a b)
Modified the default regexps and the heuristic to find the end of the region to comment out. Previously Emacs would treat the second `#_` as the commented form, and then highlight the following two forms as usual. Now it (mostly) matches what Clojure actually evaluates. Things get weird when you start mixing `#_` and forms, but this fixes the most common use cases, like key-value-pairs.
1 parent 147bf84 commit 51016fa

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

CHANGELOG.md

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

77
* [#520](https://github.com/clojure-emacs/clojure-mode/issues/508): Fix allow `clojure-align-cond-forms` to recognize qualified forms.
88
* Enhance add arity refactoring to support a defn inside a reader conditional.
9+
* [#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 (`#_#_`)
910

1011
### Changes
1112

clojure-mode.el

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -654,14 +654,16 @@ If JUSTIFY is non-nil, justify as well as fill the paragraph."
654654
;; Code heavily borrowed from Slime.
655655
;; https://github.com/slime/slime/blob/master/contrib/slime-fontifying-fu.el#L186
656656
(defvar clojure--comment-macro-regexp
657-
(rx "#_" (* " ") (group-n 1 (not (any " "))))
657+
(rx (seq (+ (seq "#_" (* " ")))) (group-n 1 (not (any " "))))
658658
"Regexp matching the start of a comment sexp.
659659
The beginning of match-group 1 should be before the sexp to be
660660
marked as a comment. The end of sexp is found with
661661
`clojure-forward-logical-sexp'.")
662662

663663
(defvar clojure--reader-and-comment-regexp
664-
"#_ *\\(?1:[^ ]\\)\\|\\(?1:(comment\\_>\\)"
664+
(rx (or (seq (+ (seq "#_" (* " ")))
665+
(group-n 1 (not (any " "))))
666+
(seq (group-n 1 "(comment" symbol-end))))
665667
"Regexp matching both `#_' macro and a comment sexp." )
666668

667669
(defcustom clojure-comment-regexp clojure--comment-macro-regexp
@@ -687,7 +689,11 @@ what is considered a comment (affecting font locking).
687689
(nth 4 state))
688690
(clojure--search-comment-macro-internal limit)
689691
(goto-char start)
690-
(clojure-forward-logical-sexp 1)
692+
;; Count how many #_ we got and step by that many sexps
693+
;; For (comment ...), step at least 1 sexp
694+
(clojure-forward-logical-sexp
695+
(max (count-matches (rx "#_") (elt md 0) (elt md 1))
696+
1))
691697
;; Data for (match-end 1).
692698
(setf (elt md 3) (point))
693699
(set-match-data md)

test/clojure-mode-font-lock-test.el

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,35 @@ DESCRIPTION is the description of the spec."
133133
("#_"
134134
(1 2 nil))
135135

136+
("#_#_"
137+
(1 2 nil))
138+
139+
("#_#_"
140+
(3 2 font-lock-comment-face))
141+
142+
("#_ #_"
143+
(1 3 nil))
144+
145+
("#_ #_"
146+
(4 2 font-lock-comment-face))
147+
136148
("#_ \n;; some crap\n (lala 0101\n lao\n\n 0 0i)"
137149
(1 2 nil))
138150

139151
("#_ \n;; some crap\n (lala 0101\n lao\n\n 0 0i)"
140-
(5 41 font-lock-comment-face)))
152+
(5 41 font-lock-comment-face))
153+
154+
("#_#_ \n;; some crap\n (lala 0101\n lao\n\n 0 0i)\n;; more crap\n (foobar tnseriao)"
155+
(1 4 nil))
156+
157+
("#_ #_ \n;; some crap\n (lala 0101\n lao\n\n 0 0i)\n;; more crap\n (foobar tnseriao)"
158+
(1 5 nil))
159+
160+
("#_#_ \n;; some crap\n (lala 0101\n lao\n\n 0 0i)\n;; more crap\n (foobar tnseriao)"
161+
(7 75 font-lock-comment-face))
162+
163+
("#_ #_ \n;; some crap\n (lala 0101\n lao\n\n 0 0i)\n;; more crap\n (foobar tnseriao)"
164+
(8 75 font-lock-comment-face)))
141165

142166
(when-fontifying-it "should handle namespace declarations"
143167
("(ns .validns)"

0 commit comments

Comments
 (0)