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