-
-
Notifications
You must be signed in to change notification settings - Fork 116
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
Conversation
Hey @maxbrunsfeld I would really appreciate if you could review this. Thank you. |
Is there any review planned soon? This feature is really cool! |
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 |
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. |
My point still stands, it's not valid in all string contexts and rust-analyzer handles this anyways |
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. |
The feature exists in Rust's LSP |
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 |
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 |
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. |
I made a separate tree-sitter grammar for Rust's string interpolation macros that use Grammar: https://github.com/nik-rev/tree-sitter-rust-format-args/tree/main Example with syntax highlighting: |
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