bzl.vim 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. " Vim filetype plugin file
  2. " Language: Bazel (http://bazel.io)
  3. " Maintainer: David Barnett (https://github.com/google/vim-ft-bzl)
  4. " Last Change: 2021 Jan 19
  5. ""
  6. " @section Introduction, intro
  7. " Core settings for the bzl filetype, used for BUILD and *.bzl files for the
  8. " Bazel build system (http://bazel.io/).
  9. if exists('b:did_ftplugin')
  10. finish
  11. endif
  12. " Vim 7.4.051 has opinionated settings in ftplugin/python.vim that try to force
  13. " PEP8 conventions on every python file, but these conflict with Google's
  14. " indentation guidelines. As a workaround, we explicitly source the system
  15. " ftplugin, but save indentation settings beforehand and restore them after.
  16. let s:save_expandtab = &l:expandtab
  17. let s:save_shiftwidth = &l:shiftwidth
  18. let s:save_softtabstop = &l:softtabstop
  19. let s:save_tabstop = &l:tabstop
  20. " NOTE: Vim versions before 7.3.511 had a ftplugin/python.vim that was broken
  21. " for compatible mode.
  22. let s:save_cpo = &cpo
  23. set cpo&vim
  24. " Load base python ftplugin (also defines b:did_ftplugin).
  25. source $VIMRUNTIME/ftplugin/python.vim
  26. " NOTE: Vim versions before 7.4.104 and later set this in ftplugin/python.vim.
  27. setlocal comments=b:#,fb:-
  28. " Restore pre-existing indentation settings.
  29. let &l:expandtab = s:save_expandtab
  30. let &l:shiftwidth = s:save_shiftwidth
  31. let &l:softtabstop = s:save_softtabstop
  32. let &l:tabstop = s:save_tabstop
  33. setlocal formatoptions-=t
  34. " Make gf work with imports in BUILD files.
  35. setlocal includeexpr=substitute(v:fname,'//','','')
  36. " Enable syntax-based folding, if specified.
  37. if get(g:, 'ft_bzl_fold', 0)
  38. setlocal foldmethod=syntax
  39. setlocal foldtext=BzlFoldText()
  40. endif
  41. if exists('*BzlFoldText')
  42. let &cpo = s:save_cpo
  43. unlet s:save_cpo
  44. finish
  45. endif
  46. function BzlFoldText() abort
  47. let l:start_num = nextnonblank(v:foldstart)
  48. let l:end_num = prevnonblank(v:foldend)
  49. if l:end_num <= l:start_num + 1
  50. " If the fold is empty, don't print anything for the contents.
  51. let l:content = ''
  52. else
  53. " Otherwise look for something matching the content regex.
  54. " And if nothing matches, print an ellipsis.
  55. let l:content = '...'
  56. for l:line in getline(l:start_num + 1, l:end_num - 1)
  57. let l:content_match = matchstr(l:line, '\m\C^\s*name = \zs.*\ze,$')
  58. if !empty(l:content_match)
  59. let l:content = l:content_match
  60. break
  61. endif
  62. endfor
  63. endif
  64. " Enclose content with start and end
  65. let l:start_text = getline(l:start_num)
  66. let l:end_text = substitute(getline(l:end_num), '^\s*', '', '')
  67. let l:text = l:start_text . ' ' . l:content . ' ' . l:end_text
  68. " Compute the available width for the displayed text.
  69. let l:width = winwidth(0) - &foldcolumn - (&number ? &numberwidth : 0)
  70. let l:lines_folded = ' ' . string(1 + v:foldend - v:foldstart) . ' lines'
  71. " Expand tabs, truncate, pad, and concatenate
  72. let l:text = substitute(l:text, '\t', repeat(' ', &tabstop), 'g')
  73. let l:text = strpart(l:text, 0, l:width - len(l:lines_folded))
  74. let l:padding = repeat(' ', l:width - len(l:lines_folded) - len(l:text))
  75. return l:text . l:padding . l:lines_folded
  76. endfunction
  77. let &cpo = s:save_cpo
  78. unlet s:save_cpo