diff --git a/lib/ace/mode/coffee_highlight_rules.js b/lib/ace/mode/coffee_highlight_rules.js index 98ba0659..98253f49 100644 --- a/lib/ace/mode/coffee_highlight_rules.js +++ b/lib/ace/mode/coffee_highlight_rules.js @@ -45,25 +45,28 @@ define(function(require, exports, module) { }; var keywords = ( - "this|throw|then|try|typeof|super|switch|return|break|by)|continue|" + + "this|throw|then|try|typeof|super|switch|return|break|by|continue|" + "catch|class|in|instanceof|is|isnt|if|else|extends|for|forown|" + "finally|function|while|when|new|no|not|delete|debugger|do|loop|of|off|" + "or|on|unless|until|and|yes" ); var langConstant = ( - "true|false|null|undefined" + "true|false|null|undefined|NaN|Infinity" ); var illegal = ( "case|const|default|function|var|void|with|enum|export|implements|" + "interface|let|package|private|protected|public|static|yield|" + - "__hasProp|extends|slice|bind|indexOf" + "__hasProp|slice|bind|indexOf" ); var supportClass = ( - "Array|Boolean|Date|Function|Number|Object|RegExp|ReferenceError|" + - "String|RangeError|SyntaxError|Error|EvalError|TypeError|URIError" + "Array|Boolean|Date|Function|Number|Object|RegExp|ReferenceError|String|" + + "Error|EvalError|InternalError|RangeError|ReferenceError|StopIteration|" + + "SyntaxError|TypeError|URIError|" + + "ArrayBuffer|Float32Array|Float64Array|Int16Array|Int32Array|Int8Array|" + + "Uint16Array|Uint32Array|Uint8Array|Uint8ClampedArray" ); var supportFunction = ( @@ -71,26 +74,44 @@ define(function(require, exports, module) { "encodeURIComponent|decodeURI|decodeURIComponent|String|" ); + var variableLanguage = ( + "window|arguments|prototype|document" + ); + var keywordMapper = this.createKeywordMapper({ "keyword": keywords, "constant.language": langConstant, "invalid.illegal": illegal, "language.support.class": supportClass, "language.support.function": supportFunction, + "variable.language": variableLanguage }, "identifier"); + var headRe = "[$A-Za-z_\\x7f-\\uffff]"; + + var functionRe = { + "({args})->": { + token: ["paren.lparen", "text", "paren.lparen", "text", "variable.parameter", "text", "paren.rparen", "text", "paren.rparen", "text", "storage.type"], + regex: "(\\()(\\s*)(\\{)(\\s*)(" + headRe + "[$\\w\\s,\\x7f-\\uffff]*" + ")(\\s*)(\\})(\\s*)(\\))(\\s*)([\\-=]>)" + }, + "({})->": { + token: ["paren.lparen", "text", "paren.lparen", "text", "paren.rparen", "text", "paren.rparen", "text", "storage.type"], + regex: "(\\()(\\s*)(\\{)(\\s*)(\\})(\\s*)(\\))(\\s*)([\\-=]>)" + }, + "(args)->": { + token: ["paren.lparen", "text", "variable.parameter", "text", "paren.rparen", "text", "storage.type"], + regex: "(\\()(\\s*)(" + [headRe, headRe + "[$\\w\\x7f-\\uffff]", headRe + "[^#)]*[^#(){}=,\\/\\\\]"].join("|") + ")(\\s*)(\\))(\\s*)([\\-=]>)" + }, + "()->": { + token: ["paren.lparen", "text", "paren.rparen", "text", "storage.type"], + regex: "(\\()(\\s*)(\\))(\\s*)([\\-=]>)" + } + }; + + this.$rules = { start : [ { - token : "identifier", - regex : "(?:(?:\\.|::)\\s*)" + identifier - }, { - token : "variable", - regex : "@(?:" + identifier + ")?" - }, { - token: keywordMapper, - regex : identifier - }, { token : "constant.numeric", regex : "(?:0x[\\da-fA-F]+|(?:\\d+(?:\\.\\d+)?|\\.\\d+)(?:[eE][+-]?\\d+)?)" }, { @@ -134,12 +155,84 @@ define(function(require, exports, module) { }, { token : "comment", regex : "#.*" + }, { + token : [ + "punctuation.operator", "identifier" + ], + regex : "(\\.)(" + illegal + ")" + }, { + token : "punctuation.operator", + regex : "\\." + }, { + //class A extends B + token : [ + "keyword", "text", "language.support.class", "text", "keyword", "text", "language.support.class" + ], + regex : "(class)(\\s+)(" + identifier + ")(\\s+)(extends)(\\s+)(" + identifier + ")" + }, { + //class A + token : [ + "keyword", "text", "language.support.class" + ], + regex : "(class)(\\s+)(" + identifier + ")" + }, { + //play = ({args}) -> + //play : ({args}) -> + token : [ + "entity.name.function", "text", "keyword.operator", "text" + ].concat(functionRe["({args})->"].token), + regex : "(" + identifier + ")(\\s*)(=|:)(\\s*)" + functionRe["({args})->"].regex + }, { + //play = ({}) -> + //play : ({}) -> + token : [ + "entity.name.function", "text", "keyword.operator", "text" + ].concat(functionRe["({})->"].token), + regex : "(" + identifier + ")(\\s*)(=|:)(\\s*)" + functionRe["({})->"].regex + }, { + //play = (args) -> + //play : (args) -> + token : [ + "entity.name.function", "text", "keyword.operator", "text" + ].concat(functionRe["(args)->"].token), + regex : "(" + identifier + ")(\\s*)(=|:)(\\s*)" + functionRe["(args)->"].regex + }, { + //play = () -> + //play : () -> + token : [ + "entity.name.function", "text", "keyword.operator", "text" + ].concat(functionRe["()->"].token), + regex : "(" + identifier + ")(\\s*)(=|:)(\\s*)" + functionRe["()->"].regex + }, { + //play = -> + //play : -> + token : [ + "entity.name.function", "text", "keyword.operator", "text", "storage.type" + ], + regex : "(" + identifier + ")(\\s*)(=|:)(\\s*)([\\-=]>)" + }, + functionRe["({args})->"], + functionRe["({})->"], + functionRe["(args)->"], + functionRe["()->"] + , { + token : "identifier", + regex : "(?:(?:\\.|::)\\s*)" + identifier + }, { + token : "variable", + regex : "@(?:" + identifier + ")?" + }, { + token: keywordMapper, + regex : identifier }, { token : "punctuation.operator", regex : "\\?|\\:|\\,|\\." + }, { + token : "storage.type", + regex : "[\\-=]>" }, { token : "keyword.operator", - regex : "(?:[\\-=]>|[-+*/%<>&|^!?=]=|>>>=?|\\-\\-|\\+\\+|::|&&=|\\|\\|=|<<=|>>=|\\?\\.|\\.{2,3}|[!*+-=><])" + regex : "(?:[-+*/%<>&|^!?=]=|>>>=?|\\-\\-|\\+\\+|::|&&=|\\|\\|=|<<=|>>=|\\?\\.|\\.{2,3}|[!*+-=><])" }, { token : "paren.lparen", regex : "[({[]" diff --git a/lib/ace/mode/coffee_highlight_rules_test.js b/lib/ace/mode/coffee_highlight_rules_test.js index eaf85531..d3914f02 100644 --- a/lib/ace/mode/coffee_highlight_rules_test.js +++ b/lib/ace/mode/coffee_highlight_rules_test.js @@ -41,6 +41,11 @@ var assert = require("../test/assertions"); module.exports = { setUp : function() { this.tokenizer = new Mode().getTokenizer(); + this.testTokens = function(tokens, correct) { + correct.forEach(function(type, i) { + assert.equal(tokens[i].type, type); + }); + }; }, "test: tokenize keyword": function() { @@ -48,7 +53,208 @@ module.exports = { assert.equal(tokens.length, 1); assert.equal(tokens[0].type, "keyword"); }, + + "test: tokenize function: 'foo = ({args}) ->'": function() { + var tokens = this.tokenizer.getLineTokens("foo = ({args}) ->", "start").tokens; + var correct = [ + "entity.name.function", "text", "keyword.operator", "text", + "paren.lparen", "paren.lparen", "variable.parameter", "paren.rparen", "paren.rparen", "text", "storage.type" + ]; + + assert.equal(tokens.length, 11); + this.testTokens(tokens, correct); + + tokens = this.tokenizer.getLineTokens("foo = ({arg1, arg2}) ->", "start").tokens; + assert.equal(tokens.length, 11); + this.testTokens(tokens, correct); + + tokens = this.tokenizer.getLineTokens("foo : ({arg1, arg2}) ->", "start").tokens; + assert.equal(tokens.length, 11); + this.testTokens(tokens, correct); + }, + + "test: tokenize function: invalid case: 'foo = ({args}) ->'": function() { + var tokens = this.tokenizer.getLineTokens("foo = ({0abc}) ->", "start").tokens; + assert.notEqual(tokens[0].type, "entity.name.function"); + + tokens = this.tokenizer.getLineTokens("foo = ({/abc}) ->", "start").tokens; + assert.notEqual(tokens[0].type, "entity.name.function"); + + tokens = this.tokenizer.getLineTokens("foo = ({abc/}) ->", "start").tokens; + assert.notEqual(tokens[0].type, "entity.name.function"); + + tokens = this.tokenizer.getLineTokens("foo = ({#abc}) ->", "start").tokens; + assert.notEqual(tokens[0].type, "entity.name.function"); + + tokens = this.tokenizer.getLineTokens("foo = ({abc#}) ->", "start").tokens; + assert.notEqual(tokens[0].type, "entity.name.function"); + + tokens = this.tokenizer.getLineTokens("foo = ({)abc}) ->", "start").tokens; + assert.notEqual(tokens[0].type, "entity.name.function"); + + tokens = this.tokenizer.getLineTokens("foo = ({abc)}) ->", "start").tokens; + assert.notEqual(tokens[0].type, "entity.name.function"); + + tokens = this.tokenizer.getLineTokens("foo = ({a{bc}) ->", "start").tokens; + assert.notEqual(tokens[0].type, "entity.name.function"); + }, + + "test: tokenize function: 'foo = ({}) ->'": function() { + var tokens = this.tokenizer.getLineTokens("foo = ({}) ->", "start").tokens; + var correct = [ + "entity.name.function", "text", "keyword.operator", "text", + "paren.lparen", "paren.lparen", "paren.rparen", "paren.rparen", "text", "storage.type" + ]; + + assert.equal(tokens.length, 10); + this.testTokens(tokens, correct); + + tokens = this.tokenizer.getLineTokens("foo : ({}) ->", "start").tokens; + assert.equal(tokens.length, 10); + this.testTokens(tokens, correct); + + tokens = this.tokenizer.getLineTokens("foo = ({ }) ->", "start").tokens; + correct = [ + "entity.name.function", "text", "keyword.operator", "text", + "paren.lparen", "paren.lparen", "text", "paren.rparen", "paren.rparen", "text", "storage.type" + ]; + assert.equal(tokens.length, 11); + this.testTokens(tokens, correct); + }, + + "test: tokenize function: 'foo = (args) ->'": function() { + var tokens = this.tokenizer.getLineTokens("foo = (args) ->", "start").tokens; + var correct = [ + "entity.name.function", "text", "keyword.operator", "text", + "paren.lparen", "variable.parameter", "paren.rparen", "text", "storage.type" + ]; + assert.equal(tokens.length, 9); + this.testTokens(tokens, correct); + + tokens = this.tokenizer.getLineTokens("foo = (arg1, arg2) ->", "start").tokens; + assert.equal(tokens.length, 9); + this.testTokens(tokens, correct); + + tokens = this.tokenizer.getLineTokens("foo = (arg1 = 1, arg2 = 'name') ->", "start").tokens; + assert.equal(tokens.length, 9); + this.testTokens(tokens, correct); + }, + "test: tokenize function: invalid case: 'foo=(args) ->'": function() { + var tokens = this.tokenizer.getLineTokens("foo=(args#) ->", "start").tokens; + assert.notEqual(tokens[0].type, "entity.name.function"); + + tokens = this.tokenizer.getLineTokens("foo=(args=) ->", "start").tokens; + assert.notEqual(tokens[0].type, "entity.name.function"); + + tokens = this.tokenizer.getLineTokens("foo=(args{) ->", "start").tokens; + assert.notEqual(tokens[0].type, "entity.name.function"); + + tokens = this.tokenizer.getLineTokens("foo=(args}) ->", "start").tokens; + assert.notEqual(tokens[0].type, "entity.name.function"); + + tokens = this.tokenizer.getLineTokens("foo=(}args) ->", "start").tokens; + assert.notEqual(tokens[0].type, "entity.name.function"); + + tokens = this.tokenizer.getLineTokens("foo=(a)rgs) ->", "start").tokens; + assert.notEqual(tokens[0].type, "entity.name.function"); + + tokens = this.tokenizer.getLineTokens("foo=(args/) ->", "start").tokens; + assert.notEqual(tokens[0].type, "entity.name.function"); + + tokens = this.tokenizer.getLineTokens("foo=(args\\) ->", "start").tokens; + assert.notEqual(tokens[0].type, "entity.name.function"); + }, + + "test: tokenize function: 'foo = () ->'": function() { + var tokens = this.tokenizer.getLineTokens("foo = () ->", "start").tokens; + var correct = [ + "entity.name.function", "text", "keyword.operator", "text", + "paren.lparen", "paren.rparen", "text", "storage.type" + ]; + + assert.equal(tokens.length, 8); + this.testTokens(tokens, correct); + + tokens = this.tokenizer.getLineTokens("foo : ( ) ->", "start").tokens; + correct = [ + "entity.name.function", "text", "keyword.operator", "text", + "paren.lparen", "text", "paren.rparen", "text", "storage.type" + ]; + assert.equal(tokens.length, 9); + this.testTokens(tokens, correct); + }, + + "test: tokenize function: 'window.foo = (args) ->'": function() { + var tokens = this.tokenizer.getLineTokens("window.foo = (args) ->", "start").tokens; + var correct = [ + "variable.language", "punctuation.operator", "entity.name.function", "text", "keyword.operator", "text", + "paren.lparen", "variable.parameter", "paren.rparen", "text", "storage.type" + ]; + + assert.equal(tokens.length, 11); + this.testTokens(tokens, correct); + + this.tokenizer.getLineTokens("window.foo = (args) ->", "start").tokens; + assert.equal(tokens.length, 11); + this.testTokens(tokens, correct); + }, + + "test: tokenize function: 'foo = ->'": function() { + var tokens = this.tokenizer.getLineTokens("foo = ->", "start").tokens; + var correct = [ + "entity.name.function", "text", "keyword.operator", "text", "storage.type" + ]; + + assert.equal(tokens.length, 5); + this.testTokens(tokens, correct); + + this.tokenizer.getLineTokens("foo : ->", "start").tokens; + assert.equal(tokens.length, 5); + this.testTokens(tokens, correct); + }, + + "test: tokenize callback function: 'foo bar: 1, (args) ->'": function() { + var tokens = this.tokenizer.getLineTokens("foo bar: 1, (args) ->", "start").tokens; + var correct = [ + "identifier", "text", "identifier", "punctuation.operator", "text", "constant.numeric", "punctuation.operator", "text", + "paren.lparen", "variable.parameter", "paren.rparen", "text", "storage.type" + ]; + + assert.equal(tokens.length, 13); + this.testTokens(tokens, correct); + }, + + "test: tokenize class: 'class Foo'": function() { + var tokens = this.tokenizer.getLineTokens("class Foo", "start").tokens; + var correct = [ + "keyword", "text", "language.support.class" + ]; + + assert.equal(tokens.length, 3); + this.testTokens(tokens, correct); + }, + + "test: tokenize class 'class Foo extends Bar'": function() { + var tokens = this.tokenizer.getLineTokens("class Foo extends Bar", "start").tokens; + var correct = [ + "keyword", "text", "language.support.class", "text", "keyword", "text", "language.support.class" + ]; + + assert.equal(tokens.length, 7); + this.testTokens(tokens, correct); + }, + + "test: tokenize illegal name property: 'foo.static.function'": function() { + var tokens = this.tokenizer.getLineTokens("foo.static.function", "start").tokens; + var correct = [ + "identifier", "punctuation.operator", "identifier", "punctuation.operator", "identifier" + ]; + + assert.equal(tokens.length, 5); + this.testTokens(tokens, correct); + }, + // TODO: disable. not yet implemented "!test tokenize string with interpolation": function() { var tokens = this.tokenizer.getLineTokens('"#{ 22 / 7 } is a decent approximation of π"', "start").tokens;