@@ -202,17 +202,26 @@ Only intended for use at development time.")
202
202
'((t (:inherit font-lock-string-face )))
203
203
" Face used to font-lock Clojure character literals." )
204
204
205
- (defconst clojure-ts--definition-symbol-regexp
206
- (rx
207
- line-start
208
- (or (group " fn" )
209
- (group " def"
210
- (+ (or alnum
211
- ; ; What are valid characters for symbols?
212
- ; ; is a negative match better?
213
- " -" " _" " !" " @" " #" " $" " %" " ^" " &"
214
- " *" " |" " ?" " <" " >" " +" " =" " :" ))))
215
- line-end))
205
+ (defun clojure-ts-symbol-regexp (symbols )
206
+ " Return a regular expression that matches one of SYMBOLS exactly."
207
+ (concat " ^" (regexp-opt symbols) " $" ))
208
+
209
+ (defvar clojure-ts-function-docstring-symbols
210
+ '(" definline"
211
+ " defmulti"
212
+ " defmacro"
213
+ " defn"
214
+ " defn-"
215
+ " defprotocol"
216
+ " ns" )
217
+ " Symbols that accept an optional docstring as their second argument." )
218
+
219
+ (defvar clojure-ts-definition-docstring-symbols
220
+ '(" def" )
221
+ " Symbols that accept an optional docstring as their second argument.
222
+ Any symbols added here should only treat their second argument as a docstring
223
+ if a third argument (the value) is provided.
224
+ \" def\" is the only builtin Clojure symbol that behaves like this." )
216
225
217
226
(defconst clojure-ts--variable-definition-symbol-regexp
218
227
(eval-and-compile
@@ -244,40 +253,49 @@ Only intended for use at development time.")
244
253
245
254
(defun clojure-ts--docstring-query (capture-symbol )
246
255
" Return a query that captures docstrings with CAPTURE-SYMBOL."
247
- `(; ; Captures docstrings in def, defonce
248
- ((list_lit :anchor (sym_lit) @def_symbol
256
+ `(; ; Captures docstrings in def
257
+ ((list_lit :anchor (sym_lit) @_def_symbol
258
+ :anchor (comment) :?
249
259
:anchor (sym_lit) ; variable name
260
+ :anchor (comment) :?
250
261
:anchor (str_lit) , capture-symbol
251
262
:anchor (_)) ; the variable's value
252
- (:match , clojure-ts--variable-definition-symbol-regexp @def_symbol))
263
+ (:match ,(clojure-ts-symbol-regexp clojure-ts-definition-docstring-symbols)
264
+ @_def_symbol))
253
265
; ; Captures docstrings in metadata of definitions
254
- ((list_lit :anchor (sym_lit) @def_symbol
266
+ ((list_lit :anchor (sym_lit) @_def_symbol
267
+ :anchor (comment) :?
255
268
:anchor (sym_lit
256
269
(meta_lit
257
270
value: (map_lit
258
- (kwd_lit) @doc -keyword
271
+ (kwd_lit) @_doc -keyword
259
272
:anchor
260
273
(str_lit) , capture-symbol ))))
261
274
; ; We're only supporting this on a fixed set of defining symbols
262
275
; ; Existing regexes don't encompass def and defn
263
276
; ; Naming another regex is very cumbersome.
264
- (:match ,(regexp-opt '(" def" " defonce" " defn" " defn-" " defmacro" " ns"
265
- " defmulti" " definterface" " defprotocol"
266
- " deftype" " defrecord" " defstruct" ))
267
- @def_symbol)
268
- (:equal @doc-keyword " :doc" ))
277
+ (:match ,(clojure-ts-symbol-regexp
278
+ '(" def" " defonce" " defn" " defn-" " defmacro" " ns"
279
+ " defmulti" " definterface" " defprotocol"
280
+ " deftest" " deftest-"
281
+ " deftype" " defrecord" " defstruct" ))
282
+ @_def_symbol)
283
+ (:equal @_doc-keyword " :doc" ))
269
284
; ; Captures docstrings defn, defmacro, ns, and things like that
270
- ((list_lit :anchor (sym_lit) @def_symbol
285
+ ((list_lit :anchor (sym_lit) @_def_symbol
286
+ :anchor (comment) :?
271
287
:anchor (sym_lit) ; function_name
288
+ :anchor (comment) :?
272
289
:anchor (str_lit) , capture-symbol )
273
- (:match , clojure-ts--definition-symbol-regexp @def_symbol))
290
+ (:match ,(clojure-ts-symbol-regexp clojure-ts-function-docstring-symbols)
291
+ @_def_symbol))
274
292
; ; Captures docstrings in defprotcol, definterface
275
- ((list_lit :anchor (sym_lit) @def_symbol
293
+ ((list_lit :anchor (sym_lit) @_def_symbol
276
294
(list_lit
277
295
:anchor (sym_lit) (vec_lit) :*
278
296
(str_lit) , capture-symbol :anchor )
279
297
:* )
280
- (:match , clojure-ts--interface-def-symbol-regexp @def_symbol ))))
298
+ (:match , clojure-ts--interface-def-symbol-regexp @_def_symbol ))))
281
299
282
300
(defvar clojure-ts--treesit-range-settings
283
301
(treesit-range-rules
@@ -752,7 +770,7 @@ forms like deftype, defrecord, reify, proxy, etc."
752
770
(and (treesit-node-eq node (treesit-node-child parent 2 t ))
753
771
(let ((first-auncle (treesit-node-child parent 0 t )))
754
772
(clojure-ts--symbol-matches-p
755
- clojure-ts--definition-symbol-regexp
773
+ ( regexp-opt clojure-ts-function-docstring-symbols)
756
774
first-auncle)))))
757
775
758
776
(defun clojure-ts--match-def-docstring (node )
@@ -765,7 +783,7 @@ forms like deftype, defrecord, reify, proxy, etc."
765
783
(treesit-node-child parent 3 t )
766
784
(let ((first-auncle (treesit-node-child parent 0 t )))
767
785
(clojure-ts--symbol-matches-p
768
- clojure-ts--variable- definition-symbol-regexp
786
+ ( regexp-opt clojure-ts-definition-docstring-symbols)
769
787
first-auncle)))))
770
788
771
789
(defun clojure-ts--match-method-docstring (node )
0 commit comments