Skip to content

Commit

Permalink
[jsx mode] Improve indention of js blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
dabbott committed Jan 6, 2016
1 parent e436098 commit d6f6afc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
17 changes: 16 additions & 1 deletion mode/jsx/jsx.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,22 @@

if (stream.peek() == "{") {
xmlMode.skipAttribute(cx.state)
state.context = new Context(CodeMirror.startState(jsMode, flatXMLIndent(cx.state)),

var indent = flatXMLIndent(cx.state), xmlContext = cx.state.context
// If JS starts on same line as tag
if (xmlContext && stream.string.slice(0, stream.pos).match(/>\s*$/)) {
while (xmlContext.prev && !xmlContext.startOfLine)
xmlContext = xmlContext.prev
// If tag starts the line, use XML indentation level
if (xmlContext.startOfLine) indent -= config.indentUnit
// Else use JS indentation level
else if (cx.prev.state.lexical) indent = cx.prev.state.lexical.indented
// Else if inside of tag
} else if (cx.depth == 1) {
indent += config.indentUnit
}

state.context = new Context(CodeMirror.startState(jsMode, indent),
jsMode, 0, state.context)
return null
}
Expand Down
4 changes: 2 additions & 2 deletions mode/jsx/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@
MT("indent_js",
"([bracket&tag <][tag foo][bracket&tag >]",
" [bracket&tag <][tag bar] [attribute baz]={[keyword function]() {",
" [keyword return] [number 10]",
" }}[bracket&tag />]",
" [keyword return] [number 10]",
" }}[bracket&tag />]",
" [bracket&tag </][tag foo][bracket&tag >])")

MT("spread",
Expand Down

0 comments on commit d6f6afc

Please sign in to comment.