neovimtreesitter

NeoVim Treesitter RST highlight for Python docstrings


I'm trying to configure treesitter to apply rst highlight group to docstrings in Python.

In after/queries/python/highlights.scm I set the following:

(module . (expression_statement (string (string_content) @comment)))

That works, but if I set it to rst:

(module . (expression_statement (string (string_content) @rst)))

It goes back to highlighting docstrings the same as normal strings.

rst is in checkhealth nvim-treesitter:

Parser/Features         H L F I J
...
 - rst                  ✓ ✓ . . ✓
...

Any help would be appreciated, thanks.


Solution

  • I found what I needed, injections go in queries/python/injections.scm, not after/queries/python/highlights.scm, and are done like so:

    ;; extends
    
    ; function/method docstring
    (function_definition
      body: (block .
        (expression_statement
          (string
            (string_content) @injection.content (#set! injection.language "rst")
          )
        )
      )
    )
    
    ; module docstring
    (module .
      (expression_statement
        (string
          (string_content) @injection.content (#set! injection.language "rst")
        )
      )
    )