extract doc comments into a separate mode

This commit is contained in:
Fabian Jakobs 2010-04-26 17:40:40 +02:00
commit 1a4f0cd054
12 changed files with 101 additions and 85 deletions

View file

@ -42,7 +42,9 @@
<script src="../src/ace/ace.js" type="text/javascript" charset="utf-8"></script>
<script src="../src/ace/mode/Text.js" type="text/javascript" charset="utf-8"></script>
<script src="../src/ace/mode/TextHighlightRules.js" type="text/javascript" charset="utf-8"></script>
<script src="../src/ace/mode/JavaScript.js" type="text/javascript" charset="utf-8"></script>
<script src="../src/ace/mode/DocCommentHighlightRules.js" type="text/javascript" charset="utf-8"></script>
<script src="../src/ace/mode/JavaScriptHighlightRules.js" type="text/javascript" charset="utf-8"></script>
<script src="../src/ace/mode/Html.js" type="text/javascript" charset="utf-8"></script>
<script src="../src/ace/mode/HtmlHighlightRules.js" type="text/javascript" charset="utf-8"></script>

View file

@ -3,8 +3,9 @@ server: http://localhost:4224
load:
- src/ace/ace.js
- src/ace/MEventEmitter.js
- src/ace/MEventEmitter.js
- src/ace/mode/Text.js
- src/ace/mode/TextHighlightRules.js
- src/ace/mode/*.js
- src/ace/layer/*.js
- src/ace/*.js

View file

@ -72,7 +72,6 @@ ace.Document = function(text, mode) {
this.$detectNewLine = function(text) {
var match = text.match(/^.*?(\r?\n)/m);
console.log(match);
if (match) {
this.$autoNewLine = match[1];
} else {

View file

@ -175,10 +175,4 @@ ace.mode.CssHighlightRules = function() {
};
};
(function() {
this.getRules = function() {
return this.$rules;
};
}).call(ace.mode.CssHighlightRules.prototype);
ace.inherits(ace.mode.CssHighlightRules, ace.mode.TextHighlightRules);

View file

@ -0,0 +1,38 @@
ace.provide("ace.mode.DocCommentHighlightRules");
ace.mode.DocCommentHighlightRules = function() {
this.$rules = {
"start" : [ {
token : "doc-comment", // closing comment
regex : "\\*\\/",
next : "start"
}, {
token : "doc-comment-tag",
regex : "@[\\w\\d_]+"
}, {
token : "doc-comment",
regex : "\s+"
}, {
token : "doc-comment",
regex : "[^@\\*]+"
}, {
token : "doc-comment",
regex : "."
}]
};
};
ace.inherits(ace.mode.DocCommentHighlightRules, ace.mode.TextHighlightRules);
(function() {
this.getStartRule = function(start) {
return {
token : "doc-comment", // doc comment
regex : "\\/\\*\\*",
next: start
};
};
}).call(ace.mode.DocCommentHighlightRules.prototype);

View file

@ -114,7 +114,7 @@ ace.mode.HtmlHighlightRules = function() {
};
var jsRules = new ace.mode.JavaScriptHighlightRules().getRules();
this.$addRules(jsRules, "js-");
this.addRules(jsRules, "js-");
this.$rules["js-start"].unshift({
token: "text",
regex: "<\\/(?=script)",
@ -122,31 +122,11 @@ ace.mode.HtmlHighlightRules = function() {
});
var cssRules = new ace.mode.CssHighlightRules().getRules();
this.$addRules(cssRules, "css-");
this.addRules(cssRules, "css-");
this.$rules["css-start"].unshift({
token: "text",
regex: "<\\/(?=style)",
next: "tag"
});
};
(function() {
this.$addRules = function(rules, prefix) {
for (var key in rules) {
var state = rules[key];
for (var i=0; i<state.length; i++) {
var rule = state[i];
if (rule.next) {
rule.next = prefix + rule.next;
}
}
this.$rules[prefix + key] = state;
}
};
this.getRules = function() {
return this.$rules;
};
}).call(ace.mode.HtmlHighlightRules.prototype);
ace.inherits(ace.mode.HtmlHighlightRules, ace.mode.TextHighlightRules);

View file

@ -31,7 +31,7 @@ ace.inherits(ace.mode.JavaScript, ace.mode.Text);
if (match) {
indent += tab;
}
} else if (state == "doc-comment") {
} else if (state == "doc-start") {
if (endState == "start") {
return "";
}

View file

@ -2,6 +2,8 @@ ace.provide("ace.mode.JavaScriptHighlightRules");
ace.mode.JavaScriptHighlightRules = function() {
var docComment = new ace.mode.DocCommentHighlightRules();
var keywords = {
"break" : 1,
"case" : 1,
@ -30,16 +32,13 @@ ace.mode.JavaScriptHighlightRules = function() {
// regexp must not have capturing parentheses. Use (?:) instead.
// regexps are ordered -> the first match is used
this.$rules = {
"start" : [ {
token : "comment",
regex : "\\/\\/.*$"
}, {
token : "doc-comment", // doc comment
regex : "\\/\\*\\*",
next : "doc-comment"
}, {
},
docComment.getStartRule("doc-start"),
{
token : "comment", // multi line comment
regex : "\\/\\*",
next : "comment"
@ -83,23 +82,6 @@ ace.mode.JavaScriptHighlightRules = function() {
token : "text",
regex : "\\s+"
} ],
"doc-comment" : [ {
token : "doc-comment", // closing comment
regex : "\\*\\/",
next : "start"
}, {
token : "doc-comment-tag",
regex : "@[\\w\\d_]+"
}, {
token : "doc-comment",
regex : "\s+"
}, {
token : "doc-comment",
regex : "[^@\\*]+"
}, {
token : "doc-comment",
regex : "."
}],
"comment" : [ {
token : "comment", // closing comment
regex : ".*?\\*\\/",
@ -125,12 +107,8 @@ ace.mode.JavaScriptHighlightRules = function() {
regex : '.+'
} ]
};
this.addRules(docComment.getRules(), "doc-");
this.$rules["doc-start"][0].next = "start";
};
(function() {
this.getRules = function() {
return this.$rules;
};
}).call(ace.mode.JavaScriptHighlightRules.prototype);
ace.inherits(ace.mode.JavaScriptHighlightRules, ace.mode.TextHighlightRules);

View file

@ -1,13 +1,7 @@
ace.provide("ace.mode.Text");
ace.mode.Text = function() {
var rules = {
"start" : [ {
token : "text",
regex : ".+"
} ]
};
this.$tokenizer = new ace.Tokenizer(rules);
this.$tokenizer = new ace.Tokenizer(new ace.mode.TextHighlightRules().getRules());
};
(function() {

View file

@ -0,0 +1,37 @@
ace.provide("ace.mode.TextHighlightRules");
ace.mode.TextHighlightRules = function() {
// regexp must not have capturing parentheses
// regexps are ordered -> the first match is used
this.$rules = {
"start" : [ {
token : "text",
regex : ".+"
} ]
};
};
(function() {
this.addRules = function(rules, prefix) {
for (var key in rules) {
var state = rules[key];
for (var i=0; i<state.length; i++) {
var rule = state[i];
if (rule.next) {
rule.next = prefix + rule.next;
} else {
rule.next = prefix + key;
}
}
this.$rules[prefix + key] = state;
}
};
this.getRules = function() {
return this.$rules;
};
}).call(ace.mode.TextHighlightRules.prototype);

View file

@ -69,11 +69,4 @@ ace.mode.XmlHighlightRules = function() {
} ]
};
};
(function() {
this.getRules = function() {
return this.$rules;
};
}).call(ace.mode.XmlHighlightRules.prototype);
ace.inherits(ace.mode.XmlHighlightRules, ace.mode.TextHighlightRules);

View file

@ -69,15 +69,15 @@ var JavaScriptTest = new TestCase("mode.JavaScriptTest", {
},
"test: special indent in doc comments" : function() {
assertEquals(" * ", this.mode.getNextLineIndent("/**", "doc-comment", " "));
assertEquals(" * ", this.mode.getNextLineIndent(" /**", "doc-comment", " "));
assertEquals(" * ", this.mode.getNextLineIndent(" *", "doc-comment", " "));
assertEquals(" * ", this.mode.getNextLineIndent(" *", "doc-comment", " "));
assertEquals(" ", this.mode.getNextLineIndent(" abc", "doc-comment", " "));
assertEquals(" * ", this.mode.getNextLineIndent("/**", "doc-start", " "));
assertEquals(" * ", this.mode.getNextLineIndent(" /**", "doc-start", " "));
assertEquals(" * ", this.mode.getNextLineIndent(" *", "doc-start", " "));
assertEquals(" * ", this.mode.getNextLineIndent(" *", "doc-start", " "));
assertEquals(" ", this.mode.getNextLineIndent(" abc", "doc-start", " "));
},
"test: no indent after doc comments" : function() {
assertEquals("", this.mode.getNextLineIndent(" */", "doc-comment", " "));
assertEquals("", this.mode.getNextLineIndent(" */", "doc-start", " "));
}
// "test: outdent if first non WS character in line is a closing brace" : function() {