Skip to content

Fixed indentation for nested if-then-else #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions indent/haskell.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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<bar>,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<bar>,0},0],0(,0),0#,0,0==

let s:save_cpo = &cpo
set cpo&vim
Expand Down Expand Up @@ -55,6 +55,11 @@ function! GetHaskellIndent() abort
return 0
endif

" then
if line =~# '\v^\s*<then>'
return s:indent_then()
endif

" else
if line =~# '\v^\s*<else>'
return s:indent_else()
Expand Down Expand Up @@ -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<if>', '', '\v<then>\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<if>\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<then>'
return match(line, '\v<then>')
endif
let i = s:prevnonblank(i - 1)
endwhile
let [lnum, col] = searchpairpos('\v<if>', '\v<then>', '\v<else>\zs', 'bnW')
return col - 1
endfunction

" |
Expand Down
5 changes: 5 additions & 0 deletions test/if/ifthenelse_nested.in.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
if foo
then if bar
then baz
else qux
else quux
5 changes: 5 additions & 0 deletions test/if/ifthenelse_nested.out.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
if foo
then if bar
then baz
else qux
else quux