Skip to content

Commit 65647b8

Browse files
authored
Fixed indentation for nested if-then-else (#35)
1 parent 5181edc commit 65647b8

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

indent/haskell.vim

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ endif
1212
let b:did_indent = 1
1313

1414
setlocal indentexpr=GetHaskellIndent()
15-
setlocal indentkeys=!^F,o,O,=wher,=deri,==,0=in,0=class,0=instance,0=data,0=type,0=else,0<bar>,0},0],0(,0),0#,0,0==
15+
setlocal indentkeys=!^F,o,O,=wher,=deri,==,0=in,0=class,0=instance,0=data,0=type,0=else,0=then,0<bar>,0},0],0(,0),0#,0,0==
1616

1717
let s:save_cpo = &cpo
1818
set cpo&vim
@@ -55,6 +55,11 @@ function! GetHaskellIndent() abort
5555
return 0
5656
endif
5757

58+
" then
59+
if line =~# '\v^\s*<then>'
60+
return s:indent_then()
61+
endif
62+
5863
" else
5964
if line =~# '\v^\s*<else>'
6065
return s:indent_else()
@@ -454,16 +459,21 @@ function! s:indent_comment() abort
454459
return indent(s:prevnonblank(v:lnum - 1))
455460
endfunction
456461

462+
" then
463+
function! s:indent_then() abort
464+
let [lnum, col] = searchpairpos('\v<if>', '', '\v<then>\zs', 'bnW')
465+
if lnum == 0 && col == 0
466+
return -1
467+
else
468+
" consider adding option to decide where to indent 'then'
469+
return match(getline(lnum)[col - 1:], '\v<if>\s*\zs') + col - 1
470+
endif
471+
endfunction
472+
457473
" else
458474
function! s:indent_else() abort
459-
let i = s:prevnonblank(v:lnum - 1)
460-
while i > 0
461-
let line = getline(i)
462-
if line =~# '\v<then>'
463-
return match(line, '\v<then>')
464-
endif
465-
let i = s:prevnonblank(i - 1)
466-
endwhile
475+
let [lnum, col] = searchpairpos('\v<if>', '\v<then>', '\v<else>\zs', 'bnW')
476+
return col - 1
467477
endfunction
468478

469479
" |

test/if/ifthenelse_nested.in.hs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
if foo
2+
then if bar
3+
then baz
4+
else qux
5+
else quux

test/if/ifthenelse_nested.out.hs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
if foo
2+
then if bar
3+
then baz
4+
else qux
5+
else quux

0 commit comments

Comments
 (0)