Merge branch 'master' of github.com:ajaxorg/ace

This commit is contained in:
Fabian Jakobs 2011-02-15 09:27:06 +01:00
commit 40fc8e966a
13 changed files with 997 additions and 77 deletions

View file

@ -122,6 +122,7 @@
}
.ace_marker-layer {
cursor: text;
}
.ace_marker-layer .ace_step {

View file

@ -67,7 +67,7 @@ var Document = function(text) {
};
this.getValue = function() {
return this.$lines.join(this.getNewLineCharacter());
return this.getAllLines().join(this.getNewLineCharacter());
};
// check for IE split bug
@ -123,11 +123,11 @@ var Document = function(text) {
* Get a verbatim copy of the given line as it is in the document
*/
this.getLine = function(row) {
return this.$lines[row] || "";
return this.getLines(row, row + 1)[0] || "";
};
this.getLines = function(firstRow, lastRow) {
return this.$lines.slice(firstRow, lastRow+1);
return this.$lines.slice(firstRow, lastRow + 1);
};
/**
@ -135,7 +135,7 @@ var Document = function(text) {
* should not modify this array!
*/
this.getAllLines = function() {
return this.$lines;
return this.getLines(0, this.getLength());
};
this.getLength = function() {
@ -183,14 +183,29 @@ var Document = function(text) {
var end = this.insertInLine(position, text);
}
else {
var end = this.insertInLine(position, newLines[0]);
this.insertNewLine(end);
if (newLines.length > 2)
this.insertLines(position.row+1, newLines.slice(1, newLines.length-1));
var end = this.insertInLine({row: position.row + newLines.length - 1, column: 0}, newLines[newLines.length-1]);
if (newLines[0].length > 0) {
var end = this.insertInLine(position, newLines[0]);
this.insertNewLine(end);
}
// If we are inserting at the end of the document, we don't need to
// use insertInLine (concorde depends on this optimization!)
if (position.row + 1 == this.getLength()) {
this.insertLines(position.row + 1,
newLines.slice(1, newLines.length));
var end = {
row: position.row + newLines.length - 1,
column: position.column + newLines[newLines.length - 1].length
};
} else {
if (newLines.length > 2)
this.insertLines(position.row + 1,
newLines.slice(1, newLines.length - 1));
var end = this.insertInLine({
row: position.row + newLines.length - 1,
column: 0
}, newLines[newLines.length - 1]);
}
}
return end;
};
@ -278,7 +293,7 @@ var Document = function(text) {
this.removeLines(firstFullRow, lastFullRow);
if (firstFullRow != firstRow) {
this.removeInLine(firstRow, range.start.column, this.$lines[firstRow].length);
this.removeInLine(firstRow, range.start.column, this.getLine(firstRow).length);
this.removeNewLine(range.start.row);
}
}

View file

@ -209,7 +209,7 @@ var EditSession = function(text, mode) {
this.addMarker = function(range, clazz, type, inFront) {
var id = this.$markerId++;
var marker = {
range : range,
type : type || "line",
@ -217,7 +217,7 @@ var EditSession = function(text, mode) {
clazz : clazz,
inFront: !!inFront
}
if (inFront) {
this.$frontMarkers[id] = marker;
this._dispatchEvent("changeFrontMarker")
@ -225,26 +225,26 @@ var EditSession = function(text, mode) {
this.$backMarkers[id] = marker;
this._dispatchEvent("changeBackMarker")
}
return id;
};
this.removeMarker = function(markerId) {
var marker = this.$frontMarkers[markerId] || this.$backMarkers[markerId];
if (!marker)
return;
var markers = marker.inFront ? this.$frontMarkers : this.$backMarkers;
if (marker) {
delete (markers[markerId]);
this._dispatchEvent(marker.inFront ? "changeFrontMarker" : "changeBackMarker");
}
};
this.getMarkers = function(inFront) {
return inFront ? this.$frontMarkers : this.$backMarkers;
};
/**
* Error:
* {
@ -334,7 +334,7 @@ var EditSession = function(text, mode) {
if (this.$worker)
this.$worker.terminate();
if (window.Worker)
if (window.Worker && !require.noWorker)
this.$worker = mode.createWorker(this);
else
this.$worker = null;

View file

@ -824,68 +824,66 @@ var Editor =function(renderer, session) {
return (row >= this.getFirstVisibleRow() && row <= this.getLastVisibleRow());
};
this.getVisibleRowCount = function() {
return this.getLastVisibleRow() - this.getFirstVisibleRow() + 1;
this.$getVisibleRowCount = function() {
return this.renderer.getScrollBottomRow() - this.renderer.getScrollTopRow() + 1;
};
this.getPageDownRow = function() {
return this.renderer.getLastVisibleRow() - 1;
this.$getPageDownRow = function() {
return this.renderer.getScrollBottomRow();
};
this.getPageUpRow = function() {
var firstRow = this.renderer.getFirstVisibleRow();
var lastRow = this.renderer.getLastVisibleRow();
this.$getPageUpRow = function() {
var firstRow = this.renderer.getScrollTopRow();
var lastRow = this.renderer.getScrollBottomRow();
return firstRow - (lastRow - firstRow) + 1;
return firstRow - (lastRow - firstRow);
};
this.selectPageDown = function() {
var row = this.getPageDownRow() + Math.floor(this.getVisibleRowCount() / 2);
var row = this.$getPageDownRow() + Math.floor(this.$getVisibleRowCount() / 2);
this.scrollPageDown();
var selection = this.getSelection();
selection.$moveSelection(function() {
selection.moveCursorTo(row, selection.getSelectionLead().column);
});
var leadScreenPos = this.session.documentToScreenPosition(selection.getSelectionLead());
var dest = this.session.screenToDocumentPosition(row, leadScreenPos.column);
selection.selectTo(dest.row, dest.column);
};
this.selectPageUp = function() {
var visibleRows = this.getLastVisibleRow() - this.getFirstVisibleRow();
var row = this.getPageUpRow() + Math.round(visibleRows / 2);
var visibleRows = this.renderer.getScrollTopRow() - this.renderer.getScrollBottomRow();
var row = this.$getPageUpRow() + Math.round(visibleRows / 2);
this.scrollPageUp();
var selection = this.getSelection();
selection.$moveSelection(function() {
selection.moveCursorTo(row, selection.getSelectionLead().column);
});
var leadScreenPos = this.session.documentToScreenPosition(selection.getSelectionLead());
var dest = this.session.screenToDocumentPosition(row, leadScreenPos.column);
selection.selectTo(dest.row, dest.column);
};
this.gotoPageDown = function() {
var row = this.getPageDownRow(),
column = Math.min(this.getCursorPosition().column,
this.session.getLine(row).length);
var row = this.$getPageDownRow();
var column = this.getCursorPositionScreen().column;
this.scrollToRow(row);
this.getSelection().moveCursorTo(row, column);
this.getSelection().moveCursorToScreen(row, column);
};
this.gotoPageUp = function() {
var row = this.getPageUpRow(),
column = Math.min(this.getCursorPosition().column,
this.session.getLine(row).length);
var row = this.$getPageUpRow();
var column = this.getCursorPositionScreen().column;
this.scrollToRow(row);
this.getSelection().moveCursorTo(row, column);
this.getSelection().moveCursorToScreen(row, column);
};
this.scrollPageDown = function() {
this.scrollToRow(this.getPageDownRow());
this.scrollToRow(this.$getPageDownRow());
};
this.scrollPageUp = function() {
this.renderer.scrollToRow(this.getPageUpRow());
this.renderer.scrollToRow(this.$getPageUpRow());
};
this.scrollToRow = function(row) {
@ -906,6 +904,10 @@ var Editor =function(renderer, session) {
return this.selection.getCursor();
};
this.getCursorPositionScreen = function() {
return this.session.documentToScreenPosition(this.getCursorPosition());
}
this.getSelectionRange = function() {
return this.selection.getRange();
};

View file

@ -408,6 +408,15 @@ var Selection = function(session) {
this.$updateDesiredColumn(this.selectionLead.column);
};
this.moveCursorToScreen = function(row, column, preventUpdateDesiredColumn) {
if (this.session.getUseWrapMode()) {
var pos = this.session.screenToDocumentPosition(row, column);
row = pos.row;
column = pos.column;
}
this.moveCursorTo(row, column, preventUpdateDesiredColumn);
};
}).call(Selection.prototype);
exports.Selection = Selection;

View file

@ -37,13 +37,14 @@
require("../../../support/paths");
require("./mockdom");
// require("./mockdom");
var async = require("asyncjs");
async.concat(
require("./anchor_test"),
require("./change_document_test"),
require("./document_test"),
// require("./anchor_test"),
// require("./change_document_test"),
require("./document_test")
/*
require("./edit_session_test"),
require("./event_emitter_test"),
require("./navigation_test"),
@ -60,4 +61,5 @@ async.concat(
require("./mode/text_test"),
require("./mode/xml_test"),
require("./mode/xml_tokenizer_test")
*/
).exec();

View file

@ -38,7 +38,8 @@
define(function(require, exports, module) {
var Document = require("../document").Document,
var Document = require("../../../../../lib/concorde/Document"),
AceAdapter = require("../../../../../lib/concorde/AceAdapter"),
Range = require("../range").Range,
assert = require("./assertions"),
async = require("asyncjs");
@ -46,7 +47,7 @@ var Document = require("../document").Document,
var Test = {
"test: insert text in line" : function() {
var doc = new Document(["12", "34"]);
var doc = new AceAdapter(new Document(["12", "34"]));
var deltas = [];
doc.on("change", function(e) { deltas.push(e.data); });
@ -63,7 +64,7 @@ var Test = {
},
"test: insert new line" : function() {
var doc = new Document(["12", "34"]);
var doc = new AceAdapter(new Document(["12", "34"]));
var deltas = [];
doc.on("change", function(e) { deltas.push(e.data); });
@ -80,7 +81,7 @@ var Test = {
},
"test: insert lines at the beginning" : function() {
var doc = new Document(["12", "34"]);
var doc = new AceAdapter(new Document(["12", "34"]));
var deltas = [];
doc.on("change", function(e) { deltas.push(e.data); });
@ -97,7 +98,7 @@ var Test = {
},
"test: insert lines at the end" : function() {
var doc = new Document(["12", "34"]);
var doc = new AceAdapter(new Document(["12", "34"]));
var deltas = [];
doc.on("change", function(e) { deltas.push(e.data); });
@ -107,7 +108,7 @@ var Test = {
},
"test: insert lines in the middle" : function() {
var doc = new Document(["12", "34"]);
var doc = new AceAdapter(new Document(["12", "34"]));
var deltas = [];
doc.on("change", function(e) { deltas.push(e.data); });
@ -124,7 +125,7 @@ var Test = {
},
"test: insert multi line string at the start" : function() {
var doc = new Document(["12", "34"]);
var doc = new AceAdapter(new Document(["12", "34"]));
var deltas = [];
doc.on("change", function(e) { deltas.push(e.data); });
@ -141,7 +142,7 @@ var Test = {
},
"test: insert multi line string at the end" : function() {
var doc = new Document(["12", "34"]);
var doc = new AceAdapter(new Document(["12", "34"]));
var deltas = [];
doc.on("change", function(e) { deltas.push(e.data); });
@ -158,7 +159,7 @@ var Test = {
},
"test: insert multi line string in the middle" : function() {
var doc = new Document(["12", "34"]);
var doc = new AceAdapter(new Document(["12", "34"]));
var deltas = [];
doc.on("change", function(e) { deltas.push(e.data); });
@ -175,7 +176,7 @@ var Test = {
},
"test: delete in line" : function() {
var doc = new Document(["1234", "5678"]);
var doc = new AceAdapter(new Document(["1234", "5678"]));
var deltas = [];
doc.on("change", function(e) { deltas.push(e.data); });
@ -192,7 +193,7 @@ var Test = {
},
"test: delete new line" : function() {
var doc = new Document(["1234", "5678"]);
var doc = new AceAdapter(new Document(["1234", "5678"]));
var deltas = [];
doc.on("change", function(e) { deltas.push(e.data); });
@ -209,7 +210,7 @@ var Test = {
},
"test: delete multi line range line" : function() {
var doc = new Document(["1234", "5678", "abcd"]);
var doc = new AceAdapter(new Document(["1234", "5678", "abcd"]));
var deltas = [];
doc.on("change", function(e) { deltas.push(e.data); });
@ -220,13 +221,13 @@ var Test = {
var d = deltas.concat();
doc.revertDeltas(d);
assert.equal(doc.getValue(), ["1234", "5678", "abcd"].join("\n"));
doc.applyDeltas(d);
assert.equal(doc.getValue(), ["12cd"].join("\n"));
},
"test: delete full lines" : function() {
var doc = new Document(["1234", "5678", "abcd"]);
var doc = new AceAdapter(new Document(["1234", "5678", "abcd"]));
var deltas = [];
doc.on("change", function(e) { deltas.push(e.data); });
@ -236,44 +237,44 @@ var Test = {
},
"test: remove lines should return the removed lines" : function() {
var doc = new Document(["1234", "5678", "abcd"]);
var doc = new AceAdapter(new Document(["1234", "5678", "abcd"]));
var removed = doc.removeLines(1, 2);
assert.equal(removed.join("\n"), ["5678", "abcd"].join("\n"));
},
"test: should handle unix style new lines" : function() {
var doc = new Document(["1", "2", "3"]);
var doc = new AceAdapter(new Document(["1", "2", "3"]));
assert.equal(doc.getValue(), ["1", "2", "3"].join("\n"));
},
"test: should handle windows style new lines" : function() {
var doc = new Document(["1", "2", "3"].join("\r\n"));
var doc = new AceAdapter(new Document(["1", "2", "3"].join("\r\n")));
doc.setNewLineMode("unix");
assert.equal(doc.getValue(), ["1", "2", "3"].join("\n"));
},
"test: set new line mode to 'windows' should use '\r\n' as new lines": function() {
var doc = new Document(["1", "2", "3"].join("\n"));
var doc = new AceAdapter(new Document(["1", "2", "3"].join("\n")));
doc.setNewLineMode("windows");
assert.equal(doc.getValue(), ["1", "2", "3"].join("\r\n"));
},
"test: set new line mode to 'unix' should use '\n' as new lines": function() {
var doc = new Document(["1", "2", "3"].join("\r\n"));
var doc = new AceAdapter(new Document(["1", "2", "3"].join("\r\n")));
doc.setNewLineMode("unix");
assert.equal(doc.getValue(), ["1", "2", "3"].join("\n"));
},
"test: set new line mode to 'auto' should detect the incoming nl type": function() {
var doc = new Document(["1", "2", "3"].join("\n"));
var doc = new AceAdapter(new Document(["1", "2", "3"].join("\n")));
doc.setNewLineMode("auto");
assert.equal(doc.getValue(), ["1", "2", "3"].join("\n"));
var doc = new Document(["1", "2", "3"].join("\r\n"));
var doc = new AceAdapter(new Document(["1", "2", "3"].join("\r\n")));
doc.setNewLineMode("auto");
assert.equal(doc.getValue(), ["1", "2", "3"].join("\r\n"));
@ -283,13 +284,13 @@ var Test = {
},
"test: set value": function() {
var doc = new Document("1");
var doc = new AceAdapter(new Document("1"));
assert.equal("1", doc.getValue());
doc.setValue(doc.getValue());
assert.equal("1", doc.getValue());
var doc = new Document("1\n2");
var doc = new AceAdapter(new Document("1\n2"));
assert.equal("1\n2", doc.getValue());
doc.setValue(doc.getValue());
@ -297,8 +298,8 @@ var Test = {
},
"test: empty document has to contain one line": function() {
var doc = new Document("");
assert.equal(doc.$lines.length, 1);
var doc = new AceAdapter(new Document(""));
assert.equal(doc.getLength(), 1);
}
};

View file

@ -94,7 +94,17 @@ MockRenderer.prototype.updateCursor = function(position) {
this.cursor.column = position.column;
};
MockRenderer.prototype.scrollToLine = function(row) {
MockRenderer.prototype.scrollToLine = function(line, center) {
var lineHeight = { lineHeight: 16 };
var row = 0;
for (var l = 1; l < line; l++) {
row += this.session.getRowHeight(lineHeight, l-1) / lineHeight.lineHeight;
}
if (center) {
row -= this.visibleRowCount / 2;
}
this.scrollToRow(row);
};
MockRenderer.prototype.scrollCursorIntoView = function() {

View file

@ -597,6 +597,10 @@ var VirtualRenderer = function(container, theme) {
return this.scrollTop / this.lineHeight;
};
this.getScrollBottomRow = function() {
return Math.max(0, Math.floor((this.scrollTop + this.$size.scrollerHeight) / this.lineHeight) - 1);
}
this.scrollToRow = function(row) {
this.scrollToY(row * this.lineHeight);
};