summaryrefslogtreecommitdiffstats
path: root/indent/stylus.vim
blob: 1254ebc348517957ab028f4fd882d6e5a4accf7a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
if polyglot#init#is_disabled(expand('<sfile>:p'), 'stylus', 'indent/stylus.vim')
  finish
endif

" Vim indent file
" Language: Stylus
" Maintainer: Ilia Loginov
" Last Change: 2018 Jan 19
" Based On: sass.vim from Tim Pope

if exists("b:did_indent")
  finish
endif
let b:did_indent = 1

setlocal indentexpr=GetStylusIndent()
setlocal autoindent nolisp nosmartindent
setlocal indentkeys=o,O,*<Return>,0),!^F
setlocal formatoptions+=r

if exists('*GetStylusIndent')
  finish
endif

function s:prevnonblanknoncomment(lnum)
  let lnum = a:lnum
  while lnum > 1
    let lnum = prevnonblank(lnum)
    let line = getline(lnum)
    if line =~ '\*/'
      while lnum > 1 && line !~ '/\*'
        let lnum -= 1
      endwhile
      if line =~ '^\s*/\*'
        let lnum -= 1
      else
        break
      endif
    else
      break
    endif
  endwhile
  return lnum
endfunction

function GetStylusIndent()
  let line = getline(v:lnum)
  if line =~ '^\s*\*'
    return cindent(v:lnum)
  endif

  let pnum = s:prevnonblanknoncomment(v:lnum - 1)
  if pnum == 0
    return 0
  endif

  let lnum = prevnonblank(v:lnum-1)
  if lnum == 0
    return 0
  endif

  let pline = getline(pnum)

  " Get last line strip ending whitespace
  let line = substitute(getline(lnum),'[\s()]\+$','','')
  " Get current line, trimmed
  let cline    = substitute(substitute(getline(v:lnum),'\s\+$','',''),'^\s\+','','')
  " Get last col in prev line
  let lastcol  = strlen(line)
  " Then remove preceeding whitespace
  let line     = substitute(line,'^\s\+','','')
  " Get indent on prev line
  let indent   = indent(lnum)
  " Get indent on current line
  let cindent  = indent(v:lnum)
  " Increase indent by the shift width
  let increase = indent + &sw
  if indent   == indent(lnum)
    let indent = cindent <= indent ? indent : increase
  endif

  let group = synIDattr(synID(lnum, cindent + 1, 1), 'name')

  if group =~ 'stylusSelector\w*'
    return increase
  elseif group =~ 'stylusAtRule\(Viewport\|Page\|Supports\|Document\|Font\|Keyframes\)\w*'
    return increase
  " Mixin or function definition
  elseif group =~ 'stylusFunction\w*' && indent(lnum) == 0
    return increase
  else
    return indent
  endif
endfunction