blob: 76c12efecfe80483a39726962150abd0814b7f01 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Vim indent file
Bram Moolenaard47d5222018-12-09 20:43:55 +01002" 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"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011
12" Only load this indent file when no other was loaded.
Bram Moolenaard47d5222018-12-09 20:43:55 +010013if exists('b:did_indent')
14 finish
Bram Moolenaar071d4272004-06-13 20:20:40 +000015endif
16let b:did_indent = 1
17
Bram Moolenaard47d5222018-12-09 20:43:55 +010018let s:save_cpo = &cpoptions
19set cpoptions&vim
Bram Moolenaar582fd852005-03-28 20:58:01 +000020
Bram Moolenaard47d5222018-12-09 20:43:55 +010021
22setlocal indentexpr=GetCSIndent(v:lnum)
23
24function! s:IsCompilerDirective(line)
25 return a:line =~? '^\s*#'
26endf
27
28function! s:IsAttributeLine(line)
29 return a:line =~? '^\s*\[[A-Za-z]' && a:line =~? '\]$'
30endf
31
32function! s:FindPreviousNonCompilerDirectiveLine(start_lnum)
33 for delta in range(0, a:start_lnum)
34 let lnum = a:start_lnum - delta
35 let line = getline(lnum)
36 let is_directive = s:IsCompilerDirective(line)
37 if !is_directive
38 return lnum
39 endif
40 endfor
41 return 0
42endf
43
44function! GetCSIndent(lnum) abort
45 " Hit the start of the file, use zero indent.
46 if a:lnum == 0
47 return 0
48 endif
49
50 let this_line = getline(a:lnum)
51
52 " Compiler directives use zero indent if so configured.
53 let is_first_col_macro = s:IsCompilerDirective(this_line) && stridx(&l:cinkeys, '0#') >= 0
54 if is_first_col_macro
55 return cindent(a:lnum)
56 endif
57
58 let lnum = s:FindPreviousNonCompilerDirectiveLine(a:lnum - 1)
59 let previous_code_line = getline(lnum)
60 if s:IsAttributeLine(previous_code_line)
61 let ind = indent(lnum)
62 return ind
63 else
64 return cindent(a:lnum)
65 endif
66endfunction
67
68let b:undo_indent = 'setlocal indentexpr<'
69
70let &cpoptions = s:save_cpo
71unlet s:save_cpo
72
73" vim:et:sw=2:sts=2