Highlight python docstrings as comments (vim syntax highlighting)

流过昼夜 提交于 2019-11-28 23:21:04

you can add the following line:

syn region Comment start=/"""/ end=/"""/

to your ~/.vim/after/syntax/python.vim. You can create this file if it doesn't exists.

The following worked for me:

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

Taken from a modified python.vim from here.

PEP 257 prescribes to use """triple double quotes""" for docstrings. It's not obligatory to include '''triple single quotes''' or "single double quotes" into docstrings. There is one difficulty that we have class docstrings, function docstrings, module docstrings, attribute docstrings and additional docstrings. That's why I decided that it's easier to consider docstring as following:

syn region pythonDocString start=+^\s*"""+ end=+"""+ keepend contains=...

And then:

HiLink pythonDocString        Comment

You may see examples in this script (search pythonDocString): https://github.com/andbar-ru/python-syntax/blob/master/syntax/python.vim

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!