Skip to content

Commit 79b9d64

Browse files
vspinubbatsov
authored andcommitted
[Fix #2040] Fix fontification of conditional expressions in cljc files
1 parent 388133c commit 79b9d64

File tree

2 files changed

+35
-28
lines changed

2 files changed

+35
-28
lines changed

cider-mode.el

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -454,15 +454,13 @@ The value can also be t, which means to font-lock as much as possible."
454454

455455
(defconst cider-reader-conditionals-regexp "\\(?:#\\?@?[[:space:]\n]*(\\)"
456456
"Regexp for matching reader conditionals with a non-capturing group.
457-
458457
Starts from the reader macro characters to the opening parentheses.")
459458

460459
(defvar cider--reader-conditionals-match-data (list nil nil)
461460
"Reusable list for `match-data` in reader conditionals font lock matchers.")
462461

463462
(defun cider--search-reader-conditionals (limit)
464463
"Matcher for finding reader conditionals.
465-
466464
Search is done with the given LIMIT."
467465
(when (and cider-font-lock-reader-conditionals
468466
(cider-connected-p))
@@ -481,25 +479,26 @@ Search is done with the given LIMIT."
481479

482480
(defun cider--anchored-search-suppressed-forms-internal (limit)
483481
"Helper function for `cider--anchored-search-suppressed-forms`.
484-
485482
LIMIT is the same as the LIMIT in `cider--anchored-search-suppressed-forms`"
486-
(let ((expr (read (current-buffer)))
487-
(start (save-excursion (backward-sexp) (point))))
488-
(when (<= (point) limit)
489-
(forward-sexp)
490-
(if (not (string-equal (symbol-name expr) (concat ":" (cider-connection-type-for-buffer))))
491-
(ignore-errors
492-
(cl-assert (<= (point) limit))
493-
(let ((md (match-data nil cider--reader-conditionals-match-data)))
494-
(setf (nth 0 md) start)
495-
(setf (nth 1 md) (point))
496-
(set-match-data md)
497-
t))
498-
(cider--anchored-search-suppressed-forms-internal limit)))))
483+
(let ((types (cider-project-connections-types)))
484+
(when (= (length types) 1)
485+
(let ((type (car types))
486+
(expr (read (current-buffer)))
487+
(start (save-excursion (backward-sexp) (point))))
488+
(when (<= (point) limit)
489+
(forward-sexp)
490+
(if (not (string-equal (symbol-name expr) (concat ":" type)))
491+
(ignore-errors
492+
(cl-assert (<= (point) limit))
493+
(let ((md (match-data nil cider--reader-conditionals-match-data)))
494+
(setf (nth 0 md) start)
495+
(setf (nth 1 md) (point))
496+
(set-match-data md)
497+
t))
498+
(cider--anchored-search-suppressed-forms-internal limit)))))))
499499

500500
(defun cider--anchored-search-suppressed-forms (limit)
501501
"Matcher for finding unused reader conditional expressions.
502-
503502
An unused reader conditional expression is an expression for a platform
504503
that does not match the CIDER connection for the buffer. Search is done
505504
with the given LIMIT."

test/cider-font-lock-tests.el

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,16 @@
6666
(describe "when cider is connected"
6767
(it "uses cider-reader-conditional-face"
6868
(spy-on 'cider-connected-p :and-return-value t)
69-
(spy-on 'cider-connection-type-for-buffer :and-return-value "clj")
69+
(spy-on 'cider-project-connections-types :and-return-value '("clj"))
7070
(cider--test-with-temp-buffer "#?(:clj 'clj :cljs 'cljs :cljr 'cljr)"
7171
(let ((cider-font-lock-reader-conditionals t)
7272
(found (cider--face-exists-in-range-p (point-min) (point-max)
73-
'cider-reader-conditional-face)))
73+
'cider-reader-conditional-face)))
7474
(expect found :to-be-truthy))))
7575

7676
(it "highlights unmatched reader conditionals"
7777
(spy-on 'cider-connected-p :and-return-value t)
78-
(spy-on 'cider-connection-type-for-buffer :and-return-value "clj")
78+
(spy-on 'cider-project-connections-types :and-return-value '("clj"))
7979
(cider--test-with-temp-buffer "#?(:clj 'clj :cljs 'cljs :cljr 'cljr)"
8080
(let ((cider-font-lock-reader-conditionals t))
8181
(expect (cider--face-exists-in-range-p 4 12 'cider-reader-conditional-face)
@@ -87,7 +87,7 @@
8787

8888
(it "works with splicing"
8989
(spy-on 'cider-connected-p :and-return-value t)
90-
(spy-on 'cider-connection-type-for-buffer :and-return-value "clj")
90+
(spy-on 'cider-project-connections-types :and-return-value '("clj"))
9191
(cider--test-with-temp-buffer "[1 2 #?(:clj [3 4] :cljs [5 6] :cljr [7 8])]"
9292
(let ((cider-font-lock-reader-conditionals t))
9393
(expect (cider--face-exists-in-range-p 1 18 'cider-reader-conditional-face)
@@ -99,38 +99,38 @@
9999

100100
(it "does not apply inside strings or comments"
101101
(spy-on 'cider-connected-p :and-return-value t)
102-
(spy-on 'cider-connection-type-for-buffer :and-return-value "clj")
102+
(spy-on 'cider-project-connections-types :and-return-value '("clj"))
103103
(cider--test-with-temp-buffer "\"#?(:clj 'clj :cljs 'cljs :cljr 'cljr)\" ;; #?(:clj 'clj :cljs 'cljs :cljr 'cljr)"
104104
(let ((cider-font-lock-reader-conditionals t))
105105
(expect (cider--face-exists-in-range-p (point-min) (point-max) 'cider-reader-conditional-face)
106106
:not :to-be-truthy))))
107107

108108
(it "does not apply inside strings or comments"
109109
(spy-on 'cider-connected-p :and-return-value t)
110-
(spy-on 'cider-connection-type-for-buffer :and-return-value "clj")
110+
(spy-on 'cider-project-connections-types :and-return-value '("clj"))
111111
(cider--test-with-temp-buffer "\"#?(:clj 'clj :cljs 'cljs :cljr 'cljr)\" ;; #?(:clj 'clj :cljs 'cljs :cljr 'cljr)"
112112
(let ((cider-font-lock-reader-conditionals t))
113113
(expect (cider--face-exists-in-range-p (point-min) (point-max) 'cider-reader-conditional-face)
114114
:not :to-be-truthy))))
115115

116116
(it "highlights all unmatched reader conditionals"
117117
(spy-on 'cider-connected-p :and-return-value t)
118-
(spy-on 'cider-connection-type-for-buffer :and-return-value "clj")
118+
(spy-on 'cider-project-connections-types :and-return-value '("cljs"))
119119
(cider--test-with-temp-buffer
120120
"#?(:clj 'clj :cljs 'cljs :cljr 'cljr)\n#?(:clj 'clj :cljs 'cljs :cljr 'cljr)\n"
121121
(let ((cider-font-lock-reader-conditionals t))
122122
(expect (cider--face-covers-range-p 14 24 'cider-reader-conditional-face)
123-
:to-be-truthy)
123+
:not :to-be-truthy)
124124
(expect (cider--face-covers-range-p 26 36 'cider-reader-conditional-face)
125125
:to-be-truthy)
126126
(expect (cider--face-covers-range-p 52 62 'cider-reader-conditional-face)
127-
:to-be-truthy)
127+
:not :to-be-truthy)
128128
(expect (cider--face-covers-range-p 64 74 'cider-reader-conditional-face)
129129
:to-be-truthy))))
130130

131131
(it "does not highlight beyond the limits of the reader conditional group"
132132
(spy-on 'cider-connected-p :and-return-value t)
133-
(spy-on 'cider-connection-type-for-buffer :and-return-value "clj")
133+
(spy-on 'cider-project-connections-types :and-return-value '("clj"))
134134
(cider--test-with-temp-buffer
135135
"#?(:clj 'clj :cljs 'cljs :cljr 'cljr)\n#?(:clj 'clj :cljs 'cljs :cljr 'cljr)\n"
136136
(let ((cider-font-lock-reader-conditionals t))
@@ -141,10 +141,18 @@
141141
(expect (cider--face-exists-in-range-p 75 (point-max) 'cider-reader-conditional-face)
142142
:not :to-be-truthy)))))
143143

144+
(describe "when multiple connections are connected"
145+
(it "is disabled"
146+
(spy-on 'cider-connected-p :and-return-value nil)
147+
(spy-on 'cider-project-connections-types :and-return-value '("clj" "cljs"))
148+
(cider--test-with-temp-buffer "#?(:clj 'clj :cljs 'cljs :cljr 'cljr)"
149+
(let ((cider-font-lock-reader-conditionals t))
150+
(expect (cider--face-exists-in-range-p (point-min) (point-max) 'cider-reader-conditional-face)
151+
:not :to-be-truthy)))))
152+
144153
(describe "when cider is not connected"
145154
(it "is disabled"
146155
(spy-on 'cider-connected-p :and-return-value nil)
147-
(spy-on 'cider-connection-type-for-buffer :and-return-value "clj")
148156
(cider--test-with-temp-buffer "#?(:clj 'clj :cljs 'cljs :cljr 'cljr)"
149157
(let ((cider-font-lock-reader-conditionals t))
150158
(expect (cider--face-exists-in-range-p (point-min) (point-max) 'cider-reader-conditional-face)

0 commit comments

Comments
 (0)