xref: /vim-8.2.3635/runtime/indent/python.vim (revision e828b762)
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  " If the previous line is inside parenthesis, use the indent of the starting
57  " line.
58  " Trick: use the non-existing "dummy" variable to break out of the loop when
59  " going too far back.
60  call cursor(plnum, 1)
61  let parlnum = searchpair('(\|{\|\[', '', ')\|}\|\]', 'nbW',
62	  \ "line('.') < " . (plnum - s:maxoff) . " ? dummy :"
63	  \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')"
64	  \ . " =~ '\\(Comment\\|Todo\\|String\\)$'")
65  if parlnum > 0
66    let plindent = indent(parlnum)
67    let plnumstart = parlnum
68  else
69    let plindent = indent(plnum)
70    let plnumstart = plnum
71  endif
72
73
74  " When inside parenthesis: If at the first line below the parenthesis add
75  " two 'shiftwidth', otherwise same as previous line.
76  " i = (a
77  "       + b
78  "       + c)
79  call cursor(a:lnum, 1)
80  let p = searchpair('(\|{\|\[', '', ')\|}\|\]', 'bW',
81	  \ "line('.') < " . (a:lnum - s:maxoff) . " ? dummy :"
82	  \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')"
83	  \ . " =~ '\\(Comment\\|Todo\\|String\\)$'")
84  if p > 0
85    if p == plnum
86      " When the start is inside parenthesis, only indent one 'shiftwidth'.
87      let pp = searchpair('(\|{\|\[', '', ')\|}\|\]', 'bW',
88	  \ "line('.') < " . (a:lnum - s:maxoff) . " ? dummy :"
89	  \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')"
90	  \ . " =~ '\\(Comment\\|Todo\\|String\\)$'")
91      if pp > 0
92	return indent(plnum) + (exists("g:pyindent_nested_paren") ? eval(g:pyindent_nested_paren) : shiftwidth())
93      endif
94      return indent(plnum) + (exists("g:pyindent_open_paren") ? eval(g:pyindent_open_paren) : (shiftwidth() * 2))
95    endif
96    if plnumstart == p
97      return indent(plnum)
98    endif
99    return plindent
100  endif
101
102
103  " Get the line and remove a trailing comment.
104  " Use syntax highlighting attributes when possible.
105  let pline = getline(plnum)
106  let pline_len = strlen(pline)
107  if has('syntax_items')
108    " If the last character in the line is a comment, do a binary search for
109    " the start of the comment.  synID() is slow, a linear search would take
110    " too long on a long line.
111    if synIDattr(synID(plnum, pline_len, 1), "name") =~ "\\(Comment\\|Todo\\)$"
112      let min = 1
113      let max = pline_len
114      while min < max
115	let col = (min + max) / 2
116	if synIDattr(synID(plnum, col, 1), "name") =~ "\\(Comment\\|Todo\\)$"
117	  let max = col
118	else
119	  let min = col + 1
120	endif
121      endwhile
122      let pline = strpart(pline, 0, min - 1)
123    endif
124  else
125    let col = 0
126    while col < pline_len
127      if pline[col] == '#'
128	let pline = strpart(pline, 0, col)
129	break
130      endif
131      let col = col + 1
132    endwhile
133  endif
134
135  " If the previous line ended with a colon, indent this line
136  if pline =~ ':\s*$'
137    return plindent + shiftwidth()
138  endif
139
140  " If the previous line was a stop-execution statement...
141  if getline(plnum) =~ '^\s*\(break\|continue\|raise\|return\|pass\)\>'
142    " See if the user has already dedented
143    if indent(a:lnum) > indent(plnum) - shiftwidth()
144      " If not, recommend one dedent
145      return indent(plnum) - shiftwidth()
146    endif
147    " Otherwise, trust the user
148    return -1
149  endif
150
151  " If the current line begins with a keyword that lines up with "try"
152  if getline(a:lnum) =~ '^\s*\(except\|finally\)\>'
153    let lnum = a:lnum - 1
154    while lnum >= 1
155      if getline(lnum) =~ '^\s*\(try\|except\)\>'
156	let ind = indent(lnum)
157	if ind >= indent(a:lnum)
158	  return -1	" indent is already less than this
159	endif
160	return ind	" line up with previous try or except
161      endif
162      let lnum = lnum - 1
163    endwhile
164    return -1		" no matching "try"!
165  endif
166
167  " If the current line begins with a header keyword, dedent
168  if getline(a:lnum) =~ '^\s*\(elif\|else\)\>'
169
170    " Unless the previous line was a one-liner
171    if getline(plnumstart) =~ '^\s*\(for\|if\|try\)\>'
172      return plindent
173    endif
174
175    " Or the user has already dedented
176    if indent(a:lnum) <= plindent - shiftwidth()
177      return -1
178    endif
179
180    return plindent - shiftwidth()
181  endif
182
183  " When after a () construct we probably want to go back to the start line.
184  " a = (b
185  "       + c)
186  " here
187  if parlnum > 0
188    return plindent
189  endif
190
191  return -1
192
193endfunction
194
195let &cpo = s:keepcpo
196unlet s:keepcpo
197
198" vim:sw=2
199