1
0
Fork 0
mirror of synced 2024-11-14 21:18:58 -05:00
ultimate-vim/sources_non_forked/nginx.vim/indent/nginx.vim

74 lines
1.6 KiB
VimL
Raw Normal View History

2022-12-03 05:29:32 -05:00
" Only load this indent file when no other was loaded.
if exists('b:did_indent')
finish
2017-11-24 08:59:41 -05:00
endif
let b:did_indent = 1
2022-12-03 05:29:32 -05:00
setlocal indentexpr=GetNginxIndent()
2017-11-24 08:59:41 -05:00
2022-12-03 05:29:32 -05:00
setlocal indentkeys=0{,0},0#,!^F,o,O
2021-10-11 05:30:43 -04:00
2022-12-03 05:29:32 -05:00
let b:undo_indent = 'setl inde< indk<'
" Only define the function once.
if exists('*GetNginxIndent')
finish
endif
2023-04-01 16:48:04 -04:00
function GetNginxIndent() abort
2022-12-03 05:29:32 -05:00
let plnum = s:PrevNotAsBlank(v:lnum - 1)
" Hit the start of the file, use zero indent.
if plnum == 0
return 0
endif
let ind = indent(plnum)
" Add a 'shiftwidth' after '{'
if s:AsEndWith(getline(plnum), '{')
let ind = ind + shiftwidth()
end
" Subtract a 'shiftwidth' on '}'
" This is the part that requires 'indentkeys'.
if getline(v:lnum) =~ '^\s*}'
let ind = ind - shiftwidth()
endif
let pplnum = s:PrevNotAsBlank(plnum - 1)
if s:IsLineContinuation(plnum)
if !s:IsLineContinuation(pplnum)
let ind = ind + shiftwidth()
end
else
if s:IsLineContinuation(pplnum)
let ind = ind - shiftwidth()
end
endif
return ind
endfunction
" Find the first line at or above {lnum} that is non-blank and not a comment.
2023-04-01 16:48:04 -04:00
function s:PrevNotAsBlank(lnum) abort
2022-12-03 05:29:32 -05:00
let lnum = prevnonblank(a:lnum)
while lnum > 0
if getline(lnum) !~ '^\s*#'
break
endif
let lnum = prevnonblank(lnum - 1)
endwhile
return lnum
endfunction
" Check whether {line} ends with {pat}, ignoring trailing comments.
2023-04-01 16:48:04 -04:00
function s:AsEndWith(line, pat) abort
2022-12-03 05:29:32 -05:00
return a:line =~ a:pat . '\m\s*\%(#.*\)\?$'
endfunction
2023-04-01 16:48:04 -04:00
function s:IsLineContinuation(lnum) abort
2022-12-03 05:29:32 -05:00
return a:lnum > 0 && !s:AsEndWith(getline(a:lnum), '[;{}]')
endfunction