diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 08:50:31 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 08:50:31 +0000 |
commit | aed8ce9da277f5ecffe968b324f242c41c3b752a (patch) | |
tree | d2e538394cb7a8a7c42a4aac6ccf1a8e3256999b /runtime/indent/nginx.vim | |
parent | Initial commit. (diff) | |
download | vim-aed8ce9da277f5ecffe968b324f242c41c3b752a.tar.xz vim-aed8ce9da277f5ecffe968b324f242c41c3b752a.zip |
Adding upstream version 2:9.0.1378.upstream/2%9.0.1378upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'runtime/indent/nginx.vim')
-rw-r--r-- | runtime/indent/nginx.vim | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/runtime/indent/nginx.vim b/runtime/indent/nginx.vim new file mode 100644 index 0000000..6550609 --- /dev/null +++ b/runtime/indent/nginx.vim @@ -0,0 +1,78 @@ +" Vim indent file +" Language: nginx.conf +" Maintainer: Chris Aumann <me@chr4.org> +" Last Change: 2022 Dec 01 + +" Only load this indent file when no other was loaded. +if exists('b:did_indent') + finish +endif +let b:did_indent = 1 + +setlocal indentexpr=GetNginxIndent() + +setlocal indentkeys=0{,0},0#,!^F,o,O + +let b:undo_indent = 'setl inde< indk<' + +" Only define the function once. +if exists('*GetNginxIndent') + finish +endif + +function GetNginxIndent() abort + 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. +function s:PrevNotAsBlank(lnum) abort + 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. +function s:AsEndWith(line, pat) abort + return a:line =~ a:pat . '\m\s*\%(#.*\)\?$' +endfunction + +function s:IsLineContinuation(lnum) abort + return a:lnum > 0 && !s:AsEndWith(getline(a:lnum), '[;{}]') +endfunction |