blob: 6904bfdedb34d3490f8b41f82e00fa24f3a13ba9 [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 Moolenaar3ec574f2017-06-13 18:12:01 +02004" Last Change: 2017 Jun 13
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 Moolenaar3ec574f2017-06-13 18:12:01 +020044 let g:pyindent_nested_paren = 'shiftwidth() * 2'
45 let g:pyindent_open_paren = 'shiftwidth() * 2'
Bram Moolenaar60cce2f2015-10-13 23:21:27 +020046 endif
47
48 let l:indent = -1
49
50 " Indent inside parens.
51 " Align with the open paren unless it is at the end of the line.
52 " E.g.
53 " open_paren_not_at_EOL(100,
54 " (200,
55 " 300),
56 " 400)
57 " open_paren_at_EOL(
58 " 100, 200, 300, 400)
59 call cursor(a:lnum, 1)
60 let [l:par_line, l:par_col] = searchpairpos('(\|{\|\[', '', ')\|}\|\]', 'bW',
61 \ "line('.') < " . (a:lnum - s:maxoff) . " ? dummy :" .
62 \ " synIDattr(synID(line('.'), col('.'), 1), 'name')" .
63 \ " =~ '\\(Comment\\|String\\)$'")
64 if l:par_line > 0
65 call cursor(l:par_line, 1)
66 if l:par_col != col('$') - 1
67 let l:indent = l:par_col
68 endif
69 endif
70
71 " Delegate the rest to the original function.
72 if l:indent == -1
73 let l:indent = GetPythonIndent(a:lnum)
74 endif
75
76 if l:use_recursive_indent
77 " Restore global variables.
78 if exists('l:pyindent_nested_paren')
79 let g:pyindent_nested_paren = l:pyindent_nested_paren
80 else
81 unlet g:pyindent_nested_paren
82 endif
83 if exists('l:pyindent_open_paren')
84 let g:pyindent_open_paren = l:pyindent_open_paren
85 else
86 unlet g:pyindent_open_paren
87 endif
88 endif
89
90 return l:indent
91endfunction
92
93let &cpo = s:save_cpo
94unlet s:save_cpo