Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1 | " Vim indent file |
Bram Moolenaar | d47d522 | 2018-12-09 20:43:55 +0100 | [diff] [blame] | 2 | " 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 Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11 | |
| 12 | " Only load this indent file when no other was loaded. |
Bram Moolenaar | d47d522 | 2018-12-09 20:43:55 +0100 | [diff] [blame] | 13 | if exists('b:did_indent') |
| 14 | finish |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 15 | endif |
| 16 | let b:did_indent = 1 |
| 17 | |
Bram Moolenaar | d47d522 | 2018-12-09 20:43:55 +0100 | [diff] [blame] | 18 | let s:save_cpo = &cpoptions |
| 19 | set cpoptions&vim |
Bram Moolenaar | 582fd85 | 2005-03-28 20:58:01 +0000 | [diff] [blame] | 20 | |
Bram Moolenaar | d47d522 | 2018-12-09 20:43:55 +0100 | [diff] [blame] | 21 | |
| 22 | setlocal indentexpr=GetCSIndent(v:lnum) |
| 23 | |
| 24 | function! s:IsCompilerDirective(line) |
| 25 | return a:line =~? '^\s*#' |
| 26 | endf |
| 27 | |
| 28 | function! s:IsAttributeLine(line) |
| 29 | return a:line =~? '^\s*\[[A-Za-z]' && a:line =~? '\]$' |
| 30 | endf |
| 31 | |
| 32 | function! 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 |
| 42 | endf |
| 43 | |
| 44 | function! 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 |
| 66 | endfunction |
| 67 | |
| 68 | let b:undo_indent = 'setlocal indentexpr<' |
| 69 | |
| 70 | let &cpoptions = s:save_cpo |
| 71 | unlet s:save_cpo |
| 72 | |
| 73 | " vim:et:sw=2:sts=2 |