blob: 716b3890b18b7a37cb1a0e9a00a79c2911aedf85 [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
dkearnsf937ab32023-08-29 05:32:27 +10005" 2023 Aug 28 by Vim Project (undo_ftplugin)
Bram Moolenaar60cce2f2015-10-13 23:21:27 +02006
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
12if exists('b:did_ftplugin')
13 finish
14endif
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.
21let s:save_expandtab = &l:expandtab
22let s:save_shiftwidth = &l:shiftwidth
23let s:save_softtabstop = &l:softtabstop
24let 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.
28let s:save_cpo = &cpo
29set cpo&vim
30
31" Load base python ftplugin (also defines b:did_ftplugin).
32source $VIMRUNTIME/ftplugin/python.vim
33
34" NOTE: Vim versions before 7.4.104 and later set this in ftplugin/python.vim.
35setlocal comments=b:#,fb:-
36
37" Restore pre-existing indentation settings.
38let &l:expandtab = s:save_expandtab
39let &l:shiftwidth = s:save_shiftwidth
40let &l:softtabstop = s:save_softtabstop
41let &l:tabstop = s:save_tabstop
42
43setlocal formatoptions-=t
44
dkearnsf937ab32023-08-29 05:32:27 +100045" Initially defined in the python ftplugin sourced above
46let b:undo_ftplugin .= " | setlocal fo<"
47
Bram Moolenaar60cce2f2015-10-13 23:21:27 +020048" Make gf work with imports in BUILD files.
49setlocal includeexpr=substitute(v:fname,'//','','')
50
51" Enable syntax-based folding, if specified.
52if get(g:, 'ft_bzl_fold', 0)
53 setlocal foldmethod=syntax
54 setlocal foldtext=BzlFoldText()
dkearnsf937ab32023-08-29 05:32:27 +100055 let b:undo_ftplugin .= " | setlocal fdm< fdt<"
Bram Moolenaar60cce2f2015-10-13 23:21:27 +020056endif
57
58if exists('*BzlFoldText')
Bram Moolenaare0e39172021-01-25 21:14:57 +010059 let &cpo = s:save_cpo
60 unlet s:save_cpo
Bram Moolenaar60cce2f2015-10-13 23:21:27 +020061 finish
62endif
63
64function 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
98endfunction
99
100let &cpo = s:save_cpo
101unlet s:save_cpo