├── README.md └── indent └── python.vim /README.md: -------------------------------------------------------------------------------- 1 | 2 | # vim-python-indent-black 3 | 4 | This modifies the built-in Python `indentexpr` function to wrap the closing parenthesis, following the convention of [black](https://github.com/psf/black). 5 | 6 | That is, this will autoindent the closing parenthesis so that it lines up with the starting column of the parent statement line: 7 | 8 | ```python 9 | 1: if ( 10 | 2: conditional1 11 | 3: and conditional2 12 | 4: ... 13 | 5: and conditional3 14 | 6: ): 15 | ``` 16 | 17 | It also automatically sets the following values: 18 | 19 | ``` 20 | let g:pyindent_open_paren = 'shiftwidth()' 21 | let g:pyindent_nested_paren = 'shiftwidth()' 22 | let g:pyindent_continue = 'shiftwidth()' 23 | let g:pyindent_close_paren = '-shiftwidth()' 24 | ``` 25 | 26 | If needed, you can get the default Vim autoindent settings by specifying the following: 27 | 28 | ``` 29 | let g:pyindent_open_paren = 'shiftwidth()*2' 30 | let g:pyindent_nested_paren = 'shiftwidth()' 31 | let g:pyindent_continue = 'shiftwidth()*2' 32 | let g:pyindent_close_paren = '0' 33 | ``` 34 | 35 | -------------------------------------------------------------------------------- /indent/python.vim: -------------------------------------------------------------------------------- 1 | " Vim indent file 2 | " Language: Python 3 | " Maintainer: Bram Moolenaar 4 | " Original Author: David Bustos 5 | " Last Change: 2021 Sep 26 6 | 7 | " Only load this indent file when no other was loaded. 8 | if exists("b:did_indent") 9 | finish 10 | endif 11 | let b:did_indent = 1 12 | 13 | " Some preliminary settings 14 | setlocal nolisp " Make sure lisp indenting doesn't supersede us 15 | setlocal autoindent " indentexpr isn't much help otherwise 16 | 17 | setlocal indentexpr=GetPythonIndent(v:lnum) 18 | setlocal indentkeys+=<:>,=elif,=except 19 | 20 | let b:undo_indent = "setl ai< inde< indk< lisp<" 21 | 22 | " Only define the function once. 23 | if exists("*GetPythonIndent") 24 | finish 25 | endif 26 | let s:keepcpo= &cpo 27 | set cpo&vim 28 | 29 | " Come here when loading the script the first time. 30 | 31 | " Use [black](https://github.com/psf/black) convention unless overridden 32 | let g:pyindent_open_paren = get(g:, "pyindent_open_paren", "shiftwidth()") 33 | let g:pyindent_nested_paren = get(g:, "pyindent_nested_paren", "shiftwidth()") 34 | let g:pyindent_continue = get(g:, "pyindent_continue", "shiftwidth()") 35 | let g:pyindent_close_paren = get(g:, "pyindent_close_paren", "-shiftwidth()") 36 | 37 | let s:maxoff = 50 " maximum number of lines to look backwards for () 38 | 39 | " See if the specified line is already user-dedented from the expected value. 40 | function s:Dedented(lnum, expected) 41 | return indent(a:lnum) <= a:expected - shiftwidth() 42 | endfunction 43 | 44 | function GetPythonIndent(lnum) 45 | 46 | " If this line is explicitly joined: If the previous line was also joined, 47 | " line it up with that one, otherwise add two 'shiftwidth' 48 | if getline(a:lnum - 1) =~ '\\$' 49 | if a:lnum > 1 && getline(a:lnum - 2) =~ '\\$' 50 | return indent(a:lnum - 1) 51 | endif 52 | return indent(a:lnum - 1) + (exists("g:pyindent_continue") ? eval(g:pyindent_continue) : (shiftwidth() * 2)) 53 | endif 54 | 55 | " If the start of the line is in a string don't change the indent. 56 | if has('syntax_items') 57 | \ && synIDattr(synID(a:lnum, 1, 1), "name") =~ "String$" 58 | return -1 59 | endif 60 | 61 | " Search backwards for the previous non-empty line. 62 | let plnum = prevnonblank(v:lnum - 1) 63 | 64 | if plnum == 0 65 | " This is the first non-empty line, use zero indent. 66 | return 0 67 | endif 68 | 69 | call cursor(plnum, 1) 70 | 71 | " Identing inside parentheses can be very slow, regardless of the searchpair() 72 | " timeout, so let the user disable this feature if he doesn't need it 73 | let disable_parentheses_indenting = get(g:, "pyindent_disable_parentheses_indenting", 0) 74 | 75 | if disable_parentheses_indenting == 1 76 | let plindent = indent(plnum) 77 | let plnumstart = plnum 78 | else 79 | " searchpair() can be slow sometimes, limit the time to 150 msec or what is 80 | " put in g:pyindent_searchpair_timeout 81 | let searchpair_stopline = 0 82 | let searchpair_timeout = get(g:, 'pyindent_searchpair_timeout', 150) 83 | 84 | " If the previous line is inside parenthesis, use the indent of the starting 85 | " line. 86 | " Trick: use the non-existing "dummy" variable to break out of the loop when 87 | " going too far back. 88 | let parlnum = searchpair('(\|{\|\[', '', ')\|}\|\]', 'nbW', 89 | \ "line('.') < " . (plnum - s:maxoff) . " ? dummy :" 90 | \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')" 91 | \ . " =~ '\\(Comment\\|Todo\\|String\\)$'", 92 | \ searchpair_stopline, searchpair_timeout) 93 | if parlnum > 0 94 | let plindent = indent(parlnum) 95 | let plnumstart = parlnum 96 | else 97 | let plindent = indent(plnum) 98 | let plnumstart = plnum 99 | endif 100 | 101 | " When inside parenthesis: If at the first line below the parenthesis add 102 | " two 'shiftwidth', otherwise same as previous line. 103 | " i = (a 104 | " + b 105 | " + c) 106 | call cursor(a:lnum, 1) 107 | let p = searchpair('(\|{\|\[', '', ')\|}\|\]', 'bW', 108 | \ "line('.') < " . (a:lnum - s:maxoff) . " ? dummy :" 109 | \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')" 110 | \ . " =~ '\\(Comment\\|Todo\\|String\\)$'", 111 | \ searchpair_stopline, searchpair_timeout) 112 | if p > 0 113 | if p == plnum 114 | " When the start is inside parenthesis, only indent one 'shiftwidth'. 115 | let pp = searchpair('(\|{\|\[', '', ')\|}\|\]', 'bW', 116 | \ "line('.') < " . (a:lnum - s:maxoff) . " ? dummy :" 117 | \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')" 118 | \ . " =~ '\\(Comment\\|Todo\\|String\\)$'", 119 | \ searchpair_stopline, searchpair_timeout) 120 | if pp > 0 121 | return indent(plnum) + (exists("g:pyindent_nested_paren") ? eval(g:pyindent_nested_paren) : shiftwidth()) 122 | endif 123 | return indent(plnum) + (exists("g:pyindent_open_paren") ? eval(g:pyindent_open_paren) : (shiftwidth() * 2)) 124 | endif 125 | if plnumstart == p 126 | if getline(a:lnum) =~ '^\s*[)}\]]' 127 | return indent(plnum) + eval(get(g:, "pyindent_close_paren", "-shiftwidth()")) 128 | else 129 | return indent(plnum) 130 | endif 131 | endif 132 | return plindent 133 | endif 134 | 135 | endif 136 | 137 | " Get the line and remove a trailing comment. 138 | " Use syntax highlighting attributes when possible. 139 | let pline = getline(plnum) 140 | let pline_len = strlen(pline) 141 | if has('syntax_items') 142 | " If the last character in the line is a comment, do a binary search for 143 | " the start of the comment. synID() is slow, a linear search would take 144 | " too long on a long line. 145 | if synIDattr(synID(plnum, pline_len, 1), "name") =~ "\\(Comment\\|Todo\\)$" 146 | let min = 1 147 | let max = pline_len 148 | while min < max 149 | let col = (min + max) / 2 150 | if synIDattr(synID(plnum, col, 1), "name") =~ "\\(Comment\\|Todo\\)$" 151 | let max = col 152 | else 153 | let min = col + 1 154 | endif 155 | endwhile 156 | let pline = strpart(pline, 0, min - 1) 157 | endif 158 | else 159 | let col = 0 160 | while col < pline_len 161 | if pline[col] == '#' 162 | let pline = strpart(pline, 0, col) 163 | break 164 | endif 165 | let col = col + 1 166 | endwhile 167 | endif 168 | 169 | " If the previous line ended with a colon, indent this line 170 | if pline =~ ':\s*$' 171 | return plindent + shiftwidth() 172 | endif 173 | 174 | " If the previous line was a stop-execution statement... 175 | if getline(plnum) =~ '^\s*\(break\|continue\|raise\|return\|pass\)\>' 176 | " See if the user has already dedented 177 | if s:Dedented(a:lnum, indent(plnum)) 178 | " If so, trust the user 179 | return -1 180 | endif 181 | " If not, recommend one dedent 182 | return indent(plnum) - shiftwidth() 183 | endif 184 | 185 | " If the current line begins with a keyword that lines up with "try" 186 | if getline(a:lnum) =~ '^\s*\(except\|finally\)\>' 187 | let lnum = a:lnum - 1 188 | while lnum >= 1 189 | if getline(lnum) =~ '^\s*\(try\|except\)\>' 190 | let ind = indent(lnum) 191 | if ind >= indent(a:lnum) 192 | return -1 " indent is already less than this 193 | endif 194 | return ind " line up with previous try or except 195 | endif 196 | let lnum = lnum - 1 197 | endwhile 198 | return -1 " no matching "try"! 199 | endif 200 | 201 | " If the current line begins with a header keyword, dedent 202 | if getline(a:lnum) =~ '^\s*\(elif\|else\)\>' 203 | 204 | " Unless the previous line was a one-liner 205 | if getline(plnumstart) =~ '^\s*\(for\|if\|elif\|try\)\>' 206 | return plindent 207 | endif 208 | 209 | " Or the user has already dedented 210 | if s:Dedented(a:lnum, plindent) 211 | return -1 212 | endif 213 | 214 | return plindent - shiftwidth() 215 | endif 216 | 217 | " When after a () construct we probably want to go back to the start line. 218 | " a = (b 219 | " + c) 220 | " here 221 | if parlnum > 0 222 | " ...unless the user has already dedented 223 | if s:Dedented(a:lnum, plindent) 224 | return -1 225 | else 226 | return plindent 227 | endif 228 | endif 229 | 230 | " If the current line begins with closing parenthesis, dedent to parent 231 | if getline(a:lnum) =~ '^\s*[)]\>' 232 | 233 | " Or the user has already dedented 234 | if s:Dedented(a:lnum, plindent) 235 | return -1 236 | endif 237 | 238 | return plindent - shiftwidth() 239 | endif 240 | 241 | return -1 242 | 243 | endfunction 244 | 245 | let &cpo = s:keepcpo 246 | unlet s:keepcpo 247 | 248 | " vim:sw=2 249 | --------------------------------------------------------------------------------