|
| 1 | +""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" |
| 2 | +" Vim indent file |
| 3 | +" |
| 4 | +" Language: javascript.jsx |
| 5 | +" Maintainer: MaxMellon <[email protected]> |
| 6 | +" Depends: pangloss/vim-javascript |
| 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:endtag = '^\s*\/\?>\s*;\=' |
| 38 | + |
| 39 | +let s:has_vim_javascript = exists('*GetJavascriptIndent') |
| 40 | + |
| 41 | +let s:true = !0 |
| 42 | +let s:false = 0 |
| 43 | + |
| 44 | +function! SynSOL(lnum) |
| 45 | + return map(synstack(a:lnum, 1), 'synIDattr(v:val, "name")') |
| 46 | +endfunction |
| 47 | + |
| 48 | +function! SynEOL(lnum) |
| 49 | + let lnum = prevnonblank(a:lnum) |
| 50 | + let col = strlen(getline(lnum)) |
| 51 | + return map(synstack(lnum, col), 'synIDattr(v:val, "name")') |
| 52 | +endfunction |
| 53 | + |
| 54 | +function! SynAttrJSX(synattr) |
| 55 | + return a:synattr =~ "^jsx" |
| 56 | +endfunction |
| 57 | + |
| 58 | +function! SynXMLish(syns) |
| 59 | + return SynAttrJSX(get(a:syns, -1)) |
| 60 | +endfunction |
| 61 | + |
| 62 | +function! SynJSXBlockEnd(syns) |
| 63 | + return get(a:syns, -1) =~ '\%(js\|javascript\)Braces' || |
| 64 | + \ SynAttrJSX(get(a:syns, -2)) |
| 65 | +endfunction |
| 66 | + |
| 67 | +function! SynJSXDepth(syns) |
| 68 | + return len(filter(copy(a:syns), 'v:val ==# "jsxRegion"')) |
| 69 | +endfunction |
| 70 | + |
| 71 | +function! SynJSXContinues(cursyn, prevsyn) |
| 72 | + let curdepth = SynJSXDepth(a:cursyn) |
| 73 | + let prevdepth = SynJSXDepth(a:prevsyn) |
| 74 | + |
| 75 | + return prevdepth == curdepth || |
| 76 | + \ (prevdepth == curdepth + 1 && get(a:cursyn, -1) ==# 'jsxRegion') |
| 77 | +endfunction |
| 78 | + |
| 79 | +function! GetJsxIndent() |
| 80 | + let cursyn = SynSOL(v:lnum) |
| 81 | + let prevsyn = SynEOL(v:lnum - 1) |
| 82 | + |
| 83 | + if (SynXMLish(prevsyn) || SynJSXBlockEnd(prevsyn)) && |
| 84 | + \ SynJSXContinues(cursyn, prevsyn) |
| 85 | + let ind = XmlIndentGet(v:lnum, 0) |
| 86 | + |
| 87 | + if getline(v:lnum) =~? s:endtag |
| 88 | + let ind = ind - s:sw() |
| 89 | + endif |
| 90 | + |
| 91 | + if getline(v:lnum - 1) =~? s:endtag |
| 92 | + let ind = ind + s:sw() |
| 93 | + endif |
| 94 | + |
| 95 | + " return ( | return ( |
| 96 | + " <div> | <div> |
| 97 | + " </div> | </div> |
| 98 | + " ##); | ); <-- |
| 99 | + if getline(v:lnum) =~? ');' |
| 100 | + let ind = ind - s:sw() |
| 101 | + endif |
| 102 | + else |
| 103 | + if s:has_vim_javascript ==# s:true |
| 104 | + let ind = GetJavascriptIndent() |
| 105 | + else |
| 106 | + let ind = cindent(v:lnum) |
| 107 | + endif |
| 108 | + endif |
| 109 | + |
| 110 | + return ind |
| 111 | +endfunction |
| 112 | + |
| 113 | +let &cpo = s:keepcpo |
| 114 | +unlet s:keepcpo |
0 commit comments