Update modes to use new embed syntax

This commit is contained in:
c-spencer 2011-05-25 20:58:45 +01:00
commit 0bad27ff92
13 changed files with 102 additions and 120 deletions

View file

@ -30,6 +30,19 @@ var XmlBehaviour = function () {
selection: [1, 1]
}
}
} else if (text == "\n") {
var cursor = editor.getCursorPosition();
var line = session.doc.getLine(cursor.row);
var rightChars = line.substring(cursor.column, cursor.column + 2);
if (rightChars == '</') {
var indent = this.$getIndent(session.doc.getLine(cursor.row)) + session.getTabString();
var next_indent = this.$getIndent(session.doc.getLine(cursor.row));
return {
text: '\n' + indent + '\n' + next_indent,
selection: [1, indent.length, 1, indent.length]
}
}
}
return false;
});

View file

@ -47,8 +47,6 @@ var TextHighlightRules = require("ace/mode/text_highlight_rules").TextHighlightR
var c_cppHighlightRules = function() {
var docComment = new DocCommentHighlightRules();
var keywords = lang.arrayToMap(
("and|double|not_eq|throw|and_eq|dynamic_cast|operator|true|" +
"asm|else|or|try|auto|enum|or_eq|typedef|bitand|explicit|private|" +
@ -73,7 +71,7 @@ var c_cppHighlightRules = function() {
token : "comment",
regex : "\\/\\/.*$"
},
docComment.getStartRule("doc-start"),
new DocCommentHighlightRules().getStartRule("start"),
{
token : "comment", // multi line comment
regex : "\\/\\*",
@ -161,9 +159,9 @@ var c_cppHighlightRules = function() {
}
]
};
this.addRules(docComment.getRules(), "doc-");
this.$rules["doc-start"][0].next = "start";
this.embedRules(DocCommentHighlightRules, "doc-",
[ new DocCommentHighlightRules().getEndRule("start") ]);
};
oop.inherits(c_cppHighlightRules, TextHighlightRules);

View file

@ -6,8 +6,7 @@ var DocCommentHighlightRules = require("ace/mode/doc_comment_highlight_rules").D
var TextHighlightRules = require("ace/mode/text_highlight_rules").TextHighlightRules;
var CSharpHighlightRules = function() {
var docComment = new DocCommentHighlightRules();
var keywords = lang.arrayToMap(
("abstract|event|new|struct|as|explicit|null|switch|base|extern|object|this|bool|false|operator|throw|break|finally|out|true|byte|fixed|override|try|case|float|params|typeof|catch|for|private|uint|char|foreach|protected|ulong|checked|goto|public|unchecked|class|if|readonly|unsafe|const|implicit|ref|ushort|continue|in|return|using|decimal|int|sbyte|virtual|default|interface|sealed|volatile|delegate|internal|short|void|do|is|sizeof|while|double|lock|stackalloc|else|long|static|enum|namespace|string|var|dynamic").split("|")
);
@ -26,7 +25,7 @@ var CSharpHighlightRules = function() {
token : "comment",
regex : "\\/\\/.*$"
},
docComment.getStartRule("doc-start"),
new DocCommentHighlightRules().getStartRule("doc-start"),
{
token : "comment", // multi line comment
regex : "\\/\\*",
@ -112,9 +111,9 @@ var CSharpHighlightRules = function() {
}
]
};
this.addRules(docComment.getRules(), "doc-");
this.$rules["doc-start"][0].next = "start";
this.embedRules(DocCommentHighlightRules, "doc-",
[ new DocCommentHighlightRules().getEndRule("start") ]);
};
oop.inherits(CSharpHighlightRules, TextHighlightRules);

View file

