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')
30 setlocal indentexpr=GetPythonPEPIndent(v:lnum)
31 setlocal indentkeys=!^F,o,O,<:>,0),0],0},=elif,=except
33 if !exists('g:python_pep8_indent_multiline_string')
34 let g:python_pep8_indent_multiline_string = 0
37 if !exists('g:python_pep8_indent_hang_closing')
38 let g:python_pep8_indent_hang_closing = 0
41 " TODO: check required patch for timeout argument, likely lower than 7.3.429 though.
42 if !exists('g:python_pep8_indent_searchpair_timeout')
43 if has('patch-8.0.1483')
44 let g:python_pep8_indent_searchpair_timeout = 150
46 let g:python_pep8_indent_searchpair_timeout = 0
51 \ '^\s*elif\>': [['if', 'elif'], ['else']],
52 \ '^\s*except\>': [['try', 'except'], []],
53 \ '^\s*finally\>': [['try', 'except', 'else'], []]
55 let s:block_rules_multiple = {
56 \ '^\s*else\>': [['if', 'elif', 'for', 'try', 'except'], []]
58 " Pairs to look for when searching for opening parenthesis.
59 " The value is the maximum offset in lines.
60 let s:paren_pairs = {'()': 50, '[]': 100, '{}': 1000}
62 if &filetype ==# 'pyrex' || &filetype ==# 'cython'
63 let b:control_statement = '\v^\s*(class|def|if|while|with|for|except|cdef|cpdef)>'
65 let b:control_statement = '\v^\s*(class|def|if|while|with|for|except)>'
67 let s:stop_statement = '^\s*\(break\|continue\|raise\|return\|pass\)\>'
69 let s:skip_after_opening_paren = 'synIDattr(synID(line("."), col("."), 0), "name") ' .
70 \ '=~? "\\vcomment|jedi\\S"'
72 let s:special_chars_syn_pattern = "\\vstring|comment|^pythonbytes%(contents)=$|pythonTodo|jedi\\S"
74 if !get(g:, 'python_pep8_indent_skip_concealed', 0) || !has('conceal')
75 " Skip strings and comments. Return 1 for chars to skip.
76 " jedi* refers to syntax definitions from jedi-vim for call signatures, which
77 " are inserted temporarily into the buffer.
78 function! s:_skip_special_chars(line, col)
79 return synIDattr(synID(a:line, a:col, 0), 'name')
80 \ =~? s:special_chars_syn_pattern
83 " Also ignore anything concealed.
84 " TODO: doc; likely only necessary with jedi-vim, where a better version is
85 " planned (https://github.com/Vimjas/vim-python-pep8-indent/pull/98).
87 " Wrapper around synconcealed for older Vim (7.3.429, used on Travis CI).
88 function! s:is_concealed(line, col)
89 let concealed = synconcealed(a:line, a:col)
90 return len(concealed) && concealed[0]
93 function! s:_skip_special_chars(line, col)
94 return synIDattr(synID(a:line, a:col, 0), 'name')
95 \ =~? s:special_chars_syn_pattern
96 \ || s:is_concealed(a:line, a:col)
100 " Use 'shiftwidth()' instead of '&sw'.
101 " (Since Vim patch 7.3.629, 'shiftwidth' can be set to 0 to follow 'tabstop').
102 if exists('*shiftwidth')
112 " Find backwards the closest open parenthesis/bracket/brace.
113 function! s:find_opening_paren(lnum, col)
114 " Return if cursor is in a comment.
115 if synIDattr(synID(a:lnum, a:col, 0), 'name') =~? 'comment'
119 call cursor(a:lnum, a:col)
122 let timeout = g:python_pep8_indent_searchpair_timeout
123 let skip_special_chars = 's:_skip_special_chars(line("."), col("."))'
124 for [p, maxoff] in items(s:paren_pairs)
125 let stopline = max([0, line('.') - maxoff, nearest[0]])
126 let next = searchpairpos(
127 \ '\V'.p[0], '', '\V'.p[1], 'bnW', skip_special_chars, stopline, timeout)
128 if next[0] && (next[0] > nearest[0] || (next[0] == nearest[0] && next[1] > nearest[1]))
135 " Find the start of a multi-line statement
136 function! s:find_start_of_multiline_statement(lnum)
139 if getline(lnum - 1) =~# '\\$'
140 let lnum = prevnonblank(lnum - 1)
142 let [paren_lnum, _] = s:find_opening_paren(lnum, 1)
146 let lnum = paren_lnum
152 " Find possible indent(s) of the block starter that matches the current line.
153 function! s:find_start_of_block(lnum, types, skip, multiple) abort
155 let re = '\V\^\s\*\('.join(a:types, '\|').'\)\>'
157 let re_skip = '\V\^\s\*\('.join(a:skip, '\|').'\)\>'
161 let last_indent = indent(a:lnum) + 1
162 let lnum = a:lnum - 1
163 while lnum > 0 && last_indent > 0
164 let indent = indent(lnum)
165 if indent < last_indent
166 let line = getline(lnum)
167 if !empty(re_skip) && line =~# re_skip
168 let last_indent = indent
173 if index(r, indent) == -1
176 let last_indent = indent
179 let lnum = prevnonblank(lnum - 1)
184 " Is "expr" true for every position in "lnum", beginning at "start"?
185 " (optionally up to a:1 / 4th argument)
186 function! s:match_expr_on_line(expr, lnum, start, ...)
187 let text = getline(a:lnum)
188 let end = a:0 ? a:1 : len(text)
192 let save_pos = getpos('.')
194 for i in range(a:start, end)
195 call cursor(a:lnum, i)
196 if !(eval(a:expr) || text[i-1] =~# '\s')
201 call setpos('.', save_pos)
205 " Line up with open parenthesis/bracket/brace.
206 function! s:indent_like_opening_paren(lnum)
207 let [paren_lnum, paren_col] = s:find_opening_paren(a:lnum, 1)
211 let text = getline(paren_lnum)
212 let base = indent(paren_lnum)
214 let nothing_after_opening_paren = s:match_expr_on_line(
215 \ s:skip_after_opening_paren, paren_lnum, paren_col+1)
216 let starts_with_closing_paren = getline(a:lnum) =~# '^\s*[])}]'
218 let hang_closing = get(b:, 'python_pep8_indent_hang_closing',
219 \ get(g:, 'python_pep8_indent_hang_closing', 0))
221 if nothing_after_opening_paren
222 if starts_with_closing_paren && !hang_closing
225 let res = base + s:sw()
227 " Special case for parenthesis.
228 if text[paren_col-1] ==# '(' && getline(a:lnum) !~# '\v\)\s*:?\s*$'
233 " Indent to match position of opening paren.
237 " If this line is the continuation of a control statement
238 " indent further to distinguish the continuation line
239 " from the next logical line.
240 if text =~# b:control_statement && res == base + s:sw()
241 " But only if not inside parens itself (Flake's E127).
242 let [paren_lnum, _] = s:find_opening_paren(paren_lnum, 1)
250 " Match indent of first block of this type.
251 function! s:indent_like_block(lnum)
252 let text = getline(a:lnum)
253 for [multiple, block_rules] in [
254 \ [0, s:block_rules],
255 \ [1, s:block_rules_multiple],
257 for [line_re, blocks_ignore] in items(block_rules)
262 let [blocks, skip] = blocks_ignore
263 let indents = s:find_start_of_block(a:lnum, blocks, skip, multiple)
271 " Multiple valid indents, e.g. for 'else' with both try and if.
272 let indent = indent(a:lnum)
273 if index(indents, indent) != -1
274 " The indent is valid, keep it.
277 " Fallback to the first/nearest one.
284 function! s:indent_like_previous_line(lnum)
285 let lnum = prevnonblank(a:lnum - 1)
287 " No previous line, keep current indent.
292 let text = getline(lnum)
293 let start = s:find_start_of_multiline_statement(lnum)
294 let base = indent(start)
295 let current = indent(a:lnum)
297 " Ignore last character in previous line?
298 let lastcol = len(text)
301 " Search for final colon that is not inside something to be ignored.
303 if col == 1 | break | endif
304 if text[col-1] =~# '\s' || s:_skip_special_chars(lnum, col)
307 elseif text[col-1] ==# ':'
313 if text =~# '\\$' && !s:_skip_special_chars(lnum, lastcol)
314 " If this line is the continuation of a control statement
315 " indent further to distinguish the continuation line
316 " from the next logical line.
317 if getline(start) =~# b:control_statement
318 return base + s:sw() * 2
321 " Nest (other) explicit continuations only one level deeper.
325 let empty = getline(a:lnum) =~# '^\s*$'
327 " Current and prev line are empty, next is not -> indent like next.
328 if empty && a:lnum > 1 &&
329 \ (getline(a:lnum - 1) =~# '^\s*$') &&
330 \ !(getline(a:lnum + 1) =~# '^\s*$')
331 return indent(a:lnum + 1)
334 " If the previous statement was a stop-execution statement or a pass
335 if getline(start) =~# s:stop_statement
336 " Remove one level of indentation if the user hasn't already dedented
337 if empty || current > base - s:sw()
340 " Otherwise, trust the user
344 if (current || !empty) && s:is_dedented_already(current, base)
348 " In all other cases, line up with the start of the previous statement.
352 " If this line is dedented and the number of indent spaces is valid
353 " (multiple of the indentation size), trust the user.
354 function! s:is_dedented_already(current, base)
355 let dedent_size = a:current - a:base
356 return (dedent_size < 0 && a:current % s:sw() == 0) ? 1 : 0
359 " Is the syntax at lnum (and optionally cnum) a python string?
360 function! s:is_python_string(lnum, ...)
361 let line = getline(a:lnum)
363 let cols = type(a:1) != type([]) ? [a:1] : a:1
365 let cols = range(1, max([1, len(line)]))
368 if match(map(synstack(a:lnum, cnum),
369 \ "synIDattr(v:val, 'name')"), 'python\S*String') == -1
376 function! GetPythonPEPIndent(lnum)
377 " First line has indent 0
382 let line = getline(a:lnum)
383 let prevline = getline(a:lnum-1)
385 " Multilinestrings: continous, docstring or starting.
386 if s:is_python_string(a:lnum-1, max([1, len(prevline)]))
387 \ && (s:is_python_string(a:lnum, 1)
388 \ || match(line, '^\%("""\|''''''\)') != -1)
390 " Indent closing quotes as the line with the opening ones.
391 let match_quotes = match(line, '^\s*\zs\%("""\|''''''\)')
392 if match_quotes != -1
393 " closing multiline string
394 let quotes = line[match_quotes:(match_quotes+2)]
395 call cursor(a:lnum, 1)
396 let pairpos = searchpairpos(quotes, '', quotes, 'bW', '', 0, g:python_pep8_indent_searchpair_timeout)
398 return indent(pairpos[0])
404 if s:is_python_string(a:lnum-1)
405 " Previous line is (completely) a string: keep current indent.
409 if match(prevline, '^\s*\%("""\|''''''\)') != -1
411 return indent(a:lnum-1)
414 let indent_multi = get(b:, 'python_pep8_indent_multiline_string',
415 \ get(g:, 'python_pep8_indent_multiline_string', 0))
416 if match(prevline, '\v%("""|'''''')$') != -1
417 " Opening multiline string, started in previous line.
418 if (&autoindent && indent(a:lnum) == indent(a:lnum-1))
419 \ || match(line, '\v^\s+$') != -1
420 " <CR> with empty line or to split up 'foo("""bar' into
421 " 'foo("""' and 'bar'.
422 if indent_multi == -2
423 return indent(a:lnum-1) + s:sw()
429 " Keep existing indent.
430 if match(line, '\v^\s*\S') != -1
434 if indent_multi != -2
438 return s:indent_like_opening_paren(a:lnum)
441 " Parens: If we can find an open parenthesis/bracket/brace, line up with it.
442 let indent = s:indent_like_opening_paren(a:lnum)
447 " Blocks: Match indent of first block of this type.
448 let indent = s:indent_like_block(a:lnum)
453 return s:indent_like_previous_line(a:lnum)