cs.vim (1928B)
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 10 if exists('b:did_indent') 11 finish 12 endif 13 let b:did_indent = 1 14 15 let s:save_cpo = &cpoptions 16 set cpoptions&vim 17 18 19 setlocal indentexpr=GetCSIndent(v:lnum) 20 21 function! s:IsCompilerDirective(line) 22 " Exclude #region and #endregion - these should be indented normally 23 return a:line =~# '^\s*#' && !s:IsRegionDirective(a:line) 24 endf 25 26 function! s:IsRegionDirective(line) 27 return a:line =~# '^\s*#\s*region' || a:line =~# '^\s*#\s*endregion' 28 endf 29 30 function! s:IsAttributeLine(line) 31 return a:line =~# '^\s*\[[A-Za-z]' && a:line =~# '\]$' 32 endf 33 34 function! s:FindPreviousNonCompilerDirectiveLine(start_lnum) 35 for delta in range(0, a:start_lnum) 36 let lnum = a:start_lnum - delta 37 let line = getline(lnum) 38 if !s:IsCompilerDirective(line) && !s:IsRegionDirective(line) 39 return lnum 40 endif 41 endfor 42 return 0 43 endf 44 45 function! GetCSIndent(lnum) abort 46 " Hit the start of the file, use zero indent. 47 if a:lnum == 0 48 return 0 49 endif 50 51 let this_line = getline(a:lnum) 52 53 " Compiler directives use zero indent if so configured. 54 let is_first_col_macro = s:IsCompilerDirective(this_line) && stridx(&l:cinkeys, '0#') >= 0 55 if is_first_col_macro 56 return cindent(a:lnum) 57 endif 58 59 let lnum = s:FindPreviousNonCompilerDirectiveLine(a:lnum - 1) 60 let previous_code_line = getline(lnum) 61 if s:IsAttributeLine(previous_code_line) 62 return indent(lnum) 63 elseif s:IsRegionDirective(this_line) 64 return cindent(lnum) 65 else 66 return cindent(a:lnum) 67 endif 68 endfunction 69 70 let b:undo_indent = 'setlocal indentexpr<' 71 72 let &cpoptions = s:save_cpo 73 unlet s:save_cpo 74 75 " vim:et:sw=2:sts=2