robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Christian Clasonvim-patch:partial:63f32603789d (#18916)6eaf10502c

master
1.9 KiB75 linesraw
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
10if exists('b:did_indent')
11  finish
12endif
13let b:did_indent = 1
14
15let s:save_cpo = &cpoptions
16set cpoptions&vim
17
18
19setlocal indentexpr=GetCSIndent(v:lnum)
20
21function! s:IsCompilerDirective(line)
22  " 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'
28endf
29
30function! s:IsAttributeLine(line)
31  return a:line =~# '^\s*\[[A-Za-z]' && a:line =~# '\]$'
32endf
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)
38    if !s:IsCompilerDirective(line) && !s:IsRegionDirective(line)
39      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)
62    return indent(lnum)
63  elseif s:IsRegionDirective(this_line)
64    return cindent(lnum)
65  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