Skip to content
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
22 changes: 17 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,33 +31,45 @@ Be sure that the following lines are in your

To configure indentation in `purescript-vim` you can use the following variables:

* `let g:purescript_indent_if = 3`
* `let purescript_indent_if = 3`

if bool
>>>then ...
>>>else ...

* `let g:purescript_indent_case = 5`
* `let purescript_indent_case = 5`

case xs of
>>>>>[] -> ...
>>>>>(y:ys) -> ...

* `let g:purescript_indent_let = 4`
* `let purescript_indent_let = 4`

let x = 0 in
>>>>x

* `let g:purescript_indent_where = 6`
* `let purescript_indent_where = 6`

where f :: Int -> Int
>>>>>>f x = x

* `let g:purescript_indent_do = 3`
* `let purescript_indent_do = 3`

do x <- a
>>>y <- b

* `let purescript_indent_in = 1`

let x = 0
>in x

* `let purescript_indent_dot = v:true`

unsnoc
:: forall a
>. List a
-> Maybe (List a, a)


[Purescript]: http://www.purescript.org
[Pathogen]: https://github.com/tpope/vim-pathogen
Expand Down
31 changes: 26 additions & 5 deletions indent/purescript.vim
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,10 @@ setlocal indentexpr=GetPurescriptIndent()
setlocal indentkeys=!^F,o,O,},=where,=in,=::,=->,==>

function! GetPurescriptIndent()
let ppline = getline(v:lnum - 2)
let prevline = getline(v:lnum - 1)
let line = getline(v:lnum)
let synStackP = map(synstack(v:lnum - 1, col(".")), { key, val -> synIDattr(val, "name") })

if line =~ '^\s*\<where\>'
let s = match(prevline, '\S')
Expand All @@ -85,17 +87,35 @@ function! GetPurescriptIndent()
return s + g:purescript_indent_in
endif

let s = match(prevline, '^\s*\zs\(--\|import\>\)')
let s = match(prevline, '^\s*\zs\(--\|import\)')
if s >= 0
" comments
" imports
return s
endif

if prevline =~ '^\S.*::' && line !~ '^\s*\(\.\|->\|=>\)' && !~ '^instance'
" f :: String
" -> String
return 0
endif

let s = match(prevline, '[[:alnum:][:blank:]]\@<=|[[:alnum:][:blank:]$]')
if s >= 0 && index(synStackP, "purescriptFunctionDecl") == -1
" ident pattern quards but not if we are in a type declaration
" what we detect using syntax groups
return s
endif

if prevline =~ '^\S'
" starting type signature or function body on next line
echom "xxx " . prevline
" starting type signature, function body, data & newtype on next line
return &shiftwidth
endif

if ppline =~ '^\S' && prevline =~ '^\s*$'
return 0
endif

if line =~ '^\s*::'
return match(prevline, '\S') + &shiftwidth
endif
Expand All @@ -107,7 +127,6 @@ function! GetPurescriptIndent()
let s = match(prevline, '^\s*\zs\%(::\|=>\|->\)')
let r = match(prevline, '^\s*\zs\.')
if s >= 0 || r >= 0
echom prevline
if s >= 0
if line !~ '^\s*\%(::\|=>\|->\)'
return s - 2
Expand Down Expand Up @@ -138,6 +157,7 @@ function! GetPurescriptIndent()
endif

if prevline =~ '[{([][^})\]]\+$'
echom "return 1"
return match(prevline, '[{([]')
endif

Expand All @@ -162,6 +182,7 @@ function! GetPurescriptIndent()
endif

if prevline =~ '[{([]\s*$'
echom "return 2"
return match(prevline, '\S') + (line !~ '^\s*[})]]' ? 0 : &shiftwidth)
endif

Expand All @@ -185,7 +206,7 @@ function! GetPurescriptIndent()
return match(prevline, '\<data\>') + &shiftwidth
endif

if (line =~ '^\s*}\s*' && prevline !~ '^\s*;')
if prevline =~ '^\s*[}\]]'
return match(prevline, '\S') - &shiftwidth
endif

Expand Down
20 changes: 16 additions & 4 deletions syntax/purescript.vim
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,27 @@ syn match purescriptImportParams "hiding" contained
\ nextgroup=purescriptModuleParams,purescriptImportParams skipwhite

" Function declaration
syn region purescriptFunctionDecl excludenl start="^\z(\s*\)\(foreign\s\+import\_s\+\)\?[_a-z]\(\w\|\'\)*\_s\{-}\(::\|∷\)" end="^\z1\=\S"me=s-1,re=s-1 keepend
syn region purescriptFunctionDecl
\ excludenl start="^\z(\s*\)\(\(foreign\s\+import\)\_s\+\)\?[_a-z]\(\w\|\'\)*\_s\{-}\(::\|∷\)"
\ end="^\z1\=\S"me=s-1,re=s-1 keepend
\ contains=purescriptFunctionDeclStart,purescriptForall,purescriptOperatorType,purescriptOperatorTypeSig,purescriptType,purescriptTypeVar,purescriptDelimiter,@purescriptComment
syn match purescriptFunctionDeclStart "^\s*\(foreign\s\+import\_s\+\)\?\([_a-z]\(\w\|\'\)*\)\_s\{-}\(::\|∷\)" contained
\ contains=purescriptImportKeyword,purescriptFunction,purescriptOperatorType
syn region purescriptFunctionDecl
\ excludenl start="^\z(\s*\)where\z(\s\+\)[_a-z]\(\w\|\'\)*\_s\{-}\(::\|∷\)"
\ end="^\(\z1\s\{5}\z2\)\=\S"me=s-1,re=s-1 keepend
\ contains=purescriptFunctionDeclStart,purescriptForall,purescriptOperatorType,purescriptOperatorTypeSig,purescriptType,purescriptTypeVar,purescriptDelimiter,@purescriptComment
syn region purescriptFunctionDecl
\ excludenl start="^\z(\s*\)let\z(\s\+\)[_a-z]\(\w\|\'\)*\_s\{-}\(::\|∷\)"
\ end="^\(\z1\s\{3}\z2\)\=\S"me=s-1,re=s-1 keepend
\ contains=purescriptFunctionDeclStart,purescriptForall,purescriptOperatorType,purescriptOperatorTypeSig,purescriptType,purescriptTypeVar,purescriptDelimiter,@purescriptComment
syn match purescriptFunctionDeclStart "^\s*\(\(foreign\s\+import\|let\|where\)\_s\+\)\?\([_a-z]\(\w\|\'\)*\)\_s\{-}\(::\|∷\)" contained
\ contains=purescriptImportKeyword,purescriptWhere,purescriptLet,purescriptFunction,purescriptOperatorType
syn keyword purescriptForall forall
syn match purescriptForall "∀"

" Keywords
syn keyword purescriptConditional if then else
syn keyword purescriptStatement do case of let in
syn keyword purescriptStatement do case of in
syn keyword purescriptLet let
syn keyword purescriptWhere where
syn match purescriptStructure "\<\(data\|newtype\|type\|class\|kind\)\>"
\ nextgroup=purescriptType skipwhite
Expand Down Expand Up @@ -166,6 +177,7 @@ highlight def link purescriptBlockComment purescriptComment
highlight def link purescriptStructure purescriptKeyword
highlight def link purescriptKeyword Keyword
highlight def link purescriptStatement Statement
highlight def link purescriptLet Statement
highlight def link purescriptOperator Operator
highlight def link purescriptFunction Function
highlight def link purescriptType Type
Expand Down