| 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
 | if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'purescript') == -1
" indentation for purescript
"
" Based on idris indentation
"
" author: raichoo (raichoo@googlemail.com)
"
" Modify g:purescript_indent_if and g:purescript_indent_case to
" change indentation for `if'(default 3) and `case'(default 5).
" Example (in .vimrc):
" > let g:purescript_indent_if = 2
if exists('b:did_indent')
  finish
endif
let b:did_indent = 1
if !exists('g:purescript_indent_if')
  " if bool
  " >>>then ...
  " >>>else ...
  let g:purescript_indent_if = 3
endif
if !exists('g:purescript_indent_case')
  " case xs of
  " >>>>>[]     -> ...
  " >>>>>(y:ys) -> ...
  let g:purescript_indent_case = 5
endif
if !exists('g:purescript_indent_let')
  " let x = 0 in
  " >>>>x
  let g:purescript_indent_let = 4
endif
if !exists('g:purescript_indent_in')
  " let x = 0
  " >in
  let g:purescript_indent_in = 1
endif
if !exists('g:purescript_indent_where')
  " where
  " >>f :: Int -> Int
  " >>f x = x
  let g:purescript_indent_where = 6
endif
if !exists('g:purescript_indent_do')
  " do x <- a
  " >>>y <- b
  let g:purescript_indent_do = 3
endif
if !exists('g:purescript_indent_dot')
  " f
  "   :: forall a
  "   >. String
  "   -> String
  let g:purescript_indent_dot = 1
endif
setlocal indentexpr=GetPurescriptIndent()
setlocal indentkeys=!^F,o,O,},=where,=in,=::,=->,=→,==>,=⇒
function! s:GetSynStack(lnum, col)
  return map(synstack(a:lnum, a:col), { key, val -> synIDattr(val, "name") })
