cs.vim 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. " Vim indent file
  2. " Language: C#
  3. " Maintainer: Nick Jensen <nickspoon@gmail.com>
  4. " Former Maintainers: Aquila Deus
  5. " Johannes Zellner <johannes@zellner.org>
  6. " Last Change: 2018-11-21
  7. " Filenames: *.cs
  8. " License: Vim (see :h license)
  9. " Repository: https://github.com/nickspoons/vim-cs
  10. "
  11. " Only load this indent file when no other was loaded.
  12. if exists('b:did_indent')
  13. finish
  14. endif
  15. let b:did_indent = 1
  16. let s:save_cpo = &cpoptions
  17. set cpoptions&vim
  18. setlocal indentexpr=GetCSIndent(v:lnum)
  19. function! s:IsCompilerDirective(line)
  20. return a:line =~? '^\s*#'
  21. endf
  22. function! s:IsAttributeLine(line)
  23. return a:line =~? '^\s*\[[A-Za-z]' && a:line =~? '\]$'
  24. endf
  25. function! s:FindPreviousNonCompilerDirectiveLine(start_lnum)
  26. for delta in range(0, a:start_lnum)
  27. let lnum = a:start_lnum - delta
  28. let line = getline(lnum)
  29. let is_directive = s:IsCompilerDirective(line)
  30. if !is_directive
  31. return lnum
  32. endif
  33. endfor
  34. return 0
  35. endf
  36. function! GetCSIndent(lnum) abort
  37. " Hit the start of the file, use zero indent.
  38. if a:lnum == 0
  39. return 0
  40. endif
  41. let this_line = getline(a:lnum)
  42. " Compiler directives use zero indent if so configured.
  43. let is_first_col_macro = s:IsCompilerDirective(this_line) && stridx(&l:cinkeys, '0#') >= 0
  44. if is_first_col_macro
  45. return cindent(a:lnum)
  46. endif
  47. let lnum = s:FindPreviousNonCompilerDirectiveLine(a:lnum - 1)
  48. let previous_code_line = getline(lnum)
  49. if s:IsAttributeLine(previous_code_line)
  50. let ind = indent(lnum)
  51. return ind
  52. else
  53. return cindent(a:lnum)
  54. endif
  55. endfunction
  56. let b:undo_indent = 'setlocal indentexpr<'
  57. let &cpoptions = s:save_cpo
  58. unlet s:save_cpo
  59. " vim:et:sw=2:sts=2