css.vim 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. " Vim indent file
  2. " Language: CSS
  3. " Maintainer: Doug Kearns <dougkearns@gmail.com>
  4. " Previous Maintainer: Nikolai Weibull <now@bitwi.se>
  5. " Last Change: 24 Sep 2021
  6. " Use of shiftwidth() added by Oleg Zubchenko.
  7. if exists("b:did_indent")
  8. finish
  9. endif
  10. let b:did_indent = 1
  11. setlocal indentexpr=GetCSSIndent()
  12. setlocal indentkeys=0{,0},!^F,o,O
  13. setlocal nosmartindent
  14. let b:undo_indent = "setl inde< indk< si<"
  15. if exists("*GetCSSIndent")
  16. finish
  17. endif
  18. let s:keepcpo= &cpo
  19. set cpo&vim
  20. function s:prevnonblanknoncomment(lnum)
  21. let lnum = a:lnum
  22. while lnum > 1
  23. let lnum = prevnonblank(lnum)
  24. let line = getline(lnum)
  25. if line =~ '\*/'
  26. while lnum > 1 && line !~ '/\*'
  27. let lnum -= 1
  28. endwhile
  29. if line =~ '^\s*/\*'
  30. let lnum -= 1
  31. else
  32. break
  33. endif
  34. else
  35. break
  36. endif
  37. endwhile
  38. return lnum
  39. endfunction
  40. function s:count_braces(lnum, count_open)
  41. let n_open = 0
  42. let n_close = 0
  43. let line = getline(a:lnum)
  44. let pattern = '[{}]'
  45. let i = match(line, pattern)
  46. while i != -1
  47. if synIDattr(synID(a:lnum, i + 1, 0), 'name') !~ 'css\%(Comment\|StringQ\{1,2}\)'
  48. if line[i] == '{'
  49. let n_open += 1
  50. elseif line[i] == '}'
  51. if n_open > 0
  52. let n_open -= 1
  53. else
  54. let n_close += 1
  55. endif
  56. endif
  57. endif
  58. let i = match(line, pattern, i + 1)
  59. endwhile
  60. return a:count_open ? n_open : n_close
  61. endfunction
  62. function GetCSSIndent()
  63. let line = getline(v:lnum)
  64. if line =~ '^\s*\*'
  65. return cindent(v:lnum)
  66. endif
  67. let pnum = s:prevnonblanknoncomment(v:lnum - 1)
  68. if pnum == 0
  69. return 0
  70. endif
  71. return indent(pnum) + s:count_braces(pnum, 1) * shiftwidth()
  72. \ - s:count_braces(v:lnum, 0) * shiftwidth()
  73. endfunction
  74. let &cpo = s:keepcpo
  75. unlet s:keepcpo