In c-mode
it's possible to show doxygen comments /** ... */
using font-lock-doc-face
. See: docs for c-doc-comment-style
.
How can this be done in c-ts-mode
?
I can find documentation pointing to treesit-font-lock-settings
but this is mainly focusing on supporting major-modes from scratch, not customizing existing modes.
This patch on Emacs-30 implements the feature but I would like to be able to achieve this without having to patch Emacs, I'm still not sure how to properly implement this as an extension though.
diff --git a/lisp/progmodes/c-ts-mode.el b/lisp/progmodes/c-ts-mode.el
index b3c48eb2c65..9e8fd18da66 100644
--- a/lisp/progmodes/c-ts-mode.el
+++ b/lisp/progmodes/c-ts-mode.el
@@ -541,13 +541,35 @@ c-ts-mode--for-each-tail-regexp
"LIVE_BUFFER" "FRAME"))
"A regexp matching all the variants of the FOR_EACH_* macro.")
+(defun c-ts-mode--comment-docstring (node override start end &rest _args)
+ "Use documentation face for /** ... */ comments."
+
+ (let* ((beg (treesit-node-start node))
+ (end (treesit-node-end node))
+ (prefix (buffer-substring-no-properties beg (+ beg 3))))
+ (cond
+ ((string-equal "/**" prefix)
+ (treesit-fontify-with-override
+ beg end
+ 'font-lock-doc-face override start end))
+ ((string-equal "//" (substring prefix 0 2))
+ (treesit-fontify-with-override
+ beg end
+ 'font-lock-preprocessor-face override start end))
+ (t
+ (treesit-fontify-with-override
+ beg end
+ 'font-lock-comment-face override start end)))))
+
(defun c-ts-mode--font-lock-settings (mode)
"Tree-sitter font-lock settings.
MODE is either `c' or `cpp'."
(treesit-font-lock-rules
:language mode
:feature 'comment
- `((comment) @font-lock-comment-face
+ `((comment) @c-ts-mode--comment-docstring
(comment) @contextual)
:language mode