blob: acc3ba75d5d28a074b958eee10202b13ca1454cd [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>
Bram Moolenaar63f32602022-06-09 20:45:54 +01006" Last Change: 2020-03-26
Bram Moolenaard47d5222018-12-09 20:43:55 +01007" License: Vim (see :h license)
8" Repository: https://github.com/nickspoons/vim-cs
Bram Moolenaar071d4272004-06-13 20:20:40 +00009
Bram Moolenaard47d5222018-12-09 20:43:55 +010010if exists('b:did_indent')
11 finish
Bram Moolenaar071d4272004-06-13 20:20:40 +000012endif
13let b:did_indent = 1
14
Bram Moolenaard47d5222018-12-09 20:43:55 +010015let s:save_cpo = &cpoptions
16set cpoptions&vim
Bram Moolenaar582fd852005-03-28 20:58:01 +000017
Bram Moolenaard47d5222018-12-09 20:43:55 +010018
19setlocal indentexpr=GetCSIndent(v:lnum)
20
21function! s:IsCompilerDirective(line)
Bram Moolenaar63f32602022-06-09 20:45:54 +010022 " Exclude #region and #endregion - these should be indented normally
23 return a:line =~# '^\s*#' && !s:IsRegionDirective(a:line)
24endf
25
26function! s:IsRegionDirective(line)
27 return a:line =~# '^\s*#\s*region' || a:line =~# '^\s*#\s*endregion'
Bram Moolenaard47d5222018-12-09 20:43:55 +010028endf
29
30function! s:IsAttributeLine(line)
Bram Moolenaar63f32602022-06-09 20:45:54 +010031 return a:line =~# '^\s*\[[A-Za-z]' && a:line =~# '\]$'
Bram Moolenaard47d5222018-12-09 20:43:55 +010032endf
33
34function! 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)
Bram Moolenaar63f32602022-06-09 20:45:54 +010038 if !s:IsCompilerDirective(line) && !s:IsRegionDirective(line)
Bram Moolenaard47d5222018-12-09 20:43:55 +010039 return lnum
40 endif
41 endfor
42 return 0
43endf
44
45function! 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)
Bram Moolenaar63f32602022-06-09 20:45:54 +010062 return indent(lnum)
63 elseif s:IsRegionDirective(this_line)
64 return cindent(lnum)
Bram Moolenaard47d5222018-12-09 20:43:55 +010065 else
66 return cindent(a:lnum)
67 endif
68endfunction
69
70let b:undo_indent = 'setlocal indentexpr<'
71
72let &cpoptions = s:save_cpo
73unlet s:save_cpo
74
75" vim:et:sw=2:sts=2