Skip to content

Commit 974caf0

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

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
@@ -139,6 +139,7 @@
139139
| make || || |
140140
| markdoc || | | `markdoc-ls` |
141141
| markdown || | | `marksman`, `markdown-oxide` |
142+
| markdown-rustdoc || | | |
142143
| markdown.inline || | | |
143144
| matlab |||| |
144145
| mermaid || | | |

languages.toml

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

1724+
[[language]]
1725+
name = "markdown-rustdoc"
1726+
scope = "source.markdown-rustdoc"
1727+
grammar = "markdown"
1728+
injection-regex = "markdown-rustdoc"
1729+
file-types = []
1730+
indent = { tab-width = 2, unit = " " }
1731+
block-comment-tokens = { start = "<!--", end = "-->" }
1732+
17241733
[[language]]
17251734
name = "markdown.inline"
17261735
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)