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

Merged
merged 3 commits into from
May 24, 2025

Conversation

nik-rev
Copy link
Contributor

@nik-rev nik-rev commented May 14, 2025

This is a revival of #12768

This adds syntax highlighting to a hard-coded set of Rust macros that accept a string literal which will be passed to format_args!.

All std macros are included as well as several crates

image

One thing that I'm not sure how to fix, is that the rust language syntax highlighter injections seems to break for other arguments when I inject rust-format-args into the 1st argument

I don't know if it's something we can do about the query or tree-house

  (macro_invocation
    macro:
      [
        (scoped_identifier
          name: (_) @_macro_name)
        (identifier) @_macro_name
      ]
    (token_tree
      . (string_literal
        (string_content) @injection.content
      )
    )
  )

Before

image

After

image

@nik-rev nik-rev changed the title feat: highlight rust macros that use format_args! feat: highlight rust string interpolation macros that use format_args! May 14, 2025
@nik-rev nik-rev marked this pull request as draft May 14, 2025 16:37
@nik-rev nik-rev marked this pull request as ready for review May 14, 2025 19:10
fix: integration test failure
@the-mikedavis
Copy link
Member

I tried some tweaks to the queries but it seems like we can't currently select the other items in the token tree to inject them as Rust. It'd be nice to fix but I don't want it to block this PR

@the-mikedavis the-mikedavis merged commit 1023e8f into helix-editor:master May 24, 2025
7 checks passed
CalebLarsen pushed a commit to CalebLarsen/helix that referenced this pull request May 25, 2025
krabodyan pushed a commit to krabodyan/helix that referenced this pull request May 27, 2025
Trivernis pushed a commit to Trivernis/helix-plus that referenced this pull request Jun 10, 2025
@Rudxain
Copy link
Contributor

Rudxain commented Jun 23, 2025

I assume this is a known issue, but I'll report it anyways. At commit 1023e8f, this buffer doesn't highlight properly:

fn main() {
	println!(
		"{}",
		std::fs::read(std::env::args().nth(1).unwrap())
			.unwrap()
			.into_iter()
			.fold(0, |count, c| count + usize::from(c == b'\n'))
	);
}

The parent commit (223ceec) does color it as expected

paulprimus pushed a commit to paulprimus/helix that referenced this pull request Jun 24, 2025
@the-mikedavis
Copy link
Member

Yeah, originally I was thinking that this wasn't a big deal but after using this change for a while it is quite noticeable, at least because I like printf debugging :P

I believe it's an issue in tree-house where we need to refine the precedence so that we can have overlapping injections like overlapping highlights, but I haven't looked into it much yet

If it isn't practical to fix before the next release (probably early July) then I would consider reverting this temporarily before the release and reapplying after so that we preserve the behavior of the last release

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants