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