Skip to content

Parse interpolation and formatting in strings#164

Closed
mucinoab wants to merge 1 commit into
tree-sitter:masterfrom
mucinoab:add-string-interpolation
Closed

Parse interpolation and formatting in strings#164
mucinoab wants to merge 1 commit into
tree-sitter:masterfrom
mucinoab:add-string-interpolation

Conversation

@mucinoab

@mucinoab mucinoab commented Nov 6, 2022

Copy link
Copy Markdown

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

mucinoab commented Nov 26, 2022

Copy link
Copy Markdown
Author

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

@ValouBambou

Copy link
Copy Markdown

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

@amaanq

amaanq commented Jul 10, 2023

Copy link
Copy Markdown
Member

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
Copy Markdown
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

amaanq commented Jul 10, 2023

Copy link
Copy Markdown
Member

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

@ValouBambou

Copy link
Copy Markdown

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

amaanq commented Jul 15, 2023

Copy link
Copy Markdown
Member

The feature exists in Rust's LSP

@ValouBambou

Copy link
Copy Markdown

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

amaanq commented Jul 19, 2023

Copy link
Copy Markdown
Member

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
Copy Markdown
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

amaanq commented Jul 19, 2023

Copy link
Copy Markdown
Member

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
Copy Markdown

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

nik-rev commented May 25, 2025

Copy link
Copy Markdown

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