summaryrefslogtreecommitdiffstats
path: root/indent/typescriptreact.vim
blob: 8564f6127ad3862fe7c5dcebf2cea694b139bdac (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
if polyglot#init#is_disabled(expand('<sfile>:p'), 'typescript', 'indent/typescriptreact.vim')
  finish
endif

runtime! indent/typescript.vim

" Save the current JavaScript indentexpr.
let b:tsx_ts_indentexpr = &indentexpr

" Prologue; load in XML indentation.
if exists('b:did_indent')
  let s:did_indent=b:did_indent
  unlet b:did_indent
endif
runtime! indent/xml.vim
if exists('s:did_indent')
  let b:did_indent=s:did_indent
endif

setlocal indentexpr=GetTsxIndent()

" JS indentkeys
setlocal indentkeys=0{,0},0),0],0\,,!^F,o,O,e
" XML indentkeys
setlocal indentkeys+=*<Return>,<>>,<<>,/

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

" Multiline end tag regex (line beginning with '>' or '/>')
let s:endtag = '^\s*\/\?>\s*;\='
let s:startexp = '[\{\(]\s*$'

" Get all syntax types at the beginning of a given line.
fu! s:SynSOL(lnum)
  return map(synstack(a:lnum, 1), 'synIDattr(v:val, "name")')
endfu

" Get all syntax types at the end of a given line.
fu! s:SynEOL(lnum)
  let lnum = prevnonblank(a:lnum)
  let col = strlen(getline(lnum))
  return map(synstack(lnum, col), 'synIDattr(v:val, "name")')
endfu

" Check if a syntax attribute is XMLish.
fu! s:SynAttrXMLish(synattr)
  return a:synattr =~ "^xml" || a:synattr =~ "^tsx"
endfu

" Check if a synstack is XMLish (i.e., has an XMLish last attribute).
fu! s:SynXMLish(syns)
  return s:SynAttrXMLish(get(a:syns, -1))
endfu

" Check if a synstack denotes the end of a TSX block.
fu! s:SynTSXBlockEnd(syns)
  return get(a:syns, -1) =~ '\%(ts\|typescript\)Braces' &&
       \ s:SynAttrXMLish(get(a:syns, -2))
endfu

" Determine how many tsxRegions deep a synstack is.
fu! s:SynTSXDepth(syns)
  return len(filter(copy(a:syns), 'v:val ==# "tsxRegion"'))
endfu

" Check whether `cursyn' continues the same tsxRegion as `prevsyn'.
fu! s:SynTSXContinues(cursyn, prevsyn)
  let curdepth = s:SynTSXDepth(a:cursyn)
  let prevdepth = s:SynTSXDepth(a:prevsyn)

  " In most places, we expect the nesting depths to be the same between any
  " two consecutive positions within a tsxRegion (e.g., between a parent and
  " child node, between two TSX attributes, etc.).  The exception is between
  " sibling nodes, where after a completed element (with depth N), we return
  " to the parent's nesting (depth N - 1).  This case is easily detected,
  " since it is the only time when the top syntax element in the synstack is
  " tsxRegion---specifically, the tsxRegion corresponding to the parent.
  return prevdepth == curdepth ||
      \ (prevdepth == curdepth + 1 && get(a:cursyn, -1) ==# 'tsxRegion')
endfu

" Cleverly mix JS and XML indentation.
fu! GetTsxIndent()
  let cursyn  = s:SynSOL(v:lnum)
  let prevsyn = s:SynEOL(v:lnum - 1)

  " Use XML indenting iff:
  "   - the syntax at the end of the previous line was either TSX or was the
  "     closing brace of a jsBlock whose parent syntax was TSX; and
  "   - the current line continues the same tsxRegion as the previous line.
  if (s:SynXMLish(prevsyn) || s:SynTSXBlockEnd(prevsyn)) &&
        \ s:SynTSXContinues(cursyn, prevsyn)
    let ind = XmlIndentGet(v:lnum, 0)
    let l:line = getline(v:lnum)
    let l:pline = getline(v:lnum - 1)

    " Align '/>' and '>' with '<' for multiline tags.
    " Align end of expression ')' or '}'.
    if l:line =~? s:endtag
      let ind = ind - shiftwidth()
    endif

    " Then correct the indentation of any TSX following '/>' or '>'.
    " Align start of expression '(' or '{'
    if l:pline =~? s:endtag || l:pline =~? s:startexp
      let ind = ind + shiftwidth()
    endif
  else
    if len(b:tsx_ts_indentexpr)
      " Invoke the base TS package's custom indenter
      let ind = eval(b:tsx_ts_indentexpr)
    else
      let ind = cindent(v:lnum)
    endif
  endif

  return ind
endfu