robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

zeertzjqMerge pull request #40472 from zeertzjq/vim-1f337327945732ed7

master
8.2 KiB202 linesraw
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
11if exists("b:did_ftplugin") | finish | endif
12let b:did_ftplugin = 1
13let s:keepcpo= &cpo
14set cpo&vim
15
16setlocal cinkeys-=0#
17setlocal indentkeys-=0#
18setlocal include=^\\s*\\(from\\\|import\\)
19setlocal define=^\\s*\\(\\(async\\s\\+\\)\\?def\\\|class\\)
20
21" For imports with leading .., append / and replace additional .s with ../
22let b:grandparent_match = '^\(.\.\)\(\.*\)'
23let b:grandparent_sub = '\=submatch(1)."/".repeat("../",strlen(submatch(2)))'
24
25" For imports with a single leading ., replace it with ./
26let b:parent_match = '^\.\(\.\)\@!'
27let b:parent_sub = './'
28
29" Replace any . sandwiched between word characters with /
30let b:child_match = '\(\w\)\.\(\w\)'
31let b:child_sub = '\1/\2'
32
33setlocal 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
39setlocal suffixesadd=.py
40setlocal comments=b:#,fb:-
41setlocal commentstring=#\ %s
42
43if has('python3')
44  setlocal omnifunc=python3complete#Complete
45elseif has('python')
46  setlocal omnifunc=pythoncomplete#Complete
47endif
48
49set wildignore+=*.pyc
50
51let b:next_toplevel='\v%$\|^(class\|def\|async def)>'
52let b:prev_toplevel='\v^(class\|def\|async def)>'
53let b:next_endtoplevel='\v%$\|\S.*\n+(def\|class)'
54let b:prev_endtoplevel='\v\S.*\n+(def\|class)'
55let b:next='\v%$\|^\s*(class\|def\|async def)>'
56let b:prev='\v^\s*(class\|def\|async def)>'
57let b:next_end='\v\S\n*(%$\|^(\s*\n*)*(class\|def\|async def)\|^\S)'
58let b:prev_end='\v\S\n*(^(\s*\n*)*(class\|def\|async def)\|^\S)'
59
60if !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>"
87endif
88
89if !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
112endif
113
114if 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
121endif
122
123if 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
127endif
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.
134if executable('python3')
135  setlocal keywordprg=python3\ -m\ pydoc
136elseif executable('python')
137  setlocal keywordprg=python\ -m\ pydoc
138endif
139
140if expand('%:t') =~# '\v^test_.*\.py$|_test\.py$' && executable('pytest')
141  compiler pytest
142  let &l:makeprg .= ' %:S'
143endif
144
145" Script for filetype switching to undo the local stuff we may have changed
146let 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
201let &cpo = s:keepcpo
202unlet s:keepcpo