Skip to content

Commit

Permalink
support list and tuple pattern matching (close #21)
Browse files Browse the repository at this point in the history
  • Loading branch information
itchyny committed Aug 6, 2021
1 parent a6142bc commit 192d01e
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 6 deletions.
16 changes: 10 additions & 6 deletions indent/haskell.vim
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
" Filename: indent/haskell.vim
" Author: itchyny
" License: MIT License
" Last Change: 2021/08/04 19:27:55.
" Last Change: 2021/08/07 07:21:36.
" =============================================================================

if exists('b:did_indent')
Expand Down Expand Up @@ -91,7 +91,11 @@ function! GetHaskellIndent() abort
return 0
endif

if line =~# '\v^\s*,([^()[\]{}]*|\([^()[\]{}]*\)|\[[^()[\]{}]*\])*(\s*--.*)?$' && nonblankline =~# '\v^\s*,'
let noparen = '[^()[\]{}]'
let noparen = '%(' . noparen . '+|\(' . noparen . '*\)|\[' . noparen . '*\])'
let noparen = '%(' . noparen . '+|\(' . noparen . '*\)|\[' . noparen . '*\])*'

if line =~# '\v^\s*,' . noparen . '(\s*--.*)?$' && nonblankline =~# '\v^\s*,'
return match(nonblankline, '^\s*\zs,')
endif

Expand All @@ -106,15 +110,15 @@ function! GetHaskellIndent() abort
return match(nonblankline, '^\s*\zs--')
endif

if nonblankline =~# '\v^\s*}?[^()[\]{}]*[([{]([^()[\]{}]*|\([^()[\]{}]*\)|\[[^()[\]{}]*\])*[-+/*\$&<>,]?(\s*--.*)?$'
if nonblankline =~# '\v^\s*}?' . noparen . '[([{]' . noparen . '[-+/*\$&<>,]?(\s*--.*)?$'
if nonblankline =~# '\v[([{](\s*--.*)?$'
return match(nonblankline, '\v^\s*(<where>|.*<let>)?\s*\zs') + &shiftwidth
elseif nonblankline =~# '\v[-+/*\$&<>,](\s*--.*)?$'
return match(nonblankline, '\v^\s*}?[^()[\]{}]*(\[.*\|\s*\zs|[([{]\s*\zs)')
return match(nonblankline, '\v^\s*}?' . noparen . '(\[.*\|\s*\zs|[([{]\s*\zs)')
elseif nonblankline =~# '\v^[^[\]]*\[([^[\]]*|\[[^[\]]*\])*\|([^[\]]*|\[[^[\]]*\])*(\s*--.*)?$'
return match(nonblankline, '\v^[^[\]]*\[([^[\]]*|\[[^[\]]*\])*\zs\|')
else
return match(nonblankline, '\v^\s*}?[^()[\]{}]*\zs[([{]')
return match(nonblankline, '\v^\s*}?' . noparen . '\zs[([{]')
endif
endif

Expand Down Expand Up @@ -187,7 +191,7 @@ function! GetHaskellIndent() abort
return match(nonblankline, '\S')
else
return match(nonblankline, '\v^\s*(<where>|.*<let>)?\s*\zs') +
\ (nonblankline =~# '\v(<where>|<let>)|^\s*\k+\s*\=.*([-+/*\$&<>]|`\k+`)(\s*--.*)?$|(\=|-\>)(\s*--.*)?$' ? &shiftwidth : 0)
\ (nonblankline =~# '\v(<where>|<let>)|^\s*\k+\s*'. noparen .'\=.*([-+/*\$&<>]|`\k+`)(\s*--.*)?$|(\=|-\>)(\s*--.*)?$' ? &shiftwidth : 0)
endif
endif

Expand Down
9 changes: 9 additions & 0 deletions test/list/arg_list.in.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
f [x, y, [z, w]] = [
x, y,
[
z,
w,
]]

g [x, y] = x `div`
y
9 changes: 9 additions & 0 deletions test/list/arg_list.out.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
f [x, y, [z, w]] = [
x, y,
[
z,
w,
]]

g [x, y] = x `div`
y
11 changes: 11 additions & 0 deletions test/list/arg_tuple.in.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
f (x, y, (z, w)) = [
x, (
y
),
(
z,
w
)]

g (x, y) = x +
y
11 changes: 11 additions & 0 deletions test/list/arg_tuple.out.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
f (x, y, (z, w)) = [
x, (
y
),
(
z,
w
)]

g (x, y) = x +
y

0 comments on commit 192d01e

Please sign in to comment.