@ -44,10 +44,6 @@ var DocCommentHighlightRules = function() {
this.$rules = {
"start" : [ {
token : "comment.doc", // closing comment
regex : "\\*\\/",
next : "start"
}, {
token : "comment.doc.tag",
regex : "@[\\w\\d_]+" // TODO: fix email addresses
}, {
@ -77,6 +73,14 @@ oop.inherits(DocCommentHighlightRules, TextHighlightRules);
next: start
};
};
this.getEndRule = function (start) {
return {
token : "comment.doc", // closing comment
regex : "\\*\\/",
next : start
};
}
}).call(DocCommentHighlightRules.prototype);

View file

@ -43,56 +43,33 @@ var JavaScriptMode = require("ace/mode/javascript").Mode;
var CssMode = require("ace/mode/css").Mode;
var Tokenizer = require("ace/tokenizer").Tokenizer;
var HtmlHighlightRules = require("ace/mode/html_highlight_rules").HtmlHighlightRules;
var XmlBehaviour = require("ace/mode/behaviour/xml").XmlBehaviour;
var Mode = function() {
this.$tokenizer = new Tokenizer(new HtmlHighlightRules().getRules());
this.$js = new JavaScriptMode();
this.$css = new CssMode();
var highlighter = new HtmlHighlightRules();
this.$tokenizer = new Tokenizer(highlighter.getRules());
this.$behaviour = new XmlBehaviour();
this.$embeds = highlighter.getEmbeds();
this.createModeDelegates({
"js-": JavaScriptMode,
"css-": CssMode
});
};
oop.inherits(Mode, TextMode);
(function() {
this.toggleCommentLines = function(state, doc, startRow, endRow) {
this.$delegate("toggleCommentLines", arguments, function() {
return 0;
});
return 0;
};
this.getNextLineIndent = function(state, line, tab) {
var self = this;
return this.$delegate("getNextLineIndent", arguments, function() {
return self.$getIndent(line);
});
return this.$getIndent(line);
};
this.checkOutdent = function(state, line, input) {
return this.$delegate("checkOutdent", arguments, function() {
return false;
});
};
this.autoOutdent = function(state, doc, row) {
this.$delegate("autoOutdent", arguments);
};
this.$delegate = function(method, args, defaultHandler) {
var state = args[0];
var split = state.split("js-");
if (!split[0] && split[1]) {
args[0] = split[1];
return this.$js[method].apply(this.$js, args);
}
var split = state.split("css-");
if (!split[0] && split[1]) {
args[0] = split[1];
return this.$css[method].apply(this.$css, args);
}
return defaultHandler ? defaultHandler() : undefined;
return false;
};
}).call(Mode.prototype);

View file

@ -173,10 +173,8 @@ var HtmlHighlightRules = function() {
regex : ".+"
} ]
};
var jsRules = new JavaScriptHighlightRules().getRules();
this.addRules(jsRules, "js-");
this.$rules["js-start"].unshift({
this.embedRules(JavaScriptHighlightRules, "js-", [{
token: "comment",
regex: "\\/\\/.*(?=<\\/script>)",
next: "tag"
@ -184,15 +182,13 @@ var HtmlHighlightRules = function() {
token: "text",
regex: "<\\/(?=script)",
next: "tag"
});
var cssRules = new CssHighlightRules().getRules();
this.addRules(cssRules, "css-");
this.$rules["css-start"].unshift({
}], ["js-start"]);
this.embedRules(CssHighlightRules, "css-", [{
token: "text",
regex: "<\\/(?=style)",
next: "tag"
});
}], ["css-start"]);
};
oop.inherits(HtmlHighlightRules, TextHighlightRules);

View file

