Add standard way to embed other mode/highlighters
This commit is contained in:
parent
cb9bdd120c
commit
e9ddafe9ee
2 changed files with 66 additions and 0 deletions
|
|
@ -140,6 +140,44 @@ var Mode = function() {
|
||||||
|
|
||||||
editor.session.$selectionOccurrences = [];
|
editor.session.$selectionOccurrences = [];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
this.createModeDelegates = function (mapping) {
|
||||||
|
if (!this.$embeds) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.$modes = {};
|
||||||
|
for (var i = 0; i < this.$embeds.length; i++) {
|
||||||
|
this.$modes[this.$embeds[i]] = new mapping[this.$embeds[i]]();
|
||||||
|
}
|
||||||
|
|
||||||
|
var delegations = ['toggleCommentLines', 'getNextLineIndent', 'checkOutdent', 'autoOutdent'];
|
||||||
|
|
||||||
|
for (var i = 0; i < delegations.length; i++) {
|
||||||
|
(function(scope) {
|
||||||
|
var functionName = delegations[i];
|
||||||
|
var defaultHandler = scope[functionName];
|
||||||
|
scope[delegations[i]] = function() {
|
||||||
|
return this.$delegator(functionName, arguments, defaultHandler);
|
||||||
|
}
|
||||||
|
} (this));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this.$delegator = function(method, args, defaultHandler) {
|
||||||
|
var state = args[0];
|
||||||
|
|
||||||
|
for (var i = 0; i < this.$embeds.length; i++) {
|
||||||
|
var split = state.split(this.$embeds[i]);
|
||||||
|
|
||||||
|
if (!split[0] && split[1]) {
|
||||||
|
args[0] = split[1];
|
||||||
|
var mode = this.$modes[this.$embeds[i]];
|
||||||
|
return mode[method].apply(mode, args);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return defaultHandler ? defaultHandler.apply(this, args) : undefined;
|
||||||
|
};
|
||||||
|
|
||||||
}).call(Mode.prototype);
|
}).call(Mode.prototype);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -73,6 +73,34 @@ var TextHighlightRules = function() {
|
||||||
this.getRules = function() {
|
this.getRules = function() {
|
||||||
return this.$rules;
|
return this.$rules;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
this.embedRules = function (HighlightRules, prefix, escapeRules, states) {
|
||||||
|
var embedRules = new HighlightRules().getRules();
|
||||||
|
if (states) {
|
||||||
|
for (var i = 0; i < states.length; i++) {
|
||||||
|
states[i] = prefix + states[i];
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
states = [];
|
||||||
|
for (var key in embedRules) {
|
||||||
|
states.push(prefix + key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.addRules(embedRules, prefix);
|
||||||
|
|
||||||
|
for (var i = 0; i < states.length; i++) {
|
||||||
|
Array.prototype.unshift.apply(this.$rules[states[i]], escapeRules);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!this.$embeds) {
|
||||||
|
this.$embeds = [];
|
||||||
|
}
|
||||||
|
this.$embeds.push(prefix);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.getEmbeds = function() {
|
||||||
|
return this.$embeds;
|
||||||
|
}
|
||||||
|
|
||||||
}).call(TextHighlightRules.prototype);
|
}).call(TextHighlightRules.prototype);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue