blob: 24e5b870cdc2ccb0b3537cc91e207a296b5f4231 [file] [log] [blame]
Bram Moolenaar60cce2f2015-10-13 23:21:27 +02001" Vim indent file
2" Language: Bazel (http://bazel.io)
3" Maintainer: David Barnett (https://github.com/google/vim-ft-bzl)
4" Last Change: 2015 Aug 11
5
6if exists('b:did_indent')
7 finish
8endif
9
10" Load base python indent.
11if !exists('*GetPythonIndent')
12 runtime! indent/python.vim
13endif
14
15let b:did_indent = 1
16
17" Only enable bzl google indent if python google indent is enabled.
18if !get(g:, 'no_google_python_indent')
19 setlocal indentexpr=GetBzlIndent(v:lnum)
20endif
21
22if exists('*GetBzlIndent')
23 finish
24endif
25
26let s:save_cpo = &cpo
27set cpo-=C
28
29" Maximum number of lines to look backwards.
30let s:maxoff = 50
31
32""
33" Determine the correct indent level given an {lnum} in the current buffer.
34function GetBzlIndent(lnum) abort
35 let l:use_recursive_indent = !get(g:, 'no_google_python_recursive_indent')
36 if l:use_recursive_indent
37 " Backup and override indent setting variables.
38 if exists('g:pyindent_nested_paren')
39 let l:pyindent_nested_paren = g:pyindent_nested_paren
40 endif
41 if exists('g:pyindent_open_paren')
42 let l:pyindent_open_paren = g:pyindent_open_paren
43 endif
44 " Vim 7.3.693 and later defines a shiftwidth() function to get the effective
45 " shiftwidth value. Fall back to &shiftwidth if the function doesn't exist.
46 let l:sw_expr = exists('*shiftwidth') ? 'shiftwidth()' : '&shiftwidth'
47 let g:pyindent_nested_paren = l:sw_expr . ' * 2'
48 let g:pyindent_open_paren = l:sw_expr . ' * 2'
49 endif
50
51 let l:indent = -1
52
53 " Indent inside parens.
54 " Align with the open paren unless it is at the end of the line.
55 " E.g.
56 " open_paren_not_at_EOL(100,
57 " (200,
58 " 300),
59 " 400)
60 " open_paren_at_EOL(
61 " 100, 200, 300, 400)
62 call cursor(a:lnum, 1)
63 let [l:par_line, l:par_col] = searchpairpos('(\|{\|\[', '', ')\|}\|\]', 'bW',
64 \ "line('.') < " . (a:lnum - s:maxoff) . " ? dummy :" .
65 \ " synIDattr(synID(line('.'), col('.'), 1), 'name')" .
66 \ " =~ '\\(Comment\\|String\\)$'")
67 if l:par_line > 0
68 call cursor(l:par_line, 1)
69 if l:par_col != col('$') - 1
70 let l:indent = l:par_col
71 endif
72 endif
73
74 " Delegate the rest to the original function.
75 if l:indent == -1
76 let l:indent = GetPythonIndent(a:lnum)
77 endif
78
79 if l:use_recursive_indent
80 " Restore global variables.
81 if exists('l:pyindent_nested_paren')
82 let g:pyindent_nested_paren = l:pyindent_nested_paren
83 else
84 unlet g:pyindent_nested_paren
85 endif
86 if exists('l:pyindent_open_paren')
87 let g:pyindent_open_paren = l:pyindent_open_paren
88 else
89 unlet g:pyindent_open_paren
90 endif
91 endif
92
93 return l:indent
94endfunction
95
96let &cpo = s:save_cpo
97unlet s:save_cpo