From 29433a8fe329c83e4d1a2514c5dd033c8e9231b3 Mon Sep 17 00:00:00 2001 From: Danny Freeman Date: Fri, 8 Sep 2023 22:54:47 -0400 Subject: [PATCH] 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 --- clojure-ts-mode.el | 60 ++++++++++++++++++++++++++++---------------- test/indentation.clj | 1 + 2 files changed, 40 insertions(+), 21 deletions(-) diff --git a/clojure-ts-mode.el b/clojure-ts-mode.el index b2554e8..22459ff 100644 --- a/clojure-ts-mode.el +++ b/clojure-ts-mode.el @@ -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 @@ -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 @@ -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) @@ -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)) diff --git a/test/indentation.clj b/test/indentation.clj index 520adc9..c8f2ae6 100644 --- a/test/indentation.clj +++ b/test/indentation.clj @@ -1,4 +1,5 @@ (ns indentation + "Docstring `important`. asdf" (:require [clojure.string :as str]) (:import