tree-sitter-rust icon indicating copy to clipboard operation
tree-sitter-rust copied to clipboard

Is it possible to highlight code examples in doc comments

Open ModProg opened this issue 3 years ago • 4 comments
trafficstars

Not sure about the capabilities of treesitter, but would it be possible to highlight the code in doc comments? e.g.:

/// ```rs
/// enum Test {
///
/// }
/// ```

ModProg avatar Jan 25 '22 10:01 ModProg

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?

kellpossible avatar Jul 12 '22 02:07 kellpossible

This would be really cool. Many IDEs are using semantic highlighting for this, but I believe treesitter would be perfect for this.

SeniorMars avatar Feb 14 '23 20:02 SeniorMars

I haven't tried this yet, but I stumbled on a technique for highlighting embedded languages in this video that might work here.

maxcountryman avatar Feb 14 '23 20:02 maxcountryman

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:

image

The problem is that this makes neovim very slow, but it's a starting point...

SeniorMars avatar Mar 05 '23 04:03 SeniorMars

out of scope for this parser, but this is doable with some post processing via queries/injections or other mechanisms.

amaanq avatar Apr 07 '24 04:04 amaanq