#1069 support ruby heredocs

This commit is contained in:
nightwing 2012-12-28 15:35:19 +04:00
commit d2d42499ca
4 changed files with 104 additions and 40 deletions

View file

@ -20,4 +20,16 @@ class Range
end end
end end
{:id => 34, :sdfasdfasdf => "asdasdads"} {:id => 34, :key => "value"}
herDocs = [<<'FOO', <<BAR, <<-BAZ, <<-`EXEC`] #comment
FOO #{literal}
FOO
BAR #{fact(10)}
BAR
BAZ indented
BAZ
echo hi
EXEC
puts herDocs

View file

@ -186,19 +186,7 @@ var LuaHighlightRules = function() {
} ] } ]
}; };
var id = 0; this.normalizeRules();
for (var key in this.$rules) {
var rule = this.$rules[key];
for (var i = 0; i < rule.length; i++) {
var state = rule[i];
if (state.next && Array.isArray(state.next)) {
console.log(state.next)
var stateName = state.stateName || ("state" + id++);
this.$rules[stateName] = state.next;
state.next = stateName;
}
}
}
} }
oop.inherits(LuaHighlightRules, TextHighlightRules); oop.inherits(LuaHighlightRules, TextHighlightRules);

View file

@ -38,32 +38,32 @@ var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;
var constantOtherSymbol = exports.constantOtherSymbol = { var constantOtherSymbol = exports.constantOtherSymbol = {
token : "constant.other.symbol.ruby", // symbol token : "constant.other.symbol.ruby", // symbol
regex : "[:](?:[A-Za-z_]|[@$](?=[a-zA-Z0-9_]))[a-zA-Z0-9_]*[!=?]?" regex : "[:](?:[A-Za-z_]|[@$](?=[a-zA-Z0-9_]))[a-zA-Z0-9_]*[!=?]?"
}; };
var qString = exports.qString = { var qString = exports.qString = {
token : "string", // single line token : "string", // single line
regex : "['](?:(?:\\\\.)|(?:[^'\\\\]))*?[']" regex : "['](?:(?:\\\\.)|(?:[^'\\\\]))*?[']"
}; };
var qqString = exports.qqString = { var qqString = exports.qqString = {
token : "string", // single line token : "string", // single line
regex : '["](?:(?:\\\\.)|(?:[^"\\\\]))*?["]' regex : '["](?:(?:\\\\.)|(?:[^"\\\\]))*?["]'
}; };
var tString = exports.tString = { var tString = exports.tString = {
token : "string", // backtick string token : "string", // backtick string
regex : "[`](?:(?:\\\\.)|(?:[^'\\\\]))*?[`]" regex : "[`](?:(?:\\\\.)|(?:[^'\\\\]))*?[`]"
}; };
var constantNumericHex = exports.constantNumericHex = { var constantNumericHex = exports.constantNumericHex = {
token : "constant.numeric", // hex token : "constant.numeric", // hex
regex : "0[xX][0-9a-fA-F](?:[0-9a-fA-F]|_(?=[0-9a-fA-F]))*\\b" regex : "0[xX][0-9a-fA-F](?:[0-9a-fA-F]|_(?=[0-9a-fA-F]))*\\b"
}; };
var constantNumericFloat = exports.constantNumericFloat = { var constantNumericFloat = exports.constantNumericFloat = {
token : "constant.numeric", // float token : "constant.numeric", // float
regex : "[+-]?\\d(?:\\d|_(?=\\d))*(?:(?:\\.\\d(?:\\d|_(?=\\d))*)?(?:[eE][+-]?\\d+)?)?\\b" regex : "[+-]?\\d(?:\\d|_(?=\\d))*(?:(?:\\.\\d(?:\\d|_(?=\\d))*)?(?:[eE][+-]?\\d+)?)?\\b"
}; };
var RubyHighlightRules = function() { var RubyHighlightRules = function() {
@ -171,6 +171,43 @@ var RubyHighlightRules = function() {
}, { }, {
token : "punctuation.separator.key-value", token : "punctuation.separator.key-value",
regex : "=>" regex : "=>"
}, {
stateName: "heredoc",
token : function(value, currentState, stack) {
var next = value[0].length == 2 ? "heredoc" : "indentedHeredoc"
stack.push(next, value[2])
return ["constant", "string", "support.class", "string"]
},
regex : "(<<-?)(['\"`]?)([\\w]+)(['\"`]?)",
rules: {
heredoc: [{
token: function(value, currentState, stack) {
if (value == stack[1]) {
stack.shift();
stack.shift();
return "support.class";
}
return "string";
},
regex: ".*$",
"next": "start"
}],
indentedHeredoc: [{
token: "string",
regex: "^ +"
}, {
token: function(value, currentState, stack) {
if (value == stack[1]) {
stack.shift();
stack.shift();
return "support.class";
}
return "string";
},
regex: ".*$",
"next": "start"
}]
}
}, { }, {
token : "keyword.operator", token : "keyword.operator",
regex : "!|\\$|%|&|\\*|\\-\\-|\\-|\\+\\+|\\+|~|===|==|=|!=|!==|<=|>=|<<=|>>=|>>>=|<>|<|>|!|&&|\\|\\||\\?\\:|\\*=|%=|\\+=|\\-=|&=|\\^=|\\b(?:in|instanceof|new|delete|typeof|void)" regex : "!|\\$|%|&|\\*|\\-\\-|\\-|\\+\\+|\\+|~|===|==|=|!=|!==|<=|>=|<<=|>>=|>>>=|<>|<|>|!|&&|\\|\\||\\?\\:|\\*=|%=|\\+=|\\-=|&=|\\^=|\\b(?:in|instanceof|new|delete|typeof|void)"
@ -196,6 +233,8 @@ var RubyHighlightRules = function() {
} }
] ]
}; };
this.normalizeRules();
}; };
oop.inherits(RubyHighlightRules, TextHighlightRules); oop.inherits(RubyHighlightRules, TextHighlightRules);

View file

@ -54,7 +54,7 @@ var TextHighlightRules = function() {
this.addRules = function(rules, prefix) { this.addRules = function(rules, prefix) {
for (var key in rules) { for (var key in rules) {
var state = rules[key]; var state = rules[key];
for (var i=0; i<state.length; i++) { for (var i = 0; i < state.length; i++) {
var rule = state[i]; var rule = state[i];
if (rule.next) { if (rule.next) {
rule.next = prefix + rule.next; rule.next = prefix + rule.next;
@ -94,7 +94,32 @@ var TextHighlightRules = function() {
this.getEmbeds = function() { this.getEmbeds = function() {
return this.$embeds; return this.$embeds;
};
this.normalizeRules = function() {
var id = 0;
for (var key in this.$rules) {
var rule = this.$rules[key];
for (var i = 0; i < rule.length; i++) {
var state = rule[i];
if (state.next && Array.isArray(state.next)) {
var stateName = state.stateName || ("state" + id++);
this.$rules[stateName] = state.next;
state.next = stateName;
} }
if (state.rules) {
for (var r in state.rules) {
if (this.$rules[r]) {
if (this.$rules[r].push)
this.$rules[r].push.apply(this.$rules[r], state.rules[r]);
} else {
this.$rules[r] = state.rules[r];
}
}
}
}
}
};
this.createKeywordMapper = function(map, defaultToken, ignoreCase, splitChar) { this.createKeywordMapper = function(map, defaultToken, ignoreCase, splitChar) {
var keywords = Object.create(null); var keywords = Object.create(null);