diff --git a/indent/cs.vim b/indent/cs.vim index fc9cdb9..a42f4da 100644 --- a/indent/cs.vim +++ b/indent/cs.vim @@ -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