|
| 1 | +" PEP8 compatible Python indent file |
| 2 | +" Language: Python |
| 3 | +" Maintainer: Hynek Schlawack <hs@ox.cx> |
| 4 | +" Prev Maintainer: Eric Mc Sween <em@tomcom.de> (address invalid) |
| 5 | +" Original Author: David Bustos <bustos@caltech.edu> (address invalid) |
| 6 | +" Last Change: 2012-06-21 |
| 7 | +" License: Public Domainlet |
| 8 | + |
| 9 | + |
| 10 | +function! pymode#indent#Indent(lnum) |
| 11 | + |
| 12 | + " First line has indent 0 |
| 13 | + if a:lnum == 1 |
| 14 | + return 0 |
| 15 | + endif |
| 16 | + |
| 17 | + " If we can find an open parenthesis/bracket/brace, line up with it. |
| 18 | + call cursor(a:lnum, 1) |
| 19 | + let parlnum = s:SearchParensPair() |
| 20 | + if parlnum > 0 |
| 21 | + let parcol = col('.') |
| 22 | + let closing_paren = match(getline(a:lnum), '^\s*[])}]') != -1 |
| 23 | + if match(getline(parlnum), '[([{]\s*$', parcol - 1) != -1 |
| 24 | + if closing_paren |
| 25 | + return indent(parlnum) |
| 26 | + else |
| 27 | + return indent(parlnum) + &shiftwidth |
| 28 | + endif |
| 29 | + else |
| 30 | + return parcol |
| 31 | + endif |
| 32 | + endif |
| 33 | + |
| 34 | + " Examine this line |
| 35 | + let thisline = getline(a:lnum) |
| 36 | + let thisindent = indent(a:lnum) |
| 37 | + |
| 38 | + " If the line starts with 'elif' or 'else', line up with 'if' or 'elif' |
| 39 | + if thisline =~ '^\s*\(elif\|else\)\>' |
| 40 | + let bslnum = s:BlockStarter(a:lnum, '^\s*\(if\|elif\)\>') |
| 41 | + if bslnum > 0 |
| 42 | + return indent(bslnum) |
| 43 | + else |
| 44 | + return -1 |
| 45 | + endif |
| 46 | + endif |
| 47 | + |
| 48 | + " If the line starts with 'except' or 'finally', line up with 'try' |
| 49 | + " or 'except' |
| 50 | + if thisline =~ '^\s*\(except\|finally\)\>' |
| 51 | + let bslnum = s:BlockStarter(a:lnum, '^\s*\(try\|except\)\>') |
| 52 | + if bslnum > 0 |
| 53 | + return indent(bslnum) |
| 54 | + else |
| 55 | + return -1 |
| 56 | + endif |
| 57 | + endif |
| 58 | + |
| 59 | + " Examine previous line |
| 60 | + let plnum = a:lnum - 1 |
| 61 | + let pline = getline(plnum) |
| 62 | + let sslnum = s:StatementStart(plnum) |
| 63 | + |
| 64 | + " If the previous line is blank, keep the same indentation |
| 65 | + if pline =~ '^\s*$' |
| 66 | + return -1 |
| 67 | + endif |
| 68 | + |
| 69 | + " If this line is explicitly joined, try to find an indentation that looks |
| 70 | + " good. |
| 71 | + if pline =~ '\\$' |
| 72 | + let compound_statement = '^\s*\(if\|while\|for\s.*\sin\|except\)\s*' |
| 73 | + let maybe_indent = matchend(getline(sslnum), compound_statement) |
| 74 | + if maybe_indent != -1 |
| 75 | + return maybe_indent |
| 76 | + else |
| 77 | + return indent(sslnum) + &sw * 2 |
| 78 | + endif |
| 79 | + endif |
| 80 | + |
| 81 | + " If the previous line ended with a colon and is not a comment, indent |
| 82 | + " relative to statement start. |
| 83 | + if pline =~ ':\s*$' && pline !~ '^\s*#' |
| 84 | + return indent(sslnum) + &sw |
| 85 | + endif |
| 86 | + |
| 87 | + " If the previous line was a stop-execution statement or a pass |
| 88 | + if getline(sslnum) =~ '^\s*\(break\|continue\|raise\|return\|pass\)\>' |
| 89 | + " See if the user has already dedented |
| 90 | + if indent(a:lnum) > indent(sslnum) - &sw |
| 91 | + " If not, recommend one dedent |
| 92 | + return indent(sslnum) - &sw |
| 93 | + endif |
| 94 | + " Otherwise, trust the user |
| 95 | + return -1 |
| 96 | + endif |
| 97 | + |
| 98 | + " In all other cases, line up with the start of the previous statement. |
| 99 | + return indent(sslnum) |
| 100 | +endfunction |
| 101 | + |
| 102 | + |
| 103 | +" Find backwards the closest open parenthesis/bracket/brace. |
| 104 | +function! s:SearchParensPair() |
| 105 | + let line = line('.') |
| 106 | + let col = col('.') |
| 107 | + |
| 108 | + " Skip strings and comments and don't look too far |
| 109 | + let skip = "line('.') < " . (line - 50) . " ? dummy :" . |
| 110 | + \ 'synIDattr(synID(line("."), col("."), 0), "name") =~? ' . |
| 111 | + \ '"string\\|comment"' |
| 112 | + |
| 113 | + " Search for parentheses |
| 114 | + call cursor(line, col) |
| 115 | + let parlnum = searchpair('(', '', ')', 'bW', skip) |
| 116 | + let parcol = col('.') |
| 117 | + |
| 118 | + " Search for brackets |
| 119 | + call cursor(line, col) |
| 120 | + let par2lnum = searchpair('\[', '', '\]', 'bW', skip) |
| 121 | + let par2col = col('.') |
| 122 | + |
| 123 | + " Search for braces |
| 124 | + call cursor(line, col) |
| 125 | + let par3lnum = searchpair('{', '', '}', 'bW', skip) |
| 126 | + let par3col = col('.') |
| 127 | + |
| 128 | + " Get the closest match |
| 129 | + if par2lnum > parlnum || (par2lnum == parlnum && par2col > parcol) |
| 130 | + let parlnum = par2lnum |
| 131 | + let parcol = par2col |
| 132 | + endif |
| 133 | + if par3lnum > parlnum || (par3lnum == parlnum && par3col > parcol) |
| 134 | + let parlnum = par3lnum |
| 135 | + let parcol = par3col |
| 136 | + endif |
| 137 | + |
| 138 | + " Put the cursor on the match |
| 139 | + if parlnum > 0 |
| 140 | + call cursor(parlnum, parcol) |
| 141 | + endif |
| 142 | + return parlnum |
| 143 | +endfunction |
| 144 | + |
| 145 | + |
| 146 | +" Find the start of a multi-line statement |
| 147 | +function! s:StatementStart(lnum) |
| 148 | + let lnum = a:lnum |
| 149 | + while 1 |
| 150 | + if getline(lnum - 1) =~ '\\$' |
| 151 | + let lnum = lnum - 1 |
| 152 | + else |
| 153 | + call cursor(lnum, 1) |
| 154 | + let maybe_lnum = s:SearchParensPair() |
| 155 | + if maybe_lnum < 1 |
| 156 | + return lnum |
| 157 | + else |
| 158 | + let lnum = maybe_lnum |
| 159 | + endif |
| 160 | + endif |
| 161 | + endwhile |
| 162 | +endfunction |
| 163 | + |
| 164 | + |
| 165 | +" Find the block starter that matches the current line |
| 166 | +function! s:BlockStarter(lnum, block_start_re) |
| 167 | + let lnum = a:lnum |
| 168 | + let maxindent = 10000 " whatever |
| 169 | + while lnum > 1 |
| 170 | + let lnum = prevnonblank(lnum - 1) |
| 171 | + if indent(lnum) < maxindent |
| 172 | + if getline(lnum) =~ a:block_start_re |
| 173 | + return lnum |
| 174 | + else |
| 175 | + let maxindent = indent(lnum) |
| 176 | + " It's not worth going further if we reached the top level |
| 177 | + if maxindent == 0 |
| 178 | + return -1 |
| 179 | + endif |
| 180 | + endif |
| 181 | + endif |
| 182 | + endwhile |
| 183 | + return -1 |
| 184 | +endfunction |
0 commit comments