Mercurial > vim
annotate runtime/indent/python.vim @ 19345:c221e35656dc
Added tag v8.2.0230 for changeset 77794a1d7e98d9b8dc0fe8f44c9d33a533fe9ac5
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Fri, 07 Feb 2020 22:30:04 +0100 |
parents | c3c1abe7aff2 |
children | 7f88f6a3ed4c |
rev | line source |
---|---|
7 | 1 " Vim indent file |
164 | 2 " Language: Python |
3 " Maintainer: Bram Moolenaar <Bram@vim.org> | |
7 | 4 " Original Author: David Bustos <bustos@caltech.edu> |
15932 | 5 " Last Change: 2019 Feb 21 |
7 | 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 " Only define the function once. | |
21 if exists("*GetPythonIndent") | |
22 finish | |
23 endif | |
3507
8201108e9cf0
More runtime file fixes for 'compatible' mode.
Bram Moolenaar <bram@vim.org>
parents:
3496
diff
changeset
|
24 let s:keepcpo= &cpo |
8201108e9cf0
More runtime file fixes for 'compatible' mode.
Bram Moolenaar <bram@vim.org>
parents:
3496
diff
changeset
|
25 set cpo&vim |
7 | 26 |
170 | 27 " Come here when loading the script the first time. |
28 | |
7 | 29 let s:maxoff = 50 " maximum number of lines to look backwards for () |
30 | |
31 function GetPythonIndent(lnum) | |
856 | 32 |
7 | 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 | |
4992 | 39 return indent(a:lnum - 1) + (exists("g:pyindent_continue") ? eval(g:pyindent_continue) : (shiftwidth() * 2)) |
7 | 40 endif |
41 | |
42 " If the start of the line is in a string don't change the indent. | |
43 if has('syntax_items') | |
8 | 44 \ && synIDattr(synID(a:lnum, 1, 1), "name") =~ "String$" |
7 | 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 | |
15932 | 56 call cursor(plnum, 1) |
14945 | 57 |
15932 | 58 " Identing inside parentheses can be very slow, regardless of the searchpair() |
59 " timeout, so let the user disable this feature if he doesn't need it | |
60 let disable_parentheses_indenting = get(g:, "pyindent_disable_parentheses_indenting", 0) | |
61 | |
62 if disable_parentheses_indenting == 1 | |
7 | 63 let plindent = indent(plnum) |
64 let plnumstart = plnum | |
15932 | 65 else |
66 " searchpair() can be slow sometimes, limit the time to 150 msec or what is | |
67 " put in g:pyindent_searchpair_timeout | |
68 let searchpair_stopline = 0 | |
69 let searchpair_timeout = get(g:, 'pyindent_searchpair_timeout', 150) | |
7 | 70 |
15932 | 71 " If the previous line is inside parenthesis, use the indent of the starting |
72 " line. | |
73 " Trick: use the non-existing "dummy" variable to break out of the loop when | |
74 " going too far back. | |
75 let parlnum = searchpair('(\|{\|\[', '', ')\|}\|\]', 'nbW', | |
76 \ "line('.') < " . (plnum - s:maxoff) . " ? dummy :" | |
77 \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')" | |
78 \ . " =~ '\\(Comment\\|Todo\\|String\\)$'", | |
79 \ searchpair_stopline, searchpair_timeout) | |
80 if parlnum > 0 | |
81 let plindent = indent(parlnum) | |
82 let plnumstart = parlnum | |
83 else | |
84 let plindent = indent(plnum) | |
85 let plnumstart = plnum | |
86 endif | |
7 | 87 |
15932 | 88 " When inside parenthesis: If at the first line below the parenthesis add |
89 " two 'shiftwidth', otherwise same as previous line. | |
90 " i = (a | |
91 " + b | |
92 " + c) | |
93 call cursor(a:lnum, 1) | |
94 let p = 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 p > 0 | |
100 if p == plnum | |
101 " When the start is inside parenthesis, only indent one 'shiftwidth'. | |
102 let pp = searchpair('(\|{\|\[', '', ')\|}\|\]', 'bW', | |
103 \ "line('.') < " . (a:lnum - s:maxoff) . " ? dummy :" | |
104 \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')" | |
105 \ . " =~ '\\(Comment\\|Todo\\|String\\)$'", | |
106 \ searchpair_stopline, searchpair_timeout) | |
107 if pp > 0 | |
108 return indent(plnum) + (exists("g:pyindent_nested_paren") ? eval(g:pyindent_nested_paren) : shiftwidth()) | |
109 endif | |
110 return indent(plnum) + (exists("g:pyindent_open_paren") ? eval(g:pyindent_open_paren) : (shiftwidth() * 2)) | |
7 | 111 endif |
15932 | 112 if plnumstart == p |
113 return indent(plnum) | |
114 endif | |
115 return plindent | |
7 | 116 endif |
15932 | 117 |
7 | 118 endif |
119 | |
120 | |
121 " Get the line and remove a trailing comment. | |
122 " Use syntax highlighting attributes when possible. | |
123 let pline = getline(plnum) | |
124 let pline_len = strlen(pline) | |
20 | 125 if has('syntax_items') |
126 " If the last character in the line is a comment, do a binary search for | |
127 " the start of the comment. synID() is slow, a linear search would take | |
128 " too long on a long line. | |
5220 | 129 if synIDattr(synID(plnum, pline_len, 1), "name") =~ "\\(Comment\\|Todo\\)$" |
20 | 130 let min = 1 |
131 let max = pline_len | |
132 while min < max | |
133 let col = (min + max) / 2 | |
5220 | 134 if synIDattr(synID(plnum, col, 1), "name") =~ "\\(Comment\\|Todo\\)$" |
20 | 135 let max = col |
136 else | |
137 let min = col + 1 | |
138 endif | |
139 endwhile | |
140 let pline = strpart(pline, 0, min - 1) | |
7 | 141 endif |
20 | 142 else |
143 let col = 0 | |
144 while col < pline_len | |
145 if pline[col] == '#' | |
146 let pline = strpart(pline, 0, col) | |
147 break | |
148 endif | |
149 let col = col + 1 | |
150 endwhile | |
151 endif | |
7 | 152 |
153 " If the previous line ended with a colon, indent this line | |
154 if pline =~ ':\s*$' | |
4992 | 155 return plindent + shiftwidth() |
7 | 156 endif |
157 | |
158 " If the previous line was a stop-execution statement... | |
1121 | 159 if getline(plnum) =~ '^\s*\(break\|continue\|raise\|return\|pass\)\>' |
7 | 160 " See if the user has already dedented |
4992 | 161 if indent(a:lnum) > indent(plnum) - shiftwidth() |
7 | 162 " If not, recommend one dedent |
4992 | 163 return indent(plnum) - shiftwidth() |
7 | 164 endif |
165 " Otherwise, trust the user | |
166 return -1 | |
167 endif | |
168 | |
169 " If the current line begins with a keyword that lines up with "try" | |
170 if getline(a:lnum) =~ '^\s*\(except\|finally\)\>' | |
171 let lnum = a:lnum - 1 | |
172 while lnum >= 1 | |
173 if getline(lnum) =~ '^\s*\(try\|except\)\>' | |
174 let ind = indent(lnum) | |
175 if ind >= indent(a:lnum) | |
176 return -1 " indent is already less than this | |
177 endif | |
178 return ind " line up with previous try or except | |
179 endif | |
180 let lnum = lnum - 1 | |
181 endwhile | |
182 return -1 " no matching "try"! | |
183 endif | |
184 | |
185 " If the current line begins with a header keyword, dedent | |
186 if getline(a:lnum) =~ '^\s*\(elif\|else\)\>' | |
187 | |
188 " Unless the previous line was a one-liner | |
189 if getline(plnumstart) =~ '^\s*\(for\|if\|try\)\>' | |
190 return plindent | |
191 endif | |
192 | |
193 " Or the user has already dedented | |
4992 | 194 if indent(a:lnum) <= plindent - shiftwidth() |
7 | 195 return -1 |
196 endif | |
197 | |
4992 | 198 return plindent - shiftwidth() |
7 | 199 endif |
200 | |
201 " When after a () construct we probably want to go back to the start line. | |
202 " a = (b | |
203 " + c) | |
204 " here | |
205 if parlnum > 0 | |
206 return plindent | |
207 endif | |
208 | |
209 return -1 | |
210 | |
211 endfunction | |
212 | |
3496
d1e4abe8342c
Fixed compatible mode in most runtime files.
Bram Moolenaar <bram@vim.org>
parents:
1121
diff
changeset
|
213 let &cpo = s:keepcpo |
d1e4abe8342c
Fixed compatible mode in most runtime files.
Bram Moolenaar <bram@vim.org>
parents:
1121
diff
changeset
|
214 unlet s:keepcpo |
d1e4abe8342c
Fixed compatible mode in most runtime files.
Bram Moolenaar <bram@vim.org>
parents:
1121
diff
changeset
|
215 |
7 | 216 " vim:sw=2 |