|
97 | 97 | ] |
98 | 98 | ) |
99 | 99 | (#set! injection.language "sql")) |
| 100 | + |
| 101 | +; Special language `tree-sitter-rust-format-args` for Rust macros, |
| 102 | +; which use `format_args!` under the hood and therefore have |
| 103 | +; the `format_args!` syntax. |
| 104 | +; |
| 105 | +; This language is injected into a hard-coded set of macros. |
| 106 | + |
| 107 | +; 1st argument is `format_args!` |
| 108 | +( |
| 109 | + (macro_invocation |
| 110 | + macro: |
| 111 | + [ |
| 112 | + (scoped_identifier |
| 113 | + name: (_) @_macro_name) |
| 114 | + (identifier) @_macro_name |
| 115 | + ] |
| 116 | + (token_tree |
| 117 | + . (string_literal |
| 118 | + (string_content) @injection.content |
| 119 | + ) |
| 120 | + ) |
| 121 | + ) |
| 122 | + (#any-of? @_macro_name |
| 123 | + ; std |
| 124 | + "print" "println" "eprint" "eprintln" |
| 125 | + "format" "format_args" "todo" "panic" |
| 126 | + "unreachable" "unimplemented" "compile_error" |
| 127 | + ; log |
| 128 | + "crit" "trace" "debug" "info" "warn" "error" |
| 129 | + ; anyhow |
| 130 | + "anyhow" "bail" |
| 131 | + ; syn |
| 132 | + "format_ident" |
| 133 | + ; indoc |
| 134 | + "formatdoc" "printdoc" "eprintdoc" "writedoc" |
| 135 | + ; iced |
| 136 | + "text" |
| 137 | + ; ratatui |
| 138 | + "span" |
| 139 | + ; eyre |
| 140 | + "eyre" |
| 141 | + ; miette |
| 142 | + "miette" |
| 143 | + ) |
| 144 | + (#set! injection.language "rust-format-args") |
| 145 | + (#set! injection.include-children) |
| 146 | +) |
| 147 | + |
| 148 | +; 2nd argument is `format_args!` |
| 149 | +( |
| 150 | + (macro_invocation |
| 151 | + macro: |
| 152 | + [ |
| 153 | + (scoped_identifier |
| 154 | + name: (_) @_macro_name) |
| 155 | + (identifier) @_macro_name |
| 156 | + ] |
| 157 | + (token_tree |
| 158 | + . (_) |
| 159 | + . (string_literal |
| 160 | + (string_content) @injection.content |
| 161 | + ) |
| 162 | + ) |
| 163 | + ) |
| 164 | + (#any-of? @_macro_name |
| 165 | + ; std |
| 166 | + "write" "writeln" "assert" "debug_assert" |
| 167 | + ; defmt |
| 168 | + "expect" "unwrap" |
| 169 | + ; ratatui |
| 170 | + "span" |
| 171 | + ) |
| 172 | + (#set! injection.language "rust-format-args") |
| 173 | + (#set! injection.include-children) |
| 174 | +) |
| 175 | + |
| 176 | +; 3rd argument is `format_args!` |
| 177 | +( |
| 178 | + (macro_invocation |
| 179 | + macro: |
| 180 | + [ |
| 181 | + (scoped_identifier |
| 182 | + name: (_) @_macro_name) |
| 183 | + (identifier) @_macro_name |
| 184 | + ] |
| 185 | + (token_tree |
| 186 | + . (_) |
| 187 | + . (_) |
| 188 | + . (string_literal |
| 189 | + (string_content) @injection.content |
| 190 | + ) |
| 191 | + ) |
| 192 | + ) |
| 193 | + (#any-of? @_macro_name |
| 194 | + ; std |
| 195 | + "assert_eq" "debug_assert_eq" "assert_ne" "debug_assert_ne" |
| 196 | + ) |
| 197 | + (#set! injection.language "rust-format-args") |
| 198 | + (#set! injection.include-children) |
| 199 | +) |
| 200 | + |
| 201 | +; Dioxus' "rsx!" macro relies heavily on string interpolation as well. The strings can be nested very deeply |
| 202 | +( |
| 203 | + (macro_invocation |
| 204 | + macro: [ |
| 205 | + (scoped_identifier |
| 206 | + name: (_) @_macro_name) |
| 207 | + (identifier) @_macro_name |
| 208 | + ] |
| 209 | + ; TODO: This only captures 1 level of string literals. But in dioxus you can have |
| 210 | + ; nested string literals. For instance: |
| 211 | + ; |
| 212 | + ; rsx! { "{hello} world" }: |
| 213 | + ; -> (token_tree (string_literal)) |
| 214 | + ; rsx! { div { "{hello} world" } } |
| 215 | + ; -> (token_tree (token_tree (string_literal))) |
| 216 | + ; rsx! { div { div { "{hello} world" } } } |
| 217 | + ; -> (token_tree (token_tree (token_tree (string_literal)))) |
| 218 | + (token_tree (string_literal) @injection.content) |
| 219 | + ) |
| 220 | + (#eq? @_macro_name "rsx") |
| 221 | + (#set! injection.language "rust-format-args") |
| 222 | + (#set! injection.include-children) |
| 223 | +) |
0 commit comments