OLD | NEW |
1 // CodeMirror, copyright (c) by Marijn Haverbeke and others | 1 // CodeMirror, copyright (c) by Marijn Haverbeke and others |
2 // Distributed under an MIT license: http://codemirror.net/LICENSE | 2 // Distributed under an MIT license: http://codemirror.net/LICENSE |
3 | 3 |
4 (function(mod) { | 4 (function(mod) { |
5 if (typeof exports == "object" && typeof module == "object") // CommonJS | 5 if (typeof exports == "object" && typeof module == "object") // CommonJS |
6 mod(require("../../lib/codemirror"), require("../xml/xml"), require("../java
script/javascript")) | 6 mod(require("../../lib/codemirror"), require("../xml/xml"), require("../java
script/javascript")) |
7 else if (typeof define == "function" && define.amd) // AMD | 7 else if (typeof define == "function" && define.amd) // AMD |
8 define(["../../lib/codemirror", "../xml/xml", "../javascript/javascript"], m
od) | 8 define(["../../lib/codemirror", "../xml/xml", "../javascript/javascript"], m
od) |
9 else // Plain browser env | 9 else // Plain browser env |
10 mod(CodeMirror) | 10 mod(CodeMirror) |
11 })(function(CodeMirror) { | 11 })(function(CodeMirror) { |
12 "use strict" | 12 "use strict" |
13 | 13 |
14 function copyContext(context) { | 14 // Depth means the amount of open braces in JS context, in XML |
15 return {state: CodeMirror.copyState(context.mode, context.state), | 15 // context 0 means not in tag, 1 means in tag, and 2 means in tag |
16 mode: context.mode, | 16 // and js block comment. |
17 depth: context.depth, | 17 function Context(state, mode, depth, prev) { |
18 prev: context.prev && copyContext(context.prev)} | 18 this.state = state; this.mode = mode; this.depth = depth; this.prev = prev |
19 } | 19 } |
20 | 20 |
21 CodeMirror.defineMode("jsx", function(config) { | 21 function copyContext(context) { |
22 var xmlMode = CodeMirror.getMode(config, "xml") | 22 return new Context(CodeMirror.copyState(context.mode, context.state), |
23 var jsMode = CodeMirror.getMode(config, "javascript") | 23 context.mode, |
| 24 context.depth, |
| 25 context.prev && copyContext(context.prev)) |
| 26 } |
| 27 |
| 28 CodeMirror.defineMode("jsx", function(config, modeConfig) { |
| 29 var xmlMode = CodeMirror.getMode(config, {name: "xml", allowMissing: true, m
ultilineTagIndentPastTag: false}) |
| 30 var jsMode = CodeMirror.getMode(config, modeConfig && modeConfig.base || "ja
vascript") |
| 31 |
| 32 function flatXMLIndent(state) { |
| 33 var tagName = state.tagName |
| 34 state.tagName = null |
| 35 var result = xmlMode.indent(state, "") |
| 36 state.tagName = tagName |
| 37 return result |
| 38 } |
| 39 |
| 40 function token(stream, state) { |
| 41 if (state.context.mode == xmlMode) |
| 42 return xmlToken(stream, state, state.context) |
| 43 else |
| 44 return jsToken(stream, state, state.context) |
| 45 } |
| 46 |
| 47 function xmlToken(stream, state, cx) { |
| 48 if (cx.depth == 2) { // Inside a JS /* */ comment |
| 49 if (stream.match(/^.*?\*\//)) cx.depth = 1 |
| 50 else stream.skipToEnd() |
| 51 return "comment" |
| 52 } |
| 53 |
| 54 if (stream.peek() == "{") { |
| 55 xmlMode.skipAttribute(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.match(/^[^>]*>\s*$/, false)) { |
| 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.indente
d |
| 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), |
| 72 jsMode, 0, state.context) |
| 73 return null |
| 74 } |
| 75 |
| 76 if (cx.depth == 1) { // Inside of tag |
| 77 if (stream.peek() == "<") { // Tag inside of tag |
| 78 xmlMode.skipAttribute(cx.state) |
| 79 state.context = new Context(CodeMirror.startState(xmlMode, flatXMLInde
nt(cx.state)), |
| 80 xmlMode, 0, state.context) |
| 81 return null |
| 82 } else if (stream.match("//")) { |
| 83 stream.skipToEnd() |
| 84 return "comment" |
| 85 } else if (stream.match("/*")) { |
| 86 cx.depth = 2 |
| 87 return token(stream, state) |
| 88 } |
| 89 } |
| 90 |
| 91 var style = xmlMode.token(stream, cx.state), cur = stream.current(), stop |
| 92 if (/\btag\b/.test(style)) { |
| 93 if (/>$/.test(cur)) { |
| 94 if (cx.state.context) cx.depth = 0 |
| 95 else state.context = state.context.prev |
| 96 } else if (/^</.test(cur)) { |
| 97 cx.depth = 1 |
| 98 } |
| 99 } else if (!style && (stop = cur.indexOf("{")) > -1) { |
| 100 stream.backUp(cur.length - stop) |
| 101 } |
| 102 return style |
| 103 } |
| 104 |
| 105 function jsToken(stream, state, cx) { |
| 106 if (stream.peek() == "<" && jsMode.expressionAllowed(stream, cx.state)) { |
| 107 jsMode.skipExpression(cx.state) |
| 108 state.context = new Context(CodeMirror.startState(xmlMode, jsMode.indent
(cx.state, "")), |
| 109 xmlMode, 0, state.context) |
| 110 return null |
| 111 } |
| 112 |
| 113 var style = jsMode.token(stream, cx.state) |
| 114 if (!style && cx.depth != null) { |
| 115 var cur = stream.current() |
| 116 if (cur == "{") { |
| 117 cx.depth++ |
| 118 } else if (cur == "}") { |
| 119 if (--cx.depth == 0) state.context = state.context.prev |
| 120 } |
| 121 } |
| 122 return style |
| 123 } |
24 | 124 |
25 return { | 125 return { |
26 startState: function() { | 126 startState: function() { |
27 return {context: {state: CodeMirror.startState(jsMode), mode: jsMode}} | 127 return {context: new Context(CodeMirror.startState(jsMode), jsMode)} |
28 }, | 128 }, |
29 | 129 |
30 copyState: function(state) { | 130 copyState: function(state) { |
31 return {context: copyContext(state.context)} | 131 return {context: copyContext(state.context)} |
32 }, | 132 }, |
33 | 133 |
34 token: function(stream, state) { | 134 token: token, |
35 var cx = state.context | |
36 if (cx.mode == xmlMode) { | |
37 if (stream.peek() == "{") { | |
38 xmlMode.skipAttribute(cx.state) | |
39 state.context = {state: CodeMirror.startState(jsMode, xmlMode.indent
(cx.state, "")), | |
40 mode: jsMode, | |
41 depth: 1, | |
42 prev: state.context} | |
43 return jsMode.token(stream, state.context.state) | |
44 } else { // FIXME skip attribute | |
45 var style = xmlMode.token(stream, cx.state), cur, brace | |
46 if (/\btag\b/.test(style) && !cx.state.context && /^\/?>$/.test(stre
am.current())) | |
47 state.context = state.context.prev | |
48 else if (!style && (brace = (cur = stream.current()).indexOf("{")) >
-1) | |
49 stream.backUp(cur.length - brace) | |
50 return style | |
51 } | |
52 } else { // jsMode | |
53 if (stream.peek() == "<" && jsMode.expressionAllowed(stream, cx.state)
) { | |
54 jsMode.skipExpression(cx.state) | |
55 state.context = {state: CodeMirror.startState(xmlMode, jsMode.indent
(cx.state, "")), | |
56 mode: xmlMode, | |
57 prev: state.context} | |
58 return xmlMode.token(stream, state.context.state) | |
59 } else { | |
60 var style = jsMode.token(stream, cx.state) | |
61 if (!style && cx.depth != null) { | |
62 var cur = stream.current() | |
63 if (cur == "{") { | |
64 cx.depth++ | |
65 } else if (cur == "}") { | |
66 if (--cx.depth == 0) state.context = state.context.prev | |
67 } | |
68 } | |
69 return style | |
70 } | |
71 } | |
72 }, | |
73 | 135 |
74 indent: function(state, textAfter, fullLine) { | 136 indent: function(state, textAfter, fullLine) { |
75 return state.context.mode.indent(state.context.state, textAfter, fullLin
e) | 137 return state.context.mode.indent(state.context.state, textAfter, fullLin
e) |
76 }, | 138 }, |
77 | 139 |
78 innerMode: function(state) { | 140 innerMode: function(state) { |
79 return state.context[state.context.length - 1] | 141 return state.context |
80 } | 142 } |
81 } | 143 } |
82 }, "xml", "javascript") | 144 }, "xml", "javascript") |
83 | 145 |
84 CodeMirror.defineMIME("text/jsx", "jsx") | 146 CodeMirror.defineMIME("text/jsx", "jsx") |
85 }) | 147 }); |
OLD | NEW |