emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Adding jsdoc support to js-ts-mode


From: Dmitry Gutov
Subject: Re: Adding jsdoc support to js-ts-mode
Date: Sun, 9 Jun 2024 06:02:31 +0300
User-agent: Mozilla Thunderbird

Hi Damien,

On 07/06/2024 10:55, Damien Cassou wrote:
Currently, jsdoc comments are not recognized in js-ts-mode (see
before.png screenshot attached). I'm trying to fix that (see current
status in after.png).

I guess you're using the jsdoc tree-sitter grammar in addition to Lisp code. The alternative would be pure-regexp matching, but this might be easier indeed.

As you can see in the after.png screenshot, there is no face applied to
the comment markup "/*" and "*". I would like to see
`font-lock-comment-face` applied. I haven't written any code to do that
because I don't know how to apply a face to characters that are not
treesit nodes. Can someone please help?

You could try adding a custom rule which applies highlights to specific ranges. The function to call is treesit-fontify-with-override.

See its usage examples in the tree - most of them still deal with nodes, but you just need to pass the character positions, which can be calculated in different ways.

The code to get after.png is below. If you have any feedback, that would
be greatly appreciated. When everything is fine, I will submit a patch.

Please make sure I'm on CC when replying as I'm not subscribed to the
mailing list.

   (defun js-ts-language-at-point (point)
         "Return the language at POINT."
         (let ((node (treesit-node-at point 'javascript)))
           (if (and (treesit-ready-p 'jsdoc)
                    (equal (treesit-node-type node) "comment")
                    (string-match-p
                     (rx bos "/**")
                     (treesit-node-text node)))
               'jsdoc
             'javascript)))


To be added to js-ts-mode body:

   (setq treesit-range-settings
         (treesit-range-rules
          :embed 'jsdoc
          :host 'javascript
          `(((comment) @capture (:match ,(rx bos "/**") @capture)))))

Nice.

   (setq-local treesit-language-at-point-function #js-ts-language-at-point)

Seems like this needs ' after #.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]