cs.vim 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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: 2020-03-26
  7. " License: Vim (see :h license)
  8. " Repository: https://github.com/nickspoons/vim-cs
  9. if exists('b:did_indent')
  10. finish
  11. endif
  12. let b:did_indent = 1
  13. let s:save_cpo = &cpoptions
  14. set cpoptions&vim
  15. setlocal indentexpr=GetCSIndent(v:lnum)
  16. function! s:IsCompilerDirective(line)
  17. " Exclude #region and #endregion - these should be indented normally
  18. return a:line =~# '^\s*#' && !s:IsRegionDirective(a:line)
  19. endf
  20. function! s:IsRegionDirective(line)
  21. return a:line =~# '^\s*#\s*region' || a:line =~# '^\s*#\s*endregion'
  22. endf
  23. function! s:IsAttributeLine(line)
  24. return a:line =~# '^\s*\[[A-Za-z]' && a:line =~# '\]$'
  25. endf
  26. function! s:FindPreviousNonCompilerDirectiveLine(start_lnum)
  27. for delta in range(0, a:start_lnum)
  28. let lnum = a:start_lnum - delta
  29. let line = getline(lnum)
  30. if !s:IsCompilerDirective(line) && !s:IsRegionDirective(line)
  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. return indent(lnum)
  51. elseif s:IsRegionDirective(this_line)
  52. return cindent(lnum)
  53. else
  54. return cindent(a:lnum)
  55. endif
  56. endfunction
  57. let b:undo_indent = 'setlocal indentexpr<'
  58. let &cpoptions = s:save_cpo
  59. unlet s:save_cpo
  60. " vim:et:sw=2:sts=2