-
Notifications
You must be signed in to change notification settings - Fork 31
Fix parsing of python f-strings #160
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
Double-quoted f-strings mostly worked, but single-quoted did not:
a = f'what'
Reported that `f'what` was mispelled. I think this is due to:
1. the `(strings) @string` query give back in the whole node string
node - including `f'`
2. the word-extraction in parser.rs (`processor.extract_words()`)
does not consider `'` a word separator (rightly so)
(2) explains why double-quote f-strings works - the word splitting
split on `"`, resulting in "f" and "what", and I assume single letter
words is not checked.
(1) can be confirmed in the tree-sitter playgound [1], where we
also see that there is an `string_content` node type more suitable.
Using the `string_content` node means that code inside interpolation
units wont be spell-checked, but this aligns with the current behavior
that only definitions, comments, and strings are checked.
[1] https://tree-sitter.github.io/tree-sitter/7-playground.html
|
From here it seems like I see now that the same node-type is used in rust.scm and many of the other languages (but not all). |
|
Hey! Thanks for looking into this. I thought this was an issue, but was unable to reproduce when I looked into it. Oddly, this issue isn't triggered when the f-string is in a function call, like print(). If you have time, I'd love to see a test for this though. Something in |
Sadly, this isn't always the case, and even if a grammar has it, it can mean different things depending on the language. All we can do is test it per application. 😅 |
Wow, yeah, my experiment was bad! I tried |
|
Ah, right - because capital letters splits words. I pushed a test quikcly, but I admit that I let Claude write it. It's perhaps unnecessary long, but otherwise seems fine as a basic test. Thanks for creating this language server btw. It works very well and it's nice that only definitions are checked ❤️ |
|
Nice, thanks! Claude didn't misspell any of the f-string variables, but I can fix that up :) Glad you've enjoyed using Codebook! |
|
v0.3.18 is out with the fixes! |
Ah, that was sloppy of me. Seems it just repeated the text I gave it. |

Double-quoted f-strings mostly worked, but single-quoted did not:
Reported that
f'whatwas mispelled.I think this is due to:
(strings) @stringquery give back in the whole node string node - includingf'processor.extract_words()) does not consider'a word separator (rightly so)(2) explains why double-quote f-strings works - the word splitting split on
", resulting infandwhat, and I assume single letter words is not checked.(1) can be confirmed in the tree-sitter playground [1], where we also see that there is an
string_contentnode type more suitable.Screenshot from the playground
Using the
string_contentnode means that code inside interpolation units wont be spell-checked, but this aligns with the current behavior that only definitions, comments, and strings are checked.[1] https://tree-sitter.github.io/tree-sitter/7-playground.html
Ref: #136 (comment)