├── README.md ├── ftdetect └── cython.vim ├── ftplugin └── cython.vim └── syntax └── cython.vim /README.md: -------------------------------------------------------------------------------- 1 | # vim-cython-syntax 2 | 3 | A vim syntax plugin for [Cython](https://cython.org/) based on Vim's [Python 4 | syntax file](https://github.com/vim/vim/blob/master/runtime/syntax/python.vim). 5 | -------------------------------------------------------------------------------- /ftdetect/cython.vim: -------------------------------------------------------------------------------- 1 | " overwrite filetype only when the filetype is pyrex 2 | function! s:overwrite_filetype() abort 3 | if empty(&filetype) || &filetype ==# 'pyrex' 4 | setlocal filetype=cython 5 | endif 6 | endfunction 7 | 8 | " Cython 9 | " https://docs.cython.org/src/userguide/language_basics.html 10 | autocmd BufNewFile,BufRead *.pyx call s:overwrite_filetype() " Implementation file 11 | autocmd BufNewFile,BufRead *.pxd call s:overwrite_filetype() " Definition file 12 | autocmd BufNewFile,BufRead *.pxi call s:overwrite_filetype() " Include files 13 | 14 | " pyximport 15 | " https://github.com/cython/cython/blob/master/pyximport/pyximport.py 16 | autocmd BufNewFile,BufRead *.pyxdep setfiletype python 17 | autocmd BufNewFile,BufRead *.pyxbld setfiletype python 18 | -------------------------------------------------------------------------------- /ftplugin/cython.vim: -------------------------------------------------------------------------------- 1 | if exists('b:did_ftplugin') 2 | finish 3 | endif 4 | let b:did_ftplugin = 1 5 | 6 | " Behaves just like Python 7 | runtime! ftplugin/python.vim ftplugin/python_*.vim ftplugin/python/*.vim 8 | 9 | " Use cython to compile the file 10 | let g:cython_compile_options = get(g:, 'cython_compile_options', [ 11 | \ '--embed-positions', 12 | \ '--fast-fail', 13 | \]) 14 | let &l:makeprg=printf( 15 | \ 'cython %s %%', 16 | \ join(g:cython_compile_options), 17 | \) 18 | 19 | -------------------------------------------------------------------------------- /syntax/cython.vim: -------------------------------------------------------------------------------- 1 | if exists('b:current_syntax') 2 | finish 3 | endif 4 | 5 | " Do not highlight cython builtin 6 | let g:cython_no_builtin_highlight = 7 | \ get(g:, 'cython_no_builtin_highlight', get(g:, 'python_no_builtin_highlight')) 8 | 9 | " Do not highlight compile directives 10 | let g:cython_no_directive_highlight = 11 | \ get(g:, 'cython_no_directive_highlight', 0) 12 | 13 | " Use Python syntax as base syntax 14 | runtime! syntax/python.vim 15 | unlet b:current_syntax 16 | 17 | " C types 18 | syntax keyword cythonType const signed unsigned 19 | syntax keyword cythonType char short int long bint 20 | syntax keyword cythonType float double 21 | syntax keyword cythonType void object 22 | syntax keyword cythonNull NULL 23 | 24 | " While Cython use 'from' in import/cimport, cdef-extern, and for-loop, it 25 | " could not be listed in 'pythonInclude' syntax as a keyword. 26 | " So remove 'pythonInclude' and re-define it as 'cythonInclude' without 'from' 27 | " keyword here. 28 | syntax clear pythonInclude 29 | syntax keyword cythonInclude import cimport include 30 | syntax match cythonInclude display '\\ze.*\' 31 | 32 | " While Cython has a bit different syntax for for-loop, re-define pythonRepeat 33 | " as cythonRepeat to support 'from' and 'from ... by' syntax 34 | syntax clear pythonRepeat 35 | syntax keyword cythonRepeat while for 36 | syntax keyword cythonRepeat contained from by 37 | syntax match cythonRepeatCondition '\%(\\)\@<=.*\.*:' contains=cythonRepeat 38 | syntax match cythonRepeatCondition '\%(\\)\@<=.*\.*\.*:' contains=cythonRepeat 39 | 40 | " DEF ... 41 | " IF ... ELIF ... ELSE 42 | syntax keyword cythonDefine DEF IF ELIF ELSE 43 | 44 | " ... nogil: 45 | " ... expect 46 | syntax match cythonStatement display '\.*:$' containedin=cythonStatement 48 | 49 | " Typedef 50 | syntax match cythonStatement display '\' nextgroup=cythonType skipwhite 51 | 52 | " Function 53 | syntax match cythonStatement display '\' nextgroup=cythonAccessor,cythonReturnType,cythonStructure skipwhite 54 | syntax match cythonStatement display '\' nextgroup=cythonReturnType skipwhite 55 | syntax match cythonStatement display '\' nextgroup=pythonString skipwhite 56 | syntax keyword cythonAccessor inline extern public api readonly contained nextgroup=cythonAccessor,cythonReturnType,cythonStructure 57 | syntax match cythonReturnType display '\%(\h\%(\w\|\.\)\+\)@=.{-}' contains=cythonType nextgroup=cythonFunction skipwhite 58 | syntax match cythonFunction display '\%(\.*\)\@<=\h\%(\w\|\.\)\+\ze(' 59 | 60 | " Structure 61 | syntax match cythonStructure display '\' nextgroup=cythonFunction skipwhite 62 | syntax match cythonStructure display '\' nextgroup=cythonFunction skipwhite 63 | syntax match cythonStructure display '\' nextgroup=cythonFunction skipwhite 64 | syntax match cythonStructure display '\' nextgroup=cythonFunction skipwhite 65 | syntax match cythonStructure display '\' nextgroup=cythonFunction skipwhite 66 | syntax match cythonFunction display contained '\h\%(\w\|\.\)\+\ze\%((\|:\)' 67 | 68 | " Compiler directives 69 | if !g:cython_no_directive_highlight 70 | syntax match cythonDirective display '\