bzl.vim 3.2 KB

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