xref: /vim-8.2.3635/runtime/indent/cs.vim (revision d47d5223)
1" Vim indent file
2" Language:            C#
3" Maintainer:          Nick Jensen <[email protected]>
4" Former Maintainers:  Aquila Deus
5"                      Johannes Zellner <[email protected]>
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
12" Only load this indent file when no other was loaded.
13if exists('b:did_indent')
14  finish
15endif
16let b:did_indent = 1
17
18let s:save_cpo = &cpoptions
19set cpoptions&vim
20
21
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
74