Philip H | 2d919d2 | 2024-05-01 18:42:11 +0200 | [diff] [blame] | 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 | |
| 12 | setlocal indentexpr=GetStylusIndent() |
| 13 | setlocal indentkeys=o,O,*<Return>,},],0),!^F |
| 14 | let b:undo_indent = "setl indentexpr< indentkeys<" |
| 15 | |
| 16 | if exists("*GetStylusIndent") " only define once |
| 17 | finish |
| 18 | endif |
| 19 | |
| 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 | |
| 41 | function s:count_braces(lnum, count_open) |
| 42 | let n_open = 0 |
| 43 | let n_close = 0 |
| 44 | let line = getline(a:lnum) |
| 45 | let pattern = '[{}]' |
| 46 | let i = match(line, pattern) |
| 47 | while i != -1 |
| 48 | if synIDattr(synID(a:lnum, i + 1, 0), 'name') !~ 'css\%(Comment\|StringQ\{1,2}\)' |
| 49 | if line[i] == '{' |
| 50 | let n_open += 1 |
| 51 | elseif line[i] == '}' |
| 52 | if n_open > 0 |
| 53 | let n_open -= 1 |
| 54 | else |
| 55 | let n_close += 1 |
| 56 | endif |
| 57 | endif |
| 58 | endif |
| 59 | let i = match(line, pattern, i + 1) |
| 60 | endwhile |
| 61 | return a:count_open ? n_open : n_close |
| 62 | endfunction |
| 63 | |
| 64 | " function CheckCSSIndent() |
| 65 | " let line = getline(v:lnum) |
| 66 | " if line =~ '^\s*\*' |
| 67 | " return cindent(v:lnum) |
| 68 | " endif |
| 69 | " |
| 70 | " let pnum = s:prevnonblanknoncomment(v:lnum - 1) |
| 71 | " if pnum == 0 |
| 72 | " return 0 |
| 73 | " endif |
| 74 | |
| 75 | function! GetStylusIndent() |
| 76 | let line = getline(v:lnum) |
| 77 | if line =~ '^\s*\*' |
| 78 | return cindent(v:lnum) |
| 79 | endif |
| 80 | |
| 81 | let pnum = s:prevnonblanknoncomment(v:lnum - 1) |
| 82 | if pnum == 0 |
| 83 | return 0 |
| 84 | endif |
| 85 | |
| 86 | let lnum = prevnonblank(v:lnum-1) |
| 87 | if lnum == 0 |
| 88 | return 0 |
| 89 | endif |
| 90 | |
| 91 | let pline = getline(pnum) |
| 92 | |
| 93 | if pline =~ '[}{]' |
| 94 | return indent(pnum) + s:count_braces(pnum, 1) * &sw - s:count_braces(v:lnum, 0) * &sw |
| 95 | endif |
| 96 | |
| 97 | let line = substitute(getline(lnum),'[\s()]\+$','','') " get last line strip ending whitespace |
| 98 | let cline = substitute(substitute(getline(v:lnum),'\s\+$','',''),'^\s\+','','') " get current line, trimmed |
| 99 | let lastcol = strlen(line) " get last col in prev line |
| 100 | let line = substitute(line,'^\s\+','','') " then remove preceeding whitespace |
| 101 | let indent = indent(lnum) " get indent on prev line |
| 102 | let cindent = indent(v:lnum) " get indent on current line |
| 103 | let increase = indent + &sw " increase indent by the shift width |
| 104 | if indent == indent(lnum) |
| 105 | let indent = cindent <= indent ? indent : increase |
| 106 | endif |
| 107 | |
| 108 | let group = synIDattr(synID(lnum,lastcol,1),'name') |
| 109 | |
| 110 | " if group !~? 'css.*' && line =~? ')\s*$' " match user functions |
| 111 | " return increase |
| 112 | if group =~? '\v^%(cssTagName|cssClassName|cssIdentifier|cssSelectorOp|cssSelectorOp2|cssBraces|cssAttributeSelector|cssPseudo|stylusId|stylusClass)$' |
| 113 | return increase |
| 114 | elseif (group == 'stylusUserFunction') && (indent(lnum) == '0') " mixin definition |
| 115 | return increase |
| 116 | else |
| 117 | return indent |
| 118 | endif |
| 119 | endfunction |
| 120 | |
| 121 | " vim:set sw=2; |