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