add support for line annotations

This commit is contained in:
Fabian Jakobs 2011-01-23 00:01:29 +01:00
commit 327927147a
4 changed files with 81 additions and 4 deletions

View file

@ -198,6 +198,41 @@ var EditSession = function(text, mode) {
this._dispatchEvent("changeBreakpoint", {});
};
this.getBreakpoints = function() {
return this.$breakpoints;
};
/**
* Error:
* {
* row: 12,
* column: 2, //can be undefined
* text: "Missing argument",
* type: "error" // or "warning" or "info"
* }
*/
this.setAnnotations = function(annotations) {
this.$annotations = [];
for (var i=0; i<annotations.length; i++) {
var annotation = annotations[i];
var row = annotation.row;
if (this.$annotations[row])
this.$annotations[row].push(annotation);
else
this.$annotations[row] = [annotation];
}
this._dispatchEvent("changeAnnotation", {});
};
this.getAnnotations = function() {
return this.$annotations;
};
this.clearAnnotations = function() {
this.$annotations = [];
this._dispatchEvent("changeAnnotation", {});
};
this.$detectNewLine = function(text) {
var match = text.match(/^.*?(\r?\n)/m);
if (match) {

View file

@ -126,6 +126,7 @@ var Editor =function(renderer, session) {
this.session.removeEventListener("changeMode", this.$onDocumentModeChange);
this.session.removeEventListener("changeTabSize", this.$onDocumentChangeTabSize);
this.session.removeEventListener("changeBreakpoint", this.$onDocumentChangeBreakpoint);
this.session.removeEventListener("changeAnnotation", this.$onDocumentChangeAnnotation);
var selection = this.session.getSelection();
selection.removeEventListener("changeCursor", this.$onCursorChange);
@ -148,6 +149,9 @@ var Editor =function(renderer, session) {
this.$onDocumentChangeBreakpoint = this.onDocumentChangeBreakpoint.bind(this);
this.session.addEventListener("changeBreakpoint", this.$onDocumentChangeBreakpoint);
this.$onDocumentChangeAnnotation = this.onDocumentChangeAnnotation.bind(this);
this.session.addEventListener("changeAnnotation", this.$onDocumentChangeAnnotation);
this.selection = session.getSelection();
this.$desiredColumn = 0;
@ -165,6 +169,7 @@ var Editor =function(renderer, session) {
this.onCursorChange();
this.onSelectionChange();
this.onDocumentChangeBreakpoint();
this.onDocumentChangeAnnotation();
this.renderer.scrollToRow(session.getScrollTopRow());
this.renderer.updateFull();
};
@ -289,6 +294,10 @@ var Editor =function(renderer, session) {
this.renderer.setBreakpoints(this.session.getBreakpoints());
};
this.onDocumentChangeAnnotation = function() {
this.renderer.setAnnotations(this.session.getAnnotations());
};
this.onDocumentModeChange = function() {
var mode = this.session.getMode();
if (this.mode == mode)

View file

@ -45,6 +45,7 @@ var Gutter = function(parentEl) {
parentEl.appendChild(this.element);
this.$breakpoints = [];
this.$annotations = [];
this.$decorations = [];
};
@ -57,22 +58,49 @@ var Gutter = function(parentEl) {
}
this.removeGutterDecoration = function(row, className){
this.$decorations[row] =
this.$decorations[row].replace(" ace_" + className, "");
}
this.$decorations[row] = this.$decorations[row].replace(" ace_" + className, "");
};
this.setBreakpoints = function(rows) {
this.$breakpoints = rows.concat();
};
this.setAnnotations = function(annotations) {
// iterate over sparse array
this.$annotations = [];
for (var row in annotations) {
var rowInfo = this.$annotations[row] = {
text: []
};
var rowAnnotations = annotations[row];
for (var i=0; i<rowAnnotations.length; i++) {
var annotation = rowAnnotations[i];
rowInfo.text.push(annotation.text.replace(/"/g, "&quot;").replace(/'/g, "&rsquo;").replace(/</, "&lt;"));
var type = annotation.type;
if (type == "error")
rowInfo.className = "ace_error";
else if (type == "warning" && rowInfo.className != "ace_error")
rowInfo.className = "ace_warning";
else if (type == "info" && (!rowInfo.className))
rowInfo.className = "ace_info";
}
}
};
this.update = function(config) {
this.$config = config;
var html = [];
for ( var i = config.firstRow; i <= config.lastRow; i++) {
var annotation = this.$annotations[i] || {
className: "",
text: []
};
html.push("<div class='ace_gutter-cell",
this.$decorations[i] || "",
this.$breakpoints[i] ? " ace_breakpoint" : "",
this.$breakpoints[i] ? " ace_breakpoint " : " ",
annotation.className,
"' title='", annotation.text.join("\n"),
"' style='height:", config.lineHeight, "px;'>", (i+1), "</div>");
html.push("</div>");
}

View file

@ -474,6 +474,11 @@ var VirtualRenderer = function(container, theme) {
this.$loop.schedule(this.CHANGE_GUTTER);
};
this.setAnnotations = function(annotations) {
this.$gutterLayer.setAnnotations(annotations);
this.$loop.schedule(this.CHANGE_GUTTER);
};
this.updateCursor = function(position, overwrite) {
this.$cursorLayer.setCursor(position, overwrite);
this.$loop.schedule(this.CHANGE_CURSOR);