Skip to content

Commit 7b3c975

Browse files
committed
Fix: indent
1 parent c60bf4e commit 7b3c975

2 files changed

Lines changed: 43 additions & 2 deletions

File tree

after/indent/javascript.vim

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ function! SynJSXDepth(syns)
6868
return len(filter(copy(a:syns), 'v:val ==# "jsxRegion"'))
6969
endfunction
7070

71+
function! SynJsFuncBrace(syns)
72+
return len(filter(copy(a:syns), 'v:val ==# "jsFuncBraces"'))
73+
endfunction
74+
75+
function! SynJSXCloseTag(syns)
76+
return len(filter(copy(a:syns), 'v:val ==# "jsxCloseTag"'))
77+
endfunction
78+
7179
function! SynJSXContinues(cursyn, prevsyn)
7280
let curdepth = SynJSXDepth(a:cursyn)
7381
let prevdepth = SynJSXDepth(a:prevsyn)
@@ -79,12 +87,13 @@ endfunction
7987
function! GetJsxIndent()
8088
let cursyn = SynSOL(v:lnum)
8189
let prevsyn = SynEOL(v:lnum - 1)
90+
let nextsyn = SynEOL(v:lnum + 1)
8291

8392
if (SynXMLish(prevsyn) || SynJSXBlockEnd(prevsyn)) &&
8493
\ SynJSXContinues(cursyn, prevsyn)
8594
let ind = XmlIndentGet(v:lnum, 0)
8695

87-
if cursyn == 0 || && (getline(v:lnum - 1) =~? '(' || getline(v:lnum - 1) =~? '{')
96+
if getline(v:lnum - 1) =~? '(' || getline(v:lnum - 1) =~? '{'
8897
let ind = ind + s:sw()
8998
endif
9099

@@ -96,10 +105,36 @@ function! GetJsxIndent()
96105
let ind = ind + s:sw()
97106
endif
98107

108+
" <div> | <div>
109+
" {(test => test.length| {(test => test.length
110+
" ? 'foo' | ? 'foo'
111+
" : 'bar' | : 'bar'
112+
" ####)()} | )()} <----
113+
" </div> | </div>
99114
if getline(v:lnum - 1) =~? ':' && getline(v:lnum - 2) =~? '?'
100115
let ind = ind - s:sw() * 2
101116
endif
102117

118+
" <div | <div
119+
" foo={ | foo={
120+
" <Bar />| <Bar />
121+
" ##} | } <--
122+
" > | >
123+
if getline(v:lnum) =~? '}' && SynJSXCloseTag(prevsyn) && !SynJsFuncBrace(nextsyn)
124+
let ind = ind - s:sw()
125+
endif
126+
127+
" </div> | </div>
128+
" {(hoge => { | {(hoge => {
129+
" if (hoge) { | if (hoge) {
130+
" return <div />;| return <div />;
131+
" } | }
132+
" })()} | })()}
133+
" ##</div> | </div> <--
134+
if match(getline(v:lnum), '<[^!?<>]\+>', '') != -1 && SynJsFuncBrace(prevsyn)
135+
let ind = ind - s:sw()
136+
endif
137+
103138
" return ( | return (
104139
" <div> | <div>
105140
" </div> | </div>

sample.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@ class Hoge extends React.Component {
1111
return (
1212
<div
1313
foo={
14-
<bar />
14+
<bar foo='aaa' >
15+
<div
16+
hoge={
17+
<div></div>
18+
}
19+
/>
20+
</bar>
1521
}
1622
>
1723
<span>hoge</span>

0 commit comments

Comments
 (0)