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