Remove FoldLine.getRowLength and .getRowSplits. Use already existing functions which simplifies the code.
This commit is contained in:
parent
47207a6b9f
commit
ff72467c10
4 changed files with 122 additions and 82 deletions
|
|
@ -1002,29 +1002,31 @@ var EditSession = function(text, mode) {
|
||||||
var tabSize = this.getTabSize();
|
var tabSize = this.getTabSize();
|
||||||
var wrapData = this.$wrapData;
|
var wrapData = this.$wrapData;
|
||||||
var wrapLimit = this.$wrapLimit;
|
var wrapLimit = this.$wrapLimit;
|
||||||
|
var tokens;
|
||||||
|
|
||||||
for (var row = firstRow; row <= lastRow; row++) {
|
for (var row = firstRow; row <= lastRow; row++) {
|
||||||
|
tokens = this.$getDisplayTokens(lang.stringTrimRight(lines[row]));
|
||||||
wrapData[row] =
|
wrapData[row] =
|
||||||
this.$computeWrapSplits(lines[row], wrapLimit, tabSize);
|
this.$computeWrapSplits(tokens, wrapLimit, tabSize);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// "Tokens"
|
// "Tokens"
|
||||||
var CHAR = 1,
|
var CHAR = 1,
|
||||||
CHAR_EXT = 2,
|
CHAR_EXT = 2,
|
||||||
SPACE = 3,
|
PLACEHOLDER_START = 3,
|
||||||
TAB = 4,
|
PLACEHOLDER_BODY = 4,
|
||||||
TAB_SPACE = 5;
|
SPACE = 10,
|
||||||
|
TAB = 11,
|
||||||
|
TAB_SPACE = 12;
|
||||||
|
|
||||||
this.$computeWrapSplits = function(textLine, wrapLimit, tabSize) {
|
this.$computeWrapSplits = function(tokens, wrapLimit, tabSize) {
|
||||||
textLine = lang.stringTrimRight(textLine);
|
if (tokens.length == 0) {
|
||||||
if (textLine.length == 0) {
|
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
var tabSize = this.getTabSize();
|
var tabSize = this.getTabSize();
|
||||||
var splits = [];
|
var splits = [];
|
||||||
var tokens = this.$getDisplayTokens(textLine);
|
|
||||||
var displayLength = tokens.length;
|
var displayLength = tokens.length;
|
||||||
var lastSplit = 0, lastDocSplit = 0;
|
var lastSplit = 0, lastDocSplit = 0;
|
||||||
|
|
||||||
|
|
@ -1035,11 +1037,11 @@ var EditSession = function(text, mode) {
|
||||||
// and multipleWidth characters.
|
// and multipleWidth characters.
|
||||||
var len = displayed.length;
|
var len = displayed.length;
|
||||||
displayed.join("").
|
displayed.join("").
|
||||||
// Get all the tabs spaces.
|
// Get all the TAB_SPACEs.
|
||||||
replace(/5/g, function(m) {
|
replace(/12/g, function(m) {
|
||||||
len -= 1;
|
len -= 1;
|
||||||
}).
|
}).
|
||||||
// Get all the multipleWidth characters.
|
// Get all the CHAR_EXT/multipleWidth characters.
|
||||||
replace(/2/g, function(m) {
|
replace(/2/g, function(m) {
|
||||||
len -= 1;
|
len -= 1;
|
||||||
});
|
});
|
||||||
|
|
@ -1053,15 +1055,19 @@ var EditSession = function(text, mode) {
|
||||||
// This is, where the split should be.
|
// This is, where the split should be.
|
||||||
var split = lastSplit + wrapLimit;
|
var split = lastSplit + wrapLimit;
|
||||||
|
|
||||||
// If there is a space or tab at this split position.
|
// If there is a space or tab at this split position, then making
|
||||||
|
// a split is simple.
|
||||||
if (tokens[split] >= SPACE) {
|
if (tokens[split] >= SPACE) {
|
||||||
// Include all following spaces + tabs in this split as well.
|
// Include all following spaces + tabs in this split as well.
|
||||||
while (tokens[split] >= SPACE) {
|
while (tokens[split] >= SPACE) {
|
||||||
split ++;
|
split ++;
|
||||||
}
|
}
|
||||||
addSplit(split);
|
addSplit(split);
|
||||||
} else {
|
continue;
|
||||||
// Search for the first non space/tab token.
|
}
|
||||||
|
|
||||||
|
// === ELSE ===
|
||||||
|
// Search for the first non space/tab token backwards.
|
||||||
for (split; split != lastSplit - 1; split--) {
|
for (split; split != lastSplit - 1; split--) {
|
||||||
if (tokens[split] >= SPACE) {
|
if (tokens[split] >= SPACE) {
|
||||||
split++;
|
split++;
|
||||||
|
|
@ -1071,25 +1077,81 @@ var EditSession = function(text, mode) {
|
||||||
// If we found one, then add the split.
|
// If we found one, then add the split.
|
||||||
if (split > lastSplit) {
|
if (split > lastSplit) {
|
||||||
addSplit(split);
|
addSplit(split);
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// === ELSE ===
|
||||||
|
split = lastSplit + wrapLimit;
|
||||||
// No space or tab around? Well, force a split then.
|
// No space or tab around? Well, force a split then.
|
||||||
else {
|
// Check if the split position is inside of a placeholder.
|
||||||
|
// Placeholder are not splitable!
|
||||||
|
if (tokens[split] == PLACEHOLDER_START
|
||||||
|
|| tokens[split] == PLACEHOLDER_BODY)
|
||||||
|
{
|
||||||
|
// Seek the start of the placeholder and do the split
|
||||||
|
// before the placeholder. By definition there always
|
||||||
|
// a PLACEHOLDER_START between split and lastSplit.
|
||||||
|
for (split; split != lastSplit - 1; split--) {
|
||||||
|
if (tokens[split] == PLACEHOLDER_START) {
|
||||||
|
// split++; << No incremental here as we want to
|
||||||
|
// have the position before the Placeholder.
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the PLACEHOLDER_START is not the index of the
|
||||||
|
// last split, then we can do the split
|
||||||
|
if (split > lastSplit) {
|
||||||
|
addSplit(split);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the PLACEHOLDER_START IS the index of the last
|
||||||
|
// split, then we have to place the split after the
|
||||||
|
// placeholder. So, let's seek for the end of the placeholder.
|
||||||
|
split = lastSplit + wrapLimit;
|
||||||
|
for (split; split < tokens.length; split++) {
|
||||||
|
if (tokens[split] != PLACEHOLDER_START &&
|
||||||
|
tokens[split] != PLACEHOLDER_BODY)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If spilt == tokens.length, then the placeholder is the last
|
||||||
|
// thing in the line and adding a new split doesn't make sense.
|
||||||
|
if (split == tokens.length) {
|
||||||
|
break; // Breaks the while-loop.
|
||||||
|
}
|
||||||
|
|
||||||
|
// Finally, add the split...
|
||||||
|
addSplit(split);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// === ELSE ===
|
||||||
|
// The split is inside of a CHAR or CHAR_EXT token. Forcing
|
||||||
|
// a split here is all right.
|
||||||
addSplit(lastSplit + wrapLimit);
|
addSplit(lastSplit + wrapLimit);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
return splits;
|
return splits;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.$getDisplayTokens = function(str) {
|
/**
|
||||||
|
* @param
|
||||||
|
* offset: The offset in screenColumn at which position str starts.
|
||||||
|
* Important for calculating the realTabSize.
|
||||||
|
*/
|
||||||
|
this.$getDisplayTokens = function(str, offset) {
|
||||||
var arr = [];
|
var arr = [];
|
||||||
var tabSize;
|
var tabSize;
|
||||||
|
offset = offset || 0;
|
||||||
|
|
||||||
for (var i = 0; i < str.length; i++) {
|
for (var i = 0; i < str.length; i++) {
|
||||||
var c = str.charCodeAt(i);
|
var c = str.charCodeAt(i);
|
||||||
// Tab
|
// Tab
|
||||||
if (c == 9) {
|
if (c == 9) {
|
||||||
tabSize = this.getScreenTabSize(arr.length);
|
tabSize = this.getScreenTabSize(arr.length + offset);
|
||||||
arr.push(TAB);
|
arr.push(TAB);
|
||||||
for (var n = 1; n < tabSize; n++) {
|
for (var n = 1; n < tabSize; n++) {
|
||||||
arr.push(TAB_SPACE);
|
arr.push(TAB_SPACE);
|
||||||
|
|
@ -1193,34 +1255,25 @@ var EditSession = function(text, mode) {
|
||||||
docRow = 0,
|
docRow = 0,
|
||||||
docColumn = 0, column,
|
docColumn = 0, column,
|
||||||
foldLine,
|
foldLine,
|
||||||
lastFoldLine,
|
|
||||||
foldLineRowLength;
|
foldLineRowLength;
|
||||||
|
|
||||||
var row = 0,
|
var row = 0,
|
||||||
rowLength = 0;
|
rowLength = 0;
|
||||||
|
|
||||||
while (row <= screenRow) {
|
while (row <= screenRow) {
|
||||||
foldLine = this.getFoldLine(docRow, lastFoldLine);
|
|
||||||
if (foldLine) {
|
|
||||||
lastFoldLine = foldLine;
|
|
||||||
rowLength = foldLine.getRowLength();
|
|
||||||
} else {
|
|
||||||
rowLength = this.getRowLength(docRow);
|
rowLength = this.getRowLength(docRow);
|
||||||
}
|
|
||||||
if (row + rowLength - 1 >= screenRow) {
|
if (row + rowLength - 1 >= screenRow) {
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
docRow ++;
|
|
||||||
row += rowLength;
|
row += rowLength;
|
||||||
if (foldLine) {
|
docRow = this.getRowFoldEnd(docRow) + 1;
|
||||||
docRow = foldLine.end.row + 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var splits = null;
|
var splits = null;
|
||||||
|
splits = this.$wrapData[docRow] || [];
|
||||||
|
foldLine = this.getFoldLine(docRow);
|
||||||
if (foldLine) {
|
if (foldLine) {
|
||||||
splits = foldLine.getSplitData();
|
|
||||||
docColumn = splits[screenRow - row] || 0;
|
docColumn = splits[screenRow - row] || 0;
|
||||||
walkScreenColumn = 0;
|
walkScreenColumn = 0;
|
||||||
|
|
||||||
|
|
@ -1249,7 +1302,6 @@ var EditSession = function(text, mode) {
|
||||||
}.bind(this), foldLine.end.row, this.getLine(foldLine.end.row).length);
|
}.bind(this), foldLine.end.row, this.getLine(foldLine.end.row).length);
|
||||||
} else {
|
} else {
|
||||||
line = this.getLine(docRow);
|
line = this.getLine(docRow);
|
||||||
splits = this.$wrapData[docRow];
|
|
||||||
if (this.$useWrapMode && splits) {
|
if (this.$useWrapMode && splits) {
|
||||||
docColumn = splits[screenRow - row - 1] || 0;
|
docColumn = splits[screenRow - row - 1] || 0;
|
||||||
line = line.substring(docColumn);
|
line = line.substring(docColumn);
|
||||||
|
|
@ -1258,7 +1310,7 @@ var EditSession = function(text, mode) {
|
||||||
docColumn += this.$getStringScreenWidth(line, screenColumn)[1];
|
docColumn += this.$getStringScreenWidth(line, screenColumn)[1];
|
||||||
|
|
||||||
// Need to do some clamping action here.
|
// Need to do some clamping action here.
|
||||||
if (splits) {
|
if (this.$useWrapMode) {
|
||||||
column = this.$wrapData[docRow][screenRow - row]
|
column = this.$wrapData[docRow][screenRow - row]
|
||||||
if (docColumn >= column) {
|
if (docColumn >= column) {
|
||||||
// We remove one character at the end such that the docColumn
|
// We remove one character at the end such that the docColumn
|
||||||
|
|
@ -1291,6 +1343,7 @@ var EditSession = function(text, mode) {
|
||||||
|
|
||||||
this.doc2Screen = function(docRow, docColumn) {
|
this.doc2Screen = function(docRow, docColumn) {
|
||||||
var wrapData;
|
var wrapData;
|
||||||
|
// Special case in wrapMode if the doc is at the end of the document.
|
||||||
if (this.$useWrapMode) {
|
if (this.$useWrapMode) {
|
||||||
wrapData = this.$wrapData;
|
wrapData = this.$wrapData;
|
||||||
if (docRow > wrapData.length - 1) {
|
if (docRow > wrapData.length - 1) {
|
||||||
|
|
@ -1307,7 +1360,7 @@ var EditSession = function(text, mode) {
|
||||||
fold = null,
|
fold = null,
|
||||||
folds,
|
folds,
|
||||||
comp,
|
comp,
|
||||||
foldLine = null, lastFoldLine = null;
|
foldLine = null;
|
||||||
|
|
||||||
// Clamp the docRow position in case it's inside of a folded block.
|
// Clamp the docRow position in case it's inside of a folded block.
|
||||||
fold = this.getFoldAt(docRow, docColumn, 1);
|
fold = this.getFoldAt(docRow, docColumn, 1);
|
||||||
|
|
@ -1316,23 +1369,19 @@ var EditSession = function(text, mode) {
|
||||||
docColumn = fold.start.column;
|
docColumn = fold.start.column;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (var row = 0; row < docRow; row++) {
|
var rowEnd, row = 0;
|
||||||
foldLine = this.getFoldLine(row, lastFoldLine);
|
while (row < docRow) {
|
||||||
if (foldLine) {
|
rowEnd = this.getRowFoldEnd(row);
|
||||||
lastFoldLine = foldLine;
|
if (rowEnd >= docRow) {
|
||||||
if (foldLine.end.row >= docRow) {
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
row = foldLine.end.row;
|
|
||||||
screenRow += foldLine.getRowLength();
|
|
||||||
} else {
|
|
||||||
screenRow += this.getRowLength(row);
|
screenRow += this.getRowLength(row);
|
||||||
}
|
row = rowEnd + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calculate the text line that is displayed in docRow on the screen.
|
// Calculate the text line that is displayed in docRow on the screen.
|
||||||
var textLine = "";
|
var textLine = "";
|
||||||
foldLine = this.getFoldLine(docRow, lastFoldLine);
|
foldLine = this.getFoldLine(docRow);
|
||||||
// Check if the final row we want to reach is inside of a fold.
|
// Check if the final row we want to reach is inside of a fold.
|
||||||
if (!foldLine) {
|
if (!foldLine) {
|
||||||
textLine = this.getLine(docRow).substring(0, docColumn);
|
textLine = this.getLine(docRow).substring(0, docColumn);
|
||||||
|
|
@ -1522,16 +1571,6 @@ var EditSession = function(text, mode) {
|
||||||
fold.foldLine = this;
|
fold.foldLine = this;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.getRowLength = function() {
|
|
||||||
// TODO: Add support for wrapped lines here.
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.getSplitData = function() {
|
|
||||||
// TODO: Add support for wrapped lines here.
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
this.containsRow = function(row) {
|
this.containsRow = function(row) {
|
||||||
return row >= this.start.row && row <= this.end.row;
|
return row >= this.start.row && row <= this.end.row;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -264,7 +264,8 @@ module.exports = {
|
||||||
function computeAndAssert(line, assertEqual, wrapLimit, tabSize) {
|
function computeAndAssert(line, assertEqual, wrapLimit, tabSize) {
|
||||||
wrapLimit = wrapLimit || 12;
|
wrapLimit = wrapLimit || 12;
|
||||||
tabSize = tabSize || 4;
|
tabSize = tabSize || 4;
|
||||||
var splits = EditSession.prototype.$computeWrapSplits(line, wrapLimit, tabSize);
|
var tokens = EditSession.prototype.$getDisplayTokens(line);
|
||||||
|
var splits = EditSession.prototype.$computeWrapSplits(tokens, wrapLimit, tabSize);
|
||||||
// console.log("String:", line, "Result:", splits, "Expected:", assertEqual);
|
// console.log("String:", line, "Result:", splits, "Expected:", assertEqual);
|
||||||
assert.ok(splits.length == assertEqual.length);
|
assert.ok(splits.length == assertEqual.length);
|
||||||
for (var i = 0; i < splits.length; i++) {
|
for (var i = 0; i < splits.length; i++) {
|
||||||
|
|
|
||||||
|
|
@ -474,7 +474,7 @@ var Text = function(parentEl) {
|
||||||
}.bind(this), foldLine.end.row, this.session.getLine(foldLine.end.row).length);
|
}.bind(this), foldLine.end.row, this.session.getLine(foldLine.end.row).length);
|
||||||
|
|
||||||
// TODO: Build a fake splits array!
|
// TODO: Build a fake splits array!
|
||||||
var splits = foldLine.getSplitData();
|
var splits = this.session.$wrapData[row];
|
||||||
this.$renderLineCore(stringBuilder, row, renderTokens, splits);
|
this.$renderLineCore(stringBuilder, row, renderTokens, splits);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -10,27 +10,27 @@ var failed = 0
|
||||||
var log = document.getElementById("log")
|
var log = document.getElementById("log")
|
||||||
|
|
||||||
var tests = [
|
var tests = [
|
||||||
require("ace/editor_change_document_test"),
|
// require("ace/editor_change_document_test"),
|
||||||
require("ace/editor_navigation_test"),
|
// require("ace/editor_navigation_test"),
|
||||||
require("ace/editor_highlight_selected_word_test"),
|
// require("ace/editor_highlight_selected_word_test"),
|
||||||
require("ace/editor_text_edit_test"),
|
// require("ace/editor_text_edit_test"),
|
||||||
require("ace/document_test"),
|
// require("ace/document_test"),
|
||||||
require("ace/edit_session_test"),
|
require("ace/edit_session_test"),
|
||||||
require("ace/test/event_emitter_test"),
|
// require("ace/test/event_emitter_test"),
|
||||||
require("ace/range_test"),
|
// require("ace/range_test"),
|
||||||
require("ace/search_test"),
|
// require("ace/search_test"),
|
||||||
require("ace/selection_test"),
|
// require("ace/selection_test"),
|
||||||
require("ace/virtual_renderer_test"),
|
// require("ace/virtual_renderer_test"),
|
||||||
require("ace/anchor_test"),
|
// require("ace/anchor_test"),
|
||||||
require("ace/mode/css_test"),
|
// require("ace/mode/css_test"),
|
||||||
require("ace/mode/css_tokenizer_test"),
|
// require("ace/mode/css_tokenizer_test"),
|
||||||
require("ace/mode/html_test"),
|
// require("ace/mode/html_test"),
|
||||||
require("ace/mode/html_tokenizer_test"),
|
// require("ace/mode/html_tokenizer_test"),
|
||||||
require("ace/mode/javascript_test"),
|
// require("ace/mode/javascript_test"),
|
||||||
require("ace/mode/javascript_tokenizer_test"),
|
// require("ace/mode/javascript_tokenizer_test"),
|
||||||
require("ace/mode/text_test"),
|
// require("ace/mode/text_test"),
|
||||||
require("ace/mode/xml_test"),
|
// require("ace/mode/xml_test"),
|
||||||
require("ace/mode/xml_tokenizer_test")
|
// require("ace/mode/xml_tokenizer_test")
|
||||||
]
|
]
|
||||||
|
|
||||||
async.list(tests)
|
async.list(tests)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue