Nesting reStructuredText in Python docstrings

I would like to see beautiful syntax highlighting and coloring in my Python dokstels, which are (of course) valid RESt. For example:

'''
A section
=========

an example::

    some code
'''
rest of python code

The closest I have is in my .vim/after/syntax/python.vim:

syn include syntax/rst.vim 
syn region pythonDocstring  start=+^\s*'''+ end=+'''+ contained

According to the include syntax documentation , which should be enough. Also note that rst.vimoverriding a bunch of python elements, so I had to comment out all sections related to the code:

" 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')
[...]

Finally, I cannot use !runtimeit because it rst.vimdoes nothing if the variable is b:current_syntaxalready defined:

if exists("b:current_syntax")
  finish
endif

Despite my efforts, my docstring remains the same color as the other comments, without syntax highlighting.

I also tried this:

syn region pythonDocstring  start=+^\s*'''+ end=+'''+ contains=CONTAINED

Special, Comment.

, , pythonDocstring ?

: python raw python.vim, , python.


Update

after/syntax/python.vim:

syn include @pythonRst syntax/rst.vim 
syn region pythonDocstring  start=+^\s*'''+ end=+'''+ contains=@pythonRst

RESt, .py:

python syntax

.rst. , , ( , ):

rest syntax

, , colorscheme .vimrc

+4
2

reST python doc, (: @pythonRst). , Vim .

syn include @pythonRst syntax/rst.vim

, doc, Vim reST ( contains=)

syn region pythonDocstring  start=+^\s*'''+ end=+'''+ contains=@pythonRst
+3

- .

rst.vim $VIMRUNTIME/ .vim/syntax/

-, .vim/after/syntax/python.vim ( @Ingo):

syn include @pythonRst syntax/rst.vim 
syn region pythonDocstring  start=+^\s*"""+ end=+"""+ contains=@pythonRst

-, , :

if exists("b:current_syntax")
  finish
endif

( , python, :

for code in g:rst_syntax_code_list
    unlet! b:current_syntax
[...]
   unlet! prior_isk
endfor

, , :

let b:current_syntax = "rst"

.

:

success!

+2

Source: https://habr.com/ru/post/1614637/


All Articles