Mercurial > vim
annotate runtime/indent/python.vim @ 27673:ba7dcf54d309 v8.2.4362
patch 8.2.4362: :retab may allocate too much memory
Commit: https://github.com/vim/vim/commit/33f3c5985491032d5bdfc30e722e85d5a0285e64
Author: Bram Moolenaar <Bram@vim.org>
Date: Sat Feb 12 20:46:15 2022 +0000
patch 8.2.4362: :retab may allocate too much memory
Problem: :retab may allocate too much memory.
Solution: Bail out when allocating more than MAXCOL bytes.
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Sat, 12 Feb 2022 22:00:03 +0100 |
parents | 9c221ad9634a |
children | 67f31c24291b |
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> |
25880 | 5 " Last Change: 2021 Sep 26 |
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 | |
25880 | 20 let b:undo_indent = "setl ai< inde< indk< lisp<" |
21 | |
7 | 22 " Only define the function once. |
23 if exists("*GetPythonIndent") | |
24 finish | |
25 endif | |
3507
8201108e9cf0
More runtime file fixes for 'compatible' mode.
Bram Moolenaar <bram@vim.org>
parents:
3496
diff
changeset
|
26 let s:keepcpo= &cpo |
8201108e9cf0
More runtime file fixes for 'compatible' mode.
Bram Moolenaar <bram@vim.org>
parents:
3496
diff
changeset
|
27 set cpo&vim |
7 | 28 |
170 | 29 " Come here when loading the script the first time. |
30 | |
7 | 31 let s:maxoff = 50 " maximum number of lines to look backwards for () |
32 | |
20379 | 33 " See if the specified line is already user-dedented from the expected value. |
34 function s:Dedented(lnum, expected) | |
35 return indent(a:lnum) <= a:expected - shiftwidth() | |
36 endfunction | |
37 | |
7 | 38 function GetPythonIndent(lnum) |
856 | 39 |
7 | 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 | |
4992 | 46 return indent(a:lnum - 1) + (exists("g:pyindent_continue") ? eval(g:pyindent_continue) : (shiftwidth() * 2)) |
7 | 47 endif |
48 | |
49 " If the start of the line is in a string don't change the indent. | |
50 if has('syntax_items') | |
8 | 51 \ && synIDattr(synID(a:lnum, 1, 1), "name") =~ "String$" |
7 | 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 | |
15932 | 63 call cursor(plnum, 1) |
14945 | 64 |
15932 | 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 | |
7 | 70 let plindent = indent(plnum) |
71 let plnumstart = plnum | |
15932 | 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) | |
7 | 77 |
15932 | 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 | |
7 | 94 |
15932 | 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)) | |
7 | 118 endif |
15932 | 119 if plnumstart == p |
120 return indent(plnum) | |
121 endif | |
122 return plindent | |
7 | 123 endif |
15932 | 124 |
7 | 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) | |
20 | 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. | |
5220 | 136 if synIDattr(synID(plnum, pline_len, 1), "name") =~ "\\(Comment\\|Todo\\)$" |
20 | 137 let min = 1 |
138 let max = pline_len | |
139 while min < max | |
140 let col = (min + max) / 2 | |
5220 | 141 if synIDattr(synID(plnum, col, 1), "name") =~ "\\(Comment\\|Todo\\)$" |
20 | 142 let max = col |
143 else | |
144 let min = col + 1 | |
145 endif | |
146 endwhile | |
147 let pline = strpart(pline, 0, min - 1) | |
7 | 148 endif |
20 | 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 | |
7 | 159 |
160 " If the previous line ended with a colon, indent this line | |
161 if pline =~ ':\s*$' | |
4992 | 162 return plindent + shiftwidth() |
7 | 163 endif |
164 | |
165 " If the previous line was a stop-execution statement... | |
1121 | 166 if getline(plnum) =~ '^\s*\(break\|continue\|raise\|return\|pass\)\>' |
7 | 167 " See if the user has already dedented |
20379 | 168 if s:Dedented(a:lnum, indent(plnum)) |
169 " If so, trust the user | |
170 return -1 | |
7 | 171 endif |
20379 | 172 " If not, recommend one dedent |
173 return indent(plnum) - shiftwidth() | |
7 | 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 | |
24751 | 196 if getline(plnumstart) =~ '^\s*\(for\|if\|elif\|try\)\>' |
7 | 197 return plindent |
198 endif | |
199 | |
200 " Or the user has already dedented | |
20379 | 201 if s:Dedented(a:lnum, plindent) |
7 | 202 return -1 |
203 endif | |
204 | |
4992 | 205 return plindent - shiftwidth() |
7 | 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 | |
20379 | 213 " ...unless the user has already dedented |
214 if s:Dedented(a:lnum, plindent) | |
215 return -1 | |
216 else | |
217 return plindent | |
218 endif | |
7 | 219 endif |
220 | |
221 return -1 | |
222 | |
223 endfunction | |
224 | |
3496
d1e4abe8342c
Fixed compatible mode in most runtime files.
Bram Moolenaar <bram@vim.org>
parents:
1121
diff
changeset
|
225 let &cpo = s:keepcpo |
d1e4abe8342c
Fixed compatible mode in most runtime files.
Bram Moolenaar <bram@vim.org>
parents:
1121
diff
changeset
|
226 unlet s:keepcpo |
d1e4abe8342c
Fixed compatible mode in most runtime files.
Bram Moolenaar <bram@vim.org>
parents:
1121
diff
changeset
|
227 |
7 | 228 " vim:sw=2 |