Skip to content

Commit 2225190

Browse files
committed
Make font lock feature list more conforming with recommendations
Level 1 is really simple: comments, definitions Level 2 is for some primitives, types, builtins (let, fn, etc) Level 3 is for numbers, nil, bools, metadata Level 4 is for brackets, function calls, fancy syntax constructs like @ Additionally, this commit fixes some font-locking where we matched faces that don't exist, moves type hints under the 'type feature, font-lock `def` like functions as regular builtins instead of features for the things they are actually defining.
1 parent 724e40d commit 2225190

File tree

2 files changed

+66
-112
lines changed

2 files changed

+66
-112
lines changed

CHANGELOG.md

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

55
- Re-enable treesit-transpose-sexps on Emacs 30 after fixes released by @casouri.
66
- Pin grammar revision in treesit-language-source-alist
7+
- Make font lock feature list more conforming with recommendations
78

89
## 0.1.5
910

clojure-ts-mode.el

Lines changed: 65 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -155,95 +155,35 @@ Only intended for use at development time.")
155155
(eval-and-compile
156156
(concat "^"
157157
(regexp-opt
158-
'("do"
159-
"if"
160-
"let*"
161-
"var"
162-
"fn"
163-
"fn*"
164-
"loop*"
165-
"recur"
166-
"throw"
167-
"try"
168-
"catch"
169-
"finally"
170-
"set!"
171-
"new"
172-
"."
173-
"monitor-enter"
174-
"monitor-exit"
158+
'("do" "if" "let*" "var"
159+
"fn" "fn*" "loop*" "recur"
160+
"throw" "try" "catch" "finally"
161+
"set!" "new"
162+
"monitor-enter" "monitor-exit"
175163
"quote"
176164

177-
"->"
178-
"->>"
179-
".."
180-
"amap"
181-
"and"
182-
"areduce"
183-
"as->"
184-
"assert"
185-
"binding"
186-
"bound-fn"
187-
"case"
188-
"comment"
189-
"cond"
190-
"cond->"
191-
"cond->>"
192-
"condp"
193-
"declare"
194-
"delay"
195-
"doall"
196-
"dorun"
197-
"doseq"
198-
"dosync"
199-
"dotimes"
200-
"doto"
201-
"extend-protocol"
202-
"extend-type"
203-
"for"
204-
"future"
205-
"gen-class"
206-
"gen-interface"
207-
"if-let"
208-
"if-not"
209-
"if-some"
210-
"import"
211-
"in-ns"
212-
"io!"
213-
"lazy-cat"
214-
"lazy-seq"
215-
"let"
216-
"letfn"
217-
"locking"
218-
"loop"
219-
"memfn"
220-
"ns"
221-
"or"
222-
"proxy"
223-
"proxy-super"
224-
"pvalues"
225-
"refer-clojure"
226-
"reify"
227-
"some->"
228-
"some->>"
229-
"sync"
230-
"time"
231-
"vswap!"
232-
"when"
233-
"when-first"
234-
"when-let"
235-
"when-not"
236-
"when-some"
237-
"while"
238-
"with-bindings"
239-
"with-in-str"
240-
"with-loading-context"
241-
"with-local-vars"
242-
"with-open"
243-
"with-out-str"
244-
"with-precision"
245-
"with-redefs"
246-
"with-redefs-fn"))
165+
"->" "->>" ".." "."
166+
"amap" "and" "areduce" "as->" "assert"
167+
"binding" "bound-fn"
168+
"case" "comment" "cond" "cond->" "cond->>" "condp"
169+
"declare" "def" "definline" "definterface" "defmacro" "defmethod"
170+
"defmulti" "defn" "defn-" "defonce" "defprotocol" "defrecord"
171+
"defstruct" "deftype"
172+
"delay" "doall" "dorun" "doseq" "dosync" "dotimes" "doto"
173+
"extend-protocol" "extend-type"
174+
"for" "future"
175+
"gen-class" "gen-interface"
176+
"if-let" "if-not" "if-some" "import" "in-ns""io!"
177+
"lazy-cat" "lazy-seq" "let" "letfn" "locking" "loop"
178+
"memfn" "ns" "or"
179+
"proxy" "proxy-super" "pvalues"
180+
"refer-clojure" "reify"
181+
"some->" "some->>""sync"
182+
"time" "vswap!"
183+
"when" "when-first" "when-let" "when-not" "when-some" "while"
184+
"with-bindings" "with-in-str" "with-loading-context"
185+
"with-local-vars" "with-open" "with-out-str" "with-precision"
186+
"with-redefs" "with-redefs-fn"))
247187
"$")))
248188

249189
(defface clojure-ts-keyword-face
@@ -268,13 +208,9 @@ Only intended for use at development time.")
268208
(rx line-start (or "def" "defonce") line-end))
269209

270210
(defconst clojure-ts--type-keyword-regexp
271-
(rx line-start (or "defprotocol"
272-
"defmulti"
273-
"deftype"
274-
"defrecord"
275-
"definterface"
276-
"defmethod"
277-
"defstruct")
211+
(rx line-start
212+
(or "defprotocol" "defmulti" "deftype" "defrecord"
213+
"definterface" "defmethod" "defstruct")
278214
line-end))
279215

280216
(defun clojure-ts--font-lock-settings ()
@@ -283,7 +219,7 @@ Only intended for use at development time.")
283219
:feature 'string
284220
:language 'clojure
285221
'((str_lit) @font-lock-string-face
286-
(regex_lit) @font-lock-string-face)
222+
(regex_lit) @font-lock-regexp-face)
287223

