tree-sitter / tree-sitter-rust

Rust grammar for tree-sitter
MIT License
340 stars 97 forks source link

Parse interpolation and formatting in strings #164

Closed mucinoab closed 1 year ago

mucinoab commented 1 year ago

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 commented 1 year ago

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

ValouBambou commented 1 year ago

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

amaanq commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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

ValouBambou commented 1 year ago

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 commented 1 year ago

The feature exists in Rust's LSP

ValouBambou commented 1 year ago

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 commented 1 year ago

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

mucinoab commented 1 year ago

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 commented 1 year ago

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 commented 9 months ago

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