18

I am trying to modify the default Python.sublime_syntax file to handle Python’s f-string literals properly. My goal is to have expressions in interpolated strings recognised as such:

f"hello {person.name if person else 'there'}"
         -----------source.python----------
------string.quoted.double.block.python------

Within f-strings, ranges of text between a single { and another } (but terminating before format specifiers such as !r}, :<5}, etc—see PEP 498) should be recognised as expressions. As far as I know, that might look a little like this:

...
string:
 - match: "(?<=[^\{]\{)[^\{].*)(?=(!(s|r|a))?(:.*)?\})" # I'll need a better regex
   push: expressions

However, upon inspecting the build-in Python.sublime_syntax file, the string contexts especially are to unwieldy to even approach (~480 lines?) and I have no idea how to begin. Thanks heaps for any info.

2
  • 2
    Using a regex debugger such as regex101.com might help parsing the very log regex pattern
    – maackle
    Commented Mar 5, 2017 at 4:36
  • 7
    This is now fixed in Build 3127
    – Rafael
    Commented Mar 21, 2018 at 17:16

1 Answer 1

2

There was an update to syntax highlighting in BUILD 3127 (Which includes: Significant improvements to Python syntax highlighting).

However, a couple users have stated that in BUILD 3176 syntax highlighting still is not set to correctly highlight Python expressions that are located within f strings. According to @Jollywatt, it is set to source.python f"string.quoted.double.block {constant.other.placeholder}" rather than f"string.quoted.double.block {source.python}"

It looks like Sublime uses this tool, PackageDev, "to ease the creation of snippets, syntax definitions, etc. for Sublime Text."

3
  • 2
    It doesn’t actually seem like this is ‘fixed’. In Build 3176, Python is still interpreted like source.python f"string.quoted.double.block {constant.other.placeholder}", not source.python f"string.quoted.double.block {source.python}". I wonder if the latter shouldn’t be the desired behaviour after all…
    – Jollywatt
    Commented Jun 2, 2018 at 0:15
  • @Jollywatt that may be a better answer than mine. I switched my answer to a community wiki if you'd like to add that info.
    – Zev
    Commented Jun 2, 2018 at 0:34
  • I'm also using Build 3176 and unlike in Atom Editor, I don't see any the ordinary Python syntax highlighting within f-strings.
    – Kurt Peek
    Commented Aug 29, 2018 at 20:14

Not the answer you're looking for? Browse other questions tagged or ask your own question.