summaryrefslogtreecommitdiffstats
path: root/bitbake/bitbake/contrib/vim/indent/bitbake.vim
diff options
context:
space:
mode:
Diffstat (limited to 'bitbake/bitbake/contrib/vim/indent/bitbake.vim')
-rw-r--r--bitbake/bitbake/contrib/vim/indent/bitbake.vim269
1 files changed, 269 insertions, 0 deletions
diff --git a/bitbake/bitbake/contrib/vim/indent/bitbake.vim b/bitbake/bitbake/contrib/vim/indent/bitbake.vim
new file mode 100644
index 0000000000..1c35cb3c32
--- /dev/null
+++ b/bitbake/bitbake/contrib/vim/indent/bitbake.vim
@@ -0,0 +1,269 @@
1if exists("b:did_indent")
2 finish
3endif
4
5runtime! indent/sh.vim
6unlet b:did_indent
7
8setlocal indentexpr=BitbakeIndent(v:lnum)
9setlocal autoindent nolisp
10
11function s:is_python_func_def(lnum)
12 let stack = synstack(a:lnum, 1)
13 if len(stack) == 0
14 return 0
15 endif
16
17 let top = synIDattr(stack[0], "name")
18 echo top
19
20 return synIDattr(stack[0], "name") == "bbPyFuncDef"
21endfunction
22
23"""" begin modified from indent/python.vim, upstream commit 7a9bd7c1e0ce1baf5a02daf36eeae3638aa315c7
24"""" This copied code is licensed the same as Vim itself.
25setlocal indentkeys+=<:>,=elif,=except
26
27let s:keepcpo= &cpo
28set cpo&vim
29
30let s:maxoff = 50 " maximum number of lines to look backwards for ()
31
32function GetPythonIndent(lnum)
33
34 " If this line is explicitly joined: If the previous line was also joined,
35 " line it up with that one, otherwise add two 'shiftwidth'
36 if getline(a:lnum - 1) =~ '\\$'
37 if a:lnum > 1 && getline(a:lnum - 2) =~ '\\$'
38 return indent(a:lnum - 1)
39 endif
40 return indent(a:lnum - 1) + (exists("g:pyindent_continue") ? eval(g:pyindent_continue) : (shiftwidth() * 2))
41 endif
42
43 " If the start of the line is in a string don't change the indent.
44 if has('syntax_items')
45 \ && synIDattr(synID(a:lnum, 1, 1), "name") =~ "String$"
46 return -1
47 endif
48
49 " Search backwards for the previous non-empty line.
50 let plnum = prevnonblank(v:lnum - 1)
51
52 if plnum == 0
53 " This is the first non-empty line, use zero indent.
54 return 0
55 endif
56
57 call cursor(plnum, 1)
58
59 " Identing inside parentheses can be very slow, regardless of the searchpair()
60 " timeout, so let the user disable this feature if he doesn't need it
61 let disable_parentheses_indenting = get(g:, "pyindent_disable_parentheses_indenting", 0)
62
63 if disable_parentheses_indenting == 1
64 let plindent = indent(plnum)
65 let plnumstart = plnum
66 else
67 " searchpair() can be slow sometimes, limit the time to 150 msec or what is
68 " put in g:pyindent_searchpair_timeout
69 let searchpair_stopline = 0
70 let searchpair_timeout = get(g:, 'pyindent_searchpair_timeout', 150)
71
72 " If the previous line is inside parenthesis, use the indent of the starting
73 " line.
74 " Trick: use the non-existing "dummy" variable to break out of the loop when
75 " going too far back.
76 let parlnum = searchpair('(\|{\|\[', '', ')\|}\|\]', 'nbW',
77 \ "line('.') < " . (plnum - s:maxoff) . " ? dummy :"
78 \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')"
79 \ . " =~ '\\(Comment\\|Todo\\|String\\)$'",
80 \ searchpair_stopline, searchpair_timeout)
81 if parlnum > 0
82 " We may have found the opening brace of a BitBake Python task, e.g. 'python do_task {'
83 " If so, ignore it here - it will be handled later.
84 if s:is_python_func_def(parlnum)
85 let parlnum = 0
86 let plindent = indent(plnum)
87 let plnumstart = plnum
88 else
89 let plindent = indent(parlnum)
90 let plnumstart = parlnum
91 endif
92 else
93 let plindent = indent(plnum)
94 let plnumstart = plnum
95 endif
96
97 " When inside parenthesis: If at the first line below the parenthesis add
98 " two 'shiftwidth', otherwise same as previous line.
99 " i = (a
100 " + b
101 " + c)
102 call cursor(a:lnum, 1)
103 let p = searchpair('(\|{\|\[', '', ')\|}\|\]', 'bW',
104 \ "line('.') < " . (a:lnum - s:maxoff) . " ? dummy :"
105 \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')"
106 \ . " =~ '\\(Comment\\|Todo\\|String\\)$'",
107 \ searchpair_stopline, searchpair_timeout)
108 if p > 0
109 if s:is_python_func_def(p)
110 " Handle the user actually trying to close a BitBake Python task
111 let line = getline(a:lnum)
112 if line =~ '^\s*}'
113 return -2
114 endif
115
116 " Otherwise ignore the brace
117 let p = 0
118 else
119 if p == plnum
120 " When the start is inside parenthesis, only indent one 'shiftwidth'.
121 let pp = searchpair('(\|{\|\[', '', ')\|}\|\]', 'bW',
122 \ "line('.') < " . (a:lnum - s:maxoff) . " ? dummy :"
123 \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')"
124 \ . " =~ '\\(Comment\\|Todo\\|String\\)$'",
125 \ searchpair_stopline, searchpair_timeout)
126 if pp > 0
127 return indent(plnum) + (exists("g:pyindent_nested_paren") ? eval(g:pyindent_nested_paren) : shiftwidth())
128 endif
129 return indent(plnum) + (exists("g:pyindent_open_paren") ? eval(g:pyindent_open_paren) : (shiftwidth() * 2))
130 endif
131 if plnumstart == p
132 return indent(plnum)
133 endif
134 return plindent
135 endif
136 endif
137
138 endif
139
140
141 " Get the line and remove a trailing comment.
142 " Use syntax highlighting attributes when possible.
143 let pline = getline(plnum)
144 let pline_len = strlen(pline)
145 if has('syntax_items')
146 " If the last character in the line is a comment, do a binary search for
147 " the start of the comment. synID() is slow, a linear search would take
148 " too long on a long line.
149 if synIDattr(synID(plnum, pline_len, 1), "name") =~ "\\(Comment\\|Todo\\)$"
150 let min = 1
151 let max = pline_len
152 while min < max
153 let col = (min + max) / 2
154 if synIDattr(synID(plnum, col, 1), "name") =~ "\\(Comment\\|Todo\\)$"
155 let max = col
156 else
157 let min = col + 1
158 endif
159 endwhile
160 let pline = strpart(pline, 0, min - 1)
161 endif
162 else
163 let col = 0
164 while col < pline_len
165 if pline[col] == '#'
166 let pline = strpart(pline, 0, col)
167 break
168 endif
169 let col = col + 1
170 endwhile
171 endif
172
173 " If the previous line ended with a colon, indent this line
174 if pline =~ ':\s*$'
175 return plindent + shiftwidth()
176 endif
177
178 " If the previous line was a stop-execution statement...
179 if getline(plnum) =~ '^\s*\(break\|continue\|raise\|return\|pass\)\>'
180 " See if the user has already dedented
181 if indent(a:lnum) > indent(plnum) - shiftwidth()
182 " If not, recommend one dedent
183 return indent(plnum) - shiftwidth()
184 endif
185 " Otherwise, trust the user
186 return -1
187 endif
188
189 " If the current line begins with a keyword that lines up with "try"
190 if getline(a:lnum) =~ '^\s*\(except\|finally\)\>'
191 let lnum = a:lnum - 1
192 while lnum >= 1
193 if getline(lnum) =~ '^\s*\(try\|except\)\>'
194 let ind = indent(lnum)
195 if ind >= indent(a:lnum)
196 return -1 " indent is already less than this
197 endif
198 return ind " line up with previous try or except
199 endif
200 let lnum = lnum - 1
201 endwhile
202 return -1 " no matching "try"!
203 endif
204
205 " If the current line begins with a header keyword, dedent
206 if getline(a:lnum) =~ '^\s*\(elif\|else\)\>'
207
208 " Unless the previous line was a one-liner
209 if getline(plnumstart) =~ '^\s*\(for\|if\|try\)\>'
210 return plindent
211 endif
212
213 " Or the user has already dedented
214 if indent(a:lnum) <= plindent - shiftwidth()
215 return -1
216 endif
217
218 return plindent - shiftwidth()
219 endif
220
221 " When after a () construct we probably want to go back to the start line.
222 " a = (b
223 " + c)
224 " here
225 if parlnum > 0
226 return plindent
227 endif
228
229 return -1
230
231endfunction
232
233let &cpo = s:keepcpo
234unlet s:keepcpo
235
236""" end of stuff from indent/python.vim
237
238
239let b:did_indent = 1
240
241
242function BitbakeIndent(lnum)
243 if !has('syntax_items')
244 return -1
245 endif
246
247 let stack = synstack(a:lnum, col("."))
248 if len(stack) == 0
249 return -1
250 endif
251
252 let name = synIDattr(stack[0], "name")
253 "echo name
254
255 if index(["bbPyDefRegion", "bbPyFuncRegion"], name) != -1
256 let ret = GetPythonIndent(a:lnum)
257 " Should always be indented by at least one shiftwidth; but allow
258 " return of -1 (defer to autoindent) or -2 (force indent to 0)
259 if ret == 0
260 return shiftwidth()
261 elseif ret == -2
262 return 0
263 endif
264 return ret
265 endif
266
267 return -1
268 "return s:pythonIndentExpr()
269endfunction