-
Notifications
You must be signed in to change notification settings - Fork 765
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
way to convert string to f-string #3757
Comments
Thanks for the feature request! We are going to give the community 60 days from when this issue was created to provide 5 👍 upvotes on the opening comment to gauge general interest in this idea. If there's enough upvotes then we will consider this feature request in our future planning. If there's unfortunately not enough upvotes then we will close this issue. |
Thank you to everyone who upvoted this issue! Since the community showed interest in this feature request we will leave this issue open as something to consider implementing at some point in the future. We do encourage people to continue 👍 this issue as it helps us prioritize our work based on what the community seems to want the most. |
I'm going to move this to Pylance as this seems to be a potential quick fix. |
We actually already have an item for this: |
Discussed in microsoft/vscode-python#15549
Originally posted by zcutlip March 3, 2021
I waste so many keystrokes by starting a string, realizing I want to format an object in the middle of it, skipping back to the beginning to prepend
f
, then returning to my typing position.It'd be awesome if there was a keyboard shortcut for this, or, even better, if the need for an f-string could be automatically detected (e.g., the presence of curly braces and an in-scope object is referenced).
I like the idea of just typing a curly bracket inside a string to convert it to an f-string, as long as this is pushed to the undo stack and I can Cmd-Z quickly to undo it. I type literal curly brackets in my strings infrequently enough it wouldn't bug me much (no more than other kinds of quote/bracket auto-complete).
I think the best way to do this would be to trigger on
}
and apply a few rules:Scan backwards for a matching
{
in the same string. If none is found, do not convert to an f-string. This avoids a number of false positives likeprint("int main() { return ", code, "}")
.Scan forwards and backwards for another
}
in the same string. If one is found, this probably means the string is not meant to be an f-string, and it guarantees that conversion will be attempted at most once per string.Plus two heuristics for avoiding conversion of strings that are formatted with
str.format()
:Do not convert if the sequence
{}
appears, because this is an invalid f-string specified and may indicate the use ofstr.format()
.Try to detect if
str.format()
is being called on the string. This is imperfect because if the user is typing a new line of code, it may not have been written yet. For existing code the user is editing, one could just see if.format(
comes directly after the closing quote -- by far the most common pattern.But note that, as long as you are targeting a version of Python with f-strings, the user probably won't be writing new code that uses
str.format()
.The text was updated successfully, but these errors were encountered: