aboutsummaryrefslogtreecommitdiff
path: root/runtime/indent/nginx.vim
diff options
context:
space:
mode:
authorJosh Rahm <joshuarahm@gmail.com>2023-01-25 18:31:31 +0000
committerJosh Rahm <joshuarahm@gmail.com>2023-01-25 18:31:31 +0000
commit9243becbedbb6a1592208051f8fa2b090dcc5e7d (patch)
tree607c2a862ec3f4399b8766383f6f8e04c4aa43b4 /runtime/indent/nginx.vim
parent9e40b6e9e1bc67f2d856adb837ee64dd0e25b717 (diff)
parent3c48d3c83fc21dbc0841f9210f04bdb073d73cd1 (diff)
downloadrneovim-usermarks.tar.gz
rneovim-usermarks.tar.bz2
rneovim-usermarks.zip
Merge remote-tracking branch 'upstream/master' into usermarksusermarks
Diffstat (limited to 'runtime/indent/nginx.vim')
-rw-r--r--runtime/indent/nginx.vim77
1 files changed, 68 insertions, 9 deletions
diff --git a/runtime/indent/nginx.vim b/runtime/indent/nginx.vim
index 8cef7662e0..65506099f4 100644
--- a/runtime/indent/nginx.vim
+++ b/runtime/indent/nginx.vim
@@ -1,19 +1,78 @@
" Vim indent file
" Language: nginx.conf
" Maintainer: Chris Aumann <me@chr4.org>
-" Last Change: 2022 Apr 06
+" Last Change: 2022 Dec 01
-if exists("b:did_indent")
- finish
+" Only load this indent file when no other was loaded.
+if exists('b:did_indent')
+ finish
endif
let b:did_indent = 1
-setlocal indentexpr=
+setlocal indentexpr=GetNginxIndent()
-" cindent actually works for nginx' simple file structure
-setlocal cindent
+setlocal indentkeys=0{,0},0#,!^F,o,O
-" Just make sure that the comments are not reset as defs would be.
-setlocal cinkeys-=0#
+let b:undo_indent = 'setl inde< indk<'
-let b:undo_indent = "setl inde< cin< cink<"
+" 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