bitbake.vim 10 KB

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