blob: 6f204688966ad6717f07f93b249eda754ff54c9a [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Vim filetype plugin file
2" Language: python
Tom Picton86f6e2c2024-05-11 14:26:06 -04003" Maintainer: Tom Picton <tom@tompicton.com>
Bram Moolenaar01164a62017-11-02 22:58:42 +01004" Previous Maintainer: James Sully <sullyj3@gmail.com>
Bram Moolenaarc95a3022016-06-12 23:01:46 +02005" Previous Maintainer: Johannes Zellner <johannes@zellner.org>
Konfekt3c2596a2024-11-30 11:32:49 +01006" Repository: https://github.com/tpict/vim-ftplugin-python
7" Last Change: 2024/05/13
8" 2024 Nov 30 use pytest compiler (#16130)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009
10if exists("b:did_ftplugin") | finish | endif
11let b:did_ftplugin = 1
Bram Moolenaar9a7224b2012-04-30 15:56:52 +020012let s:keepcpo= &cpo
13set cpo&vim
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15setlocal cinkeys-=0#
16setlocal indentkeys-=0#
Bram Moolenaar92dff182014-02-11 19:15:50 +010017setlocal include=^\\s*\\(from\\\|import\\)
Tom Picton79612102024-05-13 16:00:40 -040018setlocal define=^\\s*\\(\\(async\\s\\+\\)\\?def\\\|class\\)
Bram Moolenaar98ef2332018-03-18 14:44:37 +010019
20" For imports with leading .., append / and replace additional .s with ../
21let b:grandparent_match = '^\(.\.\)\(\.*\)'
22let b:grandparent_sub = '\=submatch(1)."/".repeat("../",strlen(submatch(2)))'
23
24" For imports with a single leading ., replace it with ./
25let b:parent_match = '^\.\(\.\)\@!'
26let b:parent_sub = './'
27
28" Replace any . sandwiched between word characters with /
29let b:child_match = '\(\w\)\.\(\w\)'
30let b:child_sub = '\1/\2'
31
32setlocal includeexpr=substitute(substitute(substitute(
33 \v:fname,
34 \b:grandparent_match,b:grandparent_sub,''),
35 \b:parent_match,b:parent_sub,''),
36 \b:child_match,b:child_sub,'g')
37
Bram Moolenaar071d4272004-06-13 20:20:40 +000038setlocal suffixesadd=.py
Bram Moolenaar36782082013-11-28 13:53:34 +010039setlocal comments=b:#,fb:-
40setlocal commentstring=#\ %s
Bram Moolenaar071d4272004-06-13 20:20:40 +000041
Bram Moolenaar40962ec2018-01-28 22:47:25 +010042if has('python3')
Bram Moolenaar4f4d51a2020-10-11 13:57:40 +020043 setlocal omnifunc=python3complete#Complete
Bram Moolenaar63b74a82019-03-24 15:09:13 +010044elseif has('python')
45 setlocal omnifunc=pythoncomplete#Complete
Bram Moolenaar40962ec2018-01-28 22:47:25 +010046endif
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000047
Bram Moolenaar071d4272004-06-13 20:20:40 +000048set wildignore+=*.pyc
49
Bram Moolenaardc083282016-10-11 08:57:33 +020050let b:next_toplevel='\v%$\|^(class\|def\|async def)>'
51let b:prev_toplevel='\v^(class\|def\|async def)>'
Bram Moolenaar01164a62017-11-02 22:58:42 +010052let b:next_endtoplevel='\v%$\|\S.*\n+(def\|class)'
53let b:prev_endtoplevel='\v\S.*\n+(def\|class)'
Bram Moolenaardc083282016-10-11 08:57:33 +020054let b:next='\v%$\|^\s*(class\|def\|async def)>'
55let b:prev='\v^\s*(class\|def\|async def)>'
Bram Moolenaar7f2e9d72017-11-11 20:58:53 +010056let b:next_end='\v\S\n*(%$\|^(\s*\n*)*(class\|def\|async def)\|^\S)'
57let b:prev_end='\v\S\n*(^(\s*\n*)*(class\|def\|async def)\|^\S)'
Bram Moolenaare18dbe82016-07-02 21:42:23 +020058
Bram Moolenaar63b74a82019-03-24 15:09:13 +010059if !exists('g:no_plugin_maps') && !exists('g:no_python_maps')
Tom Picton86f6e2c2024-05-11 14:26:06 -040060 execute "nnoremap <silent> <buffer> ]] :<C-U>call <SID>Python_jump('n', '". b:next_toplevel."', 'W', v:count1)<cr>"
61 execute "nnoremap <silent> <buffer> [[ :<C-U>call <SID>Python_jump('n', '". b:prev_toplevel."', 'Wb', v:count1)<cr>"
62 execute "nnoremap <silent> <buffer> ][ :<C-U>call <SID>Python_jump('n', '". b:next_endtoplevel."', 'W', v:count1, 0)<cr>"
63 execute "nnoremap <silent> <buffer> [] :<C-U>call <SID>Python_jump('n', '". b:prev_endtoplevel."', 'Wb', v:count1, 0)<cr>"
64 execute "nnoremap <silent> <buffer> ]m :<C-U>call <SID>Python_jump('n', '". b:next."', 'W', v:count1)<cr>"
65 execute "nnoremap <silent> <buffer> [m :<C-U>call <SID>Python_jump('n', '". b:prev."', 'Wb', v:count1)<cr>"
66 execute "nnoremap <silent> <buffer> ]M :<C-U>call <SID>Python_jump('n', '". b:next_end."', 'W', v:count1, 0)<cr>"
67 execute "nnoremap <silent> <buffer> [M :<C-U>call <SID>Python_jump('n', '". b:prev_end."', 'Wb', v:count1, 0)<cr>"
Bram Moolenaarabd468e2016-09-08 22:22:43 +020068
Bram Moolenaar63b74a82019-03-24 15:09:13 +010069 execute "onoremap <silent> <buffer> ]] :call <SID>Python_jump('o', '". b:next_toplevel."', 'W', v:count1)<cr>"
70 execute "onoremap <silent> <buffer> [[ :call <SID>Python_jump('o', '". b:prev_toplevel."', 'Wb', v:count1)<cr>"
71 execute "onoremap <silent> <buffer> ][ :call <SID>Python_jump('o', '". b:next_endtoplevel."', 'W', v:count1, 0)<cr>"
72 execute "onoremap <silent> <buffer> [] :call <SID>Python_jump('o', '". b:prev_endtoplevel."', 'Wb', v:count1, 0)<cr>"
73 execute "onoremap <silent> <buffer> ]m :call <SID>Python_jump('o', '". b:next."', 'W', v:count1)<cr>"
74 execute "onoremap <silent> <buffer> [m :call <SID>Python_jump('o', '". b:prev."', 'Wb', v:count1)<cr>"
75 execute "onoremap <silent> <buffer> ]M :call <SID>Python_jump('o', '". b:next_end."', 'W', v:count1, 0)<cr>"
76 execute "onoremap <silent> <buffer> [M :call <SID>Python_jump('o', '". b:prev_end."', 'Wb', v:count1, 0)<cr>"
Bram Moolenaarabd468e2016-09-08 22:22:43 +020077
Bram Moolenaar63b74a82019-03-24 15:09:13 +010078 execute "xnoremap <silent> <buffer> ]] :call <SID>Python_jump('x', '". b:next_toplevel."', 'W', v:count1)<cr>"
79 execute "xnoremap <silent> <buffer> [[ :call <SID>Python_jump('x', '". b:prev_toplevel."', 'Wb', v:count1)<cr>"
80 execute "xnoremap <silent> <buffer> ][ :call <SID>Python_jump('x', '". b:next_endtoplevel."', 'W', v:count1, 0)<cr>"
81 execute "xnoremap <silent> <buffer> [] :call <SID>Python_jump('x', '". b:prev_endtoplevel."', 'Wb', v:count1, 0)<cr>"
82 execute "xnoremap <silent> <buffer> ]m :call <SID>Python_jump('x', '". b:next."', 'W', v:count1)<cr>"
83 execute "xnoremap <silent> <buffer> [m :call <SID>Python_jump('x', '". b:prev."', 'Wb', v:count1)<cr>"
84 execute "xnoremap <silent> <buffer> ]M :call <SID>Python_jump('x', '". b:next_end."', 'W', v:count1, 0)<cr>"
85 execute "xnoremap <silent> <buffer> [M :call <SID>Python_jump('x', '". b:prev_end."', 'Wb', v:count1, 0)<cr>"
86endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000087
Bram Moolenaar36782082013-11-28 13:53:34 +010088if !exists('*<SID>Python_jump')
Bram Moolenaar7dda86f2018-04-20 22:36:41 +020089 fun! <SID>Python_jump(mode, motion, flags, count, ...) range
Bram Moolenaar01164a62017-11-02 22:58:42 +010090 let l:startofline = (a:0 >= 1) ? a:1 : 1
91
Bram Moolenaare18dbe82016-07-02 21:42:23 +020092 if a:mode == 'x'
93 normal! gv
94 endif
95
Bram Moolenaar01164a62017-11-02 22:58:42 +010096 if l:startofline == 1
97 normal! 0
98 endif
Bram Moolenaare18dbe82016-07-02 21:42:23 +020099
Bram Moolenaar7dda86f2018-04-20 22:36:41 +0200100 let cnt = a:count
Bram Moolenaar36782082013-11-28 13:53:34 +0100101 mark '
102 while cnt > 0
Bram Moolenaare18dbe82016-07-02 21:42:23 +0200103 call search(a:motion, a:flags)
104 let cnt = cnt - 1
Bram Moolenaar36782082013-11-28 13:53:34 +0100105 endwhile
Bram Moolenaare18dbe82016-07-02 21:42:23 +0200106
Bram Moolenaar01164a62017-11-02 22:58:42 +0100107 if l:startofline == 1
108 normal! ^
109 endif
Bram Moolenaar36782082013-11-28 13:53:34 +0100110 endfun
111endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112
Bram Moolenaar36782082013-11-28 13:53:34 +0100113if has("browsefilter") && !exists("b:browsefilter")
Doug Kearns93197fd2024-01-14 20:59:02 +0100114 let b:browsefilter = "Python Files (*.py)\t*.py\n"
115 if has("win32")
116 let b:browsefilter .= "All Files (*.*)\t*\n"
117 else
118 let b:browsefilter .= "All Files (*)\t*\n"
119 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120endif
Bram Moolenaar9a7224b2012-04-30 15:56:52 +0200121
Bram Moolenaarabd468e2016-09-08 22:22:43 +0200122if !exists("g:python_recommended_style") || g:python_recommended_style != 0
123 " As suggested by PEP8.
Bram Moolenaar4f4d51a2020-10-11 13:57:40 +0200124 setlocal expandtab tabstop=4 softtabstop=4 shiftwidth=4
Bram Moolenaarabd468e2016-09-08 22:22:43 +0200125endif
Bram Moolenaarb1332082013-10-06 14:22:40 +0200126
Bram Moolenaar4f4d51a2020-10-11 13:57:40 +0200127" Use pydoc for keywordprg.
128" Unix users preferentially get pydoc3, then pydoc2.
129" Windows doesn't have a standalone pydoc executable in $PATH by default, nor
130" does it have separate python2/3 executables, so Windows users just get
131" whichever version corresponds to their installed Python version.
132if executable('python3')
133 setlocal keywordprg=python3\ -m\ pydoc
134elseif executable('python')
135 setlocal keywordprg=python\ -m\ pydoc
Bram Moolenaarb1332082013-10-06 14:22:40 +0200136endif
137
Konfekt3c2596a2024-11-30 11:32:49 +0100138if expand('%:t') =~# '\v^test_.*\.py$|_test\.py$' && executable('pytest')
139 compiler pytest
140 let &l:makeprg .= ' %:S'
141endif
142
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100143" Script for filetype switching to undo the local stuff we may have changed
144let b:undo_ftplugin = 'setlocal cinkeys<'
145 \ . '|setlocal comments<'
146 \ . '|setlocal commentstring<'
147 \ . '|setlocal expandtab<'
148 \ . '|setlocal include<'
149 \ . '|setlocal includeexpr<'
150 \ . '|setlocal indentkeys<'
151 \ . '|setlocal keywordprg<'
152 \ . '|setlocal omnifunc<'
153 \ . '|setlocal shiftwidth<'
154 \ . '|setlocal softtabstop<'
155 \ . '|setlocal suffixesadd<'
156 \ . '|setlocal tabstop<'
Konfekt3c2596a2024-11-30 11:32:49 +0100157 \ . '|setlocal makeprg<'
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100158 \ . '|silent! nunmap <buffer> [M'
159 \ . '|silent! nunmap <buffer> [['
160 \ . '|silent! nunmap <buffer> []'
161 \ . '|silent! nunmap <buffer> [m'
162 \ . '|silent! nunmap <buffer> ]M'
163 \ . '|silent! nunmap <buffer> ]['
164 \ . '|silent! nunmap <buffer> ]]'
165 \ . '|silent! nunmap <buffer> ]m'
166 \ . '|silent! ounmap <buffer> [M'
167 \ . '|silent! ounmap <buffer> [['
168 \ . '|silent! ounmap <buffer> []'
169 \ . '|silent! ounmap <buffer> [m'
170 \ . '|silent! ounmap <buffer> ]M'
171 \ . '|silent! ounmap <buffer> ]['
172 \ . '|silent! ounmap <buffer> ]]'
173 \ . '|silent! ounmap <buffer> ]m'
174 \ . '|silent! xunmap <buffer> [M'
175 \ . '|silent! xunmap <buffer> [['
176 \ . '|silent! xunmap <buffer> []'
177 \ . '|silent! xunmap <buffer> [m'
178 \ . '|silent! xunmap <buffer> ]M'
179 \ . '|silent! xunmap <buffer> ]['
180 \ . '|silent! xunmap <buffer> ]]'
181 \ . '|silent! xunmap <buffer> ]m'
182 \ . '|unlet! b:browsefilter'
183 \ . '|unlet! b:child_match'
184 \ . '|unlet! b:child_sub'
185 \ . '|unlet! b:grandparent_match'
186 \ . '|unlet! b:grandparent_sub'
187 \ . '|unlet! b:next'
188 \ . '|unlet! b:next_end'
189 \ . '|unlet! b:next_endtoplevel'
190 \ . '|unlet! b:next_toplevel'
191 \ . '|unlet! b:parent_match'
192 \ . '|unlet! b:parent_sub'
193 \ . '|unlet! b:prev'
194 \ . '|unlet! b:prev_end'
195 \ . '|unlet! b:prev_endtoplevel'
196 \ . '|unlet! b:prev_toplevel'
197 \ . '|unlet! b:undo_ftplugin'
198
Bram Moolenaar9a7224b2012-04-30 15:56:52 +0200199let &cpo = s:keepcpo
200unlet s:keepcpo