Sublime Text语法:Python 3.6 f-strings

Jol*_*att 18 python syntax syntax-highlighting sublimetext3

我正在尝试修改默认Python.sublime_syntax文件以正确处理Python的f-string文字.我的目标是在插值字符串中使用表达式识别:

f"hello {person.name if person else 'there'}"
         -----------source.python----------
------string.quoted.double.block.python------
Run Code Online (Sandbox Code Playgroud)

内的f-串,范围单一之间的文本{和另一个}(但格式说明如之前终止!r},:<5}等等见PEP 498)应该被识别为表达式.据我所知,这可能看起来像这样:

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

但是,在检查内置Python.sublime_syntax文件时,字符串上下文尤其是难以接近(~480行?),我不知道如何开始.感谢大家的任何信息.

Zev*_*Zev 2

BUILD 3127 中对语法突出显示进行了更新(其中包括:对 Python 语法突出显示的重大改进)。

然而,一些用户表示,在 BUILD 3176 中语法突出显示仍然没有设置为正确突出显示位于 f 字符串内的 Python 表达式。根据@Jollywatt,它被设置为source.pythonf"string.quoted.double.block {constant.other.placeholder}"而不是f"string.quoted.double.block {source.python}"

看起来 Sublime 使用PackageDev这个工具“来简化 Sublime Text 的片段、语法定义等的创建”。

  • 实际上这似乎并不是“固定”的。在 Build 3176 中,Python 仍然被解释为 `source.python f"string.quoted.double.block {constant.other.placeholder}"`,而不是 `source.python f"string.quoted.double.block {source.python }“`。我想知道后者是否不应该是理想的行为...... (2认同)