python.vim 8.1 KB

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