jsdoc.el icon indicating copy to clipboard operation
jsdoc.el copied to clipboard

Add syntax highlighting for JSDoc blocks

Open minikN opened this issue 1 year ago • 0 comments

Continuing the discussion about JSDoc syntax highlighting:

While working on this I thought it would be nice to have some syntax highlighting for jsdoc blocks. There actualls is a grammer for JSDoc. I tried using it but my understanding for tree-sitter is still very limited. As far as I understood it, one has to set ranges for each parser to act upon.. this would mean we need to patch js-ts-mode upstream to implement those ranges... or am I understanding it wrong? Maybe you have a better grasp on it.

Nice idea! I don't really know how *-ts-mode's work, so I looked a bit for js-ts-mode and from my understanding:

  • First, we need to define treesit-font-lock-settings using treesit-font-lock-rules.
  • Then we need to update js--treesit-font-lock-setting to something like the following.

This is the default:

(defvar js--treesit-font-lock-settings
  (treesit-font-lock-rules
    :language 'javascript
    :feature 'comment
    '((comment) @font-lock-comment-face)
   ...

Documentation says instead of a face, we can give a function:

(defvar js--treesit-font-lock-settings
  (treesit-font-lock-rules
    :language 'javascript
    :feature 'comment
    `((comment) (lambda (node override start end &rest _) <somehow fontify the region with jsdoc-ts font lock settings?>)
   ...

The part in angle brackets might be something like this:

(let ((treesit-font-lock-settings <font lock settings we defined for jsdoc-ts>))
  (treesit-font-lock-fontify-region start end))

These are just a mild guess, not quite sure if this is the right way or even if it works or not. I might try when I get a chance.

I don't think the upstream patch would be accepted because not everyone uses JSDoc in their comments. What can be done is simply overriding js--treesit-font-lock-settings with a newly defined one or something similar that has the same effect.

I will take a more closer looking into your suggestions when I have time. This issue can serve as a discussion basis.

minikN avatar Feb 21 '23 13:02 minikN