Skip to content

Commit 5e8a7d2

Browse files
committed
Improve syntax highlighting
1 parent 4f52eae commit 5e8a7d2

File tree

4 files changed

+61
-14
lines changed

4 files changed

+61
-14
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44

55
- [#62](https://github.com/clojure-emacs/clojure-ts-mode/issues/62): Define `list` "thing" to improve navigation in Emacs 31.
66
- [#64]: Add defcustom `clojure-ts-auto-remap` to control remapping of `clojure-mode` buffers.
7+
- Improve syntax highlighting:
8+
- Highlight metadata with single keyword with `clojure-ts-keyword-face`.
9+
- Only highlight built-ins from `clojure.core` namespace.
10+
- Highlight named lambda functions properly.
11+
- Fix syntax highlighting for functions and vars with metadata on the previous
12+
line.
713

814
## 0.2.3 (2025-03-04)
915

clojure-ts-mode.el

+20-12
Original file line numberDiff line numberDiff line change
@@ -384,12 +384,19 @@ with the markdown_inline grammar."
384384
marker: _ @clojure-ts-keyword-face
385385
delimiter: _ :? @default))
386386

387+
;; Highlight as built-in only if there is no namespace or namespace is
388+
;; `clojure.core'.
387389
:feature 'builtin
388390
:language 'clojure
389-
`(((list_lit meta: _ :? :anchor (sym_lit (sym_name) @font-lock-keyword-face))
390-
(:match ,clojure-ts--builtin-symbol-regexp @font-lock-keyword-face))
391-
((sym_name) @font-lock-builtin-face
392-
(:match ,clojure-ts--builtin-dynamic-var-regexp @font-lock-builtin-face)))
391+
`(((list_lit meta: _ :? :anchor (sym_lit !namespace name: (sym_name) @font-lock-keyword-face))
392+
(:match ,clojure-ts--builtin-symbol-regexp @font-lock-keyword-face))
393+
((list_lit meta: _ :? :anchor
394+
(sym_lit namespace: ((sym_ns) @ns
395+
(:equal "clojure.core" @ns))
396+
name: (sym_name) @font-lock-keyword-face))
397+
(:match ,clojure-ts--builtin-symbol-regexp @font-lock-keyword-face))
398+
((sym_name) @font-lock-builtin-face
399+
(:match ,clojure-ts--builtin-dynamic-var-regexp @font-lock-builtin-face)))
393400

394401
;; Any function calls, not built-ins.
395402
;; This can give false positives (macros, quoted lists, namespace imports)
@@ -408,12 +415,13 @@ with the markdown_inline grammar."
408415
;; No wonder the tree-sitter-clojure grammar only touches syntax, and not semantics
409416
:feature 'definition ;; defn and defn like macros
410417
:language 'clojure
411-
`(((list_lit :anchor meta: _ :?
412-
:anchor (sym_lit (sym_name) @def)
418+
`(((list_lit :anchor meta: _ :*
419+
:anchor (sym_lit (sym_name) @font-lock-keyword-face)
413420
:anchor (sym_lit (sym_name) @font-lock-function-name-face))
414421
(:match ,(rx-to-string
415422
`(seq bol
416423
(or
424+
"fn"
417425
"defn"
418426
"defn-"
419427
"defmulti"
@@ -423,7 +431,7 @@ with the markdown_inline grammar."
423431
"defmacro"
424432
"definline")
425433
eol))
426-
@def))
434+
@font-lock-keyword-face))
427435
((anon_fn_lit
428436
marker: "#" @font-lock-property-face))
429437
;; Methods implementation
@@ -450,10 +458,10 @@ with the markdown_inline grammar."
450458

451459
:feature 'variable ;; def, defonce
452460
:language 'clojure
453-
`(((list_lit :anchor meta: _ :?
454-
:anchor (sym_lit (sym_name) @def)
461+
`(((list_lit :anchor meta: _ :*
462+
:anchor (sym_lit (sym_name) @font-lock-keyword-face)
455463
:anchor (sym_lit (sym_name) @font-lock-variable-name-face))
456-
(:match ,clojure-ts--variable-definition-symbol-regexp @def)))
464+
(:match ,clojure-ts--variable-definition-symbol-regexp @font-lock-keyword-face)))
457465

458466
;; Can we support declarations in the namespace form?
459467
:feature 'type
@@ -479,10 +487,10 @@ with the markdown_inline grammar."
479487
:override t
480488
`((meta_lit
481489
marker: "^" @font-lock-operator-face
482-
value: (kwd_lit (kwd_name) @font-lock-property-name-face))
490+
value: (kwd_lit (kwd_name) @clojure-ts-keyword-face))
483491
(old_meta_lit
484492
marker: "#^" @font-lock-operator-face
485-
value: (kwd_lit (kwd_name) @font-lock-property-name-face)))
493+
value: (kwd_lit (kwd_name) @clojure-ts-keyword-face)))
486494

487495
:feature 'tagged-literals
488496
:language 'clojure

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

+27-1
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,30 @@ DESCRIPTION is the description of the spec."
136136
(13 16 font-lock-keyword-face)
137137
(18 20 font-lock-function-name-face)
138138
(25 31 font-lock-doc-face)
139-
(40 46 font-lock-string-face))))
139+
(40 46 font-lock-string-face)))
140+
141+
(when-fontifying-it "fn-with-name"
142+
("(fn named-lambda [x] x)"
143+
(2 3 font-lock-keyword-face)
144+
(5 16 font-lock-function-name-face)))
145+
146+
(when-fontifying-it "single-keyword-metadata"
147+
("(def ^:private my-private-var true)"
148+
(2 4 font-lock-keyword-face)
149+
(6 6 font-lock-operator-face)
150+
(7 14 clojure-ts-keyword-face)
151+
(16 29 font-lock-variable-name-face)
152+
(31 34 font-lock-constant-face)))
153+
154+
(when-fontifying-it "built-ins"
155+
("(for [x [1 2 3]] x)"
156+
(2 4 font-lock-keyword-face))
157+
158+
("(clojure.core/for [x [1 2 3]] x)"
159+
(2 13 font-lock-type-face)
160+
(15 17 font-lock-keyword-face)))
161+
162+
(when-fontifying-it "non-built-ins-with-same-name"
163+
("(h/for query {})"
164+
(2 2 font-lock-type-face)
165+
(4 6 default))))

test/samples/test.clj

+8-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020
~x
2121
~@x))
2222

23+
;; Built-ins should be highlighted only for `clojure.core` namespace.
24+
(for [])
25+
(clojure.core/for [])
26+
(honey.sql/for {})
27+
2328
;; the myfn sexp should have a comment face
2429
(mysfn 101
2530
foo
@@ -237,7 +242,6 @@
237242
([x y & more]
238243
(reduce1 max (max x y) more)))
239244

240-
241245
;; definitions with metadata only don't cause freezing
242246
(def ^String)
243247
;; clojure-mode regression: the hanging metadata doesn't cause freezing
@@ -292,6 +296,9 @@ clojure.core/map
292296
^{:foo true}
293297
(defn b "hello" [] "world")
294298

299+
^{:foo bar}
300+
(def foo "usage" "hello")
301+
295302
(comment
296303
(defrecord TestRecord [field]
297304
AutoCloseable

0 commit comments

Comments
 (0)