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