summaryrefslogtreecommitdiffstats
path: root/indent/nix.vim
blob: 7b3f3defa94f372e81b66b2ea3026f413682e9a8 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
let files = filter(globpath(&rtp, 'indent/nix.vim', 1, 1), { _, v -> v !~ "vim-polyglot" && v !~ $VIMRUNTIME && v !~ "after" })
if len(files) > 0
  exec 'source ' . files[0]
  finish
endif
if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'nix') == -1

" Vim indent file
" Language:    Nix
" Maintainer:  Daiderd Jordan <daiderd@gmail.com>
" URL:         https://github.com/LnL7/vim-nix

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

setlocal indentexpr=GetNixIndent()
setlocal indentkeys+=0=then,0=else,0=inherit,0=in,*<Return>

if exists("*GetNixIndent")
  finish
endif

let s:cpo_save = &cpo
set cpo&vim

let s:skip_syntax = '\%(Comment\|String\)$'
let s:binding_open = '\%(\<let\>\)'
let s:binding_close = '\%(\<in\>\)'
let s:block_open  = '\%({\|[\)'
let s:block_close = '\%(}\|]\)'

function! GetNixIndent()
  let lnum = prevnonblank(v:lnum - 1)
  let ind  = indent(lnum)

  " At the start of the file use zero indent.
  if lnum == 0
    return 0
  endif

  " Skip indentation for single line comments explicitly, in case a
  " comment was just inserted (eg. visual block mode)
  if getline(v:lnum) =~ '^\s*#'
    return indent(v:lnum)
  endif

  if synIDattr(synID(v:lnum, 1, 1), "name") !~ s:skip_syntax
    let current_line = getline(v:lnum)
    let last_line = getline(lnum)

    if current_line =~ '^\s*in\>'
      let save_cursor = getcurpos()
      normal ^
      let bslnum = searchpair(s:binding_open, '', s:binding_close, 'bnW',
            \ 'synIDattr(synID(line("."), col("."), 0), "name") =~? "StringSpecial$"')
      call setpos('.', save_cursor)
      return indent(bslnum)
    endif

    if last_line =~ s:block_open . '\s*$'
      let ind += &sw
    endif

    if current_line =~ '^\s*' . s:block_close
      let ind -= &sw
    endif

    if last_line =~ '[(=]$'
      let ind += &sw
    endif

    if last_line =~ '\<let\s*$'
      let ind += &sw
    endif

    if last_line =~ '^\<in\s*$'
      let ind += &sw
    endif

    if current_line =~ '^\s*in\>'
      let ind -= &sw
    endif
  endif

  if synIDattr(synID(v:lnum, 1, 1), "name") =~ '^nixString'
    let current_line = getline(v:lnum)

    let ind = indent(v:lnum)
    let bslnum = searchpair('''''', '', '''''', 'bnW',
          \ 'synIDattr(synID(line("."), col("."), 0), "name") =~? "StringSpecial$"')

    if ind <= indent(bslnum)
      let ind = indent(bslnum) + &sw
    endif

    if current_line =~ '^\s*''''[^''\$]'
      let ind = indent(bslnum)
    endif
    if current_line =~ '^\s*''''$'
      let ind = indent(bslnum)
    endif
  endif

  return ind
endfunction

let &cpo = s:cpo_save
unlet s:cpo_save

endif