這有點難以做到,因爲您需要計算大括號來檢測javascript模式何時結束。以下代碼似乎效果不錯
define(function(require, exports, module) {
"use strict";
var oop = require("../lib/oop");
var JsonHighlightRules = require("./json_highlight_rules").JsonHighlightRules;
var JavaScriptHighlightRules = require("./javascript_highlight_rules")
.JavaScriptHighlightRules;
var JsonPlusHighlightRules = function() {
JsonHighlightRules.call(this);
this.$rules.start.unshift({
regex: "function",
next: "js-start",
token: "keyword"
});
this.embedRules(JavaScriptHighlightRules, "js-", [{
regex: "[{}]", onMatch: function(val, state, stack) {
this.next = "";
console.log(stack, this.next, val);
if (val == "{") {
stack.unshift("js-start", state);
return "paren";
}
if (val == "}" && stack.length) {
stack.shift();
this.next = stack.shift();
if (this.next.indexOf("quasi") != -1)
return "paren.string";
}
if (val == "}" && !stack.length) {
this.next = "start";
}
return "paren";
}
}], ["no_regex"]);
};
oop.inherits(JsonPlusHighlightRules, JsonHighlightRules);
exports.JsonPlusHighlightRules = JsonPlusHighlightRules;
});
謝謝。任何想法爲什麼要求(「ace/mode/javascript_highlight_rules」)返回未定義?有沒有辦法包括這個? – todotresde
你使用ace-builds還是ace?使用ace-builds,你需要包含'mode-javascript.js'和'mode-json.js',如果你配置require路徑有ace:「path/to/ace/lib」 –