summaryrefslogtreecommitdiffstats
path: root/indent/haskell.vim
blob: 4820b4da79299392a20ab12cd58c83c8598cc5b5 (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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
if !has_key(g:polyglot_is_disabled, 'haskell')
  finish
endif

" indentation for haskell
"
" author: raichoo (raichoo@googlemail.com)
"
" Modify g:haskell_indent_if and g:haskell_indent_case to
" change indentation for `if'(default 3) and `case'(default 5).
" Example (in .vimrc):
" > let g:haskell_indent_if = 2

if exists('b:did_indent')
  finish
endif

if get(g:, 'haskell_indent_disable', 0)
  finish
endif

let b:did_indent = 1

if !exists('g:haskell_indent_if')
  " if x
  " >>>then ...
  " >>>else ...
  let g:haskell_indent_if = 3
endif

if !exists('g:haskell_indent_case')
  " case xs of
  " >>[]     -> ...
  " >>(y:ys) -> ...
  let g:haskell_indent_case = 2
endif

if !exists('g:haskell_indent_let')
  " let x = 0 in
  " >>>>x
  "
  " let x = 0
  "     y = 1
  let g:haskell_indent_let = 4
endif

if !exists('g:haskell_indent_where')
  " where f :: Int -> Int
  " >>>>>>f x = x
  let g:haskell_indent_where = 6
endif

if !exists('g:haskell_indent_do')
  " do x <- a
  " >>>y <- b
  let g:haskell_indent_do = 3
endif

if !exists('g:haskell_indent_in')
  " let x = 1
  " >in x
  let g:haskell_indent_in = 1
endif

if !exists('g:haskell_indent_guard')
  " f x y
  " >>|
  let g:haskell_indent_guard = 2
endif

setlocal indentexpr=GetHaskellIndent()
setlocal indentkeys=!^F,o,O,0{,0},0(,0),0[,0],0,,0=where,0=let,0=deriving,0=in\ ,0=::\ ,0=\-\>\ ,0=\=\>\ ,0=\|\ ,=\=\ 

function! s:isInBlock(hlstack)
  return index(a:hlstack, 'haskellDelimiter') > -1 || index(a:hlstack, 'haskellParens') > -1 || index(a:hlstack, 'haskellBrackets') > -1 || index(a:hlstack, 'haskellBlock') > -1 || index(a:hlstack, 'haskellBlockComment') > -1 || index(a:hlstack, 'haskellPragma') > -1
endfunction

function! s:stripComment(line)
  if a:line =~ '^\s*--\(-*\s\+\|$\)'
    return ''
  else
    let l:stripped = split(a:line, '-- ')
    if len(l:stripped) > 1
      return substitute(l:stripped[0], '\s*$', '', '')
    else
      return a:line
    endif
  endif
endfunction

function! s:isSYN(grp, line, col)
  return index(s:getHLStack(a:line, a:col), a:grp) != -1
endfunction

function! s:getNesting(hlstack)
  return filter(a:hlstack, 'v:val == "haskellBlock" || v:val == "haskellBrackets" || v:val == "haskellParens" || v:val == "haskellBlockComment" || v:val == "haskellPragma" ')
endfunction

function! s:getHLStack(line, col)
  return map(synstack(a:line, a:col), 'synIDattr(v:val, "name")')
endfunction

" indent matching character
function! s:indentMatching(char)
  normal! 0
  call search(a:char, 'cW')
  normal! %
  return col('.') - 1
endfunction

" backtrack to find guard clause
function! s:indentGuard(pos, prevline)
  let l:l = a:prevline
  let l:c = v:lnum - 1
  let l:s = indent(l:c)

  while l:c >= 1
    if l:s == 0 && strlen(l:l) > 0
      " top-level start, stop looking
      return g:haskell_indent_guard
    elseif l:l =~ '^\s\+[|,=]\s\+'
      " guard block found
      return match(l:l, '[|,=]')
    else
      if l:s > 0 && l:s <= a:pos
        " found less deeper indentation (not starting with `,` or `=`)
        " stop looking
        return l:s + g:haskell_indent_guard
      endif
    endif
    let l:c -= 1
    let l:l = getline(l:c)
    let l:s = indent(l:c)
  endwhile

  return -1
endfunction

function! GetHaskellIndent()
  let l:hlstack = s:getHLStack(line('.'), col('.'))

  " do not indent in strings and quasiquotes
  if index(l:hlstack, 'haskellQuasiQuote') > -1 || index(l:hlstack, 'haskellBlockComment') > -1
    return -1
  endif

  let l:prevline = s:stripComment(getline(v:lnum - 1))
  let l:line     = getline(v:lnum)

  " indent multiline strings
  if index(l:hlstack, 'haskellString') > -1
    if l:line =~ '^\s*\\'
      return match(l:prevline, '["\\]')
    else
      return - 1
    endif
  endif

  " reset
  if l:prevline =~ '^\s*$' && l:line !~ '^\s*\S'
    return 0
  endif

  "   { foo :: Int
  " >>,
  "
  "   |
  "   ...
  " >>,
  if l:line =~ '^\s*,'
    if s:isInBlock(s:getHLStack(line('.'), col('.')))
      normal! 0
      call search(',', 'cW')
      let l:n = s:getNesting(s:getHLStack(line('.'), col('.')))
      call search('[([{]', 'bW')
      let l:cl = line('.')
      let l:cc = col('.')

      while l:n != s:getNesting(s:getHLStack(l:cl, l:cc)) || s:isSYN('haskellString', l:cl, l:cc) || s:isSYN('haskellChar', l:cl, l:cc)
        call search('[([{]', 'bW')
        let l:cl = line('.')
        let l:cc = col('.')
      endwhile

      return l:cc - 1
    else
      let l:s = s:indentGuard(match(l:line, ','), l:prevline)
      if l:s > -1
        return l:s
      end
    endif
  endif

  " operator at end of previous line
  if l:prevline =~ '[!#$%&*+./<>?@\\^|~-]\s*$'
    return indent(v:lnum - 1) + &shiftwidth
  endif

  " let foo =
  " >>>>>>bar
  if l:prevline =~ '\C\<let\>\s\+[^=]\+=\s*$'
    return match(l:prevline, '\C\<let\>') + g:haskell_indent_let + &shiftwidth
  endif

  " let x = 1 in
  " >>>>x
  if l:prevline =~ '\C\<let\>.\{-}\<in\>\s*$' && l:line !~ '\C^\s*\<in\>'
    return match(l:prevline, '\C\<let\>') + g:haskell_indent_let
  endif

  " let x = 1
  " let y = 2
  "
  " let x = 1
  " >>>>y = 2
  "
  " let x = 1
  " y 2
  if l:prevline =~ '\C\<let\>\s\+.\+$'
    if l:line =~ '\C^\s*\<let\>'
      let l:s = match(l:prevline, '\C\<let\>')
      if s:isSYN('haskellLet', v:lnum - 1, l:s + 1)
        return l:s
      endif
    elseif l:line =~ '\s=\s'
      let l:s = match(l:prevline, '\C\<let\>')
      if s:isSYN('haskellLet', v:lnum - 1, l:s + 1)
        return l:s + g:haskell_indent_let
      endif
    endif
  endif

  " if handling
  if l:prevline !~ '\C\<else\>'
    let l:s = match(l:prevline, '\C\<if\>.*\&.*\zs\<then\>')
    if l:s > 0
      return l:s
    endif

    let l:s = match(l:prevline, '\C\<if\>')
    if l:s > 0
      return l:s + g:haskell_indent_if
    endif
  endif

  " where
  " >>foo
  "
  if l:prevline =~ '\C\<where\>\s*$'
    return indent(v:lnum - 1) + get(g:, 'haskell_indent_after_bare_where', &shiftwidth)
  endif

  " do
  " >>foo
  "
  " foo =
  " >>bar
  if l:prevline =~ '\C\(\<do\>\|=\)\s*$'
    return indent(v:lnum - 1) + &shiftwidth
  endif

  " do foo
  " >>>bar
  if l:prevline =~ '\C\<do\>\s\+\S\+.*$'
    let l:s = match(l:prevline, '\C\<do\>')
    if s:isSYN('haskellKeyword', v:lnum - 1, l:s + 1)
      return l:s + g:haskell_indent_do
    endif
  endif

  " case foo of
  " >>bar -> quux
  if l:prevline =~ '\C\<case\>.\+\<of\>\s*$'
    if get(g:,'haskell_indent_case_alternative', 0)
      return indent(v:lnum - 1) + &shiftwidth
    else
      return match(l:prevline, '\C\<case\>') + g:haskell_indent_case
    endif
  endif

  "" where foo
  "" >>>>>>bar
  ""
  "" where foo :: Int
  "" >>>>>>>>>>-> Int
  ""
  "" where foo x
  "" >>>>>>>>|
  if l:prevline =~ '\C\<where\>\s\+\S\+.*$'
    if  l:line =~ '^\s*[=-]>\s' && l:prevline =~ ' :: '
      return match(l:prevline, ':: ')
    elseif  l:line =~ '^\s*|\s'
      let l:s = match(l:prevline, '\C\<where\>')
      if s:isSYN('haskellWhere', v:lnum - 1, l:s + 1)
        return l:s + g:haskell_indent_where + g:haskell_indent_guard
      endif
    else
      let l:s = match(l:prevline, '\C\<where\>')
      if s:isSYN('haskellWhere', v:lnum - 1, l:s + 1)
        return l:s + g:haskell_indent_where
      endif
    endif
  endif

  " newtype Foo = Foo
  " >>deriving
  if l:prevline =~ '\C^\s*\<\(newtype\|data\)\>[^{]\+' && l:line =~ '\C^\s*\<deriving\>'
    return indent(v:lnum - 1) + &shiftwidth
  endif

  " foo :: Int
  " >>>>-> Int
  "
  " foo
  "   :: Int
  " foo
  if l:prevline =~ '\s::\s'
    if l:line =~ '^\s*[-=]>'
      return match(l:prevline, '::\s')
    elseif match(l:prevline, '^\s\+::') > -1
      return match(l:prevline, '::\s') - &shiftwidth
    endif
  endif

  " foo :: Int
  "     -> Int
  " >>>>-> Int
  "
  " foo :: Monad m
  "     => Functor f
  " >>>>=> Int
  "
  " foo :: Int
  "     -> Int
  " foo x
  "
  " foo
  "   :: Int
  "   -> Int
  " foo x
  if l:prevline =~ '^\s*[-=]>'
    if l:line =~ '^\s*[-=]>'
      return match(l:prevline, '[-=]')
    else
      if s:isInBlock(l:hlstack)
        return match(l:prevline, '[^-=]')
      else
        let l:m = matchstr(l:line, '^\s*\zs\<\S\+\>\ze')
        let l:l = l:prevline
        let l:c = v:lnum - 1

        while l:c >= 1
          " fun decl
          if l:l =~ ('^\s*' . l:m . '\(\s*::\|\n\s\+::\)')
            let l:s = match(l:l, l:m)
            if match(l:l, '\C^\s*\<default\>') > -1
              return l:s - 8
            else
              return l:s
            endif
          " empty line, stop looking
          elseif l:l =~ '^$'
             return 0
          endif
          let l:c -= 1
          let l:l = getline(l:c)
        endwhile

        return 0
      endif
    endif
  endif

  "   | otherwise = ...
  " foo
  "
  "   | foo
  " >>, bar
  "
  "   | foo
  " >>= bar
  "
  "   | Foo
  " >>deriving
  if l:prevline =~ '^\s\+|' && !s:isInBlock(l:hlstack)
    if l:line =~ '\s*[,=]'
      return match(l:prevline, '|')
    elseif l:line =~ '\C^\s*\<deriving\>'
      return match(l:prevline, '|')
    elseif l:line !~ '^\s*|'
      return match(l:prevline, '|') - g:haskell_indent_guard
    endif
  endif

  " foo :: ( Monad m
  "        , Functor f
  "        )
  ">>>>>=> Int
  if l:prevline =~ '^\s*)' && l:line =~ '^\s*=>'
    let l:s = match(l:prevline, ')')
    return l:s - (&shiftwidth + 1)
  endif

  " module Foo
  " >>( bar
  if l:prevline =~ '\C^\<module\>'
    return &shiftwidth
  endif

  " foo
  " >>{
  if l:line =~ '^\s*{'
    let l:s = indent(v:lnum - 1)
    if l:s >= 0
      return l:s + &shiftwidth
    endif
  endif

  "  in foo
  " where bar
  "
  " or
  "
  " foo
  " >>where
  if l:line =~ '\C^\s*\<where\>'
    if match(l:prevline, '\C^\s\+in\s\+') == 0
      return match(l:prevline, 'in') - g:haskell_indent_in
    endif

    return indent(v:lnum - 1) + get(g:, 'haskell_indent_before_where', &shiftwidth)
  endif

  " let x = 1
  "     y = 2
  " >in x + 1
  if l:line =~ '\C^\s*\<in\>'
    let l:s = 0
    let l:c = v:lnum - 1

    while l:s <= 0 && l:c >= 1
      let l:l = getline(l:c)
      let l:s = match(l:l, '\C\<let\>')
      if l:s >= 1 && s:isSYN('haskellLet', l:c, l:s + 1)
        break
      elseif l:l =~ '^\S'
        return -1
      endif
      let l:c -= 1
    endwhile
    return l:s + g:haskell_indent_in
  endif

  " data Foo
  " >>= Bar
  "
  "   |
  "   ...
  " >>=
  "
  " foo
  " >>=
  if l:line =~ '^\s*='
    if l:prevline =~ '\C^\<data\>\s\+[^=]\+\s*$'
      return match(l:prevline, '\C\<data\>') + &shiftwidth
    else
      let l:s = s:indentGuard(match(l:line, '='), l:prevline)
      if l:s > 0
        return l:s
      else
        return &shiftwidth
      endif
    endif
  endif

  "   |
  "   ...
  " >>|
  "
  " data Foo = Bar
  " >>>>>>>>>|
  if l:line =~ '^\s*|\s'
    if l:prevline =~ '\C^\s*\<data\>.\+=.\+$'
      return match(l:prevline, '=')
    else
      let l:s = s:indentGuard(match(l:line, '|'), l:prevline)
      if l:s > -1
        return l:s
      endif
    endif
  endif

  " foo
  " >>:: Int
  if l:line =~ '^\s*::\s'
    return indent(v:lnum - 1) + &shiftwidth
  endif

  " indent closing brace, paren or bracket
  if l:line =~ '^\s*}'
    return s:indentMatching('}')
  endif

  if l:line =~ '^\s*)'
    return s:indentMatching(')')
  endif

  if l:line =~ '^\s*]'
    return s:indentMatching(']')
  endif

  return -1
endfunction