Skip to content
New issue

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

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

Already on GitHub? # 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 3 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" }
4 changes: 4 additions & 0 deletions runtime/queries/rust/injections.scm
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,7 @@
[(string_literal) (raw_string_literal)] @injection.content
)
(#set! injection.language "sql"))

; TODO: change this so it injects into all strings within a macro call
((string_content) @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.

I think we need to be smarter here and match on the macro name

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree, I think just injecting it for stdlib and a few known crate macros (e.g. slog and tracing macros) would be a huge improvement already, and then you could always add more crates over time.

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.

The following query should inject the language into the 1st argument, but it doesn't work due to a bug in tree-sitter which has been maybe fixed in v0.25

((macro_invocation
   macro:
     [
       (scoped_identifier
         name: (_) @_macro_name)
       (identifier) @_macro_name
     ]
   (token_tree . (string_literal) @injection.content))
 (#eq? @_macro_name "format_args")
 (#set! injection.language "rustfmt")
 (#set! injection.include-children))

For now, i made this PR such that it injects the rustfmt parser into every argument of these macros:

 (#any-of? @_macro_name
  ; std
  "format"
  "write"
  "writeln"
  "print"
  "println"
  "eprint"
  "eprintln"
  "format_args"
  ; log
  "crit"
  "error"
  "warn"
  "info"
  "debug"
  "trace"
  ; anyhow
  "anyhow"
  "bail"
  "ensure")

Once Helix updates to Tree-Sitter v0.25, we can make a new PR that will inject only into positional arguments

(#set! injection.language "rustfmt"))
31 changes: 31 additions & 0 deletions runtime/queries/rustfmt/highlights.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
; (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

(ERROR) @error
nik-rev marked this conversation as resolved.
Show resolved Hide resolved
Loading