python.vim 6.8 KB

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