endfunction
function! GetPurescriptIndent()
  let ppline = getline(v:lnum - 2)
  let prevline = getline(v:lnum - 1)
  let line = getline(v:lnum)
  if line =~ '^\s*\<where\>'
    let s = indent(v:lnum - 1)
    return max([s, &l:shiftwidth])
  endif
  if line =~ '^\s*\<in\>'
    let n = v:lnum
    let s = 0
    while s <= 0 && n > 0
      let n = n - 1
      let s = match(getline(n), '\<let\>')
      if s >= 0 && index(s:GetSynStack(v:lnum - 1, s), 'purescriptString') != -1
	let s = -1
      endif
    endwhile
    return s + g:purescript_indent_in
  endif
  let s = match(prevline, '^\s*\zs\(--\|import\)')
  if s >= 0
    " comments
    " imports
    return s
  endif
  if prevline =~ '^\S.*::' && line !~ '^\s*\(\.\|->\|→\|=>\|⇒\)' && prevline !~ '^instance'
    " f :: String
    "	-> String
    return 0
  endif
  let s = match(prevline, '[[:alnum:][:blank:]]\@<=|[[:alnum:][:blank:]$]')
  if s >= 0 && prevline !~ '^class\>' && index(s:GetSynStack(v:lnum - 1, s), "purescriptFunctionDecl") == -1
    " ident pattern guards but not if we are in a type declaration
    " what we detect using syntax groups
    if prevline =~ '|\s*otherwise\>'
      return indent(search('^\s*\k', 'bnW'))
      " somehow this pattern does not work :/
      " return indent(search('^\(\s*|\)\@!', 'bnW'))
    else
      return s
    endif
  endif
  let s = match(line, '\%(\\.\{-}\)\@<=->')
  if s >= 0
    " inline lambda
    return indent(v:lnum)
  endif
  " indent rules for -> (lambdas and case expressions)
  let s = match(line, '->')
  let p = match(prevline, '\\')
  " protect that we are not in a type signature
  " and not in a case expression
  if s >= 0 && index(s:GetSynStack(s == 0 ? v:lnum - 1 : v:lnum, max([1, s])), "purescriptFunctionDecl") == -1
    \ && p >= 0 &&  index(s:GetSynStack(v:lnum - 1, p), "purescriptString") == -1
    return p
  endif
  if prevline =~ '^\S'
    " start typing signature, function body, data & newtype on next line
    return &l:shiftwidth
  endif
  if ppline =~ '^\S' && prevline =~ '^\s*$'
    return 0
  endif
  if line =~ '^\s*\%(::\|∷\)'
    return match(prevline, '\S') + &l:shiftwidth
  endif
  if prevline =~ '^\s*\(::\|∷\)\s*forall'
    return match(prevline, '\S') + g:purescript_indent_dot
  endif
  let s = match(prevline, '^\s*\zs\%(::\|∷\|=>\|⇒\|->\|→\)')
  let r = match(prevline, '^\s*\zs\.')
  if s >= 0 || r >= 0
    if s >= 0
      if line !~ '^\s*\%(::\|∷\|=>\|⇒\|->\|→\)' && line !~ '^\s*$'
	return s - 2
      else
	return s
      endif
    elseif r >= 0
      if line !~ '^\s\%(::\|∷\|=>\|⇒\|->\|→\)'
	return r - g:purescript_indent_dot
      else
	return r
      endif
    endif
  endif
  if prevline =~ '[!#$%&*+./<>?@\\^~-]\s*$'
    let s = match(prevline, '=')
    if s > 0
      return s + &l:shiftwidth
    endif
    let s = match(prevline, '\<:\>')
    if s > 0
      return s + &l:shiftwidth
    else
      return match(prevline, '\S') + &l:shiftwidth
    endif
  endif
  if prevline =~ '[{([][^})\]]\+$'
    echom "return 1"
    return match(prevline, '[{([]')
  endif
  let s = match(prevline, '\<let\>\s\+\zs\S')
  if s >= 0 && index(s:GetSynStack(v:lnum - 1, s), 'purescriptString') == -1
    return s
  endif
  let s = match(prevline, '\<let\>\s*$')
  if s >= 0 && index(s:GetSynStack(v:lnum - 1, s), 'purescriptString') == -1
    return s + g:purescript_indent_let
  endif
  let s = match(prevline, '\<let\>\s\+.\+\(\<in\>\)\?\s*$')
  if s >= 0 && index(s:GetSynStack(v:lnum - 1, s), 'purescriptString') == -1
    return match(prevline, '\<let\>') + g:purescript_indent_let
  endif
  let s = searchpairpos('\%(--.\{-}\)\@<!\<if\>', '\<then\>', '\<else\>.*\zs$', 'bnrc')[0]
  if s > 0
    " this rule ensures that using `=` in visual mode will correctly indent
    " `if then else`, but it does not handle lines after `then` and `else`
    if line =~ '\<\%(then\|else\)\>'
      return match(getline(s), '\<if\>') + &l:shiftwidth
    endif
  endif
  let p = match(prevline, '\<if\>\%(.\{-}\<then\>.\{-}\<else\>\)\@!')
  if p > 0
    return p + &l:shiftwidth
  endif
  let s = match(prevline, '=\s*$')
  if s >= 0 && index(s:GetSynStack(v:lnum - 1, s), 'purescriptString') == -1
    return match(prevline, '\S') + &l:shiftwidth
  endif
  let s = match(prevline, '[{([]\s*$')
  if s >= 0 && index(s:GetSynStack(v:lnum - 1, s), 'purescriptString') == -1
    return match(prevline, '\S') + (line !~ '^\s*[})]]' ? 0 : &l:shiftwidth)
  endif
  if prevline =~ '^class'
    return &l:shiftwidth
  endif
  let s = match(prevline, '\<where\>\s*$')
  if s >= 0 && index(s:GetSynStack(v:lnum - 1, s), 'purescriptString') == -1
    return match(prevline, '\S') + g:purescript_indent_where
  endif
  let s = match(prevline, '\<where\>\s\+\zs\S\+.*$')
  if s >= 0 && index(s:GetSynStack(v:lnum - 1, s), 'purescriptString') == -1
    return s
  endif
  let s = match(prevline, '\<do\>\s*$')
  if s >= 0 && index(s:GetSynStack(v:lnum - 1, s), 'purescriptString') == -1
    return match(prevline, '\S') + g:purescript_indent_do
  endif
  let s = match(prevline, '\<do\>\s\+\zs\S\+.*$')
  if s >= 0 && index(s:GetSynStack(v:lnum - 1, s), 'purescriptString') == -1
    return s
  endif
  let s = match(prevline, '^\s*\<data\>\s\+[^=]\+\s\+=\s\+\S\+.*$')
  if s >= 0 && index(s:GetSynStack(v:lnum - 1, s), 'purescriptString') == -1
    return match(prevline, '=')
  endif
  let s = match(prevline, '\<case\>\s\+.\+\<of\>\s*$')
  if s >= 0 && index(s:GetSynStack(v:lnum - 1, s), 'purescriptString') == -1
    return match(prevline, '\<case\>') + g:purescript_indent_case
  endif
  if prevline =~ '^\s*\<\data\>\s\+\S\+\s*$'
    return match(prevline, '\<data\>') + &l:shiftwidth
  endif
  let s = match(prevline, '^\s*[}\]]')
  if s >= 0 && index(s:GetSynStack(v:lnum - 1, s), 'purescriptString') == -1
    return match(prevline, '\S') - &l:shiftwidth
  endif
  return match(prevline, '\S')
endfunction
endif
 |