突出显示python docstring作为注释(vim语法突出显示)

2024-04-30 05:14:14 发布

您现在位置:Python中文网/ 问答频道 /正文

是否可以修改python.vim(以及相应的colorscheme文件),以便在vim下的python语法高亮显示期间,class和def语句(a.k.a.docstrings)下的三重引号字符串作为注释突出显示吗?在

class URLopener:
  """Class to open URLs.
  This is a class rather than just a subroutine because we may need
  more than one set of global protocol-specific options.
  Note -- this is a base class for those who don't want the
  automatic handling of errors type 302 (relocated) and 401
  (authorization needed)."""

def addheader(self, *args):
  """Add a header to be used by the HTTP interface only
  e.g. u.addheader('Accept', 'sound/basic')"""

# sample comment

Tags: 文件ofthetoisdef语法语句
3条回答

您可以添加以下行:

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

到您的~/.vim/after/syntax/Python.vim. 如果此文件不存在,可以创建它。在

PEP257规定对docstring使用“三重双引号”。在docstring中不必包含''triple single quotes''或''single double quotes''。有一个困难,我们有类docstrings、函数docstrings、模块docstrings、属性docstrings和其他docstring。这就是为什么我决定将docstring考虑为以下更容易:

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

然后:

^{pr2}$

您可以在这个脚本中看到示例(search pythonDocString):https://github.com/andbar-ru/python-syntax/blob/master/syntax/python.vim

以下几点对我有用:

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

从修改后的Python.vim来自here。在

相关问题 更多 >