Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 36 additions & 10 deletions indent/cs.vim
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,57 @@
"

" Only load this indent file when no other was loaded.
if exists("b:did_indent")
if exists('b:did_indent')
finish
endif
let b:did_indent = 1


setlocal indentexpr=GetCSIndent()
setlocal indentexpr=GetCSIndent(v:lnum)

function! s:IsCompilerDirective(line)
return a:line =~? '^\s*#'
endf

function! GetCSIndent()
function! s:IsAttributeLine(line)
return a:line =~? '^\s*\[[A-Za-z]' && a:line =~? '\]$'
endf

let this_line = getline(v:lnum)
let previous_line = getline(v:lnum - 1)
function! s:FindPreviousNonCompilerDirectiveLine(start_lnum)
for delta in range(0, a:start_lnum)
let lnum = a:start_lnum - delta
let line = getline(lnum)
let is_directive = s:IsCompilerDirective(line)
if !is_directive
return lnum
endif
endfor
return 0
endf

function! GetCSIndent(lnum) abort
" Hit the start of the file, use zero indent.
if v:lnum == 0
if a:lnum == 0
return 0
endif

" If previous_line is an attribute line:
if previous_line =~? '^\s*\[[A-Za-z]' && previous_line =~? '\]$'
let ind = indent(v:lnum - 1)
let this_line = getline(a:lnum)

" Compiler directives use zero indent if so configured.
let is_first_col_macro = s:IsCompilerDirective(this_line) && stridx(&l:cinkeys, '0#') >= 0
if is_first_col_macro
return cindent(a:lnum)
endif

let lnum = s:FindPreviousNonCompilerDirectiveLine(a:lnum - 1)
let previous_code_line = getline(lnum)
if s:IsAttributeLine(previous_code_line)
let ind = indent(lnum)
return ind
else
return cindent(v:lnum)
return cindent(a:lnum)
endif

endfunction

" vim:et:sw=2:sts=2