vim

Fork of vim, an acceptable text editor
git clone git://git.laack.co/vim.git
Log | Files | Refs | README | LICENSE

stylus.vim (3161B)


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