Compute wrappings, don't just split after wrapLimit chars.
This commit is contained in:
parent
2df81fffae
commit
c6b32c96ef
3 changed files with 224 additions and 56 deletions
|
|
@ -51,6 +51,7 @@ var Document = function(text, mode) {
|
|||
this.selection = new Selection(this);
|
||||
this.$breakpoints = [];
|
||||
this.$wrapData = [];
|
||||
this.$computeWrapDataFn = this.$computeWrapData.bind(this);
|
||||
|
||||
this.listeners = [];
|
||||
if (mode) {
|
||||
|
|
@ -775,39 +776,18 @@ var Document = function(text, mode) {
|
|||
(function() {
|
||||
this.$wrapLimit = 80;
|
||||
this.$useWrapMode = false;
|
||||
|
||||
this.setUseWrapMode = function(useWrapMode) {
|
||||
var _self = this;
|
||||
|
||||
function computeWrapData(e) {
|
||||
var lines = _self.lines, wrapData = _self.$wrapData;
|
||||
var wrapLimit = _self.$wrapLimit;
|
||||
|
||||
// Remove lines that are no longer there.
|
||||
wrapData.splice(lines.length, wrapData.length - lines.length);
|
||||
|
||||
if (!e.data.lastRow) {
|
||||
e.data.lastRow = _self.lines.length - 1;
|
||||
}
|
||||
|
||||
for (var row = e.data.firstRow; row <= e.data.lastRow; row++) {
|
||||
var col = wrapLimit;
|
||||
wrapData[row] = [];
|
||||
while (col < lines[row].length) {
|
||||
wrapData[row].push(col);
|
||||
col += wrapLimit;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.$useWrapMode = useWrapMode;
|
||||
computeWrapData({ data: { firstRow: 0 } });
|
||||
this._dispatchEvent("changeWrapMode");
|
||||
|
||||
if (useWrapMode) {
|
||||
this.addEventListener("change", computeWrapData);
|
||||
this.addEventListener("change", this.$computeWrapDataFn);
|
||||
} else {
|
||||
this.removeEventListener("change", computeWrapData);
|
||||
this.removeEventListener("change", this.$computeWrapDataFn);
|
||||
}
|
||||
|
||||
this.$computeWrapDataFn({ data: { firstRow: 0 } });
|
||||
this._dispatchEvent("changeWrapMode");
|
||||
};
|
||||
|
||||
this.getUseWrapMode = function() {
|
||||
|
|
@ -822,6 +802,157 @@ var Document = function(text, mode) {
|
|||
return this.$wrapLimit;
|
||||
};
|
||||
|
||||
this.$computeWrapSplits = function(textLine, wrapLimit, tabSize) {
|
||||
if (textLine.length == 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
var tabLine = textLine.split("\t");
|
||||
var wrapSplits = [];
|
||||
|
||||
var screenColumn = 0;
|
||||
var documentColumn = 0;
|
||||
var lastSplitColumn = 0;
|
||||
|
||||
var reSpaces = /^\s*$/;
|
||||
|
||||
// Remove all tablines from the end of the tabLine array that are empty
|
||||
// or contain only space. This is necessary to prevent splits at the
|
||||
// line end if only tabs/spaces are following.
|
||||
// E.g: "foo bar\t " -> "foo bar".
|
||||
var toRemove = tabLine.length - 1;
|
||||
while (toRemove != -1 && reSpaces.test(tabLine[toRemove])) {
|
||||
toRemove --;
|
||||
}
|
||||
tabLine.splice(toRemove + 1);
|
||||
|
||||
// Remove spaces from the last tabLine to prevent splits only for
|
||||
// whitespaces as well.
|
||||
// E.g: "foo \tbar " -> "foo \tbar"
|
||||
tabLine[tabLine.length - 1] = tabLine[tabLine.length - 1].trimRight();
|
||||
|
||||
var value;
|
||||
var addedNewSplit;
|
||||
|
||||
function addSplit(incrDocumentColumn) {
|
||||
var valueLenLeadingWhitespaces;
|
||||
value = value.substring(incrDocumentColumn);
|
||||
valueLenLeadingWhitespaces = value.length;
|
||||
value = value.trimLeft();
|
||||
|
||||
incrDocumentColumn += valueLenLeadingWhitespaces - value.length;
|
||||
|
||||
// If there was a split saved before, then add it to the resulting
|
||||
// array. It's not possible to store the split column here directly,
|
||||
// as there migth be tabs/spaces following the current column that
|
||||
// will cause the split position to get incremented.
|
||||
if (lastSplitColumn) {
|
||||
wrapSplits.push(lastSplitColumn);
|
||||
}
|
||||
|
||||
lastSplitColumn = documentColumn += incrDocumentColumn;
|
||||
screenColumn = 0;
|
||||
|
||||
addedNewSplit = true;
|
||||
}
|
||||
|
||||
for (var i = 0; i < tabLine.length; i++) {
|
||||
value = tabLine[i];
|
||||
while (screenColumn + value.length >= wrapLimit) {
|
||||
// Find the last space that we use to split the line.
|
||||
var lastSpaceIdx =
|
||||
value.substring(0, wrapLimit - screenColumn + 1).
|
||||
lastIndexOf(" ");
|
||||
|
||||
// There is no space to break the line.
|
||||
if (lastSpaceIdx == -1) {
|
||||
// True if this is the first tabLine we face
|
||||
// during this split. This means, there is no
|
||||
// space or tab to make the split -> force split.
|
||||
if (screenColumn == 0) {
|
||||
addSplit(wrapLimit);
|
||||
}
|
||||
// True if we've added some content in this split line
|
||||
// already. Add a split after this already existing content
|
||||
// in the split.
|
||||
else {
|
||||
addSplit(0);
|
||||
}
|
||||
}
|
||||
// There is at least one space to wrap the line at.
|
||||
else {
|
||||
addSplit(lastSpaceIdx);
|
||||
}
|
||||
}
|
||||
|
||||
// Add new content to the current split.
|
||||
if (!addedNewSplit) {
|
||||
screenColumn += value.length + tabSize;
|
||||
documentColumn += value.length + 1;
|
||||
}
|
||||
// True if at least one new split was added during the while loop.
|
||||
else {
|
||||
var lenBefore = value.length;
|
||||
value = value.trimLeft();
|
||||
// True if value has some content. This content goes directly
|
||||
// after the last split.
|
||||
if (value.length != 0) {
|
||||
documentColumn = lastSplitColumn += lenBefore - value.length;
|
||||
screenColumn += value.length;
|
||||
documentColumn += 1; // The tab after this for-loop.
|
||||
}
|
||||
// There is no value left after the split. This means, following
|
||||
// spaces and tabs have to be ignored.
|
||||
else {
|
||||
// Tab for this for-loop.
|
||||
documentColumn = lastSplitColumn += 1;
|
||||
|
||||
// While the following tabLines are empty/only spaces, set
|
||||
// the latest split position behind them.
|
||||
while ((i + 1) < tabLine.length - 1
|
||||
&& reSpaces.test(tabLine[i + 1]))
|
||||
{
|
||||
i++;
|
||||
documentColumn = lastSplitColumn += tabLine[i].length + 1;
|
||||
}
|
||||
|
||||
// The tabLine[i + 1] is not empty. However, the trailing
|
||||
// spaces have still to be removed.
|
||||
value = tabLine[i + 1];
|
||||
lenBefore = value.length;
|
||||
value = value.trimLeft();
|
||||
documentColumn = lastSplitColumn += lenBefore - value.length;
|
||||
tabLine[i + 1] = value; // Store the trimed value.
|
||||
}
|
||||
addedNewSplit = false;
|
||||
}
|
||||
}
|
||||
if (lastSplitColumn) {
|
||||
wrapSplits.push(lastSplitColumn);
|
||||
}
|
||||
|
||||
return wrapSplits;
|
||||
};
|
||||
|
||||
this.$computeWrapData = function(e) {
|
||||
var lines = this.lines;
|
||||
var tabSize = this.getTabSize();
|
||||
var wrapData = this.$wrapData;
|
||||
var wrapLimit = this.$wrapLimit;
|
||||
|
||||
// Remove lines that are no longer there.
|
||||
wrapData.splice(lines.length, wrapData.length - lines.length);
|
||||
|
||||
if (!e.data.lastRow) {
|
||||
e.data.lastRow = lines.length - 1;
|
||||
}
|
||||
|
||||
for (var row = e.data.firstRow; row <= e.data.lastRow; row++) {
|
||||
wrapData[row] =
|
||||
this.$computeWrapSplits(lines[row], wrapLimit, tabSize);
|
||||
}
|
||||
};
|
||||
|
||||
this.getRowHeight = function(config, row) {
|
||||
var rows;
|
||||
if (!this.$useWrapMode) {
|
||||
|
|
|
|||
|
|
@ -283,6 +283,43 @@ var Test = {
|
|||
|
||||
undoManager.redo();
|
||||
assert.equal(doc.toString(), ["1", "", "ad"].join("\n"));
|
||||
},
|
||||
|
||||
"test: wrapLine split function" : function() {
|
||||
var splits;
|
||||
var tabSize = 4;
|
||||
var wrapLimit = 12;
|
||||
var computeWrapSplits = Document.prototype.$computeWrapSplits;
|
||||
var c = 0;
|
||||
|
||||
function computeAndAssert(line, assertEqual) {
|
||||
splits = computeWrapSplits(line, wrapLimit, tabSize);
|
||||
assert.ok(splits.length == assertEqual.length);
|
||||
for (var i = 0; i < splits.length; i++) {
|
||||
assert.ok(splits[i] == assertEqual[i]);
|
||||
}
|
||||
}
|
||||
|
||||
// Basic splitting.
|
||||
computeAndAssert("foo bar foo bar", [ 12 ]);
|
||||
computeAndAssert("foo bar f bar", [ 12 ]);
|
||||
computeAndAssert("foo bar f r", [ 14 ]);
|
||||
computeAndAssert("foo bar foo bar foo bara foo", [12, 25]);
|
||||
|
||||
// Don't split if there is only whitespaces/tabs at the end of the line.
|
||||
computeAndAssert("foo foo foo \t \t", [ ]);
|
||||
|
||||
// If there is no space to split, force split.
|
||||
computeAndAssert("foooooooooooooo", [ 12 ]);
|
||||
computeAndAssert("fooooooooooooooooooooooooooo", [12, 24]);
|
||||
computeAndAssert("foo bar fooooooooooobooooooo", [8, 20]);
|
||||
|
||||
// Basic splitting + tabs.
|
||||
computeAndAssert("foo \t\tbar", [ 6 ]);
|
||||
computeAndAssert("foo \t \tbar", [ 7 ]);
|
||||
|
||||
// Ignore spaces/tabs at beginning of split.
|
||||
computeAndAssert("foo \t \t \t \t bar", [ 14]);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue