Skip to content

Parse interpolation and formatting in strings #164

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

Closed

Conversation

mucinoab
Copy link

@mucinoab mucinoab commented Nov 6, 2022

I based my approach in the python implementation.
This is my fist time working with the tree-sitter machinery, please let me know of any issues.

Solves #129

@mucinoab
Copy link
Author

mucinoab commented Nov 26, 2022

Hey @maxbrunsfeld I would really appreciate if you could review this. Thank you.

@ValouBambou
Copy link

Is there any review planned soon? This feature is really cool!

@amaanq
Copy link
Member

amaanq commented Jul 10, 2023

The difference with Rust & Python is that in Python, an f string's interpolations are always valid, whereas in Rust it's only valid in certain macro invocations, a regular string with brackets is not an interpolation, so I don't think this makes much sense and is better left to an lsp

@mucinoab
Copy link
Author

This is kind of a new feature for Rust. Announcing Rust 1.58.0: Captured identifiers in format strings

And there is still a lot of work going on to improve things.

@amaanq
Copy link
Member

amaanq commented Jul 10, 2023

My point still stands, it's not valid in all string contexts and rust-analyzer handles this anyways

@ValouBambou
Copy link

Sure, maybe it needs to be only in macro context, but the feature exists for other editors like intellij and vscode since at least one year if I remember correctly, so it shouldn't be so hard to implement.

@amaanq
Copy link
Member

amaanq commented Jul 15, 2023

The feature exists in Rust's LSP

@ValouBambou
Copy link

Since which version ? Because I can't see any sign of this working in my installation of rust-analyzer 1.71.0. Renaming doesn't apply to the variable inside the string format and coloration is still the same as regular string.

@amaanq
Copy link
Member

amaanq commented Jul 19, 2023

Since which version ? Because I can't see any sign of this working in my installation of rust-analyzer 1.71.0. Renaming doesn't apply to the variable inside the string format and coloration is still the same as regular string.

I can't comment on your IDE/setup, but in Neovim inspecting the LSP semantic tokens shows the following under my cursor

image
image
image

@amaanq amaanq closed this Jul 19, 2023
@mucinoab
Copy link
Author

I believe that it is okay for tree-sitter to have features that another system already has.

tree-sitter is designed to be very fast and run on every keystroke, rust analyzer on the other hand is not, it has to do a bunch of additional work and, depending the crate/project, this can take seconds or even minutes.

Two systems with two different goals. @amaanq

@amaanq
Copy link
Member

amaanq commented Jul 19, 2023

I believe that it is okay for tree-sitter to have features that another system already has.

Tree-sitter is not an lsp at all, if so, we'd have knowledge of when an identifier is a type, variable, or function when used anywhere. So that makes no sense

Introducing this would bring false positives in normal strings, rather than false negatives in print-like macro invocations. To me false negatives are better than false positives in this case, anywhere brackets are used would be highlighted incorrectly. You can also solve this with injections by writing a tiny rust format specifier grammar to inject in strings.

@jfvillablanca
Copy link

Since which version ? Because I can't see any sign of this working in my installation of rust-analyzer 1.71.0. Renaming doesn't apply to the variable inside the string format and coloration is still the same as regular string.

I can't comment on your IDE/setup, but in Neovim inspecting the LSP semantic tokens shows the following under my cursor

image image image

Hello, I am trying to figure out why I don't get syntax highlighting for string interpolation in rust
2023-12-13_21-32

I am currently :Inspect-ing on params in the line println!("->> {:<12} - handler_hello - {params:?}", "HANDLER");

I'm wondering if I missed something in my config

@nik-rev
Copy link

nik-rev commented May 25, 2025

I made a separate tree-sitter grammar for Rust's string interpolation macros that use format_args!.

Grammar: https://github.com/nik-rev/tree-sitter-rust-format-args/tree/main
It is used in Helix: helix-editor/helix@1023e8f

Example with syntax highlighting:

image

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.

5 participants