Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 29433a8

Browse files
committedSep 9, 2023
Use nested markdown grammar for highlighting docstrings
This uses the "inline" version of the markdown grammar. https://github.com/MDeiml/tree-sitter-markdown see issue #18
1 parent 881756c commit 29433a8

File tree

2 files changed

+40
-21
lines changed

2 files changed

+40
-21
lines changed
 

‎clojure-ts-mode.el

+39-21
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,33 @@ Only intended for use at development time.")
233233
(rx line-start (or "defprotocol" "definterface") line-end))
234234
"A regular expression matching a symbol used to define an interface.")
235235

236+
(defun clojure-ts--docstring-query (capture-symbol)
237+
"Return a query that captures docstrings with CAPTURE-SYMBOL."
238+
`(;; Captures docstrings in def, defonce
239+
((list_lit :anchor (sym_lit) @def_symbol
240+
:anchor (sym_lit) ; variable name
241+
:anchor (str_lit) ,capture-symbol
242+
:anchor (_)) ; the variable's value
243+
(:match ,clojure-ts--variable-definition-symbol-regexp @def_symbol))
244+
;; Captures docstrings defn, defmacro, ns, and things like that
245+
((list_lit :anchor (sym_lit) @def_symbol
246+
:anchor (sym_lit) ; function_name
247+
:anchor (str_lit) ,capture-symbol)
248+
(:match ,clojure-ts--definition-symbol-regexp @def_symbol))
249+
;; Captures docstrings in defprotcol, definterface
250+
((list_lit :anchor (sym_lit) @def_symbol
251+
(list_lit
252+
:anchor (sym_lit) (vec_lit) :*
253+
(str_lit) ,capture-symbol :anchor)
254+
:*)
255+
(:match ,clojure-ts--interface-def-symbol-regexp @def_symbol))))
256+
257+
(defvar clojure-ts--treesit-range-settings
258+
(treesit-range-rules
259+
:embed 'markdown_inline
260+
:host 'clojure
261+
(clojure-ts--docstring-query '@capture)))
262+
236263
(defun clojure-ts--font-lock-settings ()
237264
"Return font lock settings suitable for use in `treesit-font-lock-settings'."
238265
(treesit-font-lock-rules
@@ -333,29 +360,17 @@ Only intended for use at development time.")
333360
'((tagged_or_ctor_lit marker: "#" @font-lock-preprocessor-face
334361
tag: (sym_lit) @font-lock-preprocessor-face))
335362

336-
;; Figure out how to highlight symbols in docstrings.
337-
;; Might require a markdown grammar
338363
:feature 'doc
339364
:language 'clojure
340365
:override t
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
350-
:anchor (str_lit) @font-lock-doc-face)
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)))
366+
(clojure-ts--docstring-query '@font-lock-doc-face)
367+
368+
:feature 'markdown-doc
369+
:language 'markdown_inline
370+
:override t
371+
`((inline (code_span
372+
(code_span_delimiter) :* @font-lock-delimiter-face)
373+
@font-lock-constant-face))
359374

360375
:feature 'quote
361376
:language 'clojure
@@ -786,6 +801,9 @@ forms like deftype, defrecord, reify, proxy, etc."
786801
(unless (treesit-language-available-p 'clojure nil)
787802
(treesit-install-language-grammar 'clojure))
788803
(setq-local comment-start ";")
804+
(when (treesit-ready-p 'markdown_inline 'message)
805+
(treesit-parser-create 'markdown_inline)
806+
(setq-local treesit-range-settings clojure-ts--treesit-range-settings))
789807
(when (treesit-ready-p 'clojure)
790808
(treesit-parser-create 'clojure)
791809
(setq-local treesit-font-lock-settings (clojure-ts--font-lock-settings)
@@ -798,7 +816,7 @@ forms like deftype, defrecord, reify, proxy, etc."
798816
treesit-font-lock-feature-list
799817
'((comment definition variable)
800818
(keyword string char symbol builtin type)
801-
(constant number quote metadata doc)
819+
(constant number quote metadata doc markdown-doc)
802820
(bracket deref function regex tagged-literals)))
803821
(when (boundp 'treesit-thing-settings) ;; Emacs 30+
804822
(setq-local treesit-thing-settings clojure-ts--thing-settings))

‎test/indentation.clj

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
(ns indentation
2+
"Docstring `important`. asdf"
23
(:require
34
[clojure.string :as str])
45
(:import

0 commit comments

Comments
 (0)
Please sign in to comment.