1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
|
if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'pony') == -1
" Vim plugin file
" Language: Pony
" Maintainer: Jak Wings
" TODO: Make sure echomsg is off for release.
let s:cpo_save = &cpo
set cpo&vim
"let s:skip = '<SID>InCommentOrLiteral(line("."), col("."))'
let s:skip2 = '<SID>InLiteral(line("."), col(".")) || <SID>InComment(line("."), col(".")) == 1'
let s:skip3 = '!<SID>InKeyword(line("."), col("."))'
let s:skip4 = '!<SID>InBracket(line("."), col("."))'
let s:cfstart = '\v<%(ifdef|if|match|while|for|repeat|try|with|recover|object|lambda|iftype)>'
let s:cfmiddle = '\v<%(then|elseif|else|until|do|in|elseiftype)>|\|'
let s:cfend = '\v<end>'
let s:bstartp = '\v<%(ifdef|if|then|elseif|else|(match)|while|for|in|do|try|with|recover|repeat|until|(object)|lambda|iftype|elseiftype)>'
function! pony#Indent()
if v:lnum <= 1
return 0
endif
call cursor(v:lnum, 1)
let l:pnzpos = searchpos('.', 'cbnW')
if l:pnzpos == [0, 0]
return 0
endif
if s:InComment2(l:pnzpos) > 1
"echomsg 'Comment' (l:pnzpos[0] . '-' . v:lnum) -1
return cindent(v:lnum)
endif
if s:InLiteral2(l:pnzpos)
"echomsg 'String' (l:pnzpos[0] . '-' . v:lnum) -1
return -1
endif
unlet! l:pnzpos
" NOTE: Lines started in comments and strings are checked already.
let l:pnblnum = s:PrevNonblank(v:lnum - 1)
if l:pnblnum < 1
return 0
endif
let l:pnbline = getline(l:pnblnum)
let l:pnbindent = indent(l:pnblnum)
let l:line = getline(v:lnum)
let l:indent = l:pnbindent
let l:shiftwidth = shiftwidth()
" FIXME?
let l:continuing = 0
" If the previous line ends with a unary or binary operator,
if s:IsContinued(l:pnblnum)
let l:contlnum = l:pnblnum
let l:ppcontinued = 0
let l:ppnblnum = s:PrevNonblank(l:pnblnum - 1)
while s:IsContinued(l:ppnblnum)
let l:ppcontinued += 1
let l:contlnum = l:ppnblnum
let l:ppnblnum = s:PrevNonblank(l:ppnblnum - 1)
endwhile
"echomsg 'Continued1' l:pnblnum l:contlnum
" If the previous line is also continuing another line,
if l:ppcontinued
let l:continuing = 1
if getline(l:contlnum) =~# '\v^\s*%(actor|class|struct|primitive|trait|type|interface)>'
" reset the indent level.
"echomsg 'Continuing0' (l:contlnum . '-' . v:lnum) (l:shiftwidth * 2)
let l:indent = l:shiftwidth * 2
else
" keep using the previous indent.
"echomsg 'Continuing1' (l:pnblnum . '-' . v:lnum) l:pnbindent
let l:indent = l:pnbindent
endif
" if the previous line is part of the definition of a class,
elseif l:pnbline =~# '\v^\s*%(actor|class|struct|primitive|trait|type|interface)>'
" reset the indent level.
"echomsg 'Continuing2' (l:pnblnum . '-' . v:lnum) (l:shiftwidth * 2)
let l:continuing = 1
let l:indent = l:shiftwidth * 2
" if the previous line is part of the definition of a method,
elseif l:pnbline =~# '\v^\s*%(fun|new|be)>'
" reset the indent level.
"echomsg 'Continuing3' (l:pnblnum . '-' . v:lnum) (l:pnbindent + l:shiftwidth)
let l:continuing = 1
let l:indent = l:pnbindent + l:shiftwidth
" if the previous line is the start of a definition body,
elseif l:pnbline =~# '=>\s*$'
" indent this line.
"echomsg 'Continuing4' (l:pnblnum . '-' . v:lnum) (l:pnbindent + l:shiftwidth)
let l:continuing = 1
let l:indent = l:pnbindent + l:shiftwidth
else
" indent this line twice as far.
"echomsg 'Continuing5' (l:pnblnum . '-' . v:lnum) (l:pnbindent + l:shiftwidth * 2)
let l:continuing = 1
let l:indent = l:pnbindent + l:shiftwidth * 2
endif
unlet! l:contlnum l:ppnblnum l:ppcontinued
endif
" If this line starts a document string,
if !l:continuing && l:line =~# '^\s*"""'
let l:ppnblnum = s:PrevNonblank(l:pnblnum - 1)
if s:IsContinued(l:ppnblnum)
let l:contlnum = l:ppnblnum
while s:IsContinued(l:ppnblnum)
let l:contlnum = l:ppnblnum
let l:ppnblnum = s:PrevNonblank(l:ppnblnum - 1)
endwhile
if getline(l:contlnum) =~# '\v^\s*%(actor|class|struct|primitive|trait|type|interface)>'
" reset the indent level.
"echomsg 'DocString' (l:contlnum . '-' . v:lnum) l:shiftwidth
return l:shiftwidth
endif
endif
unlet! l:contlnum l:ppnblnum
endif
" If the previous line contains an unmatched opening bracket
if !l:continuing && l:pnbline =~# '[{[(]'
" if the line ends with an opening bracket,
if l:pnbline =~# '[{[(]\s*$' && !s:InCommentOrLiteral(l:pnblnum, col([l:pnblnum, '$']) - 1)
" indent this line.
let l:indent += l:shiftwidth
else
" find the unmatched opening bracket,
let l:start = [0, 0]
let l:end = col([l:pnblnum, '$']) - 1
call cursor(l:pnblnum, l:end)
while l:end > 0
let l:start = s:OuterPos(l:start, searchpairpos('(', '', ')', 'bnW', s:skip4, l:pnblnum))
let l:start = s:OuterPos(l:start, searchpairpos('\[', '', '\]', 'bnW', s:skip4, l:pnblnum))
let l:start = s:OuterPos(l:start, searchpairpos('{', '', '}', 'bnW', s:skip4, l:pnblnum))
if l:start == [0, 0]
break
endif
" find the matched closing bracket on the same line,
call cursor(l:start[0], l:start[1])
let l:c = s:CharAtCursor(l:start[0], l:start[1])
if searchpair(escape(l:c, '['), '', escape(tr(l:c, '([{', ')]}'), ']'),
\ 'znW', s:skip4, l:pnblnum) < 1
" the unmatched opening bracket is found,
break
endif
let l:end = l:start[1]
let l:start = [0, 0]
endwhile
if l:start != [0, 0]
" indent this line.
"echomsg 'Open bracket' (l:pnblnum . '-' . v:lnum) (l:indent + l:shiftwidth)
let l:indent += l:shiftwidth
endif
endif
unlet! l:start l:end l:c
endif
" If there is a matched closing bracket on the previous line,
" NOTE:
" >|[
" | (1 -
" | 1) * 2]
" | command
" ^
if !l:continuing
call cursor(l:pnblnum, 1)
" find the last closing bracket,
let l:end = [0, 0]
let l:end = s:OuterPos(l:end, searchpairpos('(', '', ')', 'zncr', s:skip4, l:pnblnum))
let l:end = s:OuterPos(l:end, searchpairpos('\[', '', '\]', 'zncr', s:skip4, l:pnblnum))
let l:end = s:OuterPos(l:end, searchpairpos('{', '', '}', 'zncr', s:skip4, l:pnblnum))
if l:end != [0, 0]
" find the matched opening bracket on another line,
let l:c = s:CharAtCursor(l:end[0], l:end[1])
let l:start = searchpairpos(escape(tr(l:c, ')]}', '([{'), '['), '', escape(l:c, ']'), 'nbW', s:skip4)
if l:start[0] != l:end[0]
" and then this line has the same indent as the line the matched bracket stays.
"echomsg 'Matched bracket' (l:start[0] . '-' . v:lnum) indent(l:start[0])
let l:indent = indent(l:start[0])
endif
endif
unlet! l:start l:end l:c
endif
" If there is a matched closing bracket on this line,
" NOTE:
" |[
" >| (1 -
" | 1) * 2
" |]
" ^ ^
if l:line =~# '^\s*[)\]}]'
" find the first closing bracket,
call cursor(v:lnum, 1)
let l:end = [0, 0]
let l:end = s:InnerPos(l:end, searchpairpos('(', '', ')', 'zncW', s:skip4, v:lnum))
let l:end = s:InnerPos(l:end, searchpairpos('\[', '', '\]', 'zncW', s:skip4, v:lnum))
let l:end = s:InnerPos(l:end, searchpairpos('{', '', '}', 'zncW', s:skip4, v:lnum))
if l:end != [0, 0]
" find the matched opening bracket on another line,
let l:c = s:CharAtCursor(l:end[0], l:end[1])
let l:start = searchpairpos(escape(tr(l:c, ')]}', '([{'), '['), '', escape(l:c, ']'), 'nbW', s:skip4)
if l:start[0] != l:end[0]
" and then this line has the same indent as the line the matched bracket stays.
"echomsg 'Closing Bracket' (l:start[0] . '-' . v:lnum) indent(l:start[0])
let l:indent = indent(l:start[0])
endif
endif
unlet! l:start l:end l:c
endif
" If this line starts the definition of a method, closure or match case,
if l:line =~# '^\s*=>'
" find the start of the definition,
call cursor(v:lnum, 1)
let l:start = searchpairpos('\v<%(new|be|fun|lambda)>|\|', '', '=>\zs', 'bnW', s:skip3)
if l:start != [0, 0]
" then this line has the same indent as the start.
"echomsg 'Method body' (l:start[0] . '-' . v:lnum) indent(l:start[0])
return indent(l:start[0])
endif
unlet! l:start
endif
" If this line starts a class definition or starts an alias,
if l:line =~# '\v^\s*%(actor|class|struct|primitive|trait|interface|use|type)>'
" reset the indent level.
return 0
endif
" If this line starts a method definition,
if l:line =~# '\v^\s*%(new|be|fun)>'
call cursor(v:lnum, 1)
let l:start = searchpairpos(s:cfstart, s:cfmiddle, s:cfend, 'bW', s:skip3)
if l:start != [0, 0]
let l:start = searchpos(s:bstartp, 'zcnpW', l:start[0])
" see if it is in an object block,
if l:start[2] == 3
"echomsg 'Method in object' (l:start[0] . '-' . v:lnum) (l:shiftwidth + indent(l:start[0]))
return l:shiftwidth + indent(l:start[0])
endif
endif
return l:shiftwidth
endif
" If this line starts a match case,
call cursor(v:lnum, 1)
if l:line =~# '^\s*|' && s:InKeyword(searchpos('|', 'znW', v:lnum))
" find the start or the previous case of the match block,
let l:start = searchpairpos(s:cfstart, s:cfmiddle, s:cfend, 'bnW', s:skip3)
if l:start != [0, 0]
" then this line has the same indent as the start.
"echomsg 'Match case' (l:start[0] . '-' . v:lnum) indent(l:start[0])
return indent(l:start[0])
endif
unlet! l:start
endif
" If this line ends (part of) a control flow,
if l:line =~# '\v^\s*%(end|elseif|else|then|in|do|until|elseiftype)>'
" find the start or middle of the control block,
call cursor(v:lnum, 1)
let l:start = searchpairpos(s:cfstart, s:cfmiddle, s:cfend, 'bnW', s:skip3)
if l:start != [0, 0]
" then this line has the same indent as the start.
"echomsg 'Block end' (l:start[0] . '-' . v:lnum) indent(l:start[0])
return indent(l:start[0])
endif
unlet! l:start
endif
" If the previous line starts a class definition,
if l:pnbline =~# '\v^\s*%(actor|class|struct|primitive|trait|type|interface)>'
" reset the indent level.
if s:IsContinued(l:pnblnum)
return l:shiftwidth * 2
else
return l:shiftwidth
endif
endif
" If the previous line starts a method definition,
if l:pnbline =~# '\v^\s*%(new|be|fun)>'
return l:pnbindent + l:shiftwidth
endif
" If the previous line starts (part of) a control flow,
call cursor(l:pnblnum, 1)
while 1
" find the start of the control block,
let l:start = searchpos(s:bstartp, 'zcepW', l:pnblnum)
if l:start[2] == 0
break
endif
if !s:InKeyword(l:start[0:1])
call cursor(l:pnblnum, l:start[1] + 3)
continue
endif
let l:index = l:start[2]
" find the end of the control block on the same line,
let l:end = searchpair(s:cfstart, '', s:cfend, 'znW', s:skip3, l:pnblnum)
" if the control block is not ended,
if l:end < 1
" if this line is a case for a match,
if l:index == 2 && l:line =~# '^\s*|'
" then this line has the same indent as the start of the match block.
return l:pnbindent
else
" then indent this line.
"echomsg 'Block start' (l:pnblnum . '-' . v:lnum) (l:pnbindent + l:shiftwidth)
return l:pnbindent + l:shiftwidth
endif
endif
endwhile
unlet! l:start l:end l:index
return l:indent
endfunction
function! s:PrevNonblank(lnum)
let l:lnum = prevnonblank(a:lnum)
while l:lnum > 0 && (s:InComment2(l:lnum, 1) || s:InLiteral2(l:lnum, 1))
let l:lnum = prevnonblank(l:lnum - 1)
endwhile
return l:lnum
endfunction
" NOTE:
" v
" |1 /* comment */
" |2
function! s:IsContinued(lnum)
let l:lnum = s:PrevNonblank(a:lnum)
if l:lnum < 1
return 0
endif
let l:line = getline(l:lnum)
let l:width = strwidth(substitute(l:line, '\s*$', '', ''))
" FIXME?
" | 1 + //
" | //
" | 2
return !s:InCommentOrLiteral(a:lnum, l:width)
\ && (l:line =~# '\v<%(and|or|xor|is|isnt|as|not|consume|addressof|digestof)\s*$'
\ || l:line =~# '\v%([=\-.]\>|[<!=>]\=\~?|\<\<\~?|\>\>\~?|\<:|[+\-*/%<>]\~?|[.,|:@~])\s*$'
\ )
endfunction
function! s:InCommentOrLiteral(...)
return call('s:InComment', a:000) || call('s:InLiteral', a:000)
endfunction
function! s:InKeyword(...)
let [l:lnum, l:col] = (type(a:1) == type([]) ? a:1 : a:000)
for id in s:Or(synstack(l:lnum, l:col), [])
if synIDattr(id, 'name') =~# '^ponyKw'
return 1
endif
endfor
return 0
endfunction
function! s:InBracket(...)
let [l:lnum, l:col] = (type(a:1) == type([]) ? a:1 : a:000)
for id in s:Or(synstack(l:lnum, l:col), [])
if synIDattr(id, 'name') ==# 'ponyBracket'
return 1
endif
endfor
return 0
endfunction
function! s:InComment(...)
let [l:lnum, l:col] = (type(a:1) == type([]) ? a:1 : a:000)
let l:stack = synstack(l:lnum, l:col)
let l:i = len(l:stack)
while l:i > 0
let l:sname = synIDattr(l:stack[l:i - 1], 'name')
if l:sname =~# '^ponyNestedCommentX\?$'
return 1 + l:i - (l:sname =~# 'X$')
elseif l:sname =~# '^ponyCommentX\?$'
return 1
endif
let l:i -= 1
endwhile
return 0
endfunction
function! s:InLiteral(...)
let [l:lnum, l:col] = (type(a:1) == type([]) ? a:1 : a:000)
let l:stack = synstack(l:lnum, l:col)
let l:i = len(l:stack)
while l:i > 0
let l:sname = synIDattr(l:stack[l:i - 1], 'name')
if l:sname =~# '^ponyDocumentStringX\?$'
return 3
elseif l:sname =~# '^ponyStringX\?$'
return 2
elseif l:sname =~# '^ponyCharacterX\?$'
return 1
endif
let l:i -= 1
endwhile
return 0
endfunction
" NOTE:
" |// //inside
" ^^^^^^^^^^
" |/* /*inside*/ */
" ^^^^^^^^^^^^^^
function! s:InComment2(...)
let [l:lnum, l:col] = (type(a:1) == type([]) ? a:1 : a:000)
let l:stack = synstack(l:lnum, l:col)
let l:i = len(l:stack)
while l:i > 0
let l:sname = synIDattr(l:stack[l:i - 1], 'name')
if l:sname ==# 'ponyNestedComment'
return 1 + l:i
elseif l:sname ==# 'ponyComment'
return 1
elseif l:sname =~# '\v^pony%(Nested)?CommentX$'
return 0
endif
let l:i -= 1
endwhile
return 0
endfunction
" NOTE:
" |"inside"
" ^^^^^^
" |"""inside"""""
" ^^^^^^^^^^^^
function! s:InLiteral2(...)
let [l:lnum, l:col] = (type(a:1) == type([]) ? a:1 : a:000)
let l:stack = synstack(l:lnum, l:col)
let l:i = len(l:stack)
while l:i > 0
let l:sname = synIDattr(l:stack[l:i - 1], 'name')
if l:sname ==# 'ponyDocumentString'
return 3
elseif l:sname ==# 'ponyString'
return 2
elseif l:sname ==# 'ponyCharacter'
return 1
elseif l:sname =~# '\v^pony%(DocumentString|String|Character)X$'
return 0
endif
let l:i -= 1
endwhile
return 0
endfunction
function! s:CharAtCursor(...)
let [l:lnum, l:col] = (type(a:1) == type([]) ? a:1 : a:000)
return matchstr(getline(l:lnum), '\%' . l:col . 'c.')
endfunction
function! s:Or(x, y)
return !empty(a:x) ? a:x : a:y
endfunction
function! s:InnerPos(x, y)
if a:x == [0, 0]
return a:y
elseif a:y == [0, 0]
return a:x
else
return a:x[1] < a:y[1] ? a:x : a:y
end
endfunction
function! s:OuterPos(x, y)
if a:x == [0, 0]
return a:y
elseif a:y == [0, 0]
return a:x
else
return a:x[1] > a:y[1] ? a:x : a:y
end
endfunction
function! pony#ClearTrailingSpace(all, alt, ...)
let l:force = (a:0 > 0 ? a:1 : 0)
if !l:force && (&readonly || !&modifiable || !&modified)
return
endif
if a:all
for lnum in range(1, line('$'))
let l:line = getline(lnum)
let l:end = col([lnum, '$']) - 1
if l:end > 0 && l:line =~# '\s$' && !s:InLiteral(lnum, l:end)
if a:alt
call setline(lnum, substitute(l:line, '\S\@<=\s\s*$', '', ''))
else
call setline(lnum, substitute(l:line, '\s\+$', '', ''))
endif
endif
endfor
else
let l:lnum = line('.')
let l:end = col('$') - 1
let l:line = getline(l:lnum)
if l:line =~# '\s$' && !s:InLiteral(l:lnum, l:end)
if a:alt
call setline(l:lnum, substitute(l:line, '\s\+$', '', ''))
else
call setline(l:lnum, substitute(l:line, '\S\@<=\s\s*$', '', ''))
endif
endif
endif
endfunction
let &cpo = s:cpo_save
unlet s:cpo_save
endif
|