【发布时间】:2015-11-05 17:24:44
【问题描述】:
我希望在我的 Python 文档字符串中看到一些不错的语法高亮和着色,这些(当然)是有效的 REST。例如:
'''
A section
=========
an example::
some code
'''
rest of python code
我得到的最接近的是我的.vim/after/syntax/python.vim:
syn include syntax/rst.vim
syn region pythonDocstring start=+^\s*'''+ end=+'''+ contained
根据the documentation of syntax-include 应该足够了。另请注意,rst.vim 重新定义了一堆 python 实体,所以我不得不注释掉所有与代码相关的部分:
" syn region rstCodeBlock contained matchgroup=rstDirective
" \ start=+\%(sourcecode\|code\%(-block\)\=\)::\_s*\n\ze\z(\s\+\)+
" \ skip=+^$+
" \ end=+^\z1\@!+
" \ contains=@NoSpell
" syn cluster rstDirectives add=rstCodeBlock
" if !exists('g:rst_syntax_code_list')
[...]
最后,我不能使用!runtime,因为如果b:current_syntax 变量已经定义,rst.vim 什么都不做:
if exists("b:current_syntax")
finish
endif
尽管我努力了,但我的文档字符串与其他 cmets 保持相同的颜色,没有语法突出显示。
我也试过这个:
syn region pythonDocstring start=+^\s*'''+ end=+'''+ contains=CONTAINED
但我只设法将块的颜色更改为 Special 而不是 Comment。
也许我应该将 pythonDocstring 定义为没有任何默认颜色?
进一步说明:如果我在 python.vim 中删除对 python 原始字符串的引用,着色会消失,但我只会突出显示 python 关键字。
更新
使用我的 after/syntax/python.vim 文件尝试以下解决方案之一:
syn include @pythonRst syntax/rst.vim
syn region pythonDocstring start=+^\s*'''+ end=+'''+ contains=@pythonRst
导致打开带有.py扩展名的文件时REST文件变灰:
使用.rst 打开同一个文件时。扩展似乎工作正常(只是为了表明我有一个休息语法文件):
请注意,我在我的 .vimrc 中尝试了使用和不使用 colorscheme
【问题讨论】:
标签: python vim python-sphinx restructuredtext