@ -7,8 +7,6 @@ var TextHighlightRules = require("ace/mode/text_highlight_rules").TextHighlightR
var JavaHighlightRules = function() {
var docComment = new DocCommentHighlightRules();
// taken from http://download.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html
var keywords = lang.arrayToMap(
("abstract|continue|for|new|switch|" +
@ -66,7 +64,7 @@ var JavaHighlightRules = function() {
token : "comment",
regex : "\\/\\/.*$"
},
docComment.getStartRule("doc-start"),
new DocCommentHighlightRules().getStartRule("doc-start"),
{
token : "comment", // multi line comment
regex : "\\/\\*",
@ -156,9 +154,9 @@ var JavaHighlightRules = function() {
}
]
};
this.addRules(docComment.getRules(), "doc-");
this.$rules["doc-start"][0].next = "start";
this.embedRules(DocCommentHighlightRules, "doc-",
[ new DocCommentHighlightRules().getEndRule("start") ]);
};
oop.inherits(JavaHighlightRules, TextHighlightRules);

View file

@ -45,8 +45,6 @@ var TextHighlightRules = require("ace/mode/text_highlight_rules").TextHighlightR
var JavaScriptHighlightRules = function() {
var docComment = new DocCommentHighlightRules();
var keywords = lang.arrayToMap(
("break|case|catch|continue|default|delete|do|else|finally|for|function|" +
"if|in|instanceof|new|return|switch|throw|try|typeof|let|var|while|with|" +
@ -71,7 +69,7 @@ var JavaScriptHighlightRules = function() {
token : "comment",
regex : "\\/\\/.*$"
},
docComment.getStartRule("doc-start"),
new DocCommentHighlightRules().getStartRule("doc-start"),
{
token : "comment", // multi line comment
regex : "\\/\\*",
@ -168,9 +166,9 @@ var JavaScriptHighlightRules = function() {
}
]
};
this.addRules(docComment.getRules(), "doc-");
this.$rules["doc-start"][0].next = "start";
this.embedRules(DocCommentHighlightRules, "doc-",
[ new DocCommentHighlightRules().getEndRule("start") ]);
};
oop.inherits(JavaScriptHighlightRules, TextHighlightRules);

View file

@ -44,9 +44,7 @@ var DocCommentHighlightRules = require("ace/mode/doc_comment_highlight_rules").D
var TextHighlightRules = require("ace/mode/text_highlight_rules").TextHighlightRules;
var PhpHighlightRules = function() {
var docComment = new DocCommentHighlightRules();
var builtinFunctions = lang.arrayToMap(
('abs|acos|acosh|addcslashes|addslashes|aggregate|aggregate_info|aggregate_methods|' +
'aggregate_methods_by_list|aggregate_methods_by_regexp|aggregate_properties|aggregate_properties_by_list|' +
@ -476,7 +474,7 @@ var PhpHighlightRules = function() {
token : "comment",
regex : "#.*$"
},
docComment.getStartRule("doc-start"),
new DocCommentHighlightRules().getStartRule("doc-start"),
{
token : "comment", // multi line comment
regex : "\\/\\*",
@ -596,9 +594,9 @@ var PhpHighlightRules = function() {
}
]
};
this.addRules(docComment.getRules(), "doc-");
this.$rules["doc-start"][0].next = "start";
this.embedRules(DocCommentHighlightRules, "doc-",
[ new DocCommentHighlightRules().getEndRule("start") ]);
};
oop.inherits(PhpHighlightRules, TextHighlightRules);

View file

@ -42,10 +42,18 @@ var XmlMode = require("ace/mode/text").Mode;
var JavaScriptMode = require("ace/mode/javascript").Mode;
var Tokenizer = require("ace/tokenizer").Tokenizer;
var SvgHighlightRules = require("ace/mode/svg_highlight_rules").SvgHighlightRules;
var XmlBehaviour = require("ace/mode/behaviour/xml").XmlBehaviour;
var Mode = function() {
this.$tokenizer = new Tokenizer(new SvgHighlightRules().getRules());
this.$js = new JavaScriptMode();
this.highlighter = new SvgHighlightRules();
this.$tokenizer = new Tokenizer(highlighter.getRules());
this.$behaviour = new XmlBehaviour();
this.$embeds = highlighter.getEmbeds();
this.createModeDelegates({
"js-": JavaScriptMode
});
};
oop.inherits(Mode, XmlMode);
@ -53,38 +61,15 @@ oop.inherits(Mode, XmlMode);
(function() {
this.toggleCommentLines = function(state, doc, startRow, endRow) {
this.$delegate("toggleCommentLines", arguments, function() {
return 0;
});
return 0;
};
this.getNextLineIndent = function(state, line, tab) {
var self = this;
return this.$delegate("getNextLineIndent", arguments, function() {
return self.$getIndent(line);
});
return self.$getIndent(line);
};
this.checkOutdent = function(state, line, input) {
return this.$delegate("checkOutdent", arguments, function() {
return false;
});
};
this.autoOutdent = function(state, doc, row) {
this.$delegate("autoOutdent", arguments);
};
this.$delegate = function(method, args, defaultHandler) {
var state = args[0];
var split = state.split("js-");
if (!split[0] && split[1]) {
args[0] = split[1];
return this.$js[method].apply(this.$js, args);
}
return defaultHandler ? defaultHandler() : undefined;
return false;
};
}).call(Mode.prototype);

View file

@ -65,11 +65,9 @@ var SvgHighlightRules = function() {
}, {
token : "string",
regex : "'.*?'"
}];
var jsRules = new JavaScriptHighlightRules().getRules();
this.addRules(jsRules, "js-");
this.$rules["js-start"].unshift({
}];
this.embedRules(JavaScriptHighlightRules, "js-", [{
token: "comment",
regex: "\\/\\/.*(?=<\\/script>)",
next: "tag"
@ -77,7 +75,7 @@ var SvgHighlightRules = function() {
token: "text",
regex: "<\\/(?=script)",
next: "tag"
});
}], ["js-start"]);
};

