bzl.vim 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. " Vim indent file
  2. " Language: Bazel (http://bazel.io)
  3. " Maintainer: David Barnett (https://github.com/google/vim-ft-bzl)
  4. " Last Change: 2021 Jul 08
  5. if exists('b:did_indent')
  6. finish
  7. endif
  8. " Load base python indent.
  9. if !exists('*GetPythonIndent')
  10. runtime! indent/python.vim
  11. endif
  12. let b:did_indent = 1
  13. " Only enable bzl google indent if python google indent is enabled.
  14. if !get(g:, 'no_google_python_indent')
  15. setlocal indentexpr=GetBzlIndent(v:lnum)
  16. endif
  17. if exists('*GetBzlIndent')
  18. finish
  19. endif
  20. let s:save_cpo = &cpo
  21. set cpo-=C
  22. " Maximum number of lines to look backwards.
  23. let s:maxoff = 50
  24. ""
  25. " Determine the correct indent level given an {lnum} in the current buffer.
  26. function GetBzlIndent(lnum) abort
  27. let l:use_recursive_indent = !get(g:, 'no_google_python_recursive_indent')
  28. if l:use_recursive_indent
  29. " Backup and override indent setting variables.
  30. if exists('g:pyindent_nested_paren')
  31. let l:pyindent_nested_paren = g:pyindent_nested_paren
  32. endif
  33. if exists('g:pyindent_open_paren')
  34. let l:pyindent_open_paren = g:pyindent_open_paren
  35. endif
  36. let g:pyindent_nested_paren = 'shiftwidth()'
  37. let g:pyindent_open_paren = 'shiftwidth()'
  38. endif
  39. let l:indent = -1
  40. call cursor(a:lnum, 1)
  41. let [l:par_line, l:par_col] = searchpairpos('(\|{\|\[', '', ')\|}\|\]', 'bW',
  42. \ "line('.') < " . (a:lnum - s:maxoff) . " ? dummy :" .
  43. \ " synIDattr(synID(line('.'), col('.'), 1), 'name')" .
  44. \ " =~ '\\(Comment\\|String\\)$'")
  45. if l:par_line > 0
  46. " Indent inside parens.
  47. if searchpair('(\|{\|\[', '', ')\|}\|\]', 'W',
  48. \ "line('.') < " . (a:lnum - s:maxoff) . " ? dummy :" .
  49. \ " synIDattr(synID(line('.'), col('.'), 1), 'name')" .
  50. \ " =~ '\\(Comment\\|String\\)$'") && line('.') == a:lnum
  51. " If cursor is at close parens, match indent with open parens.
  52. " E.g.
  53. " foo(
  54. " )
  55. let l:indent = indent(l:par_line)
  56. else
  57. " Align with the open paren unless it is at the end of the line.
  58. " E.g.
  59. " open_paren_not_at_EOL(100,
  60. " (200,
  61. " 300),
  62. " 400)
  63. " open_paren_at_EOL(
  64. " 100, 200, 300, 400)
  65. call cursor(l:par_line, 1)
  66. if l:par_col != col('$') - 1
  67. let l:indent = l:par_col
  68. endif
  69. endif
  70. endif
  71. " Delegate the rest to the original function.
  72. if l:indent == -1
  73. let l:indent = GetPythonIndent(a:lnum)
  74. endif
  75. if l:use_recursive_indent
  76. " Restore global variables.
  77. if exists('l:pyindent_nested_paren')
  78. let g:pyindent_nested_paren = l:pyindent_nested_paren
  79. else
  80. unlet g:pyindent_nested_paren
  81. endif
  82. if exists('l:pyindent_open_paren')
  83. let g:pyindent_open_paren = l:pyindent_open_paren
  84. else
  85. unlet g:pyindent_open_paren
  86. endif
  87. endif
  88. return l:indent
  89. endfunction
  90. let &cpo = s:save_cpo
  91. unlet s:save_cpo