Skip to content

Commit 7c019fa

Browse files
committed
[Fix #707] Highlight [[var]] and code only in strings and comments
The rules for docstring wikilinks and code marks matched anywhere, so in code they caught Datalog relation bindings like [[?x]], destructuring like (fn [[x]] x), and two syntax-quoted symbols in a row.
1 parent 3ca6f5d commit 7c019fa

3 files changed

Lines changed: 34 additions & 4 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* [#709](https://github.com/clojure-emacs/clojure-mode/pull/709): Fix `clojure-get-indent-spec` and `put-clojure-indent` converting legacy indent specs, like those of `letfn`, `reify` or `extend-protocol`, to the wrong modern rules.
2020
* [#709](https://github.com/clojure-emacs/clojure-mode/pull/709): Fix indenting with a modern indent spec that has an `:inner` rule with a position but no `:block` rule, like `((:inner 1 0))`, which signaled an error.
2121
* [#711](https://github.com/clojure-emacs/clojure-mode/pull/711): Fix the indentation of `letfn` functions, and of other forms an `:inner` rule with an index applies to, when a `#_` form comes before them, as the discarded form and its `#_` counted as two arguments.
22+
* [#707](https://github.com/clojure-emacs/clojure-mode/issues/707): Highlight `[[var]]` and `` `code` `` only in strings and comments, so Datalog relation bindings like `[[?x]]` and destructuring like `(fn [[x]] x)` aren't colored as constants in code.
2223

2324
## 5.23.0 (2026-03-25)
2425

‎clojure-mode.el‎

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,12 +1226,18 @@ whether the paren is a direct child of a letfn binding vector."
12261226
'clojure-discard-face font-lock-comment-face)
12271227
t)
12281228
;; Highlight `code` marks, just like `elisp'.
1229-
(,(rx "`" (group-n 1 (optional "#'")
1230-
(+ (or (syntax symbol) (syntax word)))) "`")
1229+
(,(lambda (bound)
1230+
(clojure--search-in-string-or-comment
1231+
(rx "`" (group-n 1 (optional "#'")
1232+
(+ (or (syntax symbol) (syntax word)))) "`")
1233+
bound))
12311234
(1 'font-lock-constant-face prepend))
12321235
;; Highlight [[var]] comments
1233-
(,(rx "[[" (group-n 1 (optional "#'")
1234-
(+ (or (syntax symbol) (syntax word)))) "]]")
1236+
(,(lambda (bound)
1237+
(clojure--search-in-string-or-comment
1238+
(rx "[[" (group-n 1 (optional "#'")
1239+
(+ (or (syntax symbol) (syntax word)))) "]]")
1240+
bound))
12351241
(1 'font-lock-constant-face prepend))
12361242
;; Highlight escaped characters in strings.
12371243
(clojure-font-lock-escaped-chars 0 'bold prepend)
@@ -1374,6 +1380,19 @@ preceded by a #."
13741380
;; When regexp is nil, match any string (both #"..." and "...").
13751381
string-beg))))))
13761382

1383+
(defun clojure--search-in-string-or-comment (regexp bound)
1384+
"Search forward for REGEXP in a string or a comment, up to BOUND.
1385+
In code, the same text can be something else entirely, like the
1386+
[[?x]] relation binding of a Datalog query."
1387+
(let ((found nil))
1388+
(while (and (not found)
1389+
(re-search-forward regexp bound t))
1390+
(setq found (save-match-data
1391+
(save-excursion
1392+
(ppss-comment-or-string-start
1393+
(syntax-ppss (match-beginning 0)))))))
1394+
found))
1395+
13771396
(defun clojure-font-lock-escaped-chars (bound)
13781397
"Highlight \\escaped chars in strings.
13791398
BOUND denotes a buffer position to limit the search."

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,16 @@ DESCRIPTION is the description of the spec."
183183
(4 11 (font-lock-constant-face font-lock-comment-face))
184184
(12 13 font-lock-comment-face)))
185185

186+
(when-fontifying-it "should leave double brackets and backticks alone in code"
187+
("(fn [[x]] x)"
188+
(7 7 nil))
189+
190+
("[(q '[:find (min ?d)] $) [[?d]]]"
191+
(28 29 nil))
192+
193+
("(list `a`b)"
194+
(8 8 nil)))
195+
186196
(when-fontifying-it "should fontify let, when, and while type forms"
187197
("(when-alist [x 1]\n ())"
188198
("when-alist" font-lock-keyword-face))

0 commit comments

Comments
 (0)