introduce ace.provide function to create namespaces

This commit is contained in:
Fabian Jakobs 2010-04-14 09:53:17 +02:00
commit b9391734b6
16 changed files with 349 additions and 358 deletions

View file

@ -19,7 +19,7 @@
</style>
<link rel="stylesheet" href="../css/editor.css" type="text/css" charset="utf-8">
<script src="../src/lib.js" type="text/javascript" charset="utf-8"></script>
<script src="../src/ace.js" type="text/javascript" charset="utf-8"></script>
<script src="../src/TextDocument.js" type="text/javascript" charset="utf-8"></script>
<script src="../src/JavaScript.js" type="text/javascript" charset="utf-8"></script>
<script src="../src/XML.js" type="text/javascript" charset="utf-8"></script>

View file

@ -1,5 +1,4 @@
if (!window.ace)
ace = {};
ace.provide("ace.BackgroundTokenizer");
ace.BackgroundTokenizer = function(tokenizer, onUpdate, onComplete) {
this.running = false;

View file

@ -1,5 +1,4 @@
if (!window.ace)
ace = {};
ace.provide("ace.CursorLayer");
ace.CursorLayer = function(parentEl) {
this.element = document.createElement("div");

View file

@ -1,5 +1,4 @@
if (!window.ace)
ace = {};
ace.provide("ace.Editor");
ace.Editor = function(doc, renderer) {
var container = renderer.getContainerElement();

View file

@ -1,5 +1,4 @@
if (!window.ace)
ace = {};
ace.provide("ace.GutterLayer");
ace.GutterLayer = function(parentEl) {
this.element = document.createElement("div");

View file

@ -1,114 +1,111 @@
if (!window.ace)
ace = {};
ace.provide("ace.JavaScript");
(function() {
ace.JavaScript = {};
var keywords = {
"break" : 1,
"case" : 1,
"catch" : 1,
"continue" : 1,
"default" : 1,
"delete" : 1,
"do" : 1,
"else" : 1,
"finally" : 1,
"for" : 1,
"function" : 1,
"if" : 1,
"in" : 1,
"instanceof" : 1,
"new" : 1,
"return" : 1,
"switch" : 1,
"throw" : 1,
"try" : 1,
"typeof" : 1,
"var" : 1,
"while" : 1,
"with" : 1
};
var keywords = {
"break" : 1,
"case" : 1,
"catch" : 1,
"continue" : 1,
"default" : 1,
"delete" : 1,
"do" : 1,
"else" : 1,
"finally" : 1,
"for" : 1,
"function" : 1,
"if" : 1,
"in" : 1,
"instanceof" : 1,
"new" : 1,
"return" : 1,
"switch" : 1,
"throw" : 1,
"try" : 1,
"typeof" : 1,
"var" : 1,
"while" : 1,
"with" : 1
};
// regexp must not have capturing parentheses
// regexps are ordered -> the first match is used
// regexp must not have capturing parentheses
// regexps are ordered -> the first match is used
ace.JavaScript.RULES = {
start : [ {
token : "comment",
regex : "\\/\\/.*$"
}, {
token : "comment", // multi line comment in one line
regex : "\\/\\*.*?\\*\\/"
}, {
token : "comment", // multi line comment start
regex : "\\/\\*.*$",
next : "comment"
}, {
token : "string", // single line
regex : '["](?:(?:\\\\.)|(?:[^"\\\\]))*?["]'
}, {
token : "string", // multi line string start
regex : '["].*\\\\$',
next : "qqstring"
}, {
token : "string", // single line
regex : "['](?:(?:\\\\.)|(?:[^'\\\\]))*?[']"
}, {
token : "string", // multi line string start
regex : "['].*\\\\$",
next : "qstring"
}, {
token : "number", // hex
regex : "0[xX][0-9a-fA-F]+\\b"
}, {
token : "number", // float
regex : "[+-]?\\d+(?:(?:\\.\\d*)?(?:[eE][+-]?\\d+)?)?\\b"
}, {
token : function(value) {
if (keywords[value]) {
return "keyword";
}
else {
return "identifier";
}
},
regex : "[a-zA-Z_][a-zA-Z0-9_]*\\b"
}, {
token : function(value) {
// return parens[value];
return "text";
ace.JavaScript.RULES = {
start : [ {
token : "comment",
regex : "\\/\\/.*$"
}, {
token : "comment", // multi line comment in one line
regex : "\\/\\*.*?\\*\\/"
}, {
token : "comment", // multi line comment start
regex : "\\/\\*.*$",
next : "comment"
}, {
token : "string", // single line
regex : '["](?:(?:\\\\.)|(?:[^"\\\\]))*?["]'
}, {
token : "string", // multi line string start
regex : '["].*\\\\$',
next : "qqstring"
}, {
token : "string", // single line
regex : "['](?:(?:\\\\.)|(?:[^'\\\\]))*?[']"
}, {
token : "string", // multi line string start
regex : "['].*\\\\$",
next : "qstring"
}, {
token : "number", // hex
regex : "0[xX][0-9a-fA-F]+\\b"
}, {
token : "number", // float
regex : "[+-]?\\d+(?:(?:\\.\\d*)?(?:[eE][+-]?\\d+)?)?\\b"
}, {
token : function(value) {
if (keywords[value]) {
return "keyword";
}
else {
return "identifier";
}
},
regex : "[\\[\\]\\(\\)\\{\\}]"
}, {
token : "text",
regex : "\\s+"
} ],
"comment" : [ {
token : "comment", // closing comment
regex : ".*?\\*\\/",
next : "start"
}, {
token : "comment", // comment spanning whole line
regex : ".+"
} ],
"qqstring" : [ {
token : "string",
regex : '(?:(?:\\\\.)|(?:[^"\\\\]))*?"',
next : "start"
}, {
token : "string",
regex : '.+'
} ],
"qstring" : [ {
token : "string",
regex : "(?:(?:\\\\.)|(?:[^'\\\\]))*?'",
next : "start"
}, {
token : "string",
regex : '.+'
} ]
};
regex : "[a-zA-Z_][a-zA-Z0-9_]*\\b"
}, {
token : function(value) {
// return parens[value];
return "text";
},
regex : "[\\[\\]\\(\\)\\{\\}]"
}, {
token : "text",
regex : "\\s+"
} ],
"comment" : [ {
token : "comment", // closing comment
regex : ".*?\\*\\/",
next : "start"
}, {
token : "comment", // comment spanning whole line
regex : ".+"
} ],
"qqstring" : [ {
token : "string",
regex : '(?:(?:\\\\.)|(?:[^"\\\\]))*?"',
next : "start"
}, {
token : "string",
regex : '.+'
} ],
"qstring" : [ {
token : "string",
regex : "(?:(?:\\\\.)|(?:[^'\\\\]))*?'",
next : "start"
}, {
token : "string",
regex : '.+'
} ]
};
})();

View file

@ -1,5 +1,4 @@
if (!window.ace)
ace = {};
ace.provide("ace.KeyBinding");
(function() {

View file

@ -1,5 +1,4 @@
if (!window.ace)
ace = {};
ace.provide("ace.MarkerLayer");
ace.MarkerLayer = function(parentEl) {
this.element = document.createElement("div");

View file

@ -1,5 +1,4 @@
if (!window.ace)
ace = {};
ace.provide("ace.TextDocument");
ace.TextDocument = function(text) {
this.lines = this._split(text);

View file

@ -1,5 +1,4 @@
if (!window.ace)
ace = {};
ace.provide("ace.TextInput");
ace.TextInput = function(parentNode, host) {

View file

@ -1,5 +1,4 @@
if (!window.ace)
ace = {};
ace.provide("ace.TextLayer");
ace.TextLayer = function(parentEl) {
this.element = document.createElement("div");

View file

@ -1,5 +1,4 @@
if (!window.ace)
ace = {};
ace.provide("ace.Tokenizer");
ace.Tokenizer = function(rules) {
this.rules = rules;

View file

@ -1,5 +1,4 @@
if (!window.ace)
ace = {};
ace.provide("ace.VirtualRenderer");
ace.VirtualRenderer = function(container) {
this.container = container;

View file

@ -1,75 +1,72 @@
if (!window.ace)
ace = {};
ace.provide("ace.XML");
(function() {
ace.XML = {};
// regexp must not have capturing parentheses
// regexps are ordered -> the first match is used
// regexp must not have capturing parentheses
// regexps are ordered -> the first match is used
ace.XML.RULES = {
start : [ {
token : "text",
regex : "<\\!\\[CDATA\\[",
next : "cdata"
}, {
token : "xml_pe",
regex : "<\\?.*?\\?>"
}, {
token : "comment",
regex : "<\\!--",
next : "comment"
}, {
token : "text", // opening tag
regex : "<",
next : "tag"
}, {
token : "text",
regex : "\\s+"
}, {
token : "text",
regex : ".+"
} ],
ace.XML.RULES = {
start : [ {
token : "text",
regex : "<\\!\\[CDATA\\[",
next : "cdata"
}, {
token : "xml_pe",
regex : "<\\?.*?\\?>"
}, {
token : "comment",
regex : "<\\!--",
next : "comment"
}, {
token : "text", // opening tag
regex : "<",
next : "tag"
}, {
token : "text",
regex : "\\s+"
}, {
token : "text",
regex : ".+"
} ],
tag : [ {
token : "text",
regex : ">",
next : "start"
}, {
token : "keyword",
regex : "[-_a-zA-Z0-9:]+"
}, {
token : "text",
regex : "\\s+"
}, {
token : "string",
regex : '".*?"'
}, {
token : "string",
regex : "'.*?'"
} ],
tag : [ {
token : "text",
regex : ">",
next : "start"
}, {
token : "keyword",
regex : "[-_a-zA-Z0-9:]+"
}, {
token : "text",
regex : "\\s+"
}, {
token : "string",
regex : '".*?"'
}, {
token : "string",
regex : "'.*?'"
} ],
cdata : [ {
token : "text",
regex : "\\]\\]>",
next : "start"
}, {
token : "text",
regex : "\\s+"
}, {
token : "text",
regex : ".+"
} ],
cdata : [ {
token : "text",
regex : "\\]\\]>",
next : "start"
}, {
token : "text",
regex : "\\s+"
}, {
token : "text",
regex : ".+"
} ],
comment : [ {
token : "comment",
regex : ".*?-->",
next : "start"
}, {
token : "comment",
regex : ".+"
} ]
};
comment : [ {
token : "comment",
regex : ".*?-->",
next : "start"
}, {
token : "comment",
regex : ".+"
} ]
};
})();

171
src/ace.js Normal file
View file

@ -0,0 +1,171 @@
if (!window.ace)
ace = {};
ace.provide = function(namespace) {
var parts = namespace.split(".");
var obj = window;
for (var i=0; i<parts.length; i++) {
var part = parts[i];
if (!obj[part]) {
obj[part] = {};
}
obj = obj[part];
}
};
ace.addListener = function(elem, type, callback) {
if (elem.addEventListener) {
return elem.addEventListener(type, callback, false);
}
if (elem.attachEvent) {
var wrapper = function() {
callback(window.event);
};
callback.$$wrapper = wrapper;
elem.attachEvent("on" + type, wrapper);
}
};
ace.removeListener = function(elem, type, callback) {
if (elem.removeEventListener) {
return elem.removeEventListener(type, callback, false);
}
if (elem.detachEvent) {
elem.detachEvent("on" + type, callback.$$wrapper || callback);
}
};
ace.setText = function(elem, text) {
if (elem.innerText !== undefined) {
elem.innerText = text;
}
if (elem.textContent !== undefined) {
elem.textContent = text;
}
};
ace.stopEvent = function(e) {
ace.stopPropagation(e);
ace.preventDefault(e);
return false;
};
ace.stopPropagation = function(e) {
if (e.stopPropagation)
e.stopPropagation();
else
e.cancelBubble = true;
};
ace.preventDefault = function(e) {
if (e.preventDefault)
e.preventDefault();
else
e.returnValue = false;
};
ace.inherits = function(ctor, superCtor) {
var tempCtor = function() {};
tempCtor.prototype = superCtor.prototype;
ctor.super_ = superCtor.prototype;
ctor.prototype = new tempCtor();
ctor.prototype.constructor = ctor;
};
ace.getInnerWidth = function(element) {
return (parseInt(ace.computedStyle(element, "paddingLeft"))
+ parseInt(ace.computedStyle(element, "paddingRight")) + element.clientWidth);
};
ace.getInnerHeight = function(element) {
return (parseInt(ace.computedStyle(element, "paddingTop"))
+ parseInt(ace.computedStyle(element, "paddingBottom")) + element.clientHeight);
};
ace.computedStyle = function(element, style) {
if (window.getComputedStyle) {
return (window.getComputedStyle(element, null))[style];
}
else {
return element.currentStyle[style];
}
};
ace.scrollbarHeight = function() {
var el = document.createElement("div");
var style = el.style;
style.position = "absolute";
style.left = "-10000px";
style.overflow = "scroll";
style.height = "100px";
document.body.appendChild(el);
var height = el.offsetHeight - el.clientHeight;
document.body.removeChild(el);
return height;
};
ace.stringReverse = function(string) {
return string.split("").reverse().join("");
};
ace.bind = function(fcn, context) {
return function() {
return fcn.apply(context, arguments);
};
};
ace.capture = function(el, eventHandler, releaseCaptureHandler) {
function onMouseMove(e) {
eventHandler(e);
e.stopPropagation();
}
function onMouseUp(e) {
eventHandler && eventHandler(e);
releaseCaptureHandler && releaseCaptureHandler();
document.removeEventListener("mousemove", onMouseMove, true);
document.removeEventListener("mouseup", onMouseUp, true);
e.stopPropagation();
}
document.addEventListener("mousemove", onMouseMove, true);
document.addEventListener("mouseup", onMouseUp, true);
};
ace.addMouseWheelListener = function(el, callback) {
var listener = function(e) {
e.wheel = (e.wheelDelta) ? e.wheelDelta / 120
: -(e.detail || 0) / 3;
callback(e);
};
ace.addListener(el, "DOMMouseScroll", listener);
ace.addListener(el, "mousewheel", listener);
};
ace.autoremoveListener = function(el, type, callback, timeout) {
var listener = function(e) {
clearTimeout(timeoutId);
remove();
callback(e);
};
var remove = function() {
ace.removeListener(el, type, listener);
};
ace.addListener(el, type, listener);
var timeoutId = setTimeout(remove, timeout);
};
ace.addTripleClickListener = function(el, callback) {
ace.addListener(el, "mousedown", function() {
ace.autoremoveListener(el, "mousedown", function() {
ace.autoremoveListener(el, "mousedown", callback, 300);
}, 300);
});
};

View file

@ -1,163 +0,0 @@
if (!window.ace)
ace = {};
(function() {
ace.addListener = function(elem, type, callback) {
if (elem.addEventListener) { return elem.addEventListener(type,
callback,
false); }
if (elem.attachEvent) {
var wrapper = function() {
callback(window.event);
};
callback.$$wrapper = wrapper;
elem.attachEvent("on" + type, wrapper);
}
};
ace.removeListener = function(elem, type, callback) {
if (elem.removeEventListener) { return elem
.removeEventListener(type, callback, false); }
if (elem.detachEvent) {
elem.detachEvent("on" + type, callback.$$wrapper || callback);
}
};
ace.setText = function(elem, text) {
if (elem.innerText !== undefined) {
elem.innerText = text;
}
if (elem.textContent !== undefined) {
elem.textContent = text;
}
};
ace.stopEvent = function(e) {
ace.stopPropagation(e);
ace.preventDefault(e);
return false;
};
ace.stopPropagation = function(e) {
if (e.stopPropagation)
e.stopPropagation();
else
e.cancelBubble = true;
};
ace.preventDefault = function(e) {
if (e.preventDefault)
e.preventDefault();
else
e.returnValue = false;
};
ace.inherits = function(ctor, superCtor) {
var tempCtor = function() {
};
tempCtor.prototype = superCtor.prototype;
ctor.super_ = superCtor.prototype;
ctor.prototype = new tempCtor();
ctor.prototype.constructor = ctor;
};
ace.getInnerWidth = function(element) {
return (parseInt(ace.computedStyle(element, "paddingLeft"))
+ parseInt(ace.computedStyle(element, "paddingRight")) + element.clientWidth);
};
ace.getInnerHeight = function(element) {
return (parseInt(ace.computedStyle(element, "paddingTop"))
+ parseInt(ace.computedStyle(element, "paddingBottom")) + element.clientHeight);
};
ace.computedStyle = function(element, style) {
if (window.getComputedStyle) {
return (window.getComputedStyle(element, null))[style];
}
else {
return element.currentStyle[style];
}
};
ace.scrollbarHeight = function() {
var el = document.createElement("div");
var style = el.style;
style.position = "absolute";
style.left = "-10000px";
style.overflow = "scroll";
style.height = "100px";
document.body.appendChild(el);
var height = el.offsetHeight - el.clientHeight;
document.body.removeChild(el);
return height;
};
ace.stringReverse = function(string) {
return string.split("").reverse().join("");
};
ace.bind = function(fcn, context) {
return function() {
return fcn.apply(context, arguments);
};
};
ace.capture = function(el, eventHandler, releaseCaptureHandler) {
function onMouseMove(e) {
eventHandler(e);
e.stopPropagation();
}
function onMouseUp(e) {
eventHandler && eventHandler(e);
releaseCaptureHandler && releaseCaptureHandler();
document.removeEventListener("mousemove", onMouseMove, true);
document.removeEventListener("mouseup", onMouseUp, true);
e.stopPropagation();
}
document.addEventListener("mousemove", onMouseMove, true);
document.addEventListener("mouseup", onMouseUp, true);
};
ace.addMouseWheelListener = function(el, callback) {
var listener = function(e) {
e.wheel = (e.wheelDelta) ? e.wheelDelta / 120
: -(e.detail || 0) / 3;
callback(e);
};
ace.addListener(el, "DOMMouseScroll", listener);
ace.addListener(el, "mousewheel", listener);
};
ace.autoremoveListener = function(el, type, callback, timeout) {
var listener = function(e) {
clearTimeout(timeoutId);
remove();
callback(e);
};
var remove = function() {
ace.removeListener(el, type, listener);
};
ace.addListener(el, type, listener);
var timeoutId = setTimeout(remove, timeout);
};
ace.addTripleClickListener = function(el, callback) {
ace.addListener(el, "mousedown", function() {
ace.autoremoveListener(el, "mousedown", function() {
ace.autoremoveListener(el, "mousedown", callback, 300);
}, 300);
});
};
})();