@@ -202,17 +202,26 @@ Only intended for use at development time.")
202202 '((t (:inherit font-lock-string-face )))
203203 " Face used to font-lock Clojure character literals." )
204204
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." )
216225
217226(defconst clojure-ts--variable-definition-symbol-regexp
218227 (eval-and-compile
@@ -244,40 +253,49 @@ Only intended for use at development time.")
244253
245254(defun clojure-ts--docstring-query (capture-symbol )
246255 " 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) :?
249259 :anchor (sym_lit) ; variable name
260+ :anchor (comment) :?
250261 :anchor (str_lit) , capture-symbol
251262 :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))
253265 ; ; 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) :?
255268 :anchor (sym_lit
256269 (meta_lit
257270 value: (map_lit
258- (kwd_lit) @doc -keyword
271+ (kwd_lit) @_doc -keyword
259272 :anchor
260273 (str_lit) , capture-symbol ))))
261274 ; ; We're only supporting this on a fixed set of defining symbols
262275 ; ; Existing regexes don't encompass def and defn
263276 ; ; 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" ))
269284 ; ; 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) :?
271287 :anchor (sym_lit) ; function_name
288+ :anchor (comment) :?
272289 :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))
274292 ; ; Captures docstrings in defprotcol, definterface
275- ((list_lit :anchor (sym_lit) @def_symbol
293+ ((list_lit :anchor (sym_lit) @_def_symbol
276294 (list_lit
277295 :anchor (sym_lit) (vec_lit) :*
278296 (str_lit) , capture-symbol :anchor )
279297 :* )
280- (:match , clojure-ts--interface-def-symbol-regexp @def_symbol ))))
298+ (:match , clojure-ts--interface-def-symbol-regexp @_def_symbol ))))
281299
282300(defvar clojure-ts--treesit-range-settings
283301 (treesit-range-rules
@@ -752,7 +770,7 @@ forms like deftype, defrecord, reify, proxy, etc."
752770 (and (treesit-node-eq node (treesit-node-child parent 2 t ))
753771 (let ((first-auncle (treesit-node-child parent 0 t )))
754772 (clojure-ts--symbol-matches-p
755- clojure-ts--definition-symbol-regexp
773+ ( regexp-opt clojure-ts-function-docstring-symbols)
756774 first-auncle)))))
757775
758776(defun clojure-ts--match-def-docstring (node )
@@ -765,7 +783,7 @@ forms like deftype, defrecord, reify, proxy, etc."
765783 (treesit-node-child parent 3 t )
766784 (let ((first-auncle (treesit-node-child parent 0 t )))
767785 (clojure-ts--symbol-matches-p
768- clojure-ts--variable- definition-symbol-regexp
786+ ( regexp-opt clojure-ts-definition-docstring-symbols)
769787 first-auncle)))))
770788
771789(defun clojure-ts--match-method-docstring (node )
0 commit comments