Index: branches/5.3.x/core/admin_templates/incs/code_mirror/mode/javascript/javascript.js =================================================================== diff -u -N -r15690 -r15914 --- branches/5.3.x/core/admin_templates/incs/code_mirror/mode/javascript/javascript.js (.../javascript.js) (revision 15690) +++ branches/5.3.x/core/admin_templates/incs/code_mirror/mode/javascript/javascript.js (.../javascript.js) (revision 15914) @@ -2,6 +2,7 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) { var indentUnit = config.indentUnit; + var statementIndent = parserConfig.statementIndent; var jsonMode = parserConfig.json; var isTS = parserConfig.typescript; @@ -11,15 +12,16 @@ function kw(type) {return {type: type, style: "keyword"};} var A = kw("keyword a"), B = kw("keyword b"), C = kw("keyword c"); var operator = kw("operator"), atom = {type: "atom", style: "atom"}; - + var jsKeywords = { - "if": A, "while": A, "with": A, "else": B, "do": B, "try": B, "finally": B, + "if": kw("if"), "while": A, "with": A, "else": B, "do": B, "try": B, "finally": B, "return": C, "break": C, "continue": C, "new": C, "delete": C, "throw": C, "var": kw("var"), "const": kw("var"), "let": kw("var"), "function": kw("function"), "catch": kw("catch"), "for": kw("for"), "switch": kw("switch"), "case": kw("case"), "default": kw("default"), "in": operator, "typeof": operator, "instanceof": operator, - "true": atom, "false": atom, "null": atom, "undefined": atom, "NaN": atom, "Infinity": atom + "true": atom, "false": atom, "null": atom, "undefined": atom, "NaN": atom, "Infinity": atom, + "this": kw("this") }; // Extend the 'normal' keywords with the TypeScript language extensions @@ -52,7 +54,7 @@ return jsKeywords; }(); - var isOperatorChar = /[+\-*&%=<>!?|]/; + var isOperatorChar = /[+\-*&%=<>!?|~^]/; function chain(stream, state, f) { state.tokenize = f; @@ -86,7 +88,7 @@ else if (ch == "0" && stream.eat(/x/i)) { stream.eatWhile(/[\da-f]/i); return ret("number", "number"); - } + } else if (/\d/.test(ch) || ch == "-" && stream.eat(/\d/)) { stream.match(/^\d*(?:\.\d*)?(?:[eE][+\-]?\d+)?/); return ret("number", "number"); @@ -111,8 +113,8 @@ } } else if (ch == "#") { - stream.skipToEnd(); - return ret("error", "error"); + stream.skipToEnd(); + return ret("error", "error"); } else if (isOperatorChar.test(ch)) { stream.eatWhile(isOperatorChar); @@ -148,7 +150,7 @@ // Parser - var atomicTypes = {"atom": true, "number": true, "variable": true, "string": true, "regexp": true}; + var atomicTypes = {"atom": true, "number": true, "variable": true, "string": true, "regexp": true, "this": true}; function JSLexical(indented, column, type, align, prev, info) { this.indented = indented; @@ -169,7 +171,7 @@ // Communicate our context to the combinators. // (Less wasteful than consing up a hundred closures on every call.) cx.state = state; cx.stream = stream; cx.marked = null, cx.cc = cc; - + if (!state.lexical.hasOwnProperty("align")) state.lexical.align = true; @@ -225,8 +227,9 @@ } function pushlex(type, info) { var result = function() { - var state = cx.state; - state.lexical = new JSLexical(state.indented, cx.stream.column(), type, null, state.lexical, info); + var state = cx.state, indent = state.indented; + if (state.lexical.type == "stat") indent = state.lexical.indented; + state.lexical = new JSLexical(indent, cx.stream.column(), type, null, state.lexical, info); }; result.lex = true; return result; @@ -242,7 +245,7 @@ poplex.lex = true; function expect(wanted) { - return function expecting(type) { + return function(type) { if (type == wanted) return cont(); else if (wanted == ";") return pass(); else return cont(arguments.callee); @@ -255,6 +258,7 @@ if (type == "keyword b") return cont(pushlex("form"), statement, poplex); if (type == "{") return cont(pushlex("}"), block, poplex); if (type == ";") return cont(); + if (type == "if") return cont(pushlex("form"), expression, statement, poplex, maybeelse(cx.state.indented)); if (type == "function") return cont(functiondef); if (type == "for") return cont(pushlex("form"), expect("("), pushlex(")"), forspec1, expect(")"), poplex, statement, poplex); @@ -268,46 +272,80 @@ return pass(pushlex("stat"), expression, expect(";"), poplex); } function expression(type) { - if (atomicTypes.hasOwnProperty(type)) return cont(maybeoperator); + return expressionInner(type, false); + } + function expressionNoComma(type) { + return expressionInner(type, true); + } + function expressionInner(type, noComma) { + var maybeop = noComma ? maybeoperatorNoComma : maybeoperatorComma; + if (atomicTypes.hasOwnProperty(type)) return cont(maybeop); if (type == "function") return cont(functiondef); - if (type == "keyword c") return cont(maybeexpression); - if (type == "(") return cont(pushlex(")"), maybeexpression, expect(")"), poplex, maybeoperator); - if (type == "operator") return cont(expression); - if (type == "[") return cont(pushlex("]"), commasep(expression, "]"), poplex, maybeoperator); - if (type == "{") return cont(pushlex("}"), commasep(objprop, "}"), poplex, maybeoperator); + if (type == "keyword c") return cont(noComma ? maybeexpressionNoComma : maybeexpression); + if (type == "(") return cont(pushlex(")"), maybeexpression, expect(")"), poplex, maybeop); + if (type == "operator") return cont(noComma ? expressionNoComma : expression); + if (type == "[") return cont(pushlex("]"), commasep(expressionNoComma, "]"), poplex, maybeop); + if (type == "{") return cont(pushlex("}"), commasep(objprop, "}"), poplex, maybeop); return cont(); } function maybeexpression(type) { if (type.match(/[;\}\)\],]/)) return pass(); return pass(expression); } - - function maybeoperator(type, value) { - if (type == "operator" && /\+\+|--/.test(value)) return cont(maybeoperator); - if (type == "operator" && value == "?") return cont(expression, expect(":"), expression); + function maybeexpressionNoComma(type) { + if (type.match(/[;\}\)\],]/)) return pass(); + return pass(expressionNoComma); + } + + function maybeoperatorComma(type, value) { + if (type == ",") return cont(expression); + return maybeoperatorNoComma(type, value, maybeoperatorComma); + } + function maybeoperatorNoComma(type, value, me) { + if (!me) me = maybeoperatorNoComma; + if (type == "operator") { + if (/\+\+|--/.test(value)) return cont(me); + if (value == "?") return cont(expression, expect(":"), expression); + return cont(expression); + } if (type == ";") return; - if (type == "(") return cont(pushlex(")"), commasep(expression, ")"), poplex, maybeoperator); - if (type == ".") return cont(property, maybeoperator); - if (type == "[") return cont(pushlex("]"), expression, expect("]"), poplex, maybeoperator); + if (type == "(") return cont(pushlex(")", "call"), commasep(expressionNoComma, ")"), poplex, me); + if (type == ".") return cont(property, me); + if (type == "[") return cont(pushlex("]"), expression, expect("]"), poplex, me); } function maybelabel(type) { if (type == ":") return cont(poplex, statement); - return pass(maybeoperator, expect(";"), poplex); + return pass(maybeoperatorComma, expect(";"), poplex); } function property(type) { if (type == "variable") {cx.marked = "property"; return cont();} } - function objprop(type) { - if (type == "variable") cx.marked = "property"; - if (atomicTypes.hasOwnProperty(type)) return cont(expect(":"), expression); + function objprop(type, value) { + if (type == "variable") { + cx.marked = "property"; + if (value == "get" || value == "set") return cont(getterSetter); + } else if (type == "number" || type == "string") { + cx.marked = type + " property"; + } + if (atomicTypes.hasOwnProperty(type)) return cont(expect(":"), expressionNoComma); } + function getterSetter(type) { + if (type == ":") return cont(expression); + if (type != "variable") return cont(expect(":"), expression); + cx.marked = "property"; + return cont(functiondef); + } function commasep(what, end) { function proceed(type) { - if (type == ",") return cont(what, proceed); + if (type == ",") { + var lex = cx.state.lexical; + if (lex.info == "call") lex.pos = (lex.pos || 0) + 1; + return cont(what, proceed); + } if (type == end) return cont(); return cont(expect(end)); } - return function commaSeparated(type) { + return function(type) { if (type == end) return cont(); else return pass(what, proceed); }; @@ -332,23 +370,32 @@ return pass(); } function vardef2(type, value) { - if (value == "=") return cont(expression, vardef2); + if (value == "=") return cont(expressionNoComma, vardef2); if (type == ",") return cont(vardef1); } + function maybeelse(indent) { + return function(type, value) { + if (type == "keyword b" && value == "else") { + cx.state.lexical = new JSLexical(indent, 0, "form", null, cx.state.lexical); + return cont(statement, poplex); + } + return pass(); + }; + } function forspec1(type) { if (type == "var") return cont(vardef1, expect(";"), forspec2); if (type == ";") return cont(forspec2); if (type == "variable") return cont(formaybein); - return cont(forspec2); + return pass(expression, expect(";"), forspec2); } function formaybein(_type, value) { if (value == "in") return cont(expression); - return cont(maybeoperator, forspec2); + return cont(maybeoperatorComma, forspec2); } function forspec2(type, value) { if (type == ";") return cont(forspec3); if (value == "in") return cont(expression); - return cont(expression, expect(";"), forspec3); + return pass(expression, expect(";"), forspec3); } function forspec3(type) { if (type != ")") cont(expression); @@ -383,10 +430,10 @@ state.lexical.align = false; state.indented = stream.indentation(); } - if (stream.eatSpace()) return null; + if (state.tokenize != jsTokenComment && stream.eatSpace()) return null; var style = state.tokenize(stream, state); if (type == "comment") return style; - state.lastType = type; + state.lastType = type == "operator" && (content == "++" || content == "--") ? "incdec" : type; return parseJS(state, style, type, content, stream); }, @@ -395,19 +442,25 @@ if (state.tokenize != jsTokenBase) return 0; var firstChar = textAfter && textAfter.charAt(0), lexical = state.lexical; if (lexical.type == "stat" && firstChar == "}") lexical = lexical.prev; + if (statementIndent && lexical.type == ")" && lexical.prev.type == "stat") + lexical = lexical.prev; var type = lexical.type, closing = firstChar == type; + if (type == "vardef") return lexical.indented + (state.lastType == "operator" || state.lastType == "," ? 4 : 0); else if (type == "form" && firstChar == "{") return lexical.indented; else if (type == "form") return lexical.indented + indentUnit; else if (type == "stat") - return lexical.indented + (state.lastType == "operator" || state.lastType == "," ? indentUnit : 0); - else if (lexical.info == "switch" && !closing) + return lexical.indented + (state.lastType == "operator" || state.lastType == "," ? statementIndent || indentUnit : 0); + else if (lexical.info == "switch" && !closing && parserConfig.doubleIndentSwitch != false) return lexical.indented + (/^(?:case|default)\b/.test(textAfter) ? indentUnit : 2 * indentUnit); else if (lexical.align) return lexical.column + (closing ? 0 : 1); else return lexical.indented + (closing ? 0 : indentUnit); }, electricChars: ":{}", + blockCommentStart: jsonMode ? null : "/*", + blockCommentEnd: jsonMode ? null : "*/", + lineComment: jsonMode ? null : "//", jsonMode: jsonMode }; @@ -418,5 +471,6 @@ CodeMirror.defineMIME("application/javascript", "javascript"); CodeMirror.defineMIME("application/ecmascript", "javascript"); CodeMirror.defineMIME("application/json", {name: "javascript", json: true}); +CodeMirror.defineMIME("application/x-json", {name: "javascript", json: true}); CodeMirror.defineMIME("text/typescript", { name: "javascript", typescript: true }); CodeMirror.defineMIME("application/typescript", { name: "javascript", typescript: true });