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;
this.$computeWrapSplits = function(tokens, wrapLimit) {
this.$computeWrapSplits = function(tokens, wrapLimit, tabSize) {
if (tokens.length == 0) {
return [];
}
@ -1851,6 +1851,32 @@ var EditSession = function(text, mode) {
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) {
var displayed = tokens.slice(lastSplit, screenPos);
@ -1867,14 +1893,18 @@ var EditSession = function(text, mode) {
len -= 1;
});
if (splits.length === 0) {
indent = getIndentation();
splits.indent = indent;
}
lastDocSplit += len;
splits.push(lastDocSplit);
lastSplit = screenPos;
}
while (displayLength - lastSplit > wrapLimit) {
var indent = 0;
while (displayLength - lastSplit > wrapLimit - indent) {
// 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
// a split is simple.
@ -1962,7 +1992,7 @@ var EditSession = function(text, mode) {
// around -> force a split.
if (tokens[split] == CHAR_EXT)
split--;
addSplit(split);
addSplit(split - indent);
}
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.
* @param {Number} screenRow The screen row to check
@ -2206,20 +2247,21 @@ var EditSession = function(text, mode) {
line = this.getLine(docRow);
foldLine = null;
}
var wrapIndent = 0;
if (this.$useWrapMode) {
var splits = this.$wrapData[docRow];
if (splits) {
var splitIndex = Math.floor(screenRow - row);
column = splits[splitIndex];
if(splitIndex > 0 && splits.length) {
wrapIndent = splits.indent;
docColumn = splits[splitIndex - 1] || splits[splits.length - 1];
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
// 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);
foldStartRow = docRow;
}
var wrapIndent = 0;
// Clamp textLine if in wrapMode.
if (this.$useWrapMode) {
var wrapRow = this.$wrapData[foldStartRow];
@ -2323,12 +2366,13 @@ var EditSession = function(text, mode) {
textLine = textLine.substring(
wrapRow[screenRowOffset - 1] || 0, textLine.length
);
wrapIndent = screenRowOffset > 0 ? wrapRow.indent : 0;
}
}
return {
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
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);
for (row = range.start.row + 1; row < range.end.row; row++) {
lineRange.start.row = row;
lineRange.start.column = this.session.getRowWrapIndent(row);
lineRange.end.row = row;
lineRange.end.column = this.session.getScreenLastRowColumn(row);
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 ++;
screenColumn = 0;
splitChars = splits[split] || Number.MAX_VALUE;