All patches and comments are welcome. Please squash your changes to logical
commits before using git-format-patch and git-send-email to
patches@git.madduck.net.
If you'd read over the Git project's submission guidelines and adhered to them,
I'd be especially grateful.
1 " PEP8 compatible Python indent file
3 " Maintainer: Daniel Hahler <https://daniel.hahler.de/>
4 " Prev Maintainer: Hynek Schlawack <hs@ox.cx>
5 " Prev Maintainer: Eric Mc Sween <em@tomcom.de> (address invalid)
6 " Original Author: David Bustos <bustos@caltech.edu> (address invalid)
9 " vim-python-pep8-indent - A nicer Python indentation style for vim.
10 " Written in 2004 by David Bustos <bustos@caltech.edu>
11 " Maintained from 2004-2005 by Eric Mc Sween <em@tomcom.de>
12 " Maintained from 2013 by Hynek Schlawack <hs@ox.cx>
13 " Maintained from 2017 by Daniel Hahler <https://daniel.hahler.de/>
15 " To the extent possible under law, the author(s) have dedicated all copyright
16 " and related and neighboring rights to this software to the public domain
17 " worldwide. This software is distributed without any warranty.
18 " You should have received a copy of the CC0 Public Domain Dedication along
19 " with this software. If not, see
20 " <http://creativecommons.org/publicdomain/zero/1.0/>.
22 " Only load this indent file when no other was loaded.
23 if exists("b:did_indent")
31 setlocal indentexpr=GetPythonPEPIndent(v:lnum)
32 setlocal indentkeys=!^F,o,O,<:>,0),0],0},=elif,=except
34 setlocal softtabstop=4
37 if !exists('g:python_pep8_indent_multiline_string')
38 let g:python_pep8_indent_multiline_string = 0
43 \ '^\s*elif\>': ['if', 'elif'],
44 \ '^\s*except\>': ['try', 'except'],
45 \ '^\s*finally\>': ['try', 'except', 'else']
47 let s:block_rules_multiple = {
48 \ '^\s*else\>': ['if', 'elif', 'for', 'try', 'except'],
50 let s:paren_pairs = ['()', '{}', '[]']
51 if &ft == 'pyrex' || &ft == 'cython'
52 let b:control_statement = '\v^\s*(class|def|if|while|with|for|except|cdef|cpdef)>'
54 let b:control_statement = '\v^\s*(class|def|if|while|with|for|except)>'
56 let s:stop_statement = '^\s*\(break\|continue\|raise\|return\|pass\)\>'
58 " Skip strings and comments. Return 1 for chars to skip.
59 " jedi* refers to syntax definitions from jedi-vim for call signatures, which
60 " are inserted temporarily into the buffer.
61 let s:skip_special_chars = 'synIDattr(synID(line("."), col("."), 0), "name") ' .
62 \ '=~? "\\vstring|comment|jedi\\S"'
64 let s:skip_after_opening_paren = 'synIDattr(synID(line("."), col("."), 0), "name") ' .
65 \ '=~? "\\vcomment|jedi\\S"'
67 " Also ignore anything concealed.
68 " Wrapper around synconcealed for older Vim (7.3.429, used on Travis CI).
69 function! s:is_concealed(line, col)
70 let concealed = synconcealed(a:line, a:col)
71 return len(concealed) && concealed[0]
74 let s:skip_special_chars .= '|| s:is_concealed(line("."), col("."))'
78 let s:skip_search = 'synIDattr(synID(line("."), col("."), 0), "name") ' .
81 " Use 'shiftwidth()' instead of '&sw'.
82 " (Since Vim patch 7.3.629, 'shiftwidth' can be set to 0 to follow 'tabstop').
83 if exists('*shiftwidth')
93 function! s:pair_sort(x, y)
95 return a:x[1] == a:y[1] ? 0 : a:x[1] > a:y[1] ? 1 : -1
97 return a:x[0] > a:y[0] ? 1 : -1
101 " Find backwards the closest open parenthesis/bracket/brace.
102 function! s:find_opening_paren(...)
103 " optional arguments: line and column (defaults to 1) to search around
105 let view = winsaveview()
106 call cursor(a:1, a:0 > 1 ? a:2 : 1)
107 let ret = s:find_opening_paren()
108 call winrestview(view)
112 let stopline = max([0, line('.') - s:maxoff])
114 " Return if cursor is in a comment.
115 exe 'if' s:skip_search '| return [0, 0] | endif'
118 for p in s:paren_pairs
119 call add(positions, searchpairpos(
120 \ '\V'.p[0], '', '\V'.p[1], 'bnW', s:skip_special_chars, stopline))
123 " Remove empty matches and return the type with the closest match
124 call filter(positions, 'v:val[0]')
125 call sort(positions, 's:pair_sort')
127 return get(positions, -1, [0, 0])
130 " Find the start of a multi-line statement
131 function! s:find_start_of_multiline_statement(lnum)
134 if getline(lnum - 1) =~ '\\$'
135 let lnum = prevnonblank(lnum - 1)
137 let [paren_lnum, _] = s:find_opening_paren(lnum)
141 let lnum = paren_lnum
147 " Find possible indent(s) of the block starter that matches the current line.
148 function! s:find_start_of_block(lnum, types, multiple)
150 let types = copy(a:types)
151 let re = '\V\^\s\*\('.join(a:types, '\|').'\)\>'
153 let last_indent = indent(lnum) + 1
154 while lnum > 0 && last_indent > 0
155 let indent = indent(lnum)
156 if indent < last_indent
158 let re = '\v^\s*'.type.'>'
159 if getline(lnum) =~# re
163 if index(r, indent) == -1
166 " Remove any handled type, e.g. 'if'.
167 call remove(types, index(types, type))
170 let last_indent = indent(lnum)
172 let lnum = prevnonblank(lnum - 1)
177 " Is "expr" true for every position in "lnum", beginning at "start"?
178 " (optionally up to a:1 / 4th argument)
179 function! s:match_expr_on_line(expr, lnum, start, ...)
180 let text = getline(a:lnum)
181 let end = a:0 ? a:1 : len(text)
185 let save_pos = getpos('.')
187 for i in range(a:start, end)
188 call cursor(a:lnum, i)
189 if !(eval(a:expr) || text[i-1] =~ '\s')
194 call setpos('.', save_pos)
198 " Line up with open parenthesis/bracket/brace.
199 function! s:indent_like_opening_paren(lnum)
200 let [paren_lnum, paren_col] = s:find_opening_paren(a:lnum)
204 let text = getline(paren_lnum)
205 let base = indent(paren_lnum)
207 let nothing_after_opening_paren = s:match_expr_on_line(
208 \ s:skip_after_opening_paren, paren_lnum, paren_col+1)
209 let starts_with_closing_paren = getline(a:lnum) =~ '^\s*[])}]'
211 if nothing_after_opening_paren
212 if starts_with_closing_paren
215 let res = base + s:sw()
218 " Indent to match position of opening paren.
222 " If this line is the continuation of a control statement
223 " indent further to distinguish the continuation line
224 " from the next logical line.
225 if text =~# b:control_statement && res == base + s:sw()
226 return base + s:sw() * 2
232 " Match indent of first block of this type.
233 function! s:indent_like_block(lnum)
234 let text = getline(a:lnum)
235 for [multiple, block_rules] in [
236 \ [0, s:block_rules],
237 \ [1, s:block_rules_multiple]]
238 for [line_re, blocks] in items(block_rules)
243 let indents = s:find_start_of_block(a:lnum - 1, blocks, multiple)
251 " Multiple valid indents, e.g. for 'else' with both try and if.
252 let indent = indent(a:lnum)
253 if index(indents, indent) != -1
254 " The indent is valid, keep it.
257 " Fallback to the first/nearest one.
264 function! s:indent_like_previous_line(lnum)
265 let lnum = prevnonblank(a:lnum - 1)
267 " No previous line, keep current indent.
272 let text = getline(lnum)
273 let start = s:find_start_of_multiline_statement(lnum)
274 let base = indent(start)
275 let current = indent(a:lnum)
277 " Jump to last character in previous line.
278 call cursor(lnum, len(text))
279 let ignore_last_char = eval(s:skip_special_chars)
281 " Search for final colon that is not inside something to be ignored.
283 let curpos = getpos(".")[2]
284 if curpos == 1 | break | endif
285 if eval(s:skip_special_chars) || text[curpos-1] =~ '\s'
288 elseif text[curpos-1] == ':'
294 if text =~ '\\$' && !ignore_last_char
295 " If this line is the continuation of a control statement
296 " indent further to distinguish the continuation line
297 " from the next logical line.
298 if getline(start) =~# b:control_statement
299 return base + s:sw() * 2
302 " Nest (other) explicit continuations only one level deeper.
306 let empty = getline(a:lnum) =~# '^\s*$'
308 " If the previous statement was a stop-execution statement or a pass
309 if getline(start) =~# s:stop_statement
310 " Remove one level of indentation if the user hasn't already dedented
311 if empty || current > base - s:sw()
314 " Otherwise, trust the user
318 if !empty && s:is_dedented_already(current, base)
322 " In all other cases, line up with the start of the previous statement.
326 " If this line is dedented and the number of indent spaces is valid
327 " (multiple of the indentation size), trust the user.
328 function! s:is_dedented_already(current, base)
329 let dedent_size = a:current - a:base
330 return (dedent_size < 0 && a:current % s:sw() == 0) ? 1 : 0
333 " Is the syntax at lnum (and optionally cnum) a python string?
334 function! s:is_python_string(lnum, ...)
335 let line = getline(a:lnum)
336 let linelen = len(line)
340 let cols = a:0 ? type(a:1) != type([]) ? [a:1] : a:1 : range(1, linelen)
342 if match(map(synstack(a:lnum, cnum),
343 \ 'synIDattr(v:val,"name")'), 'python\S*String') == -1
350 function! GetPythonPEPIndent(lnum)
351 " First line has indent 0
356 let line = getline(a:lnum)
357 let prevline = getline(a:lnum-1)
359 " Multilinestrings: continous, docstring or starting.
360 if s:is_python_string(a:lnum-1, len(prevline))
361 \ && (s:is_python_string(a:lnum, 1)
362 \ || match(line, '^\%("""\|''''''\)') != -1)
364 " Indent closing quotes as the line with the opening ones.
365 let match_quotes = match(line, '^\s*\zs\%("""\|''''''\)')
366 if match_quotes != -1
367 " closing multiline string
368 let quotes = line[match_quotes:match_quotes+2]
369 let pairpos = searchpairpos(quotes, '', quotes, 'b')
371 return indent(pairpos[0])
373 " TODO: test to cover this!
377 if s:is_python_string(a:lnum-1)
378 " Previous line is (completely) a string.
379 return indent(a:lnum-1)
382 if match(prevline, '^\s*\%("""\|''''''\)') != -1
384 return indent(a:lnum-1)
387 let indent_multi = get(b:, 'python_pep8_indent_multiline_string',
388 \ get(g:, 'python_pep8_indent_multiline_string', 0))
389 if match(prevline, '\v%("""|'''''')$') != -1
390 " Opening multiline string, started in previous line.
391 if (&autoindent && indent(a:lnum) == indent(a:lnum-1))
392 \ || match(line, '\v^\s+$') != -1
393 " <CR> with empty line or to split up 'foo("""bar' into
394 " 'foo("""' and 'bar'.
395 if indent_multi == -2
396 return indent(a:lnum-1) + s:sw()
402 " Keep existing indent.
403 if match(line, '\v^\s*\S') != -1
407 if indent_multi != -2
411 return s:indent_like_opening_paren(a:lnum)
414 " Parens: If we can find an open parenthesis/bracket/brace, line up with it.
415 let indent = s:indent_like_opening_paren(a:lnum)
420 " Blocks: Match indent of first block of this type.
421 let indent = s:indent_like_block(a:lnum)
426 return s:indent_like_previous_line(a:lnum)