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