Skip to content

Commit be1cf09

Browse files
queries: Inject markdown into Rust doc comments
Co-authored-by: Nik Revenco <[email protected]>
1 parent a3b64b6 commit be1cf09

File tree

5 files changed

+41
-1
lines changed

5 files changed

+41
-1
lines changed

book/src/generated/lang-support.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@
141141
| make || || |
142142
| markdoc || | | `markdoc-ls` |
143143
| markdown || | | `marksman`, `markdown-oxide` |
144+
| markdown-rustdoc || | | |
144145
| markdown.inline || | | |
145146
| matlab |||| |
146147
| mermaid || | | |

languages.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1739,6 +1739,15 @@ block-comment-tokens = { start = "<!--", end = "-->" }
17391739
name = "markdown"
17401740
source = { git = "https://github.com/tree-sitter-grammars/tree-sitter-markdown", rev = "62516e8c78380e3b51d5b55727995d2c511436d8", subpath = "tree-sitter-markdown" }
17411741

1742+
[[language]]
1743+
name = "markdown-rustdoc"
1744+
scope = "source.markdown-rustdoc"
1745+
grammar = "markdown"
1746+
injection-regex = "markdown-rustdoc"
1747+
file-types = []
1748+
indent = { tab-width = 2, unit = " " }
1749+
block-comment-tokens = { start = "<!--", end = "-->" }
1750+
17421751
[[language]]
17431752
name = "markdown.inline"
17441753
scope = "source.markdown.inline"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
; inherits: markdown
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
; inherits: markdown
2+
3+
; In Rust, it is common to have documentation code blocks not specify the
4+
; language, and it is assumed to be Rust if it is not specified.
5+
6+
(fenced_code_block
7+
(code_fence_content) @injection.content
8+
(#set! injection.language "rust")
9+
(#set! injection.include-unnamed-children))
10+
11+
(fenced_code_block
12+
(info_string
13+
(language) @injection.language)
14+
(code_fence_content) @injection.content (#set! injection.include-unnamed-children))
15+
16+
(fenced_code_block
17+
(info_string
18+
(language) @__language)
19+
(code_fence_content) @injection.content
20+
; list of attributes for Rust syntax highlighting:
21+
; https://doc.rust-lang.org/rustdoc/write-documentation/documentation-tests.html#attributes
22+
(#match? @__language
23+
"(ignore|should_panic|no_run|compile_fail|standalone_crate|custom|edition*)")
24+
(#set! injection.language "rust")
25+
(#set! injection.include-unnamed-children))

runtime/queries/rust/injections.scm

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
([(line_comment) (block_comment)] @injection.content
1+
([(line_comment !doc) (block_comment !doc)] @injection.content
22
(#set! injection.language "comment"))
33

4+
((doc_comment) @injection.content
5+
(#set! injection.language "markdown-rustdoc")
6+
(#set! injection.combined))
7+
48
((macro_invocation
59
macro:
610
[

0 commit comments

Comments
 (0)