nginx.vim 1.7 KB

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