summaryrefslogtreecommitdiffstats
path: root/indent/ecrystal.vim
blob: ad3303c3b7f714cd0fc0b528bba3f12912ecd5b2 (plain) (blame)
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
if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'crystal') == -1

" Setup {{{1
" =====

if exists('b:did_indent')
  finish
endif

call ecrystal#SetSubtype()

if b:ecrystal_subtype !=# ''
  exec 'runtime! indent/'.b:ecrystal_subtype.'.vim'
  unlet! b:did_indent
endif

if &l:indentexpr ==# ''
  if &l:cindent
    let &l:indentexpr = 'cindent(v:lnum)'
  else
    let &l:indentexpr = 'indent(prevnonblank(v:lnum - 1))'
  endif
endif

let b:ecrystal_subtype_indentexpr = &l:indentexpr

" Should we use folding?
if has('folding') && get(g:, 'ecrystal_fold', 0)
  setlocal foldmethod=expr
  setlocal foldexpr=GetEcrystalFold()
endif

" Should closing control tags be aligned with their corresponding
" opening tags?
if !exists('b:ecrystal_align_end')
  if exists('g:ecrystal_align_end')
    let b:ecrystal_align_end = g:ecrystal_align_end
  else
    let b:ecrystal_align_end = b:ecrystal_subtype !=# 'html' && b:ecrystal_subtype !=# 'xml'
  endif
endif

" Should multiline tags be indented?
if !exists('b:ecrystal_indent_multiline')
  let b:ecrystal_indent_multiline = get(g:, 'ecrystal_indent_multiline', 1)
endif

if b:ecrystal_indent_multiline
  runtime! indent/crystal.vim
  unlet! b:did_indent
  setlocal indentexpr<
endif

setlocal indentexpr=GetEcrystalIndent()
setlocal indentkeys+=<>>,=end,=else,=elsif,=rescue,=ensure,=when,=in

let b:did_indent = 1

" Only define the function once.
if exists('*GetEcrystalIndent')
  finish
endif

" Helper variables and functions {{{1
" ==============================

let s:ecr_open = '<%%\@!'
let s:ecr_close = '%>'

let s:ecr_control_open = '<%%\@!-\=[=#]\@!'
let s:ecr_comment_open = '<%%\@!-\=#'

let s:ecr_indent_regex =
      \ '\<\%(if\|unless\|else\|elsif\|case\|when\|in\|while\|until\|begin\|do\|rescue\|ensure\|\)\>'

let s:ecr_dedent_regex =
      \ '\<\%(end\|else\|elsif\|when\|in\|rescue\|ensure\)\>'

" Return the value of a single shift-width
if exists('*shiftwidth')
  let s:sw = function('shiftwidth')
else
  function s:sw()
    return &shiftwidth
  endfunction
endif

" Does the given pattern match at the given position?
function! s:MatchAt(lnum, col, pattern) abort
  let idx = a:col - 1
  return match(getline(a:lnum), a:pattern, idx) == idx
endfunction

" Does the given pattern match at the cursor's position?
function! s:MatchCursor(pattern) abort
  return s:MatchAt(line('.'), col('.'), a:pattern)
endfunction

