1

For example, here I am expecting sum and FILENAME to be in different colors than the rest of the string literal:

example screenshot

I am using VSCode with rust-analyzer v0.4.1395 (Pre-release).

Seems like Markdown also has this problem:

println!("this is a formatted string with a {variable}, and an expression: {3+5}"); 

edit:

  1. An example for my expectation can be seen in @Finomnis 's answer.
  2. As noted by people in the comments, expressions such as {3+5} are not supported in formatted strings in Rust.
  • I see a string literal highlighted as a string literal. Please [edit] your question to clarify what you expected. – cafce25 Feb 07 '23 at 10:48
  • What is your expectation here? – SirDarius Feb 07 '23 at 10:59
  • Rust-analyzer can in fact do that. Markdown won't ever be able to do that, though, because it lacks the semantic context to differentiate between a string and a format string. – Finomnis Feb 07 '23 at 11:40
  • @cafce25 I'm expecting the content within the curly braces, and the curly braces themselves to be in a different color (As seen in Finomnis's comment). I'll add it to the question. – VaderFuntime Feb 07 '23 at 14:00
  • @VaderFuntime Note that `"{3+5}"` is not a valid format string. Complex expressions cannot be used in format strings, only simple variable names. – Finomnis Feb 07 '23 at 14:01
  • @SirDarius updated the original question – VaderFuntime Feb 07 '23 at 14:05

3 Answers3

0

This requires semantic information, because not every string literal is a format string, and thus cannot be done by syntactic highlighting.

rust-analyzer handles this though, with semantic highlighting.

Besides, full expressions like 3+5 are not supported in format strings.

Chayim Friedman
  • 47,971
  • 5
  • 48
  • 77
  • Point taken about the full expressions in formatted strings. Do you have any idea though why doesn't the highlighting seem to work as expected for me? – VaderFuntime Feb 07 '23 at 14:02
0

I'm running VSCode 1.74.2 and rust-analyzer v0.3.1394, and it works without a problem:

enter image description here

With rust-analyzer 0.4.1395 (PreRelease), it looks the same to me.

Note that while rust-analyzer is still loading (spinning circle in the bottom bar), or if rust-analyzer encountered an error (displayed red in the bottom), only the VSCode internal Rust highlighting is enabled, which can not highlight strings syntactically:

enter image description here

Finomnis
  • 18,094
  • 1
  • 20
  • 27
  • Weird. rust-analyzer definitely finished loading, but still, looks wrong. Also tried downgrading to the release version, no change. – VaderFuntime Feb 07 '23 at 13:57
0

It seems like it's theme dependent:
Dark+ (default dark):
default dark+ theme

Noctis Obscuro:
enter image description here

Edit: it's a theme setting: vscode editor-semantic-highlighting
settings.json:

"editor.semanticTokenColorCustomizations": {
    "[Noctis*]": {
        "enabled": true
    }
}
c-antin
  • 91
  • 6