blob: cf4cfb5fad106d22d958f7c39ff54a062d9ee4c2 [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)
Bram Moolenaar53f7fcc2021-07-28 20:10:16 +02004" Last Change: 2021 Jul 08
Bram Moolenaar60cce2f2015-10-13 23:21:27 +02005
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
Bram Moolenaar53f7fcc2021-07-28 20:10:16 +020044 let g:pyindent_nested_paren = 'shiftwidth()'
45 let g:pyindent_open_paren = 'shiftwidth()'
Bram Moolenaar60cce2f2015-10-13 23:21:27 +020046 endif
47
48 let l:indent = -1
49
Bram Moolenaar60cce2f2015-10-13 23:21:27 +020050 call cursor(a:lnum, 1)
51 let [l:par_line, l:par_col] = searchpairpos('(\|{\|\[', '', ')\|}\|\]', 'bW',
52 \ "line('.') < " . (a:lnum - s:maxoff) . " ? dummy :" .
53 \ " synIDattr(synID(line('.'), col('.'), 1), 'name')" .
54 \ " =~ '\\(Comment\\|String\\)$'")
55 if l:par_line > 0
Bram Moolenaar53f7fcc2021-07-28 20:10:16 +020056 " Indent inside parens.
57 if searchpair('(\|{\|\[', '', ')\|}\|\]', 'W',
58 \ "line('.') < " . (a:lnum - s:maxoff) . " ? dummy :" .
59 \ " synIDattr(synID(line('.'), col('.'), 1), 'name')" .
60 \ " =~ '\\(Comment\\|String\\)$'") && line('.') == a:lnum
61 " If cursor is at close parens, match indent with open parens.
62 " E.g.
63 " foo(
64 " )
65 let l:indent = indent(l:par_line)
66 else
67 " Align with the open paren unless it is at the end of the line.
68 " E.g.
69 " open_paren_not_at_EOL(100,
70 " (200,
71 " 300),
72 " 400)
73 " open_paren_at_EOL(
74 " 100, 200, 300, 400)
75 call cursor(l:par_line, 1)
76 if l:par_col != col('$') - 1
77 let l:indent = l:par_col
78 endif
Bram Moolenaar60cce2f2015-10-13 23:21:27 +020079 endif
80 endif
81
82 " Delegate the rest to the original function.
83 if l:indent == -1
84 let l:indent = GetPythonIndent(a:lnum)
85 endif
86
87 if l:use_recursive_indent
88 " Restore global variables.
89 if exists('l:pyindent_nested_paren')
90 let g:pyindent_nested_paren = l:pyindent_nested_paren
91 else
92 unlet g:pyindent_nested_paren
93 endif
94 if exists('l:pyindent_open_paren')
95 let g:pyindent_open_paren = l:pyindent_open_paren
96 else
97 unlet g:pyindent_open_paren
98 endif
99 endif
100
101 return l:indent
102endfunction
103
104let &cpo = s:save_cpo
105unlet s:save_cpo