Skip to content
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

feat: Highlight Rust String interpolation macros #12768

Open
wants to merge 9 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 @@ -188,6 +188,7 @@
| rst || | | |
| ruby |||| `ruby-lsp`, `solargraph` |
| rust |||| `rust-analyzer` |
| rustfmt || | | |
| sage ||| | |
| scala |||| `metals` |
| scheme || || |
Expand Down
10 changes: 10 additions & 0 deletions languages.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4111,3 +4111,13 @@ indent = { tab-width = 2, unit = " " }
[[grammar]]
name = "ghostty"
source = { git = "https://github.com/bezhermoso/tree-sitter-ghostty" , rev = "8438a93b44367e962b2ea3a3b6511885bebd196a" }

[[language]]
name = "rustfmt"
scope = "source.rustfmt"
file-types = []
injection-regex = "rustfmt"

[[grammar]]
name = "rustfmt"
source = { git = "https://github.com/nik-rev/tree-sitter-rustfmt", rev = "d8e517d9aca1905261a378b25688e4500ae4dd22" }
33 changes: 33 additions & 0 deletions runtime/queries/rust/injections.scm
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,39 @@
(#set! injection.language "html")
(#set! injection.include-children))

; std::fmt

((macro_invocation
macro:
[
(scoped_identifier
name: (_) @_macro_name)
(identifier) @_macro_name
]
(token_tree) @injection.content)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than injecting into the whole token tree this should be targetting specifically (string_contents) nodes, and not setting injection.include-children. The rustfmt part shouldn't need to handle escapes since the Rust parser already does that (plus it handles all cases like unicode, e.g. \u{20FF}), and rustfmt doesn't have enough information to know whether escapes should be highlighted or not - they shouldn't be inside raw string literals (r#"foo"#).

Copy link
Contributor Author

@nik-rev nik-rev Feb 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than injecting into the whole token tree this should be targetting specifically (string_contents) nodes, and not setting injection.include-children. The rustfmt part shouldn't need to handle escapes since the Rust parser already does that (plus it handles all cases like unicode, e.g. \u{20FF}), and rustfmt doesn't have enough information to know whether escapes should be highlighted or not - they shouldn't be inside raw string literals (r#"foo"#).

(escaped) is only responsible for detecting escaped {{ and }}, for example format!("{{ hello }}") will evaluate to "{ hello }". Since those aren't covered by rust's parser

Copy link
Contributor Author

@nik-rev nik-rev Feb 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried setting string_contents using a variety of ways:

Before:

   (token_tree) @injection.content)

I removed the injection.include-children and then tried changing it to all of:

   (string_contents) @injection.content)
   (token_tree (string_contents) @injection.content))
   (token_tree (string_literal (string_contents) @injection.content)))
   (token_tree (string_contents)) @injection.content)

But in each case Rust syntax highlighting turns off. is it also related to #10286 ?

(#any-of? @_macro_name
; std
"format"
"write"
"writeln"
"print"
"println"
"eprint"
"eprintln"
"format_args"
; log
"crit"
"error"
"warn"
"info"
"debug"
"trace"
; anyhow
"anyhow"
"bail")
(#set! injection.language "rustfmt")
(#set! injection.include-children))

((macro_invocation
macro:
[
Expand Down
29 changes: 29 additions & 0 deletions runtime/queries/rustfmt/highlights.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
; (format_string) @string

(escaped) @constant.character.escape

[
"#"
(type)
] @special

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

(number) @constant.numeric

(colon) @punctuation

(identifier) @variable

((identifier) @constant
(#match? @constant "^[A-Z_]+$"))

[
"{"
"}"
] @punctuation.special