Skip to content

Commit 8e84790

Browse files
Merge pull request #20 from coot/indentation
Fix indentation and let/where syntax.
2 parents f8bfe63 + 8640409 commit 8e84790

File tree

3 files changed

+59
-14
lines changed

3 files changed

+59
-14
lines changed

README.md

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,33 +31,45 @@ Be sure that the following lines are in your
3131

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

34-
* `let g:purescript_indent_if = 3`
34+
* `let purescript_indent_if = 3`
3535

3636
if bool
3737
>>>then ...
3838
>>>else ...
3939

40-
* `let g:purescript_indent_case = 5`
40+
* `let purescript_indent_case = 5`
4141

4242
case xs of
4343
>>>>>[] -> ...
4444
>>>>>(y:ys) -> ...
4545

46-
* `let g:purescript_indent_let = 4`
46+
* `let purescript_indent_let = 4`
4747

4848
let x = 0 in
4949
>>>>x
5050

51-
* `let g:purescript_indent_where = 6`
51+
* `let purescript_indent_where = 6`
5252

5353
where f :: Int -> Int
5454
>>>>>>f x = x
5555

56-
* `let g:purescript_indent_do = 3`
56+
* `let purescript_indent_do = 3`
5757

5858
do x <- a
5959
>>>y <- b
6060

61+
* `let purescript_indent_in = 1`
62+
63+
let x = 0
64+
>in x
65+
66+
* `let purescript_indent_dot = v:true`
67+
68+
unsnoc
69+
:: forall a
70+
>. List a
71+
-> Maybe (List a, a)
72+
6173

6274
[Purescript]: http://www.purescript.org
6375
[Pathogen]: https://github.com/tpope/vim-pathogen

indent/purescript.vim

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,10 @@ setlocal indentexpr=GetPurescriptIndent()
6565
setlocal indentkeys=!^F,o,O,},=where,=in,=::,=->,==>
6666

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

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

88-
let s = match(prevline, '^\s*\zs\(--\|import\>\)')
90+
let s = match(prevline, '^\s*\zs\(--\|import\)')
8991
if s >= 0
92+
" comments
93+
" imports
94+
return s
95+
endif
96+
97+
if prevline =~ '^\S.*::' && line !~ '^\s*\(\.\|->\|=>\)' && !~ '^instance'
98+
" f :: String
99+
" -> String
100+
return 0
101+
endif
102+
103+
let s = match(prevline, '[[:alnum:][:blank:]]\@<=|[[:alnum:][:blank:]$]')
104+
if s >= 0 && index(synStackP, "purescriptFunctionDecl") == -1
105+
" ident pattern quards but not if we are in a type declaration
106+
" what we detect using syntax groups
90107
return s
91108
endif
92109

93110
if prevline =~ '^\S'
94-
" starting type signature or function body on next line
95-
echom "xxx " . prevline
111+
" starting type signature, function body, data & newtype on next line
96112
return &shiftwidth
97113
endif
98114

115+
if ppline =~ '^\S' && prevline =~ '^\s*$'
116+
return 0
117+
endif
118+
99119
if line =~ '^\s*::'
100120
return match(prevline, '\S') + &shiftwidth
101121
endif
@@ -107,7 +127,6 @@ function! GetPurescriptIndent()
107127
let s = match(prevline, '^\s*\zs\%(::\|=>\|->\)')
108128
let r = match(prevline, '^\s*\zs\.')
109129
if s >= 0 || r >= 0
110-
echom prevline
111130
if s >= 0
112131
if line !~ '^\s*\%(::\|=>\|->\)'
113132
return s - 2
@@ -138,6 +157,7 @@ function! GetPurescriptIndent()
138157
endif
139158

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

@@ -162,6 +182,7 @@ function! GetPurescriptIndent()
162182
endif
163183

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

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

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

syntax/purescript.vim

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,27 @@ syn match purescriptImportParams "hiding" contained
6363
\ nextgroup=purescriptModuleParams,purescriptImportParams skipwhite
6464

6565
" Function declaration
66-
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
66+
syn region purescriptFunctionDecl
67+
\ excludenl start="^\z(\s*\)\(\(foreign\s\+import\)\_s\+\)\?[_a-z]\(\w\|\'\)*\_s\{-}\(::\|\)"
68+
\ end="^\z1\=\S"me=s-1,re=s-1 keepend
6769
\ contains=purescriptFunctionDeclStart,purescriptForall,purescriptOperatorType,purescriptOperatorTypeSig,purescriptType,purescriptTypeVar,purescriptDelimiter,@purescriptComment
68-
syn match purescriptFunctionDeclStart "^\s*\(foreign\s\+import\_s\+\)\?\([_a-z]\(\w\|\'\)*\)\_s\{-}\(::\|\)" contained
69-
\ contains=purescriptImportKeyword,purescriptFunction,purescriptOperatorType
70+
syn region purescriptFunctionDecl
71+
\ excludenl start="^\z(\s*\)where\z(\s\+\)[_a-z]\(\w\|\'\)*\_s\{-}\(::\|\)"
72+
\ end="^\(\z1\s\{5}\z2\)\=\S"me=s-1,re=s-1 keepend
73+
\ contains=purescriptFunctionDeclStart,purescriptForall,purescriptOperatorType,purescriptOperatorTypeSig,purescriptType,purescriptTypeVar,purescriptDelimiter,@purescriptComment
74+
syn region purescriptFunctionDecl
75+
\ excludenl start="^\z(\s*\)let\z(\s\+\)[_a-z]\(\w\|\'\)*\_s\{-}\(::\|\)"
76+
\ end="^\(\z1\s\{3}\z2\)\=\S"me=s-1,re=s-1 keepend
77+
\ contains=purescriptFunctionDeclStart,purescriptForall,purescriptOperatorType,purescriptOperatorTypeSig,purescriptType,purescriptTypeVar,purescriptDelimiter,@purescriptComment
78+
syn match purescriptFunctionDeclStart "^\s*\(\(foreign\s\+import\|let\|where\)\_s\+\)\?\([_a-z]\(\w\|\'\)*\)\_s\{-}\(::\|\)" contained
79+
\ contains=purescriptImportKeyword,purescriptWhere,purescriptLet,purescriptFunction,purescriptOperatorType
7080
syn keyword purescriptForall forall
7181
syn match purescriptForall ""
7282

7383
" Keywords
7484
syn keyword purescriptConditional if then else
75-
syn keyword purescriptStatement do case of let in
85+
syn keyword purescriptStatement do case of in
86+
syn keyword purescriptLet let
7687
syn keyword purescriptWhere where
7788
syn match purescriptStructure "\<\(data\|newtype\|type\|class\|kind\)\>"
7889
\ nextgroup=purescriptType skipwhite
@@ -166,6 +177,7 @@ highlight def link purescriptBlockComment purescriptComment
166177
highlight def link purescriptStructure purescriptKeyword
167178
highlight def link purescriptKeyword Keyword
168179
highlight def link purescriptStatement Statement
180+
highlight def link purescriptLet Statement
169181
highlight def link purescriptOperator Operator
170182
highlight def link purescriptFunction Function
171183
highlight def link purescriptType Type

0 commit comments

Comments
 (0)