Skip to content

Commit 5b5976a

Browse files
dabbottmarijnh
authored andcommitted
[jsx mode] Improve indention of js blocks
Issue codemirror#3745
1 parent 5b4f224 commit 5b5976a

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

mode/jsx/jsx.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,22 @@
5353

5454
if (stream.peek() == "{") {
5555
xmlMode.skipAttribute(cx.state)
56-
state.context = new Context(CodeMirror.startState(jsMode, flatXMLIndent(cx.state)),
56+
57+
var indent = flatXMLIndent(cx.state), xmlContext = cx.state.context
58+
// If JS starts on same line as tag
59+
if (xmlContext && stream.string.slice(0, stream.pos).match(/>\s*$/)) {
60+
while (xmlContext.prev && !xmlContext.startOfLine)
61+
xmlContext = xmlContext.prev
62+
// If tag starts the line, use XML indentation level
63+
if (xmlContext.startOfLine) indent -= config.indentUnit
64+
// Else use JS indentation level
65+
else if (cx.prev.state.lexical) indent = cx.prev.state.lexical.indented
66+
// Else if inside of tag
67+
} else if (cx.depth == 1) {
68+
indent += config.indentUnit
69+
}
70+
71+
state.context = new Context(CodeMirror.startState(jsMode, indent),
5772
jsMode, 0, state.context)
5873
return null
5974
}

mode/jsx/test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@
5757
MT("indent_js",
5858
"([bracket&tag <][tag foo][bracket&tag >]",
5959
" [bracket&tag <][tag bar] [attribute baz]={[keyword function]() {",
60-
" [keyword return] [number 10]",
61-
" }}[bracket&tag />]",
60+
" [keyword return] [number 10]",
61+
" }}[bracket&tag />]",
6262
" [bracket&tag </][tag foo][bracket&tag >])")
6363

6464
MT("spread",

0 commit comments

Comments
 (0)