Skip to content

Commit 3df2d87

Browse files
committed
🐛 Fix #6 Indentation breaks when passing javascript object as attribute to element.
1 parent 7b3c975 commit 3df2d87

File tree

2 files changed

+80
-33
lines changed

2 files changed

+80
-33
lines changed

after/indent/javascript.vim

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ else
3535
endif
3636

3737
let s:endtag = '^\s*\/\?>\s*;\='
38+
let s:real_endtag = '\s*<\/\+[A-Za-z]*>'
3839

3940
let s:has_vim_javascript = exists('*GetJavascriptIndent')
4041

@@ -68,14 +69,38 @@ function! SynJSXDepth(syns)
6869
return len(filter(copy(a:syns), 'v:val ==# "jsxRegion"'))
6970
endfunction
7071

71-
function! SynJsFuncBrace(syns)
72+
function! SynJsFuncBraces(syns)
7273
return len(filter(copy(a:syns), 'v:val ==# "jsFuncBraces"'))
7374
endfunction
7475

76+
function! SynJsRepeatBraces(syns)
77+
return len(filter(copy(a:syns), 'v:val ==# "jsRepeatBraces"'))
78+
endfunction
79+
80+
function! SynJsIfElseBlock(syns)
81+
return len(filter(copy(a:syns), 'v:val ==# "jsIfElseBlock"'))
82+
endfunction
83+
7584
function! SynJSXCloseTag(syns)
7685
return len(filter(copy(a:syns), 'v:val ==# "jsxCloseTag"'))
7786
endfunction
7887

88+
function! SynJsReturn(syns)
89+
return len(filter(copy(a:syns), 'v:val ==# "jsReturn"'))
90+
endfunction
91+
92+
function! SynJsxAttrib(syns)
93+
return len(filter(copy(a:syns), 'v:val ==# "jsxAttrib"'))
94+
endfunction
95+
96+
function! SynJsxTag(syns)
97+
return len(filter(copy(a:syns), 'v:val ==# "jsxTag"'))
98+
endfunction
99+
100+
function! SynJsxEscapeJs(syns)
101+
return len(filter(copy(a:syns), 'v:val ==# "jsxEscapeJs"'))
102+
endfunction
103+
79104
function! SynJSXContinues(cursyn, prevsyn)
80105
let curdepth = SynJSXDepth(a:cursyn)
81106
let prevdepth = SynJSXDepth(a:prevsyn)
@@ -93,10 +118,6 @@ function! GetJsxIndent()
93118
\ SynJSXContinues(cursyn, prevsyn)
94119
let ind = XmlIndentGet(v:lnum, 0)
95120

96-
if getline(v:lnum - 1) =~? '(' || getline(v:lnum - 1) =~? '{'
97-
let ind = ind + s:sw()
98-
endif
99-
100121
if getline(v:lnum) =~? s:endtag
101122
let ind = ind - s:sw()
102123
endif
@@ -105,41 +126,31 @@ function! GetJsxIndent()
105126
let ind = ind + s:sw()
106127
endif
107128

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

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()
130+
" <div | <div
131+
" hoge={ | hoge={
132+
" <div></div> | ##<div></div>
133+
if SynJsxEscapeJs(prevsyn) && !(getline(v:lnum - 1) =~? '}') && getline(v:lnum - 1) =~? '{'
134+
let ind = ind + s:sw()
125135
endif
126136

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)
137+
" <div | <div
138+
" hoge={ | hoge={
139+
" <div></div> | <div></div>
140+
" } | }##
141+
if SynJsxEscapeJs(cursyn) && getline(v:lnum) =~? '}' && !(getline(v:lnum) =~? '{')
135142
let ind = ind - s:sw()
136143
endif
137144

138145
" return ( | return (
139146
" <div> | <div>
140147
" </div> | </div>
141148
" ##); | ); <--
142-
if getline(v:lnum) =~? ');'
149+
if getline(v:lnum) =~? ');\?' && SynJSXCloseTag(prevsyn)
150+
let ind = ind - s:sw()
151+
endif
152+
153+
if (SynJsIfElseBlock(cursyn) || SynJsRepeatBraces(cursyn)) && SynJSXCloseTag(prevsyn)
143154
let ind = ind - s:sw()
144155
endif
145156
else

sample.js

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,33 @@ class Hoge extends React.Component {
22
constructor(props) {
33
super(props);
44
this.state = { };
5-
if ( foo <= 300 ) {
6-
5+
if (foo <= 300) {
6+
return <div style={{margin:0}}>
7+
Hello world
8+
</div>
79
}
810
}
911

12+
hoge() {
13+
Hoge.poge(
14+
<div>
15+
<div></div>
16+
{this.hoge}
17+
<div></div>
18+
</div>
19+
);
20+
}
21+
1022
renderHoge() {
1123
return (
1224
<div
1325
foo={
1426
<bar foo='aaa' >
1527
<div
1628
hoge={
17-
<div></div>
29+
<div
30+
hoge={'aaa'}
31+
></div>
1832
}
1933
/>
2034
</bar>
@@ -40,6 +54,28 @@ class Hoge extends React.Component {
4054
}
4155
}
4256

57+
export const Hoge = () => (
58+
<div>
59+
<div
60+
hoge={aaa}
61+
hoge={aaa}
62+
></div>
63+
<div
64+
hoge={aaa}
65+
hoge={aaa}
66+
/>
67+
</div>
68+
)
69+
70+
ReactDOM.render(
71+
<div>
72+
<div></div>
73+
<div></div>
74+
<div></div>
75+
</div>,
76+
document.getElementById('body');
77+
)
78+
4379
const hoge = () => {
4480
ReactDOM.render(
4581
<div></div>,

0 commit comments

Comments
 (0)