Unnamed repository; edit this file 'description' to name the repository.
queries: Inject markdown into Rust doc comments
Co-authored-by: Nik Revenco <[email protected]>
Michael Davis 10 months ago
parent a3b64b6 · commit be1cf09
-rw-r--r--book/src/generated/lang-support.md1
-rw-r--r--languages.toml9
-rw-r--r--runtime/queries/markdown-rustdoc/highlights.scm1
-rw-r--r--runtime/queries/markdown-rustdoc/injections.scm25
-rw-r--r--runtime/queries/rust/injections.scm6
5 files changed, 41 insertions, 1 deletions
diff --git a/book/src/generated/lang-support.md b/book/src/generated/lang-support.md
index 5b2962a4..1447b66d 100644
--- a/book/src/generated/lang-support.md
+++ b/book/src/generated/lang-support.md
@@ -141,6 +141,7 @@
| make | ✓ | | ✓ | |
| markdoc | ✓ | | | `markdoc-ls` |
| markdown | ✓ | | | `marksman`, `markdown-oxide` |
+| markdown-rustdoc | ✓ | | | |
| markdown.inline | ✓ | | | |
| matlab | ✓ | ✓ | ✓ | |
| mermaid | ✓ | | | |
diff --git a/languages.toml b/languages.toml
index ae140b0c..7cc37300 100644
--- a/languages.toml
+++ b/languages.toml
@@ -1740,6 +1740,15 @@ name = "markdown"
source = { git = "https://github.com/tree-sitter-grammars/tree-sitter-markdown", rev = "62516e8c78380e3b51d5b55727995d2c511436d8", subpath = "tree-sitter-markdown" }
[[language]]
+name = "markdown-rustdoc"
+scope = "source.markdown-rustdoc"
+grammar = "markdown"
+injection-regex = "markdown-rustdoc"
+file-types = []
+indent = { tab-width = 2, unit = " " }
+block-comment-tokens = { start = "<!--", end = "-->" }
+
+[[language]]
name = "markdown.inline"
scope = "source.markdown.inline"
injection-regex = "markdown\\.inline"
diff --git a/runtime/queries/markdown-rustdoc/highlights.scm b/runtime/queries/markdown-rustdoc/highlights.scm
new file mode 100644
index 00000000..a3a30e34
--- /dev/null
+++ b/runtime/queries/markdown-rustdoc/highlights.scm
@@ -0,0 +1 @@
+; inherits: markdown
diff --git a/runtime/queries/markdown-rustdoc/injections.scm b/runtime/queries/markdown-rustdoc/injections.scm
new file mode 100644
index 00000000..0778125c
--- /dev/null
+++ b/runtime/queries/markdown-rustdoc/injections.scm
@@ -0,0 +1,25 @@
+; inherits: markdown
+
+; In Rust, it is common to have documentation code blocks not specify the
+; language, and it is assumed to be Rust if it is not specified.
+
+(fenced_code_block
+ (code_fence_content) @injection.content
+ (#set! injection.language "rust")
+ (#set! injection.include-unnamed-children))
+
+(fenced_code_block
+ (info_string
+ (language) @injection.language)
+ (code_fence_content) @injection.content (#set! injection.include-unnamed-children))
+
+(fenced_code_block
+ (info_string
+ (language) @__language)
+ (code_fence_content) @injection.content
+ ; list of attributes for Rust syntax highlighting:
+ ; https://doc.rust-lang.org/rustdoc/write-documentation/documentation-tests.html#attributes
+ (#match? @__language
+ "(ignore|should_panic|no_run|compile_fail|standalone_crate|custom|edition*)")
+ (#set! injection.language "rust")
+ (#set! injection.include-unnamed-children))
diff --git a/runtime/queries/rust/injections.scm b/runtime/queries/rust/injections.scm
index 964eec0f..42ca12b5 100644
--- a/runtime/queries/rust/injections.scm
+++ b/runtime/queries/rust/injections.scm
@@ -1,6 +1,10 @@
-([(line_comment) (block_comment)] @injection.content
+([(line_comment !doc) (block_comment !doc)] @injection.content
(#set! injection.language "comment"))
+((doc_comment) @injection.content
+ (#set! injection.language "markdown-rustdoc")
+ (#set! injection.combined))
+
((macro_invocation
macro:
[