bzl.vim 2.4 KB

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