codekingpro/portable-devtools
114k
1" Vim indent file2" Language: nginx.conf3" Maintainer: Chris Aumann <me@chr4.org>4" Last Change: 2022 Dec 015 6" Only load this indent file when no other was loaded.7if exists('b:did_indent')8 finish9endif10let b:did_indent = 111 12setlocal indentexpr=GetNginxIndent()13 14setlocal indentkeys=0{,0},0#,!^F,o,O15 16let b:undo_indent = 'setl inde< indk<'17 18" Only define the function once.19if exists('*GetNginxIndent')20 finish21endif22 23function GetNginxIndent() abort24 let plnum = s:PrevNotAsBlank(v:lnum - 1)25 26 " Hit the start of the file, use zero indent.27 if plnum == 028 return 029 endif30 31 let ind = indent(plnum)32 33 " Add a 'shiftwidth' after '{'34 if s:AsEndWith(getline(plnum), '{')35 let ind = ind + shiftwidth()36 end37 38 " Subtract a 'shiftwidth' on '}'39 " This is the part that requires 'indentkeys'.40 if getline(v:lnum) =~ '^\s*}'41 let ind = ind - shiftwidth()42 endif43 44 let pplnum = s:PrevNotAsBlank(plnum - 1)45 46 if s:IsLineContinuation(plnum)47 if !s:IsLineContinuation(pplnum)48 let ind = ind + shiftwidth()49 end50 else51 if s:IsLineContinuation(pplnum)52 let ind = ind - shiftwidth()53 end54 endif55 56 return ind57endfunction58 59" Find the first line at or above {lnum} that is non-blank and not a comment.60function s:PrevNotAsBlank(lnum) abort61 let lnum = prevnonblank(a:lnum)62 while lnum > 063 if getline(lnum) !~ '^\s*#'64 break65 endif66 let lnum = prevnonblank(lnum - 1)67 endwhile68 return lnum69endfunction70 71" Check whether {line} ends with {pat}, ignoring trailing comments.72function s:AsEndWith(line, pat) abort73 return a:line =~ a:pat . '\m\s*\%(#.*\)\?$'74endfunction75 76function s:IsLineContinuation(lnum) abort77 return a:lnum > 0 && !s:AsEndWith(getline(a:lnum), '[;{}]')78endfunction79 