bzl.vim (2925B)
1 " Vim indent file 2 " Language: Bazel (http://bazel.io) 3 " Maintainer: David Barnett (https://github.com/google/vim-ft-bzl) 4 " Last Change: 2021 Jul 08 5 6 if exists('b:did_indent') 7 finish 8 endif 9 10 " Load base python indent. 11 if !exists('*GetPythonIndent') 12 runtime! indent/python.vim 13 endif 14 15 let b:did_indent = 1 16 17 " Only enable bzl google indent if python google indent is enabled. 18 if !get(g:, 'no_google_python_indent') 19 setlocal indentexpr=GetBzlIndent(v:lnum) 20 endif 21 22 if exists('*GetBzlIndent') 23 finish 24 endif 25 26 let s:save_cpo = &cpo 27 set cpo-=C 28 29 " Maximum number of lines to look backwards. 30 let s:maxoff = 50 31 32 "" 33 " Determine the correct indent level given an {lnum} in the current buffer. 34 function 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 let g:pyindent_nested_paren = 'shiftwidth()' 45 let g:pyindent_open_paren = 'shiftwidth()' 46 endif 47 48 let l:indent = -1 49 50 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 56 " 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 79 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 102 endfunction 103 104 let &cpo = s:save_cpo 105 unlet s:save_cpo