| |
|
|
| let s:keepcpo= &cpo |
| set cpo&vim |
|
|
| |
| let g:python_indent = extend(get(g:, 'python_indent', {}), #{ |
| \ closed_paren_align_last_line: v:true, |
| \ open_paren: get(g:, 'pyindent_open_paren', 'shiftwidth() * 2'), |
| \ nested_paren: get(g:, 'pyindent_nested_paren', 'shiftwidth()'), |
| \ continue: get(g:, 'pyindent_continue', 'shiftwidth() * 2'), |
| |
| |
| \ searchpair_timeout: get(g:, 'pyindent_searchpair_timeout', 150), |
| |
| |
| \ disable_parentheses_indenting: get(g:, 'pyindent_disable_parentheses_indenting', v:false), |
| \ }, 'keep') |
|
|
| let s:maxoff = 50 |
|
|
| function s:SearchBracket(fromlnum, flags) |
| return searchpairpos('[[({]', '', '[])}]', a:flags, |
| \ {-> synstack('.', col('.')) |
| \ ->indexof({_, id -> synIDattr(id, 'name') =~ '\%(Comment\|Todo\|String\)$'}) >= 0}, |
| \ [0, a:fromlnum - s:maxoff]->max(), g:python_indent.searchpair_timeout) |
| endfunction |
|
|
| |
| function s:Dedented(lnum, expected) |
| return indent(a:lnum) <= a:expected - shiftwidth() |
| endfunction |
|
|
| |
| |
| |
| function python#GetIndent(lnum, ...) |
| let ExtraFunc = a:0 > 0 ? a:1 : 0 |
|
|
| |
| |
| if getline(a:lnum - 1) =~ '\\$' |
| if a:lnum > 1 && getline(a:lnum - 2) =~ '\\$' |
| return indent(a:lnum - 1) |
| endif |
| return indent(a:lnum - 1) + get(g:, 'pyindent_continue', g:python_indent.continue)->eval() |
| endif |
|
|
| |
| if has('syntax_items') |
| \ && synIDattr(synID(a:lnum, 1, 1), "name") =~ "String$" |
| return -1 |
| endif |
|
|
| |
| let plnum = prevnonblank(v:lnum - 1) |
|
|
| if plnum == 0 |
| |
| return 0 |
| endif |
|
|
| if g:python_indent.disable_parentheses_indenting == 1 |
| let plindent = indent(plnum) |
| let plnumstart = plnum |
| else |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| call cursor(a:lnum, 1) |
| let [parlnum, parcol] = s:SearchBracket(a:lnum, 'nbW') |
| if parlnum > 0 |
| if parcol != col([parlnum, '$']) - 1 |
| return parcol |
| elseif getline(a:lnum) =~ '^\s*[])}]' && !g:python_indent.closed_paren_align_last_line |
| return indent(parlnum) |
| endif |
| endif |
|
|
| call cursor(plnum, 1) |
|
|
| |
| |
| let [parlnum, _] = s:SearchBracket(plnum, 'nbW') |
| if parlnum > 0 |
| if a:0 > 0 && ExtraFunc(parlnum) |
| |
| |
| let parlnum = 0 |
| let plindent = indent(plnum) |
| let plnumstart = plnum |
| else |
| let plindent = indent(parlnum) |
| let plnumstart = parlnum |
| endif |
| else |
| let plindent = indent(plnum) |
| let plnumstart = plnum |
| endif |
|
|
| |
| |
| |
| |
| |
| call cursor(a:lnum, 1) |
| let [p, _] = s:SearchBracket(a:lnum, 'bW') |
| if p > 0 |
| if a:0 > 0 && ExtraFunc(p) |
| |
| |
| if p == plnum |
| return shiftwidth() |
| endif |
|
|
| |
| let line = getline(a:lnum) |
| if line =~ '^\s*}' |
| return -2 |
| endif |
|
|
| |
| let p = 0 |
| else |
| if p == plnum |
| |
| let [pp, _] = s:SearchBracket(a:lnum, 'bW') |
| if pp > 0 |
| return indent(plnum) |
| \ + get(g:, 'pyindent_nested_paren', g:python_indent.nested_paren)->eval() |
| endif |
| return indent(plnum) |
| \ + get(g:, 'pyindent_open_paren', g:python_indent.open_paren)->eval() |
| endif |
| if plnumstart == p |
| return indent(plnum) |
| endif |
| return plindent |
| endif |
| endif |
| endif |
|
|
|
|
| |
| |
| let pline = getline(plnum) |
| let pline_len = strlen(pline) |
| if has('syntax_items') |
| |
| |
| |
| if synstack(plnum, pline_len) |
| \ ->indexof({_, id -> synIDattr(id, 'name') =~ '\%(Comment\|Todo\)$'}) >= 0 |
| let min = 1 |
| let max = pline_len |
| while min < max |
| let col = (min + max) / 2 |
| if synstack(plnum, col) |
| \ ->indexof({_, id -> synIDattr(id, 'name') =~ '\%(Comment\|Todo\)$'}) >= 0 |
| let max = col |
| else |
| let min = col + 1 |
| endif |
| endwhile |
| let pline = strpart(pline, 0, min - 1) |
| endif |
| else |
| let col = 0 |
| while col < pline_len |
| if pline[col] == '#' |
| let pline = strpart(pline, 0, col) |
| break |
| endif |
| let col = col + 1 |
| endwhile |
| endif |
|
|
| |
| if pline =~ ':\s*$' |
| return plindent + shiftwidth() |
| endif |
|
|
| |
| if getline(plnum) =~ '^\s*\(break\|continue\|raise\|return\|pass\)\>' |
| |
| if s:Dedented(a:lnum, indent(plnum)) |
| |
| return -1 |
| endif |
| |
| return indent(plnum) - shiftwidth() |
| endif |
|
|
| " If the current line begins with a keyword that lines up with "try |
| if getline(a:lnum) =~ '^\s*\(except\|finally\)\>' |
| let lnum = a:lnum - 1 |
| while lnum >= 1 |
| if getline(lnum) =~ '^\s*\(try\|except\)\>' |
| let ind = indent(lnum) |
| if ind >= indent(a:lnum) |
| return -1 |
| endif |
| return ind |
| endif |
| let lnum = lnum - 1 |
| endwhile |
| return -1 " no matching "try |
| endif |
|
|
| |
| if getline(a:lnum) =~ '^\s*\(elif\|else\)\>' |
|
|
| |
| if getline(plnumstart) =~ '^\s*\(for\|if\|elif\|try\)\>' |
| return plindent |
| endif |
|
|
| |
| if s:Dedented(a:lnum, plindent) |
| return -1 |
| endif |
|
|
| return plindent - shiftwidth() |
| endif |
|
|
| |
| |
| |
| |
| if parlnum > 0 |
| |
| if s:Dedented(a:lnum, plindent) |
| return -1 |
| else |
| return plindent |
| endif |
| endif |
|
|
| return -1 |
| endfunction |
|
|
| let &cpo = s:keepcpo |
| unlet s:keepcpo |
|
|