diff --git a/indent/haskell.vim b/indent/haskell.vim index 1567c34..1cb8435 100644 --- a/indent/haskell.vim +++ b/indent/haskell.vim @@ -12,7 +12,7 @@ endif let b:did_indent = 1 setlocal indentexpr=GetHaskellIndent() -setlocal indentkeys=!^F,o,O,=wher,=deri,==,0=in,0=class,0=instance,0=data,0=type,0=else,0,0},0],0(,0),0#,0,0== +setlocal indentkeys=!^F,o,O,=wher,=deri,==,0=in,0=class,0=instance,0=data,0=type,0=else,0=then,0,0},0],0(,0),0#,0,0== let s:save_cpo = &cpo set cpo&vim @@ -55,6 +55,11 @@ function! GetHaskellIndent() abort return 0 endif + " then + if line =~# '\v^\s*' + return s:indent_then() + endif + " else if line =~# '\v^\s*' return s:indent_else() @@ -454,16 +459,21 @@ function! s:indent_comment() abort return indent(s:prevnonblank(v:lnum - 1)) endfunction +" then +function! s:indent_then() abort + let [lnum, col] = searchpairpos('\v', '', '\v\zs', 'bnW') + if lnum == 0 && col == 0 + return -1 + else + " consider adding option to decide where to indent 'then' + return match(getline(lnum)[col - 1:], '\v\s*\zs') + col - 1 + endif +endfunction + " else function! s:indent_else() abort - let i = s:prevnonblank(v:lnum - 1) - while i > 0 - let line = getline(i) - if line =~# '\v' - return match(line, '\v') - endif - let i = s:prevnonblank(i - 1) - endwhile + let [lnum, col] = searchpairpos('\v', '\v', '\v\zs', 'bnW') + return col - 1 endfunction " | diff --git a/test/if/ifthenelse_nested.in.hs b/test/if/ifthenelse_nested.in.hs new file mode 100644 index 0000000..d975b7d --- /dev/null +++ b/test/if/ifthenelse_nested.in.hs @@ -0,0 +1,5 @@ +if foo +then if bar +then baz +else qux +else quux diff --git a/test/if/ifthenelse_nested.out.hs b/test/if/ifthenelse_nested.out.hs new file mode 100644 index 0000000..c345d09 --- /dev/null +++ b/test/if/ifthenelse_nested.out.hs @@ -0,0 +1,5 @@ +if foo + then if bar + then baz + else qux + else quux