突出显示三重单引号中的字符串作为注释?

时间:2014-02-13 16:31:18

标签: python python-2.7 vim vim-syntax-highlighting

this开始,我想强调三个单个引号中的字符串作为注释(或者更好地用于其他不是类/函数/模块中的第一个)

我正在使用jedi-vim。以下是文件after/syntax/python.vim的内容:

syn match pythonComment "#.*$" contains=pythonTodo,@Spell,jediFunction
syn region pythonString
    \ start=+[uU]\=\z(['"]\)+ end="\z1" skip="\\\\\|\\\z1"
    \ contains=pythonEscape,@Spell,jediFunction
syn region pythonString
    \ start=+[uU]\=\z('''\|"""\)+ end="\z1" keepend
    \ contains=pythonEscape,pythonSpaceError,pythonDoctest,@Spell,jediFunction
syn region pythonRawString
    \ start=+[uU]\=[rR]\z(['"]\)+ end="\z1" skip="\\\\\|\\\z1"
    \ contains=@Spell,jediFunction
syn region pythonRawString
    \ start=+[uU]\=[rR]\z('''\|"""\)+ end="\z1" keepend
    \ contains=pythonSpaceError,pythonDoctest,@Spell,jediFunction

我尝试删除pythonString行中的三重单引号,并添加以下内容之一:

syn region Comment start=/'''/ end=/'''/
syn region pythonDocstring  start=+^\s*[uU]\?[rR]\?'''+ end=+'''+ keepend excludenl contains=pythonEscape,@Spell,pythonDoctest,pythonDocTest2,pythonSpaceError

this主题中所述,但它不起作用(三重单引号和双引号中的字符串都突出显示为docstring)。


UPDATE Fri Feb 14 08:29:00 ICT 2014

@的 benjifisher

我确信它被识别为pythonString,因为:echo synIDattr(synID(line("."), col("."), 1), "name")告诉了我。

:syn list pythonString

--- Syntax items ---
pythonString   xxx start=/[uU]\=\z(['"]\)/ skip=/\\\\\|\\\z1/ end=/\z1/  contains=pythonEscape,@Spell
                   start=/[uU]\=\z("""\)/ end=/\z1/  keepend contains=pythonEscape,pythonSpaceError,pythonDoctest,@Spell
                   start=/[uU]\=\z(['"]\)/ skip=/\\\\\|\\\z1/ end=/\z1/  contains=pythonEscape,@Spell,jediFunction
                   start=/[uU]\=\z("""\)/ end=/\z1/  keepend contains=pythonEscape,pythonSpaceError,pythonDoctest,@Spell,jediFunction
                   links to String

:syn list pythonComment

--- Syntax items ---
pythonComment  xxx match /#.*$/  contains=pythonTodo,@Spell
                   match /#.*$/  contains=pythonTodo,@Spell,jediFunction
                   start=/'''/ end=/'''/
                   start=/[uU]\=\z('''\)/ end=/\z1/  keepend contains=pythonTodo,@Spell,jediFunction
                   start=/^\s*[uU]\?[rR]\?'''/ end=/'''/  keepend excludenl contains=pythonEscape,@Spell,pythonDoctest,pythonDocTest2,pythonSpaceError
                   links to Comment

1 个答案:

答案 0 :(得分:0)

哦,对不起,我的错。

由于我在第一行(pythonComment)之后添加了建议行,我还必须删除第二行的单引号:

syn match pythonComment "#.*$" contains=pythonTodo,@Spell,jediFunction
syn region pythonComment start=/'''/ end=/'''/
syn region pythonString
    \ start=+[uU]\=\z(["]\)+ end="\z1" skip="\\\\\|\\\z1"
    \ contains=pythonEscape,@Spell,jediFunction

教训是:我应该在下次的文件末尾添加它:D。