python.vim (8411B)
1 " Vim filetype plugin file 2 " Language: python 3 " Maintainer: Tom Picton <tom@tompicton.com> 4 " Previous Maintainer: James Sully <sullyj3@gmail.com> 5 " Previous Maintainer: Johannes Zellner <johannes@zellner.org> 6 " Repository: https://github.com/tpict/vim-ftplugin-python 7 " Last Change: 2024/05/13 8 " 2024 Nov 30 use pytest compiler (#16130) 9 " 2026 Jun 27 by Vim Project (normalize recommended style guard) 10 11 if exists("b:did_ftplugin") | finish | endif 12 let b:did_ftplugin = 1 13 let s:keepcpo= &cpo 14 set cpo&vim 15 16 setlocal cinkeys-=0# 17 setlocal indentkeys-=0# 18 setlocal include=^\\s*\\(from\\\|import\\) 19 setlocal define=^\\s*\\(\\(async\\s\\+\\)\\?def\\\|class\\) 20 21 " For imports with leading .., append / and replace additional .s with ../ 22 let b:grandparent_match = '^\(.\.\)\(\.*\)' 23 let b:grandparent_sub = '\=submatch(1)."/".repeat("../",strlen(submatch(2)))' 24 25 " For imports with a single leading ., replace it with ./ 26 let b:parent_match = '^\.\(\.\)\@!' 27 let b:parent_sub = './' 28 29 " Replace any . sandwiched between word characters with / 30 let b:child_match = '\(\w\)\.\(\w\)' 31 let b:child_sub = '\1/\2' 32 33 setlocal includeexpr=substitute(substitute(substitute( 34 \v:fname, 35 \b:grandparent_match,b:grandparent_sub,''), 36 \b:parent_match,b:parent_sub,''), 37 \b:child_match,b:child_sub,'g') 38 39 setlocal suffixesadd=.py 40 setlocal comments=b:#,fb:- 41 setlocal commentstring=#\ %s 42 43 if has('python3') 44 setlocal omnifunc=python3complete#Complete 45 elseif has('python') 46 setlocal omnifunc=pythoncomplete#Complete 47 endif 48 49 set wildignore+=*.pyc 50 51 let b:next_toplevel='\v%$\|^(class\|def\|async def)>' 52 let b:prev_toplevel='\v^(class\|def\|async def)>' 53 let b:next_endtoplevel='\v%$\|\S.*\n+(def\|class)' 54 let b:prev_endtoplevel='\v\S.*\n+(def\|class)' 55 let b:next='\v%$\|^\s*(class\|def\|async def)>' 56 let b:prev='\v^\s*(class\|def\|async def)>' 57 let b:next_end='\v\S\n*(%$\|^(\s*\n*)*(class\|def\|async def)\|^\S)' 58 let b:prev_end='\v\S\n*(^(\s*\n*)*(class\|def\|async def)\|^\S)' 59 60 if !exists('g:no_plugin_maps') && !exists('g:no_python_maps') 61 execute "nnoremap <silent> <buffer> ]] :<C-U>call <SID>Python_jump('n', '". b:next_toplevel."', 'W', v:count1)<cr>" 62 execute "nnoremap <silent> <buffer> [[ :<C-U>call <SID>Python_jump('n', '". b:prev_toplevel."', 'Wb', v:count1)<cr>" 63 execute "nnoremap <silent> <buffer> ][ :<C-U>call <SID>Python_jump('n', '". b:next_endtoplevel."', 'W', v:count1, 0)<cr>" 64 execute "nnoremap <silent> <buffer> [] :<C-U>call <SID>Python_jump('n', '". b:prev_endtoplevel."', 'Wb', v:count1, 0)<cr>" 65 execute "nnoremap <silent> <buffer> ]m :<C-U>call <SID>Python_jump('n', '". b:next."', 'W', v:count1)<cr>" 66 execute "nnoremap <silent> <buffer> [m :<C-U>call <SID>Python_jump('n', '". b:prev."', 'Wb', v:count1)<cr>" 67 execute "nnoremap <silent> <buffer> ]M :<C-U>call <SID>Python_jump('n', '". b:next_end."', 'W', v:count1, 0)<cr>" 68 execute "nnoremap <silent> <buffer> [M :<C-U>call <SID>Python_jump('n', '". b:prev_end."', 'Wb', v:count1, 0)<cr>" 69 70 execute "onoremap <silent> <buffer> ]] :call <SID>Python_jump('o', '". b:next_toplevel."', 'W', v:count1)<cr>" 71 execute "onoremap <silent> <buffer> [[ :call <SID>Python_jump('o', '". b:prev_toplevel."', 'Wb', v:count1)<cr>" 72 execute "onoremap <silent> <buffer> ][ :call <SID>Python_jump('o', '". b:next_endtoplevel."', 'W', v:count1, 0)<cr>" 73 execute "onoremap <silent> <buffer> [] :call <SID>Python_jump('o', '". b:prev_endtoplevel."', 'Wb', v:count1, 0)<cr>" 74 execute "onoremap <silent> <buffer> ]m :call <SID>Python_jump('o', '". b:next."', 'W', v:count1)<cr>" 75 execute "onoremap <silent> <buffer> [m :call <SID>Python_jump('o', '". b:prev."', 'Wb', v:count1)<cr>" 76 execute "onoremap <silent> <buffer> ]M :call <SID>Python_jump('o', '". b:next_end."', 'W', v:count1, 0)<cr>" 77 execute "onoremap <silent> <buffer> [M :call <SID>Python_jump('o', '". b:prev_end."', 'Wb', v:count1, 0)<cr>" 78 79 execute "xnoremap <silent> <buffer> ]] :call <SID>Python_jump('x', '". b:next_toplevel."', 'W', v:count1)<cr>" 80 execute "xnoremap <silent> <buffer> [[ :call <SID>Python_jump('x', '". b:prev_toplevel."', 'Wb', v:count1)<cr>" 81 execute "xnoremap <silent> <buffer> ][ :call <SID>Python_jump('x', '". b:next_endtoplevel."', 'W', v:count1, 0)<cr>" 82 execute "xnoremap <silent> <buffer> [] :call <SID>Python_jump('x', '". b:prev_endtoplevel."', 'Wb', v:count1, 0)<cr>" 83 execute "xnoremap <silent> <buffer> ]m :call <SID>Python_jump('x', '". b:next."', 'W', v:count1)<cr>" 84 execute "xnoremap <silent> <buffer> [m :call <SID>Python_jump('x', '". b:prev."', 'Wb', v:count1)<cr>" 85 execute "xnoremap <silent> <buffer> ]M :call <SID>Python_jump('x', '". b:next_end."', 'W', v:count1, 0)<cr>" 86 execute "xnoremap <silent> <buffer> [M :call <SID>Python_jump('x', '". b:prev_end."', 'Wb', v:count1, 0)<cr>" 87 endif 88 89 if !exists('*<SID>Python_jump') 90 fun! <SID>Python_jump(mode, motion, flags, count, ...) range 91 let l:startofline = (a:0 >= 1) ? a:1 : 1 92 93 if a:mode == 'x' 94 normal! gv 95 endif 96 97 if l:startofline == 1 98 normal! 0 99 endif 100 101 let cnt = a:count 102 mark ' 103 while cnt > 0 104 call search(a:motion, a:flags) 105 let cnt = cnt - 1 106 endwhile 107 108 if l:startofline == 1 109 normal! ^ 110 endif 111 endfun 112 endif 113 114 if has("browsefilter") && !exists("b:browsefilter") 115 let b:browsefilter = "Python Files (*.py)\t*.py\n" 116 if has("win32") 117 let b:browsefilter .= "All Files (*.*)\t*\n" 118 else 119 let b:browsefilter .= "All Files (*)\t*\n" 120 endif 121 endif 122 123 if get(g:, 'python_recommended_style', 124 \ get(g:, 'filetype_recommended_style', 1)) 125 " As suggested by PEP8. 126 setlocal expandtab tabstop=4 softtabstop=4 shiftwidth=4 127 endif 128 129 " Use pydoc for keywordprg. 130 " Unix users preferentially get pydoc3, then pydoc2. 131 " Windows doesn't have a standalone pydoc executable in $PATH by default, nor 132 " does it have separate python2/3 executables, so Windows users just get 133 " whichever version corresponds to their installed Python version. 134 if executable('python3') 135 setlocal keywordprg=python3\ -m\ pydoc 136 elseif executable('python') 137 setlocal keywordprg=python\ -m\ pydoc 138 endif 139 140 if expand('%:t') =~# '\v^test_.*\.py$|_test\.py$' && executable('pytest') 141 compiler pytest 142 let &l:makeprg .= ' %:S' 143 endif 144 145 " Script for filetype switching to undo the local stuff we may have changed 146 let b:undo_ftplugin = 'setlocal cinkeys<' 147 \ . '|setlocal comments<' 148 \ . '|setlocal commentstring<' 149 \ . '|setlocal expandtab<' 150 \ . '|setlocal include<' 151 \ . '|setlocal includeexpr<' 152 \ . '|setlocal indentkeys<' 153 \ . '|setlocal keywordprg<' 154 \ . '|setlocal omnifunc<' 155 \ . '|setlocal shiftwidth<' 156 \ . '|setlocal softtabstop<' 157 \ . '|setlocal suffixesadd<' 158 \ . '|setlocal tabstop<' 159 \ . '|setlocal makeprg<' 160 \ . '|silent! nunmap <buffer> [M' 161 \ . '|silent! nunmap <buffer> [[' 162 \ . '|silent! nunmap <buffer> []' 163 \ . '|silent! nunmap <buffer> [m' 164 \ . '|silent! nunmap <buffer> ]M' 165 \ . '|silent! nunmap <buffer> ][' 166 \ . '|silent! nunmap <buffer> ]]' 167 \ . '|silent! nunmap <buffer> ]m' 168 \ . '|silent! ounmap <buffer> [M' 169 \ . '|silent! ounmap <buffer> [[' 170 \ . '|silent! ounmap <buffer> []' 171 \ . '|silent! ounmap <buffer> [m' 172 \ . '|silent! ounmap <buffer> ]M' 173 \ . '|silent! ounmap <buffer> ][' 174 \ . '|silent! ounmap <buffer> ]]' 175 \ . '|silent! ounmap <buffer> ]m' 176 \ . '|silent! xunmap <buffer> [M' 177 \ . '|silent! xunmap <buffer> [[' 178 \ . '|silent! xunmap <buffer> []' 179 \ . '|silent! xunmap <buffer> [m' 180 \ . '|silent! xunmap <buffer> ]M' 181 \ . '|silent! xunmap <buffer> ][' 182 \ . '|silent! xunmap <buffer> ]]' 183 \ . '|silent! xunmap <buffer> ]m' 184 \ . '|unlet! b:browsefilter' 185 \ . '|unlet! b:child_match' 186 \ . '|unlet! b:child_sub' 187 \ . '|unlet! b:grandparent_match' 188 \ . '|unlet! b:grandparent_sub' 189 \ . '|unlet! b:next' 190 \ . '|unlet! b:next_end' 191 \ . '|unlet! b:next_endtoplevel' 192 \ . '|unlet! b:next_toplevel' 193 \ . '|unlet! b:parent_match' 194 \ . '|unlet! b:parent_sub' 195 \ . '|unlet! b:prev' 196 \ . '|unlet! b:prev_end' 197 \ . '|unlet! b:prev_endtoplevel' 198 \ . '|unlet! b:prev_toplevel' 199 \ . '|unlet! b:undo_ftplugin' 200 201 let &cpo = s:keepcpo 202 unlet s:keepcpo