|
| 1 | +""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" |
| 2 | +" Vim indent file |
| 3 | +" |
| 4 | +" Language: typescript.jsx |
| 5 | +" Maintainer: MaxMellon <[email protected]> |
| 6 | +" Depends: leafgarland/typescript-vim |
| 7 | +" |
| 8 | +""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" |
| 9 | + |
| 10 | +if exists('b:did_indent') |
| 11 | + let s:did_indent = b:did_indent |
| 12 | + unlet b:did_indent |
| 13 | +endif |
| 14 | + |
| 15 | +runtime! indent/xml.vim |
| 16 | + |
| 17 | +let s:keepcpo = &cpo |
| 18 | +set cpo&vim |
| 19 | + |
| 20 | +if exists('s:did_indent') |
| 21 | + let b:did_indent = s:did_indent |
| 22 | +endif |
| 23 | + |
| 24 | +setlocal indentexpr=GetJsxIndent() |
| 25 | +setlocal indentkeys=0{,0},0),0],0\,,!^F,o,O,e,*<Return>,<>>,<<>,/ |
| 26 | + |
| 27 | +if exists('*shiftwidth') |
| 28 | + function! s:sw() |
| 29 | + return shiftwidth() |
| 30 | + endfunction |
| 31 | +else |
| 32 | + function! s:sw() |
| 33 | + return &sw |
| 34 | + endfunction |
| 35 | +endif |
| 36 | + |
| 37 | +let s:starttag = '^\s*<' |
| 38 | +let s:endtag = '^\s*\/\?>\s*;\=' |
| 39 | +let s:real_endtag = '\s*<\/\+[A-Za-z]*>' |
| 40 | + |
| 41 | +let s:has_typescript_vim = exists('*GetTypescriptIndent') |
| 42 | + |
| 43 | +let s:true = !0 |
| 44 | +let s:false = 0 |
| 45 | + |
| 46 | +function! s:syn_sol(lnum) |
| 47 | + return map(synstack(a:lnum, 1), 'synIDattr(v:val, "name")') |
| 48 | +endfunction |
| 49 | + |
| 50 | +function! s:syn_eol(lnum) |
| 51 | + let lnum = prevnonblank(a:lnum) |
| 52 | + let col = strlen(getline(lnum)) |
| 53 | + return map(synstack(lnum, col), 'synIDattr(v:val, "name")') |
| 54 | +endfunction |
| 55 | + |
| 56 | +function! s:syn_attr_jsx(synattr) |
| 57 | + return a:synattr =~ "^jsx" |
| 58 | +endfunction |
| 59 | + |
| 60 | +function! s:syn_xmlish(syns) |
| 61 | + return s:syn_attr_jsx(get(a:syns, -1)) |
| 62 | +endfunction |
| 63 | + |
| 64 | +function! s:syn_jsx_block_end(syns) |
| 65 | + return get(a:syns, -1) =~ '\%(js\|javascript\)Braces' || |
| 66 | + \ s:syn_attr_jsx(get(a:syns, -2)) |
| 67 | +endfunction |
| 68 | + |
| 69 | +function! s:syn_jsx_region(syns) |
| 70 | + return len(filter(copy(a:syns), 'v:val ==# "jsxRegion"')) |
| 71 | +endfunction |
| 72 | + |
| 73 | +function! s:syn_js_repeat_braces(syns) |
| 74 | + return len(filter(copy(a:syns), 'v:val ==# "jsRepeatBraces"')) |
| 75 | +endfunction |
| 76 | + |
| 77 | +function! s:syn_jsx_else_block(syns) |
| 78 | + return len(filter(copy(a:syns), 'v:val ==# "jsIfElseBlock"')) |
| 79 | +endfunction |
| 80 | + |
| 81 | +function! s:syn_jsx_close_tag(syns) |
| 82 | + return len(filter(copy(a:syns), 'v:val ==# "jsxCloseTag"')) |
| 83 | +endfunction |
| 84 | + |
| 85 | +function! s:syn_jsx_escapejs(syns) |
| 86 | + return len(filter(copy(a:syns), 'v:val ==# "jsxEscapeJs"')) |
| 87 | +endfunction |
| 88 | + |
| 89 | +function! s:syn_jsx_continues(cursyn, prevsyn) |
| 90 | + let curdepth = s:syn_jsx_region(a:cursyn) |
| 91 | + let prevdepth = s:syn_jsx_region(a:prevsyn) |
| 92 | + |
| 93 | + return prevdepth == curdepth || |
| 94 | + \ (prevdepth == curdepth + 1 && get(a:cursyn, -1) ==# 'jsxRegion') |
| 95 | +endfunction |
| 96 | + |
| 97 | +function! GetJsxIndent() |
| 98 | + let cursyn = s:syn_sol(v:lnum) |
| 99 | + let prevsyn = s:syn_eol(v:lnum - 1) |
| 100 | + let nextsyn = s:syn_eol(v:lnum + 1) |
| 101 | + |
| 102 | + if (s:syn_xmlish(prevsyn) || s:syn_jsx_block_end(prevsyn)) && |
| 103 | + \ s:syn_jsx_continues(cursyn, prevsyn) |
| 104 | + let ind = XmlIndentGet(v:lnum, 0) |
| 105 | + |
| 106 | + if getline(v:lnum) =~? s:endtag |
| 107 | + let ind = ind - s:sw() |
| 108 | + endif |
| 109 | + |
| 110 | + if getline(v:lnum - 1) =~? s:endtag |
| 111 | + let ind = ind + s:sw() |
| 112 | + endif |
| 113 | + |
| 114 | + " <div | <div |
| 115 | + " hoge={ | hoge={ |
| 116 | + " <div></div> | ##<div></div> |
| 117 | + if s:syn_jsx_escapejs(prevsyn) && !(getline(v:lnum - 1) =~? '}') |
| 118 | + \&& getline(v:lnum - 1) =~? '{' |
| 119 | + let ind = ind + s:sw() |
| 120 | + endif |
| 121 | + |
| 122 | + if getline(v:lnum) =~? s:starttag |
| 123 | + \&& !getline(v:lnum) =~? '}' && getline(v:lnum) =~? '{' |
| 124 | + let ind = ind + s:sw() |
| 125 | + endif |
| 126 | + |
| 127 | + " <div | <div |
| 128 | + " hoge={ | hoge={ |
| 129 | + " <div></div> | <div></div> |
| 130 | + " } | }## |
| 131 | + if s:syn_jsx_escapejs(cursyn) && getline(v:lnum) =~? '}' |
| 132 | + \&& !(getline(v:lnum) =~? '{') |
| 133 | + let ind = ind - s:sw() |
| 134 | + endif |
| 135 | + |
| 136 | + " return ( | return ( |
| 137 | + " <div> | <div> |
| 138 | + " </div> | </div> |
| 139 | + " ##); | ); <-- |
| 140 | + if getline(v:lnum) =~? ');\?' && s:syn_jsx_close_tag(prevsyn) |
| 141 | + let ind = ind - s:sw() |
| 142 | + endif |
| 143 | + |
| 144 | + if (s:syn_jsx_else_block(cursyn) || s:syn_js_repeat_braces(cursyn)) |
| 145 | + \&& s:syn_jsx_close_tag(prevsyn) |
| 146 | + let ind = ind - s:sw() |
| 147 | + endif |
| 148 | + else |
| 149 | + if s:has_typescript_vim ==# s:true |
| 150 | + let ind = GetTypescriptIndent() |
| 151 | + else |
| 152 | + let ind = cindent(v:lnum) |
| 153 | + endif |
| 154 | + endif |
| 155 | + |
| 156 | + return ind |
| 157 | +endfunction |
| 158 | + |
| 159 | +let &cpo = s:keepcpo |
| 160 | +unlet s:keepcpo |
0 commit comments