1

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

ideasman42
  • 8,375
  • 1
  • 28
  • 105
  • What is your actual question? It looks like you’ve written the code correctly (though you need to fix the docstring for your new `c-ts-mode--comment-docstring` function). – db48x Aug 28 '23 at 11:39
  • @db48x my question is how to use `c-doc-comment-style` in `c-ts-mode`, WITHOUT a custom patched version of emacs. – ideasman42 Aug 28 '23 at 23:58

1 Answers1

0

If you just want to use this modification yourself, all you have to do is stick your new c-ts-mode--comment-docstring function and your modified c-ts-mode--font-lock-settings function into your init file (usually ~/.emacs.d/init.el).

When you load a C file and Emacs starts the c-ts-mode for you, your modified c-ts-mode--font-lock-settings function will be called instead of the original one that it replaced. Of course you could also use advice for that, but since you are replacing the whole return value there isn’t much point.

Naturally you should also send this patch off to the emacs-devel mailing list, if you haven’t already. I am sure the maintainers would be happy to see it, and then the next release of Emacs would have this change built in.

db48x
  • 15,741
  • 1
  • 19
  • 23
  • This means I need to maintain a copy of `c-ts-mode--font-lock-settings` in my `init.el` this is 163 lines on non-trivial elisp, with references to tree-sitter and `c-ts-mode` internals. Of course it can be done, but in that case I'd rather use a patched emacs. – ideasman42 Aug 30 '23 at 03:09
  • Yep. You could add a `:filter-return` advice that modifies the return value of the font lock settings. Either way, make sure you send your patch to the mailing list. – db48x Aug 30 '23 at 11:36