Skip to content

Commit a878216

Browse files
committed
Fix docstring font-locking, try to clarify various regex names
Docstrings should be highlighted for functions, vars (def, defonce), namespaces, and defprotocol/definterface methods.
1 parent 0c4bd2b commit a878216

File tree

1 file changed

+46
-21
lines changed

1 file changed

+46
-21
lines changed

clojure-ts-mode.el

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ Only intended for use at development time.")
195195
'((t (:inherit font-lock-string-face)))
196196
"Face used to font-lock Clojure character literals.")
197197

198-
(defconst clojure-ts--definition-keyword-regexp
198+
(defconst clojure-ts--definition-symbol-regexp
199199
(rx
200200
line-start
201201
(or (group (or "ns" "fn"))
@@ -205,14 +205,33 @@ Only intended for use at development time.")
205205
"-" "_" "!" "@" "#" "$" "%" "^" "&" "*" "|" "?" "<" ">" "+" "=" ":"))))
206206
line-end))
207207

208-
(defconst clojure-ts--variable-keyword-regexp
209-
(rx line-start (or "def" "defonce") line-end))
208+
(defconst clojure-ts--variable-definition-symbol-regexp
209+
(eval-and-compile
210+
(rx line-start (or "def" "defonce") line-end))
211+
"A regular expression matching a symbol used to define a variable.")
210212

211-
(defconst clojure-ts--type-keyword-regexp
212-
(rx line-start
213+
(defconst clojure-ts--typedef-symbol-regexp
214+
(eval-and-compile
215+
(rx line-start
213216
(or "defprotocol" "defmulti" "deftype" "defrecord"
214217
"definterface" "defmethod" "defstruct")
215218
line-end))
219+
"A regular expression matching a symbol used to define a type")
220+
221+
(defconst clojure-ts-type-symbol-regexp
222+
(eval-and-compile
223+
(rx line-start
224+
(or "deftype" "defrecord"
225+
;; While not reifying, helps with doc strings
226+
"defprotocol" "definterface"
227+
"reify" "proxy" "extend-type" "extend-protocol")
228+
line-end))
229+
"A regular expression matching a symbol used to define or instantiate a type.")
230+
231+
(defconst clojure-ts--interface-def-symbol-regexp
232+
(eval-and-compile
233+
(rx line-start (or "defprotocol" "definterface") line-end))
234+
"A regular expression matching a symbol used to define an interface.")
216235

217236
(defun clojure-ts--font-lock-settings ()
218237
"Return font lock settings suitable for use in `treesit-font-lock-settings'."
@@ -273,23 +292,23 @@ Only intended for use at development time.")
273292
:language 'clojure
274293
`(((list_lit :anchor (sym_lit (sym_name) @def)
275294
:anchor (sym_lit (sym_name) @font-lock-function-name-face))
276-
(:match ,clojure-ts--definition-keyword-regexp @def))
295+
(:match ,clojure-ts--definition-symbol-regexp @def))
277296
((anon_fn_lit
278297
marker: "#" @font-lock-property-face)))
279298

280299
:feature 'variable ;; def, defonce
281300
:language 'clojure
282301
`(((list_lit :anchor (sym_lit (sym_name) @def)
283302
:anchor (sym_lit (sym_name) @font-lock-variable-name-face))
284-
(:match ,clojure-ts--variable-keyword-regexp @def)))
303+
(:match ,clojure-ts--variable-definition-symbol-regexp @def)))
285304

286305
;; Can we support declarations in the namespace form?
287306
:feature 'type
288307
:language 'clojure
289308
`(;; Type Declarations
290309
((list_lit :anchor (sym_lit (sym_name) @def)
291310
:anchor (sym_lit (sym_name) @font-lock-type-face))
292-
(:match ,clojure-ts--type-keyword-regexp @def))
311+
(:match ,clojure-ts--typedef-symbol-regexp @def))
293312
;; Type Hints
294313
(meta_lit
295314
marker: "^" @font-lock-operator-face
@@ -314,16 +333,29 @@ Only intended for use at development time.")
314333
'((tagged_or_ctor_lit marker: "#" @font-lock-preprocessor-face
315334
tag: (sym_lit) @font-lock-preprocessor-face))
316335

317-
;; TODO, also account for `def'
318336
;; Figure out how to highlight symbols in docstrings.
319337
;; Might require a markdown grammar
320338
:feature 'doc
321339
:language 'clojure
322340
:override t
323-
`(((list_lit :anchor (sym_lit) @def_symbol
324-
:anchor (sym_lit) @function_name
341+
`(;; Captures docstrings in def, defonce
342+
((list_lit :anchor (sym_lit) @def_symbol
343+
:anchor (sym_lit) ; variable name
344+
:anchor (str_lit) @font-lock-doc-face
345+
:anchor (_)) ; the variable's value
346+
(:match ,clojure-ts--variable-definition-symbol-regexp @def_symbol))
347+
;; Captures docstrings defn, defmacro, ns, and things like that
348+
((list_lit :anchor (sym_lit) @def_symbol
349+
:anchor (sym_lit) ; function_name
325350
:anchor (str_lit) @font-lock-doc-face)
326-
(:match ,clojure-ts--definition-keyword-regexp @def_symbol)))
351+
(:match ,clojure-ts--definition-symbol-regexp @def_symbol))
352+
;; Captures docstrings in defprotcol, definterface
353+
((list_lit :anchor (sym_lit) @def_symbol
354+
(list_lit
355+
:anchor (sym_lit) (vec_lit) :*
356+
(str_lit) @font-lock-doc-face :anchor)
357+
:*)
358+
(:match ,clojure-ts--interface-def-symbol-regexp @def_symbol)))
327359

328360
:feature 'quote
329361
:language 'clojure
@@ -615,13 +647,6 @@ See `treesit-simple-indent-rules'."
615647
clojure-ts--symbols-with-body-expressions-regexp
616648
first-child)))))
617649

618-
(defconst clojure-ts--reifying-symbol-regexp
619-
(eval-and-compile
620-
(rx line-start
621-
(or "deftype" "defrecord"
622-
"reify" "proxy" "extend-type" "extend-protocol")))
623-
"A regular expression matching a symbol used to define a concrete type.")
624-
625650
(defun clojure-ts--match-method-body (node parent _bol)
626651
"Matches a `NODE' in the body of a `PARENT' method implementation.
627652
A method implementation referes to concrete implemntations being defined in
@@ -632,7 +657,7 @@ forms like deftype, defrecord, reify, proxy, etc."
632657
;; auncle: gender neutral sibling of parent, aka child of grandparent
633658
(first-auncle (treesit-node-child grandparent 0 t)))
634659
(and (clojure-ts--list-node-p grandparent)
635-
(clojure-ts--symbol-matches-p clojure-ts--reifying-symbol-regexp
660+
(clojure-ts--symbol-matches-p clojure-ts-type-symbol-regexp
636661
first-auncle)))))
637662

638663
(defvar clojure-ts--threading-macro
@@ -734,7 +759,7 @@ forms like deftype, defrecord, reify, proxy, etc."
734759
treesit-font-lock-feature-list
735760
'((comment definition variable)
736761
(keyword string char symbol builtin type)
737-
(constant number quote metadata)
762+
(constant number quote metadata doc)
738763
(bracket deref function regex tagged-literals)))
739764
(when (boundp 'treesit-thing-settings) ;; Emacs 30+
740765
(setq-local treesit-thing-settings clojure-ts--thing-settings))

0 commit comments

Comments
 (0)