288224
:feature 'regex
289225
:language 'clojure
@@ -317,6 +253,13 @@ Only intended for use at development time.")
317253
((sym_name) @font-lock-builtin-face
318254
(:match ,clojure-ts--builtin-dynamic-var-regexp @font-lock-builtin-face)))
319255

256+
;; Any function calls, not built-ins.
257+
;; This can give false positives (macros, quoted lists, namespace imports)
258+
;; but is a level 4 feature and never enabled by default.
259+
:feature 'function
260+
:language 'clojure
261+
'((list_lit :anchor (sym_lit (sym_name) @font-lock-function-call-face)))
262+
320263
:feature 'symbol
321264
:language 'clojure
322265
'((sym_ns) @font-lock-type-face)
@@ -327,34 +270,42 @@ Only intended for use at development time.")
327270
;; No wonder the tree-sitter-clojure grammar only touches syntax, and not semantics
328271
:feature 'definition ;; defn and defn like macros
329272
:language 'clojure
330-
`(((list_lit :anchor (sym_lit (sym_name) @font-lock-keyword-face)
273+
`(((list_lit :anchor (sym_lit (sym_name) @def)
331274
:anchor (sym_lit (sym_name) @font-lock-function-name-face))
332-
(:match ,clojure-ts--definition-keyword-regexp
333-
@font-lock-keyword-face))
275+
(:match ,clojure-ts--definition-keyword-regexp @def))
334276
((anon_fn_lit
335277
marker: "#" @font-lock-property-face)))
336278

337279
:feature 'variable ;; def, defonce
338280
:language 'clojure
339-
`(((list_lit :anchor (sym_lit (sym_name) @font-lock-keyword-face)
281+
`(((list_lit :anchor (sym_lit (sym_name) @def)
340282
:anchor (sym_lit (sym_name) @font-lock-variable-name-face))
341-
(:match ,clojure-ts--variable-keyword-regexp @font-lock-keyword-face)))
283+
(:match ,clojure-ts--variable-keyword-regexp @def)))
342284

343-
:feature 'type ;; deftype, defmulti, defprotocol, etc
285+
;; Can we support declarations in the namespace form?
286+
:feature 'type
344287
:language 'clojure
345-
`(((list_lit :anchor (sym_lit (sym_name) @font-lock-keyword-face)
288+
`(;; Type Declarations
289+
((list_lit :anchor (sym_lit (sym_name) @def)
346290
:anchor (sym_lit (sym_name) @font-lock-type-face))
347-
(:match ,clojure-ts--type-keyword-regexp @font-lock-keyword-face)))
291+
(:match ,clojure-ts--type-keyword-regexp @def))
292+
;; Type Hints
293+
(meta_lit
294+
marker: "^" @font-lock-operator-face
295+
value: (sym_lit (sym_name) @font-lock-type-face))
296+
(old_meta_lit
297+
marker: "#^" @font-lock-operator-face
298+
value: (sym_lit (sym_name) @font-lock-type-face)))
348299

349300
:feature 'metadata
350301
:language 'clojure
351302
:override t
352-
`((meta_lit marker: "^" @font-lock-property-face)
353-
(meta_lit value: (kwd_lit) @font-lock-property-face) ;; metadata
354-
(meta_lit value: (sym_lit (sym_name) @font-lock-type-face)) ;; typehint
355-
(old_meta_lit marker: "#^" @font-lock-property-face)
356-
(old_meta_lit value: (kwd_lit) @font-lock-property-face) ;; metadata
357-
(old_meta_lit value: (sym_lit (sym_name) @font-lock-type-face))) ;; typehint
303+
`((meta_lit
304+
marker: "^" @font-lock-operator-face
305+
value: (kwd_lit (kwd_name) @font-lock-property-name-face))
306+
(old_meta_lit
307+
marker: "#^" @font-lock-operator-face
308+
value: (kwd_lit (kwd_name) @font-lock-property-name-face)))
358309

359310
:feature 'tagged-literals
360311
:language 'clojure
@@ -364,6 +315,7 @@ Only intended for use at development time.")
364315

365316
;; TODO, also account for `def'
366317
;; Figure out how to highlight symbols in docstrings.
318+
;; Might require a markdown grammar
367319
:feature 'doc
368320
:language 'clojure
369321
:override t
@@ -633,9 +585,10 @@ See `clojure-ts--standard-definition-node-name' for the implementation used.")
633585
treesit-defun-name-function #'clojure-ts--standard-definition-node-name
634586
treesit-simple-imenu-settings clojure-ts--imenu-settings
635587
treesit-font-lock-feature-list
636-
'((comment string char number)
637-
(keyword constant symbol bracket builtin)
638-
(deref quote metadata definition variable type doc regex tagged-literals)))
588+
'((comment definition variable)
589+
(keyword string char symbol builtin type)
590+
(constant number quote metadata)
591+
(bracket deref function regex tagged-literals)))
639592
(when (boundp 'treesit-thing-settings) ;; Emacs 30+
640593
(setq-local treesit-thing-settings clojure-ts--thing-settings))
641594
(when clojure-ts--debug

0 commit comments

Comments
 (0)