dts.vim 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. " Vim indent file
  2. " Language: Device Tree
  3. " Maintainer: Roland Hieber, Pengutronix <rhi@pengutronix.de>
  4. "
  5. if exists("b:did_indent")
  6. finish
  7. endif
  8. let b:did_indent = 1
  9. setlocal autoindent
  10. setlocal nosmartindent
  11. setlocal indentkeys=o,O,0},0<>>,!<Ctrl-F>
  12. setlocal indentexpr=GetDTSIndent()
  13. setlocal nolisp
  14. let b:undo_indent = 'setl autoindent< smartindent< indentkeys< indentexpr< lisp<'
  15. function GetDTSIndent()
  16. let sw = shiftwidth()
  17. let lnum = v:lnum
  18. let line = getline(lnum)
  19. let prevline = getline(prevnonblank(lnum-1))
  20. let prevind = indent(prevnonblank(lnum-1))
  21. if prevnonblank(lnum-1) < 1
  22. return 0
  23. endif
  24. " Don't indent header and preprocessor directives
  25. if line =~ '^\s*\(/dts-\|#\(include\|define\|undef\|warn\(ing\)\?\|error\|if\(n\?def\)\?\|else\|elif\|endif\)\)'
  26. return 0
  27. " Don't indent /node and &label blocks
  28. elseif line =~ '^\s*[/&].\+{\s*$'
  29. return 0
  30. " Indent to matching bracket or remove one shiftwidth if line begins with } or >
  31. elseif line =~ '^\s*[}>]'
  32. " set cursor to closing bracket on current line
  33. let col = matchend(line, '^\s*[>}]')
  34. call cursor(lnum, col)
  35. " determine bracket type, {} or <>
  36. let pair = strpart('{}<>', stridx('}>', line[col-1]) * 2, 2)
  37. " find matching bracket pair
  38. let pairline = searchpair(pair[0], '', pair[1], 'bW')
  39. if pairline > 0
  40. return indent(pairline)
  41. else
  42. return prevind - sw
  43. endif
  44. " else, add one level of indent if line ends in { or < or = or ,
  45. elseif prevline =~ '[{<=,]$'
  46. return prevind + sw
  47. else
  48. return prevind
  49. endif
  50. endfunction