Skip to content

Commit

Permalink
Use nested markdown grammar for highlighting docstrings
Browse files Browse the repository at this point in the history
This uses the "inline" version of the markdown grammar.

https://github.com/MDeiml/tree-sitter-markdown

see issue #18
  • Loading branch information
dannyfreeman committed Sep 9, 2023
1 parent 881756c commit 29433a8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 21 deletions.
60 changes: 39 additions & 21 deletions clojure-ts-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,33 @@ Only intended for use at development time.")
(rx line-start (or "defprotocol" "definterface") line-end))
"A regular expression matching a symbol used to define an interface.")

(defun clojure-ts--docstring-query (capture-symbol)
"Return a query that captures docstrings with CAPTURE-SYMBOL."
`(;; Captures docstrings in def, defonce
((list_lit :anchor (sym_lit) @def_symbol
:anchor (sym_lit) ; variable name
:anchor (str_lit) ,capture-symbol
:anchor (_)) ; the variable's value
(:match ,clojure-ts--variable-definition-symbol-regexp @def_symbol))
;; Captures docstrings defn, defmacro, ns, and things like that
((list_lit :anchor (sym_lit) @def_symbol
:anchor (sym_lit) ; function_name
:anchor (str_lit) ,capture-symbol)
(:match ,clojure-ts--definition-symbol-regexp @def_symbol))
;; Captures docstrings in defprotcol, definterface
((list_lit :anchor (sym_lit) @def_symbol
(list_lit
:anchor (sym_lit) (vec_lit) :*
(str_lit) ,capture-symbol :anchor)
:*)
(:match ,clojure-ts--interface-def-symbol-regexp @def_symbol))))

(defvar clojure-ts--treesit-range-settings
(treesit-range-rules
:embed 'markdown_inline
:host 'clojure
(clojure-ts--docstring-query '@capture)))

(defun clojure-ts--font-lock-settings ()
"Return font lock settings suitable for use in `treesit-font-lock-settings'."
(treesit-font-lock-rules
Expand Down Expand Up @@ -333,29 +360,17 @@ Only intended for use at development time.")
'((tagged_or_ctor_lit marker: "#" @font-lock-preprocessor-face
tag: (sym_lit) @font-lock-preprocessor-face))

;; Figure out how to highlight symbols in docstrings.
;; Might require a markdown grammar
:feature 'doc
:language 'clojure
:override t
`(;; Captures docstrings in def, defonce
((list_lit :anchor (sym_lit) @def_symbol
:anchor (sym_lit) ; variable name
:anchor (str_lit) @font-lock-doc-face
:anchor (_)) ; the variable's value
(:match ,clojure-ts--variable-definition-symbol-regexp @def_symbol))
;; Captures docstrings defn, defmacro, ns, and things like that
((list_lit :anchor (sym_lit) @def_symbol
:anchor (sym_lit) ; function_name
:anchor (str_lit) @font-lock-doc-face)
(:match ,clojure-ts--definition-symbol-regexp @def_symbol))
;; Captures docstrings in defprotcol, definterface
((list_lit :anchor (sym_lit) @def_symbol
(list_lit
:anchor (sym_lit) (vec_lit) :*
(str_lit) @font-lock-doc-face :anchor)
:*)
(:match ,clojure-ts--interface-def-symbol-regexp @def_symbol)))
(clojure-ts--docstring-query '@font-lock-doc-face)

:feature 'markdown-doc
:language 'markdown_inline
:override t
`((inline (code_span
(code_span_delimiter) :* @font-lock-delimiter-face)
@font-lock-constant-face))

:feature 'quote
:language 'clojure
Expand Down Expand Up @@ -786,6 +801,9 @@ forms like deftype, defrecord, reify, proxy, etc."
(unless (treesit-language-available-p 'clojure nil)
(treesit-install-language-grammar 'clojure))
(setq-local comment-start ";")
(when (treesit-ready-p 'markdown_inline 'message)
(treesit-parser-create 'markdown_inline)
(setq-local treesit-range-settings clojure-ts--treesit-range-settings))
(when (treesit-ready-p 'clojure)
(treesit-parser-create 'clojure)
(setq-local treesit-font-lock-settings (clojure-ts--font-lock-settings)
Expand All @@ -798,7 +816,7 @@ forms like deftype, defrecord, reify, proxy, etc."
treesit-font-lock-feature-list
'((comment definition variable)
(keyword string char symbol builtin type)
(constant number quote metadata doc)
(constant number quote metadata doc markdown-doc)
(bracket deref function regex tagged-literals)))
(when (boundp 'treesit-thing-settings) ;; Emacs 30+
(setq-local treesit-thing-settings clojure-ts--thing-settings))
Expand Down
1 change: 1 addition & 0 deletions test/indentation.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
(ns indentation
"Docstring `important`. asdf"
(:require
[clojure.string :as str])
(:import
Expand Down

0 comments on commit 29433a8

Please sign in to comment.