robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
c4942880be
master
1" Vim indent file 2" Language: nginx.conf 3" Maintainer: Chris Aumann <me@chr4.org> 4" Last Change: 2022 Dec 01 5 6" Only load this indent file when no other was loaded. 7if exists ( 'b:did_indent' ) 8finish 9endif 10let b: did_indent = 1 11 12setlocal indentexpr = GetNginxIndent () 13 14setlocal indentkeys = 0{,0},0#,!^F,o,O 15 16let b: undo_indent = 'setl inde< indk<' 17 18" Only define the function once. 19if exists ( '*GetNginxIndent' ) 20finish 21endif 22 23function GetNginxIndent () abort 24let plnum = s: PrevNotAsBlank ( v: lnum - 1 ) 25 26" Hit the start of the file, use zero indent. 27if plnum == 0 28return 0 29endif 30 31let ind = indent ( plnum ) 32 33" Add a 'shiftwidth' after '{' 34if s: AsEndWith ( getline ( plnum ), '{' ) 35let ind = ind + shiftwidth () 36end 37 38" Subtract a 'shiftwidth' on '}' 39" This is the part that requires 'indentkeys'. 40if getline ( v: lnum ) =~ '^\s*}' 41let ind = ind - shiftwidth () 42endif 43 44let pplnum = s: PrevNotAsBlank ( plnum - 1 ) 45 46if s: IsLineContinuation ( plnum ) 47if ! s: IsLineContinuation ( pplnum ) 48let ind = ind + shiftwidth () 49end 50else 51if s: IsLineContinuation ( pplnum ) 52let ind = ind - shiftwidth () 53end 54endif 55 56return ind 57endfunction 58 59" Find the first line at or above {lnum} that is non-blank and not a comment. 60function s: PrevNotAsBlank ( lnum ) abort 61let lnum = prevnonblank ( a: lnum ) 62while lnum > 0 63if getline ( lnum ) !~ '^\s*#' 64break 65endif 66let lnum = prevnonblank ( lnum - 1 ) 67endwhile 68return lnum 69endfunction 70 71" Check whether {line} ends with {pat}, ignoring trailing comments. 72function s: AsEndWith ( line , pat ) abort 73return a: line =~ a: pat . '\m\s*\%(#.*\)\?$' 74endfunction 75 76function s: IsLineContinuation ( lnum ) abort 77return a: lnum > 0 && ! s: AsEndWith ( getline ( a: lnum ), '[;{}]' ) 78endfunction