fixes for indentedSoftWrap option

This commit is contained in:
nightwing 2014-12-14 23:07:23 +04:00
commit ef9cc542e7
3 changed files with 39 additions and 29 deletions

View file

@ -1846,15 +1846,15 @@ var EditSession = function(text, mode) {
var isCode = this.$wrapAsCode; var isCode = this.$wrapAsCode;
var indentSubsequentLines = this.$indentSubsequentLines; var indentedSoftWrap = this.$indentedSoftWrap;
var maxIndent = wrapLimit <= Math.max(2 * tabSize, 8)
|| indentedSoftWrap === false ? 0 : Math.floor(wrapLimit / 2);
var maxIndent = wrapLimit > 8 ? Math.floor(wrapLimit / 2) : 0; function getWrapIndent() {
function getIndentation() {
var indentation = 0; var indentation = 0;
if (maxIndent === 0) if (maxIndent === 0)
return indentation; return indentation;
if (indentSubsequentLines) { if (indentedSoftWrap) {
for (var i = 0; i < tokens.length; i++) { for (var i = 0; i < tokens.length; i++) {
var token = tokens[i]; var token = tokens[i];
if (token == SPACE) if (token == SPACE)
@ -1867,11 +1867,10 @@ var EditSession = function(text, mode) {
break; break;
} }
} }
if (isCode) if (isCode && indentedSoftWrap !== false)
indentation += tabSize; indentation += tabSize;
return Math.min(indentation, maxIndent); return Math.min(indentation, maxIndent);
} }
function addSplit(screenPos) { function addSplit(screenPos) {
var displayed = tokens.slice(lastSplit, screenPos); var displayed = tokens.slice(lastSplit, screenPos);
@ -1888,8 +1887,8 @@ var EditSession = function(text, mode) {
len -= 1; len -= 1;
}); });
if (splits.length === 0) { if (!splits.length) {
indent = getIndentation(); indent = getWrapIndent();
splits.indent = indent; splits.indent = indent;
} }
lastDocSplit += len; lastDocSplit += len;
@ -1959,7 +1958,7 @@ var EditSession = function(text, mode) {
// === ELSE === // === ELSE ===
// Search for the first non space/tab/placeholder/punctuation token backwards. // Search for the first non space/tab/placeholder/punctuation token backwards.
var minSplit = Math.max(split - (isCode ? 10 : wrapLimit-(wrapLimit>>2)), lastSplit - 1); var minSplit = Math.max(split - (wrapLimit -(wrapLimit>>2)), lastSplit - 1);
while (split > minSplit && tokens[split] < PLACEHOLDER_START) { while (split > minSplit && tokens[split] < PLACEHOLDER_START) {
split --; split --;
} }
@ -2095,13 +2094,12 @@ var EditSession = function(text, mode) {
}; };
this.getRowWrapIndent = function(screenRow) { this.getRowWrapIndent = function(screenRow) {
if (!this.$useWrapMode) { if (this.$useWrapMode) {
return 0; var pos = this.screenToDocumentPosition(screenRow, Number.MAX_VALUE);
var splits = this.$wrapData[pos.row];
return splits.length && splits[0] < pos.column ? splits.indent : 0;
} else { } else {
var row = this.screenToDocumentPosition(screenRow, Number.MAX_VALUE).row; return 0;
var splits = this.$wrapData[row];
var splitIndex = Math.floor(screenRow - row);
return splits.length && splitIndex > 0 ? splits.indent : 0;
} }
} }
@ -2547,7 +2545,7 @@ config.defineOptions(EditSession.prototype, "session", {
}, },
initialValue: "auto" initialValue: "auto"
}, },
indentSubsequentLines: {initialValue: true}, indentedSoftWrap: { initialValue: true },
firstLineNumber: { firstLineNumber: {
set: function() {this._signal("changeBreakpoint");}, set: function() {this._signal("changeBreakpoint");},
initialValue: 1 initialValue: 1

View file

@ -380,7 +380,8 @@ module.exports = {
line = lang.stringTrimRight(line); line = lang.stringTrimRight(line);
var tokens = EditSession.prototype.$getDisplayTokens(line); var tokens = EditSession.prototype.$getDisplayTokens(line);
var splits = EditSession.prototype.$computeWrapSplits(tokens, wrapLimit, tabSize); 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++) {
assert.ok(splits[i] == assertEqual[i]); assert.ok(splits[i] == assertEqual[i]);
@ -388,42 +389,52 @@ module.exports = {
} }
EditSession.prototype.$wrapAsCode = true; EditSession.prototype.$wrapAsCode = true;
EditSession.prototype.$indentSubsequentLines = false; EditSession.prototype.$indentedSoftWrap = false;
// Basic splitting. // Basic splitting.
computeAndAssert("foo bar foo bar", [ 12 ]); computeAndAssert("foo bar foo bar", [ 12 ]);
computeAndAssert("foo bar f bar", [ 12 ]); computeAndAssert("foo bar f bar", [ 12 ]);
computeAndAssert("foo bar f r", [ 12 ]); // 14 if we enable computeAndAssert("foo bar f r", [ 12 ]); // 14 if we enable
computeAndAssert("foo bar foo bar foo bara foo", [12, 20]); 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. // Don't split if there is only whitespaces/tabs at the end of the line.
computeAndAssert("foo foo foo \t \t", [ ]); computeAndAssert("foo foo foo \t \t", [ ]);
// If there is no space to split, force split. // If there is no space to split, force split.
computeAndAssert("foooooooooooooo", [ 12 ]); computeAndAssert("foooooooooooooo", [ 12 ]);
computeAndAssert("fooooooooooooooooooooooooooo", [12, 20]); computeAndAssert("fooooooooooooooooooooooooooo", [12, 24]);
computeAndAssert("foo bar fooooooooooobooooooo", [8, 16, 24]); computeAndAssert("foo bar fooooooooooobooooooo", [8, 20]);
// Basic splitting + tabs. // Basic splitting + tabs.
computeAndAssert("foo \t\tbar", [ 6 ]); computeAndAssert("foo \t\tbar", [ 6 ]);
computeAndAssert("foo \t \tbar", [ 7 ]); computeAndAssert("foo \t \tbar", [ 7 ]);
// Ignore spaces/tabs at beginning of split. // Ignore spaces/tabs at beginning of split.
computeAndAssert("foo \t \t \t \t bar", [7, 13]); // 14 computeAndAssert("foo \t \t \t \t bar", [ 7 ]); // 14
// Test wrapping for asian characters. // Test wrapping for asian characters.
computeAndAssert("ぁぁ", [1], 2); computeAndAssert("ぁぁ", [1], 2);
computeAndAssert(" ぁぁ", [1, 2], 2); computeAndAssert(" ぁぁ", [1, 2], 2);
computeAndAssert(" ぁ\tぁ", [1, 3], 2); computeAndAssert(" ぁ\tぁ", [1, 3], 2);
computeAndAssert(" ぁぁ\tぁ", [1, 4], 4); computeAndAssert(" ぁぁ\tぁ", [2, 4], 4);
computeAndAssert("ぁぁ ぁぁ\tぁ", [3, 6], 6);
// Test wrapping for punctuation. // Test wrapping for punctuation.
computeAndAssert(" ab.c;ef++", [1, 3, 5, 7, 8], 2); computeAndAssert(" ab.c;ef++", [2, 4, 6, 8], 2);
computeAndAssert(" ab.c;ef++", [3, 5, 8], 3);
computeAndAssert(" a.b", [1, 2, 3], 1); computeAndAssert(" a.b", [1, 2, 3], 1);
computeAndAssert("#>>", [1, 2], 1); computeAndAssert("#>>", [1, 2], 1);
// Test wrapping for punctuation in // Test wrapping for punctuation in
EditSession.prototype.$wrapAsCode = false; EditSession.prototype.$wrapAsCode = false;
computeAndAssert("ab cde, Juhu kinners", [3, 8, 13, 19], 6); computeAndAssert("ab cde, Juhu kinners", [3, 8, 13, 19], 6);
// test indented wrapping
EditSession.prototype.$indentedSoftWrap = true;
computeAndAssert("foo bar foo bar foo bara foo", [12, 25]);
computeAndAssert("fooooooooooooooooooooooooooo", [12, 24]);
computeAndAssert("\t\tfoo bar fooooooooooobooooooo", [6, 10, 16, 22, 28]);
computeAndAssert("\t\t\tfoo bar fooooooooooobooooooo", [3, 7, 11, 17, 23, 29]);
computeAndAssert("\tfoo \t \t \t \t bar", [6, 12]); // 14
}, },
"test get longest line" : function() { "test get longest line" : function() {

View file

@ -104,23 +104,24 @@ var Marker = function(parentEl) {
this.drawTextMarker = function(stringBuilder, range, clazz, layerConfig, extraStyle) { this.drawTextMarker = function(stringBuilder, range, clazz, layerConfig, extraStyle) {
// selection start // selection start
var row = range.start.row; var row = range.start.row;
var session = this.session;
var lineRange = new Range( var lineRange = new Range(
row, range.start.column, row, range.start.column,
row, this.session.getScreenLastRowColumn(row) row, session.getScreenLastRowColumn(row)
); );
this.drawSingleLineMarker(stringBuilder, lineRange, clazz + " ace_start", layerConfig, 1, extraStyle); this.drawSingleLineMarker(stringBuilder, lineRange, clazz + " ace_start", layerConfig, 1, extraStyle);
// selection end // selection end
row = range.end.row; row = range.end.row;
lineRange = new Range(row, this.session.getRowWrapIndent(row), row, range.end.column); lineRange = new Range(row, session.getRowWrapIndent(row), row, range.end.column);
this.drawSingleLineMarker(stringBuilder, lineRange, clazz, layerConfig, 0, extraStyle); this.drawSingleLineMarker(stringBuilder, lineRange, clazz, layerConfig, 0, extraStyle);
for (row = range.start.row + 1; row < range.end.row; row++) { for (row = range.start.row + 1; row < range.end.row; row++) {
lineRange.start.row = row; lineRange.start.row = row;
lineRange.start.column = this.session.getRowWrapIndent(row); lineRange.start.column = session.getRowWrapIndent(row);
lineRange.end.row = row; lineRange.end.row = row;
lineRange.end.column = this.session.getScreenLastRowColumn(row); lineRange.end.column = session.getScreenLastRowColumn(row);
this.drawSingleLineMarker(stringBuilder, lineRange, clazz, layerConfig, 1, extraStyle); this.drawSingleLineMarker(stringBuilder, lineRange, clazz, layerConfig, 1, extraStyle);
} }
}; };