tree-sitter-rust
tree-sitter-rust copied to clipboard
Is it possible to highlight code examples in doc comments
Not sure about the capabilities of treesitter, but would it be possible to highlight the code in doc comments? e.g.:
/// ```rs
/// enum Test {
///
/// }
/// ```
If tree-sitter adopts an official markdown parser implementation written in Rust, then this could perhaps be used as an embedded parser in a recursive fashion?
This would be really cool. Many IDEs are using semantic highlighting for this, but I believe treesitter would be perfect for this.
I haven't tried this yet, but I stumbled on a technique for highlighting embedded languages in this video that might work here.
Hacky fix
This works for neovim:
In my $XDG_CONFIG_HOME/nvim/queries/rust I have a file called: injections.scm with the following:
; extends
(
(line_comment) @_first
(_) @rust
(line_comment) @_last
(#match? @_first "^/// ```$")
(#match? @_last "^/// ```$")
(#offset! @rust 0 4 0 4)
)
(
(line_comment) @_first
(_) @rust
(line_comment) @_last
(#match? @_first "^/// ```rust$")
(#match? @_last "^/// ```$")
(#offset! @rust 0 4 0 4)
)
Now I get highlights like this:
The problem is that this makes neovim very slow, but it's a starting point...
out of scope for this parser, but this is doable with some post processing via queries/injections or other mechanisms.