tree-sitter / tree-sitter-rust

Rust grammar for tree-sitter
MIT License
357 stars 101 forks source link

Expose doc comment contents #212

Closed the-mikedavis closed 7 months ago

the-mikedavis commented 9 months ago

195 parsed doc comments but it exposed the nodes for the doc comment markers (!//) rather than the content of the comments. The comment content is useful for injecting markdown into the comments:

; injections.scm
([(line_comment !doc) (block_comment !doc)] @injection.content
 (#set! injection.language "comment"))

((doc_comment) @injection.content
 (#set! injection.language "markdown")
 (#set! injection.combined))

I've also changed the line doc comment slightly to fix an issue with the marker node (it unnecessarily consumed the character after the ///) and to include the line's line-ending, which is useful to tree-sitter-markdown.

~cc @ProfDoof I've only made the change to line comments, I may need some help to make block comments have the same structure~

Block comment changes are included as well.

amaanq commented 9 months ago

Do you want to alias it as a literal instead for consistency? (might be easier for queries too?)

the-mikedavis commented 9 months ago

Do you mean the marker or the comment's contents? I'm not very familiar with this grammar but it looks like the existing literal rules are used for values like ints/floats/etc. With these changes the queries we would add for Helix are pretty simple (- just what I listed above)

amaanq commented 9 months ago

I meant the markers, sorry for not being clear. I do like your content changes though

Though you'll need to rebase since there was an edge case I fixed w.r.t. empty line doc comments eating up contents on the following line

amaanq commented 9 months ago

Also regenerate with 0.21.0 :grin: (and redo the test updates with -u, that no longer updates all test header delimiters to be 80 chars long)

amaanq commented 7 months ago

Really sorry for taking forever on this

the-mikedavis commented 7 months ago

No worries, I've been testing this locally in the meantime.

Thanks!