[英]Embedding reStructuredText in Python docstrings
I'd like to see some nice syntax highlighting and colouring in my Python's docstrings which (of course) are valid RESt. 我想在我的Python文档字符串中看到一些不错的语法突出显示和着色(当然)是有效的RESt。 For example:
例如:
'''
A section
=========
an example::
some code
'''
rest of python code
The closest I've got is this in my .vim/after/syntax/python.vim
: 我最接近的是我的
.vim/after/syntax/python.vim
:
syn include syntax/rst.vim
syn region pythonDocstring start=+^\s*'''+ end=+'''+ contained
According to the documentation of syntax-include that should be sufficient to. 根据语法包括的文档,这应该足够了。 Also note that
rst.vim
re-defines a bunch of python entities so I've had to comment out all sections related to code: 另请注意,
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')
[...]
Lastly, I can't use !runtime
because rst.vim
does nothing if the b:current_syntax
variable is already defined: 最后,我不能使用
!runtime
因为如果已经定义了b:current_syntax
变量, rst.vim
不执行任何操作:
if exists("b:current_syntax")
finish
endif
Despite my efforts my docstring stays the same colour as other comments, with no syntax highlighting. 尽管经过我的努力,我的文档字符串仍然与其他注释保持相同的颜色,并且没有突出显示语法。
I've tried also this: 我也试过这个:
syn region pythonDocstring start=+^\s*'''+ end=+'''+ contains=CONTAINED
But I only managed to change the colour of the block to be Special
rather than Comment
. 但是我只设法将块的颜色更改为
Special
而不是Comment
。
Perhaps I should define the pythonDocstring not to have any default colouring? 也许我应该定义pythonDocstring不具有任何默认颜色?
Further note: if I remove references to python raw strings in python.vim, colouring disappears but I only get the python keywords highlighted. 进一步的注意:如果我在python.vim中删除了对python原始字符串的引用,颜色消失了,但是我只突出显示了python关键字。
Trying one of the solutions below with my after/syntax/python.vim file: 使用我的after / syntax / python.vim文件尝试以下解决方案之一:
syn include @pythonRst syntax/rst.vim
syn region pythonDocstring start=+^\s*'''+ end=+'''+ contains=@pythonRst
Resulted in the RESt file being grayed out when opening a file with .py
extension: 当打开扩展名为
.py
的文件时,导致RESt文件显示为灰色:
While opening the same file with a .rst
. 同时使用
.rst
打开相同的文件。 extension seems to work fine (just to show that I have a rest syntax file): 扩展似乎可以正常工作(只是为了表明我有一个rest语法文件):
Note that I've tried both with and without colorscheme
in my .vimrc 请注意,我既没有试图
colorscheme
在我的.vimrc
As the reST syntax should only be applied inside Python doc strings, you have to include them into a syntax cluster (here: @pythonRst
). 由于reST语法仅应在 Python文档字符串中应用,因此您必须将它们包括在语法集群中(此处为
@pythonRst
)。 Otherwise, Vim would try to match them everywhere. 否则,Vim会尝试在任何地方匹配它们。
syn include @pythonRst syntax/rst.vim
Then, define a region covering those doc strings, and explicitly instruct Vim to highlight reST syntax in there (via contains=
) 然后,定义一个覆盖这些文档字符串的区域,并明确指示Vim在其中突出显示reST语法(通过
contains=
)
syn region pythonDocstring start=+^\s*'''+ end=+'''+ contains=@pythonRst
I've finally managed to crack it. 我终于设法破解了。
Firstly I copied the rst.vim
file from $VIMRUNTIME/syntax into my .vim/syntax/
folder 首先,我将
rst.vim
文件从$ VIMRUNTIME / syntax复制到我的.vim/syntax/
文件夹中
Secondly, this is my .vim/after/syntax/python.vim
file (thanks @Ingo): 其次,这是我的
.vim/after/syntax/python.vim
文件(感谢@Ingo):
syn include @pythonRst syntax/rst.vim
syn region pythonDocstring start=+^\s*"""+ end=+"""+ contains=@pythonRst
Thirdly I edited the file and commented out this block to ignore whether current syntax is set: 第三,我编辑了文件并注释掉了该块,以忽略是否设置了当前语法:
if exists("b:current_syntax")
finish
endif
This block to load code plugins (it was causing some recursive problem because it was trying to load a python syntax file, which loaded this syntax file: 此代码块加载代码块(由于试图加载python语法文件而导致了一些递归问题,该语法文件已加载以下语法文件:
for code in g:rst_syntax_code_list
unlet! b:current_syntax
[...]
unlet! prior_isk
endfor
And finally this block at the end: 最后,最后一个代码块:
let b:current_syntax = "rst"
So that the syntax would stay as python. 这样语法就可以保留为python。
Result: 结果:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.