#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
{: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;
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;
}
}
}
this.normalizeRules();
}
oop.inherits(LuaHighlightRules, TextHighlightRules);

View file

@ -3,7 +3,7 @@
*
* Copyright (c) 2010, Ajax.org B.V.
* All rights reserved.
*
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
@ -14,7 +14,7 @@
* * Neither the name of Ajax.org B.V. nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
@ -35,35 +35,35 @@ var oop = require("../lib/oop");
var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;
// exports is for Haml
var constantOtherSymbol = exports.constantOtherSymbol = {
token : "constant.other.symbol.ruby", // symbol
regex : "[:](?:[A-Za-z_]|[@$](?=[a-zA-Z0-9_]))[a-zA-Z0-9_]*[!=?]?"
};
var constantOtherSymbol = exports.constantOtherSymbol = {
token : "constant.other.symbol.ruby", // symbol
regex : "[:](?:[A-Za-z_]|[@$](?=[a-zA-Z0-9_]))[a-zA-Z0-9_]*[!=?]?"
};
var qString = exports.qString = {
token : "string", // single line
regex : "['](?:(?:\\\\.)|(?:[^'\\\\]))*?[']"
};
token : "string", // single line
regex : "['](?:(?:\\\\.)|(?:[^'\\\\]))*?[']"
};
var qqString = exports.qqString = {
token : "string", // single line
regex : '["](?:(?:\\\\.)|(?:[^"\\\\]))*?["]'
};
token : "string", // single line
regex : '["](?:(?:\\\\.)|(?:[^"\\\\]))*?["]'
};
var tString = exports.tString = {
token : "string", // backtick string
regex : "[`](?:(?:\\\\.)|(?:[^'\\\\]))*?[`]"
};
token : "string", // backtick string
regex : "[`](?:(?:\\\\.)|(?:[^'\\\\]))*?[`]"
};
var constantNumericHex = exports.constantNumericHex = {
token : "constant.numeric", // hex
regex : "0[xX][0-9a-fA-F](?:[0-9a-fA-F]|_(?=[0-9a-fA-F]))*\\b"
};
token : "constant.numeric", // hex
regex : "0[xX][0-9a-fA-F](?:[0-9a-fA-F]|_(?=[0-9a-fA-F]))*\\b"
};
var constantNumericFloat = exports.constantNumericFloat = {
token : "constant.numeric", // float
regex : "[+-]?\\d(?:\\d|_(?=\\d))*(?:(?:\\.\\d(?:\\d|_(?=\\d))*)?(?:[eE][+-]?\\d+)?)?\\b"
};
token : "constant.numeric", // float
regex : "[+-]?\\d(?:\\d|_(?=\\d))*(?:(?:\\.\\d(?:\\d|_(?=\\d))*)?(?:[eE][+-]?\\d+)?)?\\b"
};
var RubyHighlightRules = function() {
@ -139,7 +139,7 @@ var RubyHighlightRules = function() {
}, {
token : "string.regexp",
regex : "[/](?:(?:\\[(?:\\\\]|[^\\]])+\\])|(?:\\\\/|[^\\]/]))*[/]\\w*\\s*(?=[).,;]|$)"
},
},
qString,
qqString,
@ -154,12 +154,12 @@ var RubyHighlightRules = function() {
}, {
token : "support.class", // class name
regex : "[A-Z][a-zA-Z_\\d]+"
},
},
constantOtherSymbol,
constantNumericHex,
constantNumericFloat,
{
token : "constant.language.boolean",
regex : "(?:true|false)\\b"
@ -171,6 +171,43 @@ var RubyHighlightRules = function() {
}, {
token : "punctuation.separator.key-value",
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",
regex : "!|\\$|%|&|\\*|\\-\\-|\\-|\\+\\+|\\+|~|===|==|=|!=|!==|<=|>=|<<=|>>=|>>>=|<>|<|>|!|&&|\\|\\||\\?\\:|\\*=|%=|\\+=|\\-=|&=|\\^=|\\b(?:in|instanceof|new|delete|typeof|void)"
@ -196,6 +233,8 @@ var RubyHighlightRules = function() {
}
]
};
this.normalizeRules();
};
oop.inherits(RubyHighlightRules, TextHighlightRules);

View file

@ -54,7 +54,7 @@ var TextHighlightRules = function() {
this.addRules = function(rules, prefix) {
for (var key in rules) {
var state = rules[key];
for (var i=0; i<state.length; i++) {
for (var i = 0; i < state.length; i++) {
var rule = state[i];
if (rule.next) {
rule.next = prefix + rule.next;
@ -94,7 +94,32 @@ var TextHighlightRules = function() {
this.getEmbeds = function() {
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) {
var keywords = Object.create(null);