" Is the cell at the given position part of a tag? If so, return the
" position of the opening delimiter.
function! s:MatchECR(...) abort
  if a:0
    let lnum = a:1
    let col = a:2

    call cursor(lnum, col)
  endif

  let pos = getcurpos()

  try
    let flags = s:MatchCursor(s:ecr_open) ? 'bcWz' : 'bWz'

    let [open_lnum, open_col] = searchpairpos(
          \ s:ecr_open, '', s:ecr_close,
          \ flags, g:crystal#indent#skip_expr)
  finally
    call setpos('.', pos)
  endtry

  return [open_lnum, open_col]
endfunction

" If the cell at the given position is part of a control tag, return the
" respective positions of the opening and closing delimiters for that
" tag.
function! s:MatchECRControl(...) abort
  let pos = getcurpos()

  if a:0
    let lnum = a:1
    let col = a:2

    call cursor(lnum, col)
  else
    let [lnum, col] = [line('.'), col('.')]
  endif

  let open = { 'lnum': 0, 'col': 0 }
  let close = { 'lnum': 0, 'col': 0 }

  let [open.lnum, open.col] = s:MatchECR(lnum, col)

  if !open.lnum
    call setpos('.', pos)
    return [open, close]
  endif

  call cursor(open.lnum, open.col)

  if !s:MatchCursor(s:ecr_control_open)
    let open.lnum = 0
    let open.col = 0

    call setpos('.', pos)
    return [open, close]
  endif

  let [close.lnum, close.col] = searchpairpos(
        \ s:ecr_control_open, '', s:ecr_close,
        \ 'Wz', g:crystal#indent#skip_expr)

  call setpos('.', pos)
  return [open, close]
endfunction

" Determine whether or not the control tag at the given position starts
" an indent.
function! s:ECRIndent(...) abort
  if a:0
    if type(a:1) == 0
      let [open, close] = s:MatchECRControl(a:1, a:2)
    elseif type(a:1) == 4
      let [open, close] = [a:1, a:2]
    endif
  else
    let [open, close] = s:MatchECRControl()
  endif

  let result = 0

  if !open.lnum
    return result
  endif

  let pos = getcurpos()

  call cursor(open.lnum, open.col)

  " Find each Crystal keyword that starts an indent; if any of them do
  " not have a corresponding ending keyword, then this tag starts an
  " indent.
  while search(s:ecr_indent_regex, 'z', close.lnum)
    let [lnum, col] = [line('.'), col('.')]

    if lnum == close.lnum && col > close.col
      break
    endif

    if crystal#indent#IsInStringOrComment(lnum, col)
      continue
    endif

    let [end_lnum, end_col] = searchpairpos(
          \ g:crystal#indent#end_start_regex,
          \ g:crystal#indent#end_middle_regex,
          \ g:crystal#indent#end_end_regex,
          \ 'nz',
          \ g:crystal#indent#skip_expr,
          \ close.lnum)

    if end_lnum
      if end_lnum == close.lnum && end_col > close.col
        let result = 1
      endif
    else
      let result = 1
    endif

    if result
      break
    endif
  endwhile

  call setpos('.', pos)
  return result
endfunction

" Determine if the control tag at the given position ends an indent or
" not.
function! s:ECRDedent(...) abort
  if a:0
    if type(a:1) == 0
      let [open, close] = s:MatchECRControl(a:1, a:2)
    elseif type(a:1) == 4
      let [open, close] = [a:1, a:2]
    endif
  else
    let [open, close] = s:MatchECRControl()
  endif

  let result = 0

  if !open.lnum
    return result
  endif

  let pos = getcurpos()

  call cursor(open.lnum, open.col)

  " Find each Crystal keyword that ends an indent; if any of them do not
  " have a corresponding starting keyword, then this tag ends an indent
  while search(s:ecr_dedent_regex, 'z', close.lnum)
    let [lnum, col] = [line('.'), col('.')]

    if lnum == close.lnum && col > close.col
      break
    endif

    if crystal#indent#IsInStringOrComment(lnum, col)
      continue
    endif

    let [begin_lnum, begin_col] = searchpairpos(
          \ g:crystal#indent#end_start_regex,
          \ g:crystal#indent#end_middle_regex,
          \ g:crystal#indent#end_end_regex,
          \ 'bnz',
          \ g:crystal#indent#skip_expr,
          \ open.lnum)

    if begin_lnum
      if begin_lnum == open.lnum && begin_col < open.col
        let result = 1
      endif
    else
      let result = 1
    endif

    if result
      break
    endif
  endwhile

  call setpos('.', pos)
  return result
endfunction

" Find and match a control tag in the given line, if one exists.
function! s:FindECRControl(...) abort
  let lnum = a:0 ? a:1 : line('.')

  let open = { 'lnum': 0, 'col': 0 }
  let close = { 'lnum': 0, 'col': 0 }

  let pos = getcurpos()

  call cursor(lnum, 1)

  while search(s:ecr_control_open, 'cz', lnum)
    let [open, close] = s:MatchECRControl()

    if open.lnum
      break
    endif
  endwhile

  call setpos('.', pos)
  return [open, close]
endfunction

" Find and match the previous control tag.
"
" This takes two arguments: the first is the line to start searching
" from (exclusive); the second is the line to stop searching at
" (inclusive).
function! s:FindPrevECRControl(...) abort
  if a:0 == 0
    let start = line('.')
    let stop = 1
  elseif a:0 == 1
    let start = a:1
    let stop = 1
  elseif a:0 == 2
    let start = a:1
    let stop = a:2
  endif

  let open = { 'lnum': 0, 'col': 0 }
  let close = { 'lnum': 0, 'col': 0 }

  let pos = getcurpos()

  call cursor(start, 1)

  let [lnum, col] = searchpos(s:ecr_close, 'bWz', stop)

  if !lnum
    call setpos('.', pos)
    return [open, close]
  endif

  let [open, close] = s:MatchECRControl()

  while !open.lnum
    let [lnum, col] = searchpos(s:ecr_close, 'bWz', stop)

    if !lnum
      break
    endif

    let [open, close] = s:MatchECRControl()
  endwhile

  call setpos('.', pos)
  return [open, close]
endfunction

" GetEcrystalIndent {{{1
" =================

function! GetEcrystalIndent() abort
  let prev_lnum = prevnonblank(v:lnum - 1)

  if b:ecrystal_indent_multiline
    let [open_tag_lnum, open_tag_col] = s:MatchECR()
  else
    let open_tag_lnum = 0
  endif

  if open_tag_lnum && open_tag_lnum < v:lnum
    " If we are inside a multiline eCrystal tag...

    let ind = indent(open_tag_lnum)

    " If this line has a closing delimiter that isn't inside a string or
    " comment, then we are done with this tag
    if crystal#indent#Match(v:lnum, s:ecr_close)
      return ind
    endif

    " All tag contents will have at least one indent
    let ind += s:sw()

    if open_tag_lnum == prev_lnum
      " If this is the first line after the opening delimiter, then one
      " indent is enough
      return ind
    elseif s:MatchAt(open_tag_lnum, open_tag_col, s:ecr_comment_open)[0]
      " eCrystal comments shouldn't be indented any further
      return ind
    else
      " Else, fall back to Crystal indentation...
      let crystal_ind = GetCrystalIndent()

      " But only if it isn't less than the default indentation for this
      " tag
      return crystal_ind < ind ? ind : crystal_ind
    endif
  else
    let [prev_ecr_open, prev_ecr_close] = s:FindPrevECRControl(v:lnum, prev_lnum)
    let [curr_ecr_open, curr_ecr_close] = s:FindECRControl()

    let prev_is_ecr = prev_ecr_open.lnum != 0
    let curr_is_ecr = curr_ecr_open.lnum != 0

    let shift = 0

    if prev_is_ecr
      if s:ECRIndent(prev_ecr_open, prev_ecr_close)
        let shift = 1
      endif
    endif

    if curr_is_ecr
      if s:ECRDedent(curr_ecr_open, curr_ecr_close)
        if !b:ecrystal_align_end
          let shift = shift ? 0 : -1
        else
          " Find the nearest previous control tag that starts an indent
          " and align this one with that one
          let end_tags = 0

          let [open, close] = s:FindPrevECRControl()

          while open.lnum
            if s:ECRIndent(open, close)
              if end_tags
                let end_tags -= 1
              else
                return indent(open.lnum)
              endif
            elseif s:ECRDedent(open, close)
              let end_tags += 1
            endif

            let [open, close] = s:FindPrevECRControl(open.lnum)
          endwhile
        endif
      endif
    endif

    if shift
      return indent(prev_lnum) + s:sw() * shift
    else
      exec 'return ' . b:ecrystal_subtype_indentexpr
    endif
  endif
endfunction

" GetEcrystalFold {{{1
" ===============

function! GetEcrystalFold() abort
  let fold = '='

  let col = crystal#indent#Match(v:lnum, s:ecr_control_open)

  if col
    let [open, close] = s:MatchECRControl(v:lnum, col)

    let starts_indent = s:ECRIndent(open, close)
    let ends_indent = s:ECRDedent(open, close)

    if starts_indent && !ends_indent
      let fold = 'a1'
    elseif ends_indent && !starts_indent
      let fold = 's1'
    endif
  endif

  return fold
endfunction

" }}}

" vim:fdm=marker

endif