View file

@ -150,7 +150,9 @@ var Mode = function() {
}
this.$modes = {};
for (var i = 0; i < this.$embeds.length; i++) {
this.$modes[this.$embeds[i]] = new mapping[this.$embeds[i]]();
if (mapping[this.$embeds[i]]) {
this.$modes[this.$embeds[i]] = new mapping[this.$embeds[i]]();
}
}
var delegations = ['toggleCommentLines', 'getNextLineIndent', 'checkOutdent', 'autoOutdent', 'transformAction'];
@ -171,15 +173,16 @@ var Mode = function() {
for (var i = 0; i < this.$embeds.length; i++) {
var split = state.split(this.$embeds[i]);
if (!this.$modes[this.$embeds[i]]) continue;
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;
var ret = defaultHandler.apply(this, args);
return defaultHandler ? ret : undefined;
};
this.transformAction = function(state, action, editor, session, param) {

View file

@ -53,6 +53,19 @@ var TextHighlightRules = function() {
};
};
// Could probably do with improving and moving into lang.
var deepCopy = function (o) {
var clone = o.constructor();
for (var k in o) {
if (typeof o[k] === "object") {
clone[k] = deepCopy(o[k]);
} else {
clone[k] = o[k];
}
}
return clone;
};
(function() {
this.addRules = function(rules, prefix) {
@ -60,11 +73,13 @@ var TextHighlightRules = function() {
var state = rules[key];
for (var i=0; i<state.length; i++) {
var rule = state[i];
var onext = rule.next;
if (rule.next) {
rule.next = prefix + rule.next;
} else {
rule.next = prefix + key;
}
console.log(prefix, key, onext, rule.next);
}
this.$rules[prefix + key] = state;
}
@ -89,7 +104,7 @@ var TextHighlightRules = function() {
this.addRules(embedRules, prefix);
for (var i = 0; i < states.length; i++) {
Array.prototype.unshift.apply(this.$rules[states[i]], escapeRules);
Array.prototype.unshift.apply(this.$rules[states[i]], deepCopy(escapeRules));
}
if (!this.$embeds) {