changeset 6788:609d6c4223f6

queries: Inject markdown into Rust doc comments Co-authored-by: Nik Revenco <[email protected]>
author Michael Davis <mcarsondavis@gmail.com>
date Tue, 25 Feb 2025 11:57:27 -0500
parents 282921f22a00
children 8923fd2fae2a
files book/src/generated/lang-support.md languages.toml runtime/queries/markdown-rustdoc/highlights.scm runtime/queries/markdown-rustdoc/injections.scm runtime/queries/rust/injections.scm
diffstat 5 files changed, 41 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/book/src/generated/lang-support.md	Mon Feb 24 12:46:34 2025 -0500
+++ b/book/src/generated/lang-support.md	Tue Feb 25 11:57:27 2025 -0500
@@ -141,6 +141,7 @@
 | make | ✓ |  | ✓ |  |
 | markdoc | ✓ |  |  | `markdoc-ls` |
 | markdown | ✓ |  |  | `marksman`, `markdown-oxide` |
+| markdown-rustdoc | ✓ |  |  |  |
 | markdown.inline | ✓ |  |  |  |
 | matlab | ✓ | ✓ | ✓ |  |
 | mermaid | ✓ |  |  |  |
--- a/languages.toml	Mon Feb 24 12:46:34 2025 -0500
+++ b/languages.toml	Tue Feb 25 11:57:27 2025 -0500
@@ -1740,6 +1740,15 @@
 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"
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/runtime/queries/markdown-rustdoc/highlights.scm	Tue Feb 25 11:57:27 2025 -0500
@@ -0,0 +1,1 @@
+; inherits: markdown
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/runtime/queries/markdown-rustdoc/injections.scm	Tue Feb 25 11:57:27 2025 -0500
@@ -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))
--- a/runtime/queries/rust/injections.scm	Mon Feb 24 12:46:34 2025 -0500
+++ b/runtime/queries/rust/injections.scm	Tue Feb 25 11:57:27 2025 -0500
@@ -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:
      [