Skip to content

feat: highlight rust string interpolation macros that use format_args! #13533

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions book/src/generated/lang-support.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@
| rst | ✓ | | | |
| ruby | ✓ | ✓ | ✓ | `ruby-lsp`, `solargraph` |
| rust | ✓ | ✓ | ✓ | `rust-analyzer` |
| rust-format-args | ✓ | | | |
| sage | ✓ | ✓ | | |
| scala | ✓ | ✓ | ✓ | `metals` |
| scheme | ✓ | | ✓ | |
Expand Down
8 changes: 4 additions & 4 deletions helix-term/tests/test/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,7 @@ async fn surround_replace_ts() -> anyhow::Result<()> {
const INPUT: &str = r#"\
fn foo() {
if let Some(_) = None {
todo!("f#[|o]#o)");
testing!("f#[|o]#o)");
}
}
"#;
Expand All @@ -744,7 +744,7 @@ fn foo() {
r#"\
fn foo() {
if let Some(_) = None {
todo!('f#[|o]#o)');
testing!('f#[|o]#o)');
}
}
"#,
Expand All @@ -757,7 +757,7 @@ fn foo() {
r#"\
fn foo() {
if let Some(_) = None [
todo!("f#[|o]#o)");
testing!("f#[|o]#o)");
]
}
"#,
Expand All @@ -770,7 +770,7 @@ fn foo() {
r#"\
fn foo() {
if let Some(_) = None {
todo!{"f#[|o]#o)"};
testing!{"f#[|o]#o)"};
}
}
"#,
Expand Down
4 changes: 2 additions & 2 deletions helix-term/tests/test/movement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,9 +379,9 @@ async fn match_around_closest_ts() -> anyhow::Result<()> {
test_with_config(
AppBuilder::new().with_file("foo.rs", None),
(
r#"fn main() {todo!{"f#[|oo]#)"};}"#,
r#"fn main() {testing!{"f#[|oo]#)"};}"#,
"mam",
r#"fn main() {todo!{#[|"foo)"]#};}"#,
r#"fn main() {testing!{#[|"foo)"]#};}"#,
),
)
.await?;
Expand Down
10 changes: 10 additions & 0 deletions languages.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4360,3 +4360,13 @@ file-types = [ { glob = "dunst/dunstrc" } ]
[[grammar]]
name = "dunstrc"
source = { git = "https://github.com/rotmh/tree-sitter-dunstrc", rev = "9cb9d5cc51cf5e2a47bb2a0e2f2e519ff11c1431" }

[[language]]
name = "rust-format-args"
scope = "source.rust-format-args"
file-types = []
injection-regex = "rust-format-args"

[[grammar]]
name = "rust-format-args"
source = { git = "https://github.com/nik-rev/tree-sitter-rustfmt", rev = "2ca0bdd763d0c9dbb1d0bd14aea7544cbe81309c" }
30 changes: 30 additions & 0 deletions runtime/queries/rust-format-args/highlights.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
; regular escapes like `\n` are detected using another grammar
; Here, we only detect `{{` and `}}` as escapes for `{` and `}`
(escaped) @constant.character.escape

[
"#"
(type)
] @special

[
(sign)
(fill)
(align)
(width)
] @operator

(number) @constant.numeric

(colon) @punctuation

(identifier) @variable

; SCREAMING_CASE is assumed to be constant
((identifier) @constant
(#match? @constant "^[A-Z_]+$"))

[
"{"
"}"
] @punctuation.special
124 changes: 124 additions & 0 deletions runtime/queries/rust/injections.scm
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,127 @@
]
)
(#set! injection.language "sql"))

; Special language `tree-sitter-rust-format-args` for Rust macros,
; which use `format_args!` under the hood and therefore have
; the `format_args!` syntax.
;
; This language is injected into a hard-coded set of macros.

; 1st argument is `format_args!`
(
(macro_invocation
macro:
[
(scoped_identifier
name: (_) @_macro_name)
(identifier) @_macro_name
]
(token_tree
. (string_literal
(string_content) @injection.content
)
)
)
(#any-of? @_macro_name
; std
"print" "println" "eprint" "eprintln"
"format" "format_args" "todo" "panic"
"unreachable" "unimplemented" "compile_error"
; log
"crit" "trace" "debug" "info" "warn" "error"
; anyhow
"anyhow" "bail"
; syn
"format_ident"
; indoc
"formatdoc" "printdoc" "eprintdoc" "writedoc"
; iced
"text"
; ratatui
"span"
; eyre
"eyre"
; miette
"miette"
)
(#set! injection.language "rust-format-args")
(#set! injection.include-children)
)

; 2nd argument is `format_args!`
(
(macro_invocation
macro:
[
(scoped_identifier
name: (_) @_macro_name)
(identifier) @_macro_name
]
(token_tree
. (_)
. (string_literal
(string_content) @injection.content
)
)
)
(#any-of? @_macro_name
; std
"write" "writeln" "assert" "debug_assert"
; defmt
"expect" "unwrap"
; ratatui
"span"
)
(#set! injection.language "rust-format-args")
(#set! injection.include-children)
)

; 3rd argument is `format_args!`
(
(macro_invocation
macro:
[
(scoped_identifier
name: (_) @_macro_name)
(identifier) @_macro_name
]
(token_tree
. (_)
. (_)
. (string_literal
(string_content) @injection.content
)
)
)
(#any-of? @_macro_name
; std
"assert_eq" "debug_assert_eq" "assert_ne" "debug_assert_ne"
)
(#set! injection.language "rust-format-args")
(#set! injection.include-children)
)

; Dioxus' "rsx!" macro relies heavily on string interpolation as well. The strings can be nested very deeply
(
(macro_invocation
macro: [
(scoped_identifier
name: (_) @_macro_name)
(identifier) @_macro_name
]
; TODO: This only captures 1 level of string literals. But in dioxus you can have
; nested string literals. For instance:
;
; rsx! { "{hello} world" }:
; -> (token_tree (string_literal))
; rsx! { div { "{hello} world" } }
; -> (token_tree (token_tree (string_literal)))
; rsx! { div { div { "{hello} world" } } }
; -> (token_tree (token_tree (token_tree (string_literal))))
(token_tree (string_literal) @injection.content)
)
(#eq? @_macro_name "rsx")
(#set! injection.language "rust-format-args")
(#set! injection.include-children)
)