Add wrap indent

This commit is contained in:
DanyaPostfactum 2014-10-12 01:56:19 +10:00
commit 60367772a9
3 changed files with 56 additions and 9 deletions

View file

@ -1840,7 +1840,7 @@ var EditSession = function(text, mode) {
TAB_SPACE = 12; TAB_SPACE = 12;
this.$computeWrapSplits = function(tokens, wrapLimit) { this.$computeWrapSplits = function(tokens, wrapLimit, tabSize) {
if (tokens.length == 0) { if (tokens.length == 0) {
return []; return [];
} }
@ -1851,6 +1851,32 @@ var EditSession = function(text, mode) {
var isCode = this.$wrapAsCode; var isCode = this.$wrapAsCode;
var indentSubsequentLines = true;
var maxIndent = wrapLimit > 8 ? Math.floor(wrapLimit / 2) : 0;
function getIndentation() {
var indentation = 0;
if (maxIndent === 0)
return indentation;
if (indentSubsequentLines) {
for (var i = 0; i < tokens.length; i++) {
var token = tokens[i];
if (token == SPACE)
indentation += 1;
else if (token == TAB)
indentation += tabSize;
else if (token == TAB_SPACE)
continue;
else
break;
}
}
if (isCode)
indentation += tabSize;
return Math.min(indentation, maxIndent);
}
function addSplit(screenPos) { function addSplit(screenPos) {
var displayed = tokens.slice(lastSplit, screenPos); var displayed = tokens.slice(lastSplit, screenPos);
@ -1867,14 +1893,18 @@ var EditSession = function(text, mode) {
len -= 1; len -= 1;
}); });
if (splits.length === 0) {
indent = getIndentation();
splits.indent = indent;
}
lastDocSplit += len; lastDocSplit += len;
splits.push(lastDocSplit); splits.push(lastDocSplit);
lastSplit = screenPos; lastSplit = screenPos;
} }
var indent = 0;
while (displayLength - lastSplit > wrapLimit) { while (displayLength - lastSplit > wrapLimit - indent) {
// This is, where the split should be. // This is, where the split should be.
var split = lastSplit + wrapLimit; var split = lastSplit + wrapLimit - indent;
// If there is a space or tab at this split position, then making // If there is a space or tab at this split position, then making
// a split is simple. // a split is simple.
@ -1962,7 +1992,7 @@ var EditSession = function(text, mode) {
// around -> force a split. // around -> force a split.
if (tokens[split] == CHAR_EXT) if (tokens[split] == CHAR_EXT)
split--; split--;
addSplit(split); addSplit(split - indent);
} }
return splits; return splits;
}; };
@ -2069,6 +2099,17 @@ var EditSession = function(text, mode) {
} }
}; };
this.getRowWrapIndent = function(screenRow) {
if (!this.$useWrapMode) {
return 0;
} else {
var row = this.screenToDocumentPosition(screenRow, Number.MAX_VALUE).row;
var splits = this.$wrapData[row];
var splitIndex = Math.floor(screenRow - row);
return splits.length && splitIndex > 0 ? splits.indent : 0;
}
}
/** /**
* Returns the position (on screen) for the last character in the provided screen row. * Returns the position (on screen) for the last character in the provided screen row.
* @param {Number} screenRow The screen row to check * @param {Number} screenRow The screen row to check
@ -2206,20 +2247,21 @@ var EditSession = function(text, mode) {
line = this.getLine(docRow); line = this.getLine(docRow);
foldLine = null; foldLine = null;
} }
var wrapIndent = 0;
if (this.$useWrapMode) { if (this.$useWrapMode) {
var splits = this.$wrapData[docRow]; var splits = this.$wrapData[docRow];
if (splits) { if (splits) {
var splitIndex = Math.floor(screenRow - row); var splitIndex = Math.floor(screenRow - row);
column = splits[splitIndex]; column = splits[splitIndex];
if(splitIndex > 0 && splits.length) { if(splitIndex > 0 && splits.length) {
wrapIndent = splits.indent;
docColumn = splits[splitIndex - 1] || splits[splits.length - 1]; docColumn = splits[splitIndex - 1] || splits[splits.length - 1];
line = line.substring(docColumn); line = line.substring(docColumn);
} }
} }
} }
docColumn += this.$getStringScreenWidth(line, screenColumn)[1]; docColumn += this.$getStringScreenWidth(line, screenColumn - wrapIndent)[1];
// We remove one character at the end so that the docColumn // We remove one character at the end so that the docColumn
// position returned is not associated to the next row on the screen. // position returned is not associated to the next row on the screen.
@ -2311,6 +2353,7 @@ var EditSession = function(text, mode) {
textLine = this.getLine(docRow).substring(0, docColumn); textLine = this.getLine(docRow).substring(0, docColumn);
foldStartRow = docRow; foldStartRow = docRow;
} }
var wrapIndent = 0;
// Clamp textLine if in wrapMode. // Clamp textLine if in wrapMode.
if (this.$useWrapMode) { if (this.$useWrapMode) {
var wrapRow = this.$wrapData[foldStartRow]; var wrapRow = this.$wrapData[foldStartRow];
@ -2323,12 +2366,13 @@ var EditSession = function(text, mode) {
textLine = textLine.substring( textLine = textLine.substring(
wrapRow[screenRowOffset - 1] || 0, textLine.length wrapRow[screenRowOffset - 1] || 0, textLine.length
); );
wrapIndent = screenRowOffset > 0 ? wrapRow.indent : 0;
} }
} }
return { return {
row: screenRow, row: screenRow,
column: this.$getStringScreenWidth(textLine)[0] column: wrapIndent + this.$getStringScreenWidth(textLine)[0]
}; };
}; };

View file

@ -113,11 +113,12 @@ var Marker = function(parentEl) {
// selection end // selection end
row = range.end.row; row = range.end.row;
lineRange = new Range(row, 0, row, range.end.column); lineRange = new Range(row, this.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.end.row = row; lineRange.end.row = row;
lineRange.end.column = this.session.getScreenLastRowColumn(row); lineRange.end.column = this.session.getScreenLastRowColumn(row);
this.drawSingleLineMarker(stringBuilder, lineRange, clazz, layerConfig, 1, extraStyle); this.drawSingleLineMarker(stringBuilder, lineRange, clazz, layerConfig, 1, extraStyle);

View file

@ -420,6 +420,8 @@ var Text = function(parentEl) {
); );
} }
stringBuilder.push(lang.stringRepeat("\xa0", splits.indent));
split ++; split ++;
screenColumn = 0; screenColumn = 0;
splitChars = splits[split] || Number.MAX_VALUE; splitChars = splits[split] || Number.MAX_VALUE;