diff options
Diffstat (limited to 'bitbake/contrib')
-rw-r--r-- | bitbake/contrib/vim/indent/bitbake.vim | 343 |
1 files changed, 343 insertions, 0 deletions
diff --git a/bitbake/contrib/vim/indent/bitbake.vim b/bitbake/contrib/vim/indent/bitbake.vim new file mode 100644 index 0000000000..1381034098 --- /dev/null +++ b/bitbake/contrib/vim/indent/bitbake.vim | |||
@@ -0,0 +1,343 @@ | |||
1 | " Vim indent file | ||
2 | " Language: BitBake | ||
3 | " Copyright: Copyright (C) 2019 Agilent Technologies, Inc. | ||
4 | " Maintainer: Chris Laplante <chris.laplante@agilent.com> | ||
5 | " License: You may redistribute this under the same terms as Vim itself | ||
6 | |||
7 | |||
8 | if exists("b:did_indent") | ||
9 | finish | ||
10 | endif | ||
11 | |||
12 | if exists("*BitbakeIndent") | ||
13 | finish | ||
14 | endif | ||
15 | |||
16 | runtime! indent/sh.vim | ||
17 | unlet b:did_indent | ||
18 | |||
19 | setlocal indentexpr=BitbakeIndent(v:lnum) | ||
20 | setlocal autoindent nolisp | ||
21 | |||
22 | function s:is_bb_python_func_def(lnum) | ||
23 | let stack = synstack(a:lnum, 1) | ||
24 | if len(stack) == 0 | ||
25 | return 0 | ||
26 | endif | ||
27 | |||
28 | let top = synIDattr(stack[0], "name") | ||
29 | echo top | ||
30 | |||
31 | return synIDattr(stack[0], "name") == "bbPyFuncDef" | ||
32 | endfunction | ||
33 | |||
34 | """" begin modified from indent/python.vim, upstream commit 7a9bd7c1e0ce1baf5a02daf36eeae3638aa315c7 | ||
35 | """" This copied code is licensed the same as Vim itself. | ||
36 | setlocal indentkeys+=<:>,=elif,=except | ||
37 | |||
38 | let s:keepcpo= &cpo | ||
39 | set cpo&vim | ||
40 | |||
41 | let s:maxoff = 50 " maximum number of lines to look backwards for () | ||
42 | |||
43 | function GetPythonIndent(lnum) | ||
44 | |||
45 | " If this line is explicitly joined: If the previous line was also joined, | ||
46 | " line it up with that one, otherwise add two 'shiftwidth' | ||
47 | if getline(a:lnum - 1) =~ '\\$' | ||
48 | if a:lnum > 1 && getline(a:lnum - 2) =~ '\\$' | ||
49 | return indent(a:lnum - 1) | ||
50 | endif | ||
51 | return indent(a:lnum - 1) + (exists("g:pyindent_continue") ? eval(g:pyindent_continue) : (shiftwidth() * 2)) | ||
52 | endif | ||
53 | |||
54 | " If the start of the line is in a string don't change the indent. | ||
55 | if has('syntax_items') | ||
56 | \ && synIDattr(synID(a:lnum, 1, 1), "name") =~ "String$" | ||
57 | return -1 | ||
58 | endif | ||
59 | |||
60 | " Search backwards for the previous non-empty line. | ||
61 | let plnum = prevnonblank(v:lnum - 1) | ||
62 | |||
63 | if plnum == 0 | ||
64 | " This is the first non-empty line, use zero indent. | ||
65 | return 0 | ||
66 | endif | ||
67 | |||
68 | call cursor(plnum, 1) | ||
69 | |||
70 | " Identing inside parentheses can be very slow, regardless of the searchpair() | ||
71 | " timeout, so let the user disable this feature if he doesn't need it | ||
72 | let disable_parentheses_indenting = get(g:, "pyindent_disable_parentheses_indenting", 0) | ||
73 | |||
74 | if disable_parentheses_indenting == 1 | ||
75 | let plindent = indent(plnum) | ||
76 | let plnumstart = plnum | ||
77 | else | ||
78 | " searchpair() can be slow sometimes, limit the time to 150 msec or what is | ||
79 | " put in g:pyindent_searchpair_timeout | ||
80 | let searchpair_stopline = 0 | ||
81 | let searchpair_timeout = get(g:, 'pyindent_searchpair_timeout', 150) | ||
82 | |||
83 | " If the previous line is inside parenthesis, use the indent of the starting | ||
84 | " line. | ||
85 | " Trick: use the non-existing "dummy" variable to break out of the loop when | ||
86 | " going too far back. | ||
87 | let parlnum = searchpair('(\|{\|\[', '', ')\|}\|\]', 'nbW', | ||
88 | \ "line('.') < " . (plnum - s:maxoff) . " ? dummy :" | ||
89 | \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')" | ||
90 | \ . " =~ '\\(Comment\\|Todo\\|String\\)$'", | ||
91 | \ searchpair_stopline, searchpair_timeout) | ||
92 | if parlnum > 0 | ||
93 | " We may have found the opening brace of a BitBake Python task, e.g. 'python do_task {' | ||
94 | " If so, ignore it here - it will be handled later. | ||
95 | if s:is_bb_python_func_def(parlnum) | ||
96 | let parlnum = 0 | ||
97 | let plindent = indent(plnum) | ||
98 | let plnumstart = plnum | ||
99 | else | ||
100 | let plindent = indent(parlnum) | ||
101 | let plnumstart = parlnum | ||
102 | endif | ||
103 | else | ||
104 | let plindent = indent(plnum) | ||
105 | let plnumstart = plnum | ||
106 | endif | ||
107 | |||
108 | " When inside parenthesis: If at the first line below the parenthesis add | ||
109 | " two 'shiftwidth', otherwise same as previous line. | ||
110 | " i = (a | ||
111 | " + b | ||
112 | " + c) | ||
113 | call cursor(a:lnum, 1) | ||
114 | let p = searchpair('(\|{\|\[', '', ')\|}\|\]', 'bW', | ||
115 | \ "line('.') < " . (a:lnum - s:maxoff) . " ? dummy :" | ||
116 | \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')" | ||
117 | \ . " =~ '\\(Comment\\|Todo\\|String\\)$'", | ||
118 | \ searchpair_stopline, searchpair_timeout) | ||
119 | if p > 0 | ||
120 | if s:is_bb_python_func_def(p) | ||
121 | " Handle first non-empty line inside a BB Python task | ||
122 | if p == plnum | ||
123 | return shiftwidth() | ||
124 | endif | ||
125 | |||
126 | " Handle the user actually trying to close a BitBake Python task | ||
127 | let line = getline(a:lnum) | ||
128 | if line =~ '^\s*}' | ||
129 | return -2 | ||
130 | endif | ||
131 | |||
132 | " Otherwise ignore the brace | ||
133 | let p = 0 | ||
134 | else | ||
135 | if p == plnum | ||
136 | " When the start is inside parenthesis, only indent one 'shiftwidth'. | ||
137 | let pp = searchpair('(\|{\|\[', '', ')\|}\|\]', 'bW', | ||
138 | \ "line('.') < " . (a:lnum - s:maxoff) . " ? dummy :" | ||
139 | \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')" | ||
140 | \ . " =~ '\\(Comment\\|Todo\\|String\\)$'", | ||
141 | \ searchpair_stopline, searchpair_timeout) | ||
142 | if pp > 0 | ||
143 | return indent(plnum) + (exists("g:pyindent_nested_paren") ? eval(g:pyindent_nested_paren) : shiftwidth()) | ||
144 | endif | ||
145 | return indent(plnum) + (exists("g:pyindent_open_paren") ? eval(g:pyindent_open_paren) : (shiftwidth() * 2)) | ||
146 | endif | ||
147 | if plnumstart == p | ||
148 | return indent(plnum) | ||
149 | endif | ||
150 | return plindent | ||
151 | endif | ||
152 | endif | ||
153 | |||
154 | endif | ||
155 | |||
156 | |||
157 | " Get the line and remove a trailing comment. | ||
158 | " Use syntax highlighting attributes when possible. | ||
159 | let pline = getline(plnum) | ||
160 | let pline_len = strlen(pline) | ||
161 | if has('syntax_items') | ||
162 | " If the last character in the line is a comment, do a binary search for | ||
163 | " the start of the comment. synID() is slow, a linear search would take | ||
164 | " too long on a long line. | ||
165 | if synIDattr(synID(plnum, pline_len, 1), "name") =~ "\\(Comment\\|Todo\\)$" | ||
166 | let min = 1 | ||
167 | let max = pline_len | ||
168 | while min < max | ||
169 | let col = (min + max) / 2 | ||
170 | if synIDattr(synID(plnum, col, 1), "name") =~ "\\(Comment\\|Todo\\)$" | ||
171 | let max = col | ||
172 | else | ||
173 | let min = col + 1 | ||
174 | endif | ||
175 | endwhile | ||
176 | let pline = strpart(pline, 0, min - 1) | ||
177 | endif | ||
178 | else | ||
179 | let col = 0 | ||
180 | while col < pline_len | ||
181 | if pline[col] == '#' | ||
182 | let pline = strpart(pline, 0, col) | ||
183 | break | ||
184 | endif | ||
185 | let col = col + 1 | ||
186 | endwhile | ||
187 | endif | ||
188 | |||
189 | " If the previous line ended with a colon, indent this line | ||
190 | if pline =~ ':\s*$' | ||
191 | return plindent + shiftwidth() | ||
192 | endif | ||
193 | |||
194 | " If the previous line was a stop-execution statement... | ||
195 | " TODO: utilize this logic to deindent when ending a bbPyDefRegion | ||
196 | if getline(plnum) =~ '^\s*\(break\|continue\|raise\|return\|pass\|bb\.fatal\)\>' | ||
197 | " See if the user has already dedented | ||
198 | if indent(a:lnum) > indent(plnum) - shiftwidth() | ||
199 | " If not, recommend one dedent | ||
200 | return indent(plnum) - shiftwidth() | ||
201 | endif | ||
202 | " Otherwise, trust the user | ||
203 | return -1 | ||
204 | endif | ||
205 | |||
206 | " If the current line begins with a keyword that lines up with "try" | ||
207 | if getline(a:lnum) =~ '^\s*\(except\|finally\)\>' | ||
208 | let lnum = a:lnum - 1 | ||
209 | while lnum >= 1 | ||
210 | if getline(lnum) =~ '^\s*\(try\|except\)\>' | ||
211 | let ind = indent(lnum) | ||
212 | if ind >= indent(a:lnum) | ||
213 | return -1 " indent is already less than this | ||
214 | endif | ||
215 | return ind " line up with previous try or except | ||
216 | endif | ||
217 | let lnum = lnum - 1 | ||
218 | endwhile | ||
219 | return -1 " no matching "try"! | ||
220 | endif | ||
221 | |||
222 | " If the current line begins with a header keyword, dedent | ||
223 | if getline(a:lnum) =~ '^\s*\(elif\|else\)\>' | ||
224 | |||
225 | " Unless the previous line was a one-liner | ||
226 | if getline(plnumstart) =~ '^\s*\(for\|if\|try\)\>' | ||
227 | return plindent | ||
228 | endif | ||
229 | |||
230 | " Or the user has already dedented | ||
231 | if indent(a:lnum) <= plindent - shiftwidth() | ||
232 | return -1 | ||
233 | endif | ||
234 | |||
235 | return plindent - shiftwidth() | ||
236 | endif | ||
237 | |||
238 | " When after a () construct we probably want to go back to the start line. | ||
239 | " a = (b | ||
240 | " + c) | ||
241 | " here | ||
242 | if parlnum > 0 | ||
243 | return plindent | ||
244 | endif | ||
245 | |||
246 | return -1 | ||
247 | |||
248 | endfunction | ||
249 | |||
250 | let &cpo = s:keepcpo | ||
251 | unlet s:keepcpo | ||
252 | |||
253 | """ end of stuff from indent/python.vim | ||
254 | |||
255 | |||
256 | let b:did_indent = 1 | ||
257 | setlocal indentkeys+=0\" | ||
258 | |||
259 | |||
260 | function BitbakeIndent(lnum) | ||
261 | if !has('syntax_items') | ||
262 | return -1 | ||
263 | endif | ||
264 | |||
265 | let stack = synstack(a:lnum, 1) | ||
266 | if len(stack) == 0 | ||
267 | return -1 | ||
268 | endif | ||
269 | |||
270 | let name = synIDattr(stack[0], "name") | ||
271 | |||
272 | " TODO: support different styles of indentation for assignments. For now, | ||
273 | " we only support like this: | ||
274 | " VAR = " \ | ||
275 | " value1 \ | ||
276 | " value2 \ | ||
277 | " " | ||
278 | " | ||
279 | " i.e. each value indented by shiftwidth(), with the final quote " completely unindented. | ||
280 | if name == "bbVarValue" | ||
281 | " Quote handling is tricky. kernel.bbclass has this line for instance: | ||
282 | " EXTRA_OEMAKE = " HOSTCC="${BUILD_CC} ${BUILD_CFLAGS} ${BUILD_LDFLAGS}" " HOSTCPP="${BUILD_CPP}"" | ||
283 | " Instead of trying to handle crazy cases like that, just assume that a | ||
284 | " double-quote on a line by itself (following an assignment) means the | ||
285 | " user is closing the assignment, and de-dent. | ||
286 | if getline(a:lnum) =~ '^\s*"$' | ||
287 | return 0 | ||
288 | endif | ||
289 | |||
290 | let prevstack = synstack(a:lnum - 1, 1) | ||
291 | if len(prevstack) == 0 | ||
292 | return -1 | ||
293 | endif | ||
294 | |||
295 | let prevname = synIDattr(prevstack[0], "name") | ||
296 | |||
297 | " Only indent if there was actually a continuation character on | ||
298 | " the previous line, to avoid misleading indentation. | ||
299 | let prevlinelastchar = synIDattr(synID(a:lnum - 1, col([a:lnum - 1, "$"]) - 1, 1), "name") | ||
300 | let prev_continued = prevlinelastchar == "bbContinue" | ||
301 | |||
302 | " Did the previous line introduce an assignment? | ||
303 | if index(["bbVarDef", "bbVarFlagDef"], prevname) != -1 | ||
304 | if prev_continued | ||
305 | return shiftwidth() | ||
306 | endif | ||
307 | endif | ||
308 | |||
309 | if !prev_continued | ||
310 | return 0 | ||
311 | endif | ||
312 | |||
313 | " Autoindent can take it from here | ||
314 | return -1 | ||
315 | endif | ||
316 | |||
317 | if index(["bbPyDefRegion", "bbPyFuncRegion"], name) != -1 | ||
318 | let ret = GetPythonIndent(a:lnum) | ||
319 | " Should normally always be indented by at least one shiftwidth; but allow | ||
320 | " return of -1 (defer to autoindent) or -2 (force indent to 0) | ||
321 | if ret == 0 | ||
322 | return shiftwidth() | ||
323 | elseif ret == -2 | ||
324 | return 0 | ||
325 | endif | ||
326 | return ret | ||
327 | endif | ||
328 | |||
329 | " TODO: GetShIndent doesn't detect tasks prepended with 'fakeroot' | ||
330 | " Need to submit a patch upstream to Vim to provide an extension point. | ||
331 | " Unlike the Python indenter, the Sh indenter is way too large to copy and | ||
332 | " modify here. | ||
333 | if name == "bbShFuncRegion" | ||
334 | return GetShIndent() | ||
335 | endif | ||
336 | |||
337 | " TODO: | ||
338 | " + heuristics for de-denting out of a bbPyDefRegion? e.g. when the user | ||
339 | " types an obvious BB keyword like addhandler or addtask, or starts | ||
340 | " writing a shell task. Maybe too hard to implement... | ||
341 | |||
342 | return -1 | ||
343 | endfunction | ||