summaryrefslogtreecommitdiffstats
path: root/autoload/jsx_pretty/indent.vim
blob: 03bdf29b1200ea6ce74ca77f60b41251efbd70a5 (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
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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
if !exists('g:polyglot_disabled') || !(index(g:polyglot_disabled, 'typescript') != -1 || index(g:polyglot_disabled, 'typescript') != -1 || index(g:polyglot_disabled, 'jsx') != -1)

if exists('*shiftwidth')
  function! s:sw()
    return shiftwidth()
  endfunction
else
  function! s:sw()
    return &sw
  endfunction
endif

" Regexp for the start tag
let s:start_tag = '<\_s*\%(>\|\${\|\%(\<[-:._$A-Za-z0-9]\+\>\)\)'
" Regexp for the end tag
let s:end_tag = '\%(<\_s*/\_s*\%(\<[-:._$A-Za-z0-9]\+\>\)\_s*>\|/\_s*>\)'

" Get the syntax stack at the given position
function s:syntax_stack_at(lnum, col)
  return map(synstack(a:lnum, a:col), 'synIDattr(v:val, "name")')
endfunction

" Get the syntax at the given position
function s:syntax_at(lnum, col)
  return synIDattr(synID(a:lnum, a:col, 1), 'name')
endfunction

" Get the start col of the non-space charactor
function s:start_col(lnum)
  return len(matchstr(getline(a:lnum), '^\s*')) + 1
endfunction

" Get the end col of the non-space charactor
function s:end_col(lnum)
  return len(substitute(getline(a:lnum), '\s*$', '', 'g'))
endfunction

" Get the start syntax of a given line number
function s:start_syntax(lnum)
  return s:syntax_at(a:lnum, s:start_col(a:lnum))
endfunction

" Get the end syntax of a given line number
function s:end_syntax(lnum)
  let end_col = len(substitute(getline(a:lnum), '\s*$', '', 'g'))
  return s:syntax_at(a:lnum, end_col)
endfunction

" The skip function for searchpair
function s:skip_if_not(current_lnum, ...)
  " Skip the match in current line
  if line('.') == a:current_lnum
    return 1
  endif

  let syntax = s:syntax_at(line('.'), col('.'))
  return syntax !~? join(a:000, '\|')
endfunction

" Whether the specified stytax group is an jsx-related syntax
function s:is_jsx(syntax_name)
  return a:syntax_name =~ '^jsx' && a:syntax_name !=? 'jsxEscapeJs'
endfunction

" Whether the specified line is start with jsx-related syntax
function s:start_with_jsx(lnum)
  let start_syntax = s:start_syntax(a:lnum)
  return s:is_jsx(start_syntax)
endfunction

" Whether the specified line is end with jsx-related syntax
function s:end_with_jsx(lnum)
  let end_syntax = s:end_syntax(a:lnum)
  return s:is_jsx(end_syntax)
endfunction

" Whether the specified line is comment related syntax
function s:is_comment(syntax)
  return a:syntax =~? 'comment'
endfunction

" Whether the specified line is embedded comment in jsxTag
function s:is_embedded_comment(lnum)
  let start_col = s:start_col(a:lnum)
  let syntax_stack = s:syntax_stack_at(a:lnum, start_col)
  let last = get(syntax_stack, -1)
  let last_2nd = get(syntax_stack, -2)

  " Patch code for pangloss/vim-javascript
  " <div
  "   /* hello */ <-- syntax stack is [..., jsxTag, jsComment, jsComment]
  " >
  if last_2nd =~ 'comment'
    let last_2nd = get(syntax_stack, -3)
  endif

  return last =~? 'comment' && last_2nd =~? 'jsxTag' &&
        \ trim(getline(a:lnum)) =~ '\%(^/[*/]\)\|\%(\*/$\)'
endfunction

" Whether the specified stytax group is the opening tag
function s:is_opening_tag(syntax)
  return a:syntax =~? 'jsxOpenPunct'
endfunction

" Whether the specified stytax group is the closing tag
function s:is_closing_tag(syntax)
  return a:syntax =~? 'jsxClose'
endfunction

" Whether the specified stytax group is the jsxAttrib
function s:is_jsx_attr(syntax)
  return a:syntax =~? 'jsxAttrib'
endfunction

" Whether the specified syntax group is the jsxElement
function s:is_jsx_element(syntax)
  return a:syntax =~? 'jsxElement'
endfunction

" Whether the specified syntax group is the jsxTag
function s:is_jsx_tag(syntax)
  return a:syntax =~? 'jsxTag'
endfunction

" Whether the specified syntax group is the jsxBraces
function s:is_jsx_brace(syntax)
  return a:syntax =~? 'jsxBraces'
endfunction

" Whether the specified syntax group is the jsxComment
function s:is_jsx_comment(syntax)
  return a:syntax =~? 'jsxComment'
endfunction

" Whether the specified syntax group is the jsxComment
function s:is_jsx_backticks(syntax)
  return a:syntax =~? 'jsxBackticks'
endfunction

" Get the prvious line number
function s:prev_lnum(lnum)
  return prevnonblank(a:lnum - 1)
endfunction

" Given a lnum and get the information of its previous line
function s:prev_info(lnum)
  let prev_lnum = s:prev_lnum(a:lnum)
  let prev_start_syntax = s:start_syntax(prev_lnum)
  let prev_end_syntax = s:end_syntax(prev_lnum)

  return [prev_lnum, prev_start_syntax, prev_end_syntax]
endfunction

" Get the length difference between syntax stack a and b
function s:syntax_stack_length_compare(lnum_a, col_a, lnum_b, col_b)
  let stack_a = s:syntax_stack_at(a:lnum_a, a:col_a)
  let stack_b = s:syntax_stack_at(a:lnum_b, a:col_b)

  return len(stack_a) - len(stack_b)
endfunction

" Determine whether child is a element of parent
function s:is_element_of(parent_lnum, child_lnum)
  let parent_stack = s:syntax_stack_at(a:parent_lnum, s:start_col(a:parent_lnum))
  let child_stack = s:syntax_stack_at(a:child_lnum, s:start_col(a:child_lnum))

  let element_index = len(child_stack) - index(reverse(child_stack), 'jsxElement')
  let rest = parent_stack[element_index:]

  return index(rest, 'jsxElement') == -1
endfunction

" Compute the indention of the opening tag
function s:jsx_indent_opening_tag(lnum)
  let [prev_lnum, prev_start_syntax, prev_end_syntax] = s:prev_info(a:lnum)

  " If current line is start with >
  if trim(getline(a:lnum)) =~ '^>'
    let pair_line = searchpair('<', '', '>', 'bW', 's:skip_if_not(a:lnum, "jsxOpenPunct", "jsxClose")')
    return indent(pair_line)
  endif

  " If both the start and the end of the previous line are not jsx-related syntax
  " return (
  "   <div></div> <--
  " )
  if !s:end_with_jsx(prev_lnum) && !s:start_with_jsx(prev_lnum)
    " return -1, so that it will use the default js indent function
    return -1
  endif

  " If the end of the previous line is not jsx-related syntax
  " [
  "   <div></div>,
  "   <div></div> <--
  " ]
  " should not affect
  " <div
  "   render={() => (
  "     <div> <--
  "     </div>
  "   )}
  " >
  " </div>
  " <div>
  "   {items.map(() => (
  "     <Item /> <--
  "   ))}
  " </div>
  if !s:end_with_jsx(prev_lnum) && 
        \ !s:is_jsx_attr(prev_start_syntax) &&
        \ !s:is_jsx_brace(prev_start_syntax)
    return indent(prev_lnum)
  endif

  " If the start of the previous line is not jsx-related syntax
  " return <div>
  "   <div></div> <--
  " </div>
  if !s:start_with_jsx(prev_lnum)
      return indent(prev_lnum) + s:sw()
    endif
  endif

  " <div>
  "   <span>
  "   </span>
  "   <div> <--
  "   </div>
  " </div>
  if s:is_closing_tag(prev_start_syntax)
    return indent(prev_lnum)
  endif

  " If the previous line is end with a closing tag
  " <div>
  "   <br />
  "   <div></div> <--
  " </div>
  " should not affect case like
  " <div><br />
  "   <span> <--
  " </div>
  if s:is_closing_tag(prev_end_syntax) &&
        \ s:syntax_stack_length_compare(
        \   prev_lnum, s:start_col(prev_lnum), prev_lnum, s:end_col(prev_lnum)) == 2
    return indent(prev_lnum)
  endif

  " If the start of the previous line is the jsxElement
  " <div>
  "   hello
  "   <div></div> <--
  " </div>
  if s:is_jsx_element(prev_start_syntax) ||
        \ s:is_jsx_comment(prev_start_syntax)
    return indent(prev_lnum)
  endif

  " If the start of the prvious line is the jsxBraces {
  " <div>
  "   {foo}
  "   <div></div> <--
  " </div>
  " should not affect case like
  " <div>
  "   {
  "     <div></div> <--
  "   }
  " </div>
  if s:is_jsx_brace(prev_start_syntax) &&
        \ trim(getline(prev_lnum)) =~ '^[$]\?{' &&
        \ s:syntax_stack_length_compare(
        \ prev_lnum, s:start_col(prev_lnum), a:lnum, s:start_col(a:lnum)) == -2
    return indent(prev_lnum)
  endif

  " If the start of the prvious line is the jsxBraces }
  " <div>
  "   {
  "     foo
  "   }
  "   <div></div> <--
  " </div>
  if s:is_jsx_brace(prev_start_syntax) &&
        \ trim(getline(prev_lnum)) =~ '}' &&
        \ s:syntax_stack_length_compare(
        \ prev_lnum, s:start_col(prev_lnum), a:lnum, s:start_col(a:lnum)) == -3
    return indent(prev_lnum)
  endif

  " Otherwise, indent s:sw() spaces
  return indent(prev_lnum) + s:sw()
endfunction

" Compute the indention of the closing tag
function s:jsx_indent_closing_tag(lnum)
  let pair_line = searchpair(s:start_tag, '', s:end_tag, 'bW', 's:skip_if_not(a:lnum, "jsxOpenPunct", "jsxClose")')
  return pair_line ? indent(pair_line) : indent(a:lnum)
endfunction

" Compute the indention of the jsxAttrib
function s:jsx_indent_attr(lnum)
  let [prev_lnum, prev_start_syntax, prev_end_syntax] = s:prev_info(a:lnum)

  " If the start of the previous line is not jsx-related syntax
  " return <div
  "   attr="hello" <--
  " >
  " should not affect
  " <div
  "   // comment here
  "   attr="hello"
  " >
  " </div>
  if !s:start_with_jsx(prev_lnum) &&
        \ !s:is_comment(prev_start_syntax)
    return indent(prev_lnum) + s:sw()
  endif

  " If the start of the previous line is the opening tag
  " <div
  "   title="title" <--
  " >
  if s:is_opening_tag(prev_start_syntax)
    return indent(prev_lnum) + s:sw()
  endif

  " Otherwise, align the attribute with its previous line
  return indent(prev_lnum)
endfunction

" Compute the indentation of the jsxElement
function s:jsx_indent_element(lnum)
  let [prev_lnum, prev_start_syntax, prev_end_syntax] = s:prev_info(a:lnum)

  " Fix test case like
  " <div|> <-- press enter
  " should indent as
  " <div
  " > <--
  if trim(getline(a:lnum)) =~ '^>' && !s:is_opening_tag(prev_end_syntax)
    return indent(prev_lnum)
  endif

  " If the start of the previous line is start with >
  " <div
  "   attr="foo"
  " >
  "   text <--
  " </div>
  if s:is_opening_tag(prev_start_syntax) &&
        \ trim(getline(prev_lnum)) =~ '^>$'
    return indent(prev_lnum) + s:sw()
  endif

  " <div>
  "   text <--
  " </div>
  " should not affect case like
  " <div>
  "   <br />
  "   hello <--
  " </div>
  if s:is_opening_tag(prev_start_syntax) &&
        \ s:is_element_of(prev_lnum, a:lnum)
    return indent(prev_lnum) + s:sw()
  endif

  " return <div>
  "   text <--
  " </div>
  if !s:start_with_jsx(prev_lnum)
    return indent(prev_lnum) + s:sw()
  endif

  " Otherwise, align with the previous line
  " <div>
  "   {foo}
  "   text <--
  " </div>
  return indent(prev_lnum)
endfunction

" Compute the indentation of jsxBraces
function s:jsx_indent_brace(lnum)
  let [prev_lnum, prev_start_syntax, prev_end_syntax] = s:prev_info(a:lnum)
  
  " If the start of the previous line is start with >
  " <div
  "   attr="foo"
  " >
  "   {foo} <--
  " </div>
  if s:is_opening_tag(prev_start_syntax) &&
        \ trim(getline(prev_lnum)) =~ '^>$'
    return indent(prev_lnum) + s:sw()
  endif

  " <div>
  "   {foo} <--
  " </div>
  " should not affect case like
  " <div>
  "   <br />
  "   {foo} <--
  "   text
  "   {foo} <--
  " </div>
  if s:is_opening_tag(prev_start_syntax) &&
        \ s:syntax_stack_length_compare(
        \ prev_lnum, s:start_col(prev_lnum), a:lnum, s:start_col(a:lnum)) == 1
    return indent(prev_lnum) + s:sw()
  endif

  " If current line is the close brace }
  if trim(getline(a:lnum)) =~ '^}'
    let pair_line = searchpair('{', '', '}', 'bW', 's:skip_if_not(a:lnum, "jsxBraces")')
    return indent(pair_line)
  endif

  " return <div>
  "   {foo} <--
  " </div>
  " should not affect
  " <div
  "   // js comment
  "   {...props} <--
  " >
  " </div>
  if !s:start_with_jsx(prev_lnum) &&
        \ !s:is_comment(prev_start_syntax)
    return indent(prev_lnum) + s:sw()
  endif
  
  return indent(prev_lnum)
endfunction

" Compute the indentation of the comment
function s:jsx_indent_comment(lnum)
  let [prev_lnum, prev_start_syntax, prev_end_syntax] = s:prev_info(a:lnum)

  " If current line is jsxComment
  if s:is_jsx_comment(s:start_syntax(a:lnum))
    let line = trim(getline(a:lnum))
    if line =~ '^<!--'
      " Patch code for yajs.vim
      " ${logs.map(log => html`
      "   <${Log} class="log" ...${log} />
      " `)} <-- The backtick here is Highlighted as javascriptTemplate
      " <!-- <-- Correct the indentation here
      if !s:start_with_jsx(prev_lnum) && s:end_with_jsx(prev_lnum)
        return indent(prev_lnum)
      endif

      " Return the element indent for the opening comment
      return s:jsx_indent_element(a:lnum)
    elseif line =~ '^-->'
      " Return the paired indent for the closing comment
      let pair_line = searchpair('<!--', '', '-->', 'bW')
      return indent(pair_line)
    else
      if trim(getline(prev_lnum)) =~ '^<!--'
        " <!--
        "   comment <--
        " -->
        return indent(prev_lnum) + s:sw()
      else
        " <!--
        "   comment
        "   comment <--
        " -->
        return indent(prev_lnum)
      endif
    endif
  endif

  " For comment inside the jsxTag
  if s:is_opening_tag(prev_start_syntax)
    return indent(prev_lnum) + s:sw()
  endif

  if trim(getline(a:lnum)) =~ '\*/$'
    let pair_line = searchpair('/\*', '', '\*/', 'bW', 's:skip_if_not(a:lnum)')
    return indent(pair_line) + 1
  endif
  
  return indent(prev_lnum)
endfunction

function s:jsx_indent_backticks(lnum)
  let tags = get(g:, 'vim_jsx_pretty_template_tags', ['html', 'jsx'])
  let start_tag = '\%(' . join(tags, '\|') . '\)`'
  let end_tag = '\%(' . join(tags, '\|') . '\)\@<!`'
  let pair_line = searchpair(start_tag, '', end_tag, 'bW', 's:skip_if_not(a:lnum)')

  return indent(pair_line)
endfunction

" Compute the indentation for the jsx-related element
function s:jsx_indent(lnum)
  let start_syntax = s:start_syntax(a:lnum)

  if s:is_opening_tag(start_syntax)
    return s:jsx_indent_opening_tag(a:lnum)
  elseif s:is_closing_tag(start_syntax)
    return s:jsx_indent_closing_tag(a:lnum)
  elseif s:is_jsx_attr(start_syntax)
    return s:jsx_indent_attr(a:lnum)
  elseif s:is_jsx_element(start_syntax)
    return s:jsx_indent_element(a:lnum)
  elseif s:is_jsx_brace(start_syntax)
    return s:jsx_indent_brace(a:lnum)
  elseif s:is_jsx_comment(start_syntax)
    return s:jsx_indent_comment(a:lnum)
  elseif s:is_jsx_backticks(start_syntax)
    return s:jsx_indent_backticks(a:lnum)
  endif

  return -1
endfunction

" Return the jsx context of the specified line 
function s:jsx_context(lnum)
  if !s:end_with_jsx(prev_lnum)
    return 'non-jsx'
  endif

  let prev_lnum = s:prev_lnum(a:lnum)
  let start_syntax = s:start_syntax(prev_lnum)
  let end_col = s:end_col(prev_lnum)
  let end_syntax_stack = s:syntax_stack_at(prev_lnum, end_col)
  let reversed = reverse(end_syntax_stack)

  for item in reversed
    if item =~? 'jsxEscapeJs'
      return 'unknown'
    elseif s:is_jsx_element(item) && s:is_jsx(start_syntax) && start_syntax !~? 'jsxEscapeJs'
      " <div>
      "   <br /> <-- press o
      " </div>
      " --------------------
      " <div>
      " {
      "     a > 0 ? 1 <div>|</div> <-- press enter
      " }
      " </div>
      return 'jsxElement'
    elseif s:is_jsx_tag(item)
      return 'jsxTag'
    elseif s:is_jsx_brace(item) && trim(getline(prev_lnum)) =~ '{$'
      return 'jsxBraces'
    endif
  endfor

  return 'unknown'
endfunction

function! jsx_pretty#indent#get(js_indent)
  " The start column index of the current line (one-based)
  let start_col = s:start_col(v:lnum)
  " The end column index of the current line (one-based)
  let end_col = s:end_col(v:lnum)

  if s:start_with_jsx(v:lnum)
    let ind = s:jsx_indent(v:lnum)
    return ind == -1 ? a:js_indent() : ind
  elseif s:is_embedded_comment(v:lnum)
    return s:jsx_indent_comment(v:lnum)
  else
    let line = trim(getline(v:lnum))
    let prev_lnum = s:prev_lnum(v:lnum)

    " Fix the case where pressing enter at the cursor
    " return <div>|</div>
    if line =~ '^' . s:end_tag &&
          \ s:end_with_jsx(s:prev_lnum(v:lnum))
      return s:jsx_indent_closing_tag(v:lnum)
    endif

    " Fix cases for the new line
    if empty(line)
      let context = s:jsx_context(v:lnum)

      if context == 'jsxElement'
        " <div> <-- press o
        " </div>
        return s:jsx_indent_element(v:lnum)
      elseif context == 'jsxTag'
        " <div <-- press o
        " >
        " </div>
        " -----------------------
        " <div
        "   attr="attr" <-- press o
        " >
        " </div>
        return s:jsx_indent_attr(v:lnum)
      elseif context == 'jsxBraces'
        " <div>
        "   { <-- press o
        "   }
        " </div>
        return indent(prev_lnum) + s:sw()
      endif
    endif

    return a:js_indent()
  endif
endfunction

endif