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