From 36caefe3a7cb10a243b6fb52d3b20c4830769008 Mon Sep 17 00:00:00 2001 From: Fabian Jakobs Date: Thu, 29 Sep 2011 12:21:05 +0000 Subject: [PATCH] align coffescript mode closer to the js mode. fix #434 --- lib/ace/mode/coffee_highlight_rules.js | 73 +++++++++++++++++--------- lib/ace/mode/coffee_tokenizer_test.js | 63 ++++++++++++++++++++++ 2 files changed, 110 insertions(+), 26 deletions(-) create mode 100644 lib/ace/mode/coffee_tokenizer_test.js diff --git a/lib/ace/mode/coffee_highlight_rules.js b/lib/ace/mode/coffee_highlight_rules.js index 7ab53559..94b68b9b 100644 --- a/lib/ace/mode/coffee_highlight_rules.js +++ b/lib/ace/mode/coffee_highlight_rules.js @@ -37,18 +37,48 @@ define(function(require, exports, module) { - require("pilot/oop").inherits(CoffeeHighlightRules, - require("ace/mode/text_highlight_rules").TextHighlightRules); + var lang = require("pilot/lang"); + var oop = require("pilot/oop"); + var TextHighlightRules = require("ace/mode/text_highlight_rules").TextHighlightRules; + + oop.inherits(CoffeeHighlightRules, TextHighlightRules); function CoffeeHighlightRules() { var identifier = "[$A-Za-z_\\x7f-\\uffff][$\\w\\x7f-\\uffff]*"; - var keywordend = "(?![$\\w]|\\s*:)"; var stringfill = { token : "string", merge : true, regex : ".+" }; + + var keywords = lang.arrayToMap(( + "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|not|delete|debugger|do|loop|of|off|" + + "or|on|unless|until|and|yes").split("|") + ); + var langConstant = lang.arrayToMap(( + "true|false|null|undefined").split("|") + ); + + var illegal = lang.arrayToMap(( + "case|const|default|function|var|void|with|enum|export|implements|" + + "interface|let|package|private|protected|public|static|yield|" + + "__hasProp|extends|slice|bind|indexOf").split("|") + ); + + var supportClass = lang.arrayToMap(( + "Array|Boolean|Date|Function|Number|Object|RegExp|ReferenceError|" + + "RangeError|String|SyntaxError|Error|EvalError|TypeError|URIError").split("|") + ); + + var supportFunction = lang.arrayToMap(( + "Math|JSON|isNaN|isFinite|parseInt|parseFloat|encodeURI|" + + "encodeURIComponent|decodeURI|decodeURIComponent|RangeError|String|" + + "SyntaxError|Error|EvalError|TypeError|URIError").split("|") + ); + this.$rules = { start : [ { @@ -58,29 +88,20 @@ define(function(require, exports, module) { token : "variable", regex : "@" + identifier }, { - token : "entity.name.function", - regex : identifier + "(?=\\s*:\\s*(?:\\(.*?\\)\\s*)?->)" - }, { - token : "keyword", - regex : "(?:t(?:h(?:is|row|en)|ry|ypeof)|s(?:uper|witch)|return|b(?:reak|y)|c(?:ontinue|atch|lass)|i(?:n(?:stanceof)?|s(?:nt)?|f)|e(?:lse|xtends)|f(?:or (?:own)?|inally|unction)|wh(?:ile|en)|n(?:ew|ot?)|d(?:e(?:lete|bugger)|o)|loop|o(?:ff?|[rn])|un(?:less|til)|and|yes)" - + keywordend - }, { - token : "constant.language", - regex : "(?:true|false|null|undefined)" + keywordend - }, { - token : "invalid.illegal", - regex : "(?:c(?:ase|onst)|default|function|v(?:ar|oid)|with|e(?:num|xport)|i(?:mplements|nterface)|let|p(?:ackage|r(?:ivate|otected)|ublic)|static|yield|__(?:hasProp|extends|slice|bind|indexOf))" - + keywordend - }, { - token : "language.support.class", - regex : "(?:Array|Boolean|Date|Function|Number|Object|R(?:e(?:gExp|ferenceError)|angeError)|S(?:tring|yntaxError)|E(?:rror|valError)|TypeError|URIError)" - + keywordend - }, { - token : "language.support.function", - regex : "(?:Math|JSON|is(?:NaN|Finite)|parse(?:Int|Float)|encodeURI(?:Component)?|decodeURI(?:Component)?)" - + keywordend - }, { - token : "identifier", + token: function(value) { + if (keywords.hasOwnProperty(value)) + return "keyword"; + else if (langConstant.hasOwnProperty(value)) + return "constant.language"; + else if (illegal.hasOwnProperty(value)) + return "invalid.illegal"; + else if (supportClass.hasOwnProperty(value)) + return "language.support.class"; + else if (supportFunction.hasOwnProperty(value)) + return "language.support.function"; + else + return "identifier"; + }, regex : identifier }, { token : "constant.numeric", diff --git a/lib/ace/mode/coffee_tokenizer_test.js b/lib/ace/mode/coffee_tokenizer_test.js new file mode 100644 index 00000000..95c06381 --- /dev/null +++ b/lib/ace/mode/coffee_tokenizer_test.js @@ -0,0 +1,63 @@ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is Ajax.org Code Editor (ACE). + * + * The Initial Developer of the Original Code is + * Ajax.org B.V. + * Portions created by the Initial Developer are Copyright (C) 2010 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Fabian Jakobs + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +if (typeof process !== "undefined") { + require("../../../support/paths"); +} + +define(function(require, exports, module) { + +var Mode = require("ace/mode/coffee").Mode; +var assert = require("ace/test/assertions"); + +module.exports = { + setUp : function() { + this.tokenizer = new Mode().getTokenizer(); + }, + + "test: tokenize keyword": function() { + var tokens = this.tokenizer.getLineTokens("for", "start").tokens; + assert.equal(tokens.length, 1); + assert.equal(tokens[0].type, "keyword"); + } +}; + +}); + +if (typeof module !== "undefined" && module === require.main) { + require("asyncjs").test.testcase(module.exports).exec() +} \ No newline at end of file