emacs-devel
[Top][All Lists]
Advanced

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

Adding jsdoc support to js-ts-mode


From: Damien Cassou
Subject: Adding jsdoc support to js-ts-mode
Date: Fri, 07 Jun 2024 09:55:25 +0200

Hi,

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).

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?

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)))))

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

Some new rules for `treesit-font-lock-settings':

  (treesit-font-lock-rules
              :language 'jsdoc
              :override t
              :feature 'keyword
              '((tag_name) @font-lock-keyword-face)

              :language 'jsdoc
              :override t
              :feature 'bracket
              '((["{" "}"]) @font-lock-bracket-face)

              :language 'jsdoc
              :override t
              :feature 'property
              '((type) @font-lock-variable-use-face)

              :language 'jsdoc
              :override t
              :feature 'definition
              '((identifier) @font-lock-variable-name-face)

              :language 'jsdoc
              :override t
              :feature 'comment
              '((description) @font-lock-comment-face))

Best

-- 
Damien Cassou

"Success is the ability to go from one failure to another without
losing enthusiasm." --Winston Churchill

Attachment: after.png
Description: PNG image

Attachment: before.png
Description: PNG image


reply via email to

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