stylus.vim 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. " Vim indent file
  2. " Language: Stylus
  3. " Maintainer: Marc Harter
  4. " Last Change: 2010 May 21
  5. " Based On: sass.vim from Tim Pope
  6. "
  7. if exists("b:did_indent")
  8. finish
  9. endif
  10. let b:did_indent = 1
  11. setlocal indentexpr=GetStylusIndent()
  12. setlocal indentkeys=o,O,*<Return>,},],0),!^F
  13. let b:undo_indent = "setl indentexpr< indentkeys<"
  14. if exists("*GetStylusIndent") " only define once
  15. finish
  16. endif
  17. function s:prevnonblanknoncomment(lnum)
  18. let lnum = a:lnum
  19. while lnum > 1
  20. let lnum = prevnonblank(lnum)
  21. let line = getline(lnum)
  22. if line =~ '\*/'
  23. while lnum > 1 && line !~ '/\*'
  24. let lnum -= 1
  25. endwhile
  26. if line =~ '^\s*/\*'
  27. let lnum -= 1
  28. else
  29. break
  30. endif
  31. else
  32. break
  33. endif
  34. endwhile
  35. return lnum
  36. endfunction
  37. function s:count_braces(lnum, count_open)
  38. let n_open = 0
  39. let n_close = 0
  40. let line = getline(a:lnum)
  41. let pattern = '[{}]'
  42. let i = match(line, pattern)
  43. while i != -1
  44. if synIDattr(synID(a:lnum, i + 1, 0), 'name') !~ 'css\%(Comment\|StringQ\{1,2}\)'
  45. if line[i] == '{'
  46. let n_open += 1
  47. elseif line[i] == '}'
  48. if n_open > 0
  49. let n_open -= 1
  50. else
  51. let n_close += 1
  52. endif
  53. endif
  54. endif
  55. let i = match(line, pattern, i + 1)
  56. endwhile
  57. return a:count_open ? n_open : n_close
  58. endfunction
  59. " function CheckCSSIndent()
  60. " let line = getline(v:lnum)
  61. " if line =~ '^\s*\*'
  62. " return cindent(v:lnum)
  63. " endif
  64. "
  65. " let pnum = s:prevnonblanknoncomment(v:lnum - 1)
  66. " if pnum == 0
  67. " return 0
  68. " endif
  69. function! GetStylusIndent()
  70. let line = getline(v:lnum)
  71. if line =~ '^\s*\*'
  72. return cindent(v:lnum)
  73. endif
  74. let pnum = s:prevnonblanknoncomment(v:lnum - 1)
  75. if pnum == 0
  76. return 0
  77. endif
  78. let lnum = prevnonblank(v:lnum-1)
  79. if lnum == 0
  80. return 0
  81. endif
  82. let pline = getline(pnum)
  83. if pline =~ '[}{]'
  84. return indent(pnum) + s:count_braces(pnum, 1) * &sw - s:count_braces(v:lnum, 0) * &sw
  85. endif
  86. let line = substitute(getline(lnum),'[\s()]\+$','','') " get last line strip ending whitespace
  87. let cline = substitute(substitute(getline(v:lnum),'\s\+$','',''),'^\s\+','','') " get current line, trimmed
  88. let lastcol = strlen(line) " get last col in prev line
  89. let line = substitute(line,'^\s\+','','') " then remove preceeding whitespace
  90. let indent = indent(lnum) " get indent on prev line
  91. let cindent = indent(v:lnum) " get indent on current line
  92. let increase = indent + &sw " increase indent by the shift width
  93. if indent == indent(lnum)
  94. let indent = cindent <= indent ? indent : increase
  95. endif
  96. let group = synIDattr(synID(lnum,lastcol,1),'name')
  97. " if group !~? 'css.*' && line =~? ')\s*$' " match user functions
  98. " return increase
  99. if group =~? '\v^%(cssTagName|cssClassName|cssIdentifier|cssSelectorOp|cssSelectorOp2|cssBraces|cssAttributeSelector|cssPseudo|stylusId|stylusClass)$'
  100. return increase
  101. elseif (group == 'stylusUserFunction') && (indent(lnum) == '0') " mixin definition
  102. return increase
  103. else
  104. return indent
  105. endif
  106. endfunction
  107. " vim:set sw=2;