diff --git a/lib/ace/edit_session.js b/lib/ace/edit_session.js index addbe66a..0ac4be4e 100644 --- a/lib/ace/edit_session.js +++ b/lib/ace/edit_session.js @@ -471,11 +471,25 @@ var EditSession = function(text, mode) { var longestScreenLine = 0; for ( var i = 0; i < lines.length; i++) { - var line = lines[i], - len = line.length, - screenLen = this.$getStringScreenWidth(line)[0]; + var foldLine = this.getFoldLine(i), + line, len; + + line = lines[i]; + if (foldLine) { + var end = foldLine.range.end; + line = this.getFoldDisplayLine(foldLine, end.row, line.length); + // Continue after the foldLine.end.row. All the lines in + // between are folded. + i = end.row; + } + len = line.length; longestLine = Math.max(longestLine, len); - longestScreenLine = Math.max(longestScreenLine, screenLen); + if (!this.$useWrapMode) { + longestScreenLine = Math.max( + longestScreenLine, + this.$getStringScreenWidth(line)[0] + ); + } } this.width = longestLine; @@ -875,7 +889,7 @@ var EditSession = function(text, mode) { start = e.data.range.start, end = e.data.range.end; - console.log("onChange", action, e.data.range + ""); +// console.log("onChange", action, e.data.range + ""); if (action.indexOf("Lines") != -1) { if (action == "insertLines") { @@ -1147,34 +1161,16 @@ var EditSession = function(text, mode) { return this.getRowLength(row) * config.lineHeight; } - this.getScreenLastRowColumn = function(screenRow, returnDocPosition) { - if (!this.$useWrapMode) { - return this.$getStringScreenWidth(this.getLine(screenRow))[0]; - } - - var rowData = this.$screenToDocumentRow(screenRow); - var docRow = rowData[0], - row = rowData[1]; - - var start, end; - if (this.$wrapData[docRow][row]) { - start = (this.$wrapData[docRow][row - 1] || 0); - end = this.$wrapData[docRow][row]; - returnDocPosition && end--; - } else { - end = this.getLine(docRow).length; - start = (this.$wrapData[docRow][row - 1] || 0); - } - if (!returnDocPosition) { - return this.$getStringScreenWidth(this.getLine(docRow).substring(start, end))[0]; - } else { - return end; - } + this.getScreenLastRowColumn = function(screenRow) { + // Note: This won't work if someone has more then + // 1.7976931348623158e+307 characters in one row. But I think we can + // live with this limitation ;) + return this.screenToDocumentColumn(screenRow, Number.MAX_VALUE / 10) }; this.getDocumentLastRowColumn = function(docRow, docColumn) { var screenRow = this.documentToScreenRow(docRow, docColumn); - return this.getScreenLastRowColumn(screenRow, true); + return this.getScreenLastRowColumn(screenRow); } this.getRowSplitData = function(row) { @@ -1201,7 +1197,7 @@ var EditSession = function(text, mode) { foldLineRowLength; var row = 0, - rowLength; + rowLength = 0; while (row <= screenRow) { foldLine = this.getFoldLine(docRow, lastFoldLine); @@ -1209,7 +1205,7 @@ var EditSession = function(text, mode) { lastFoldLine = foldLine; rowLength = foldLine.getRowLength(); } else { - rowLength = this.getRowLength(row); + rowLength = this.getRowLength(docRow); } if (row + rowLength - 1 >= screenRow) { break; @@ -1294,6 +1290,17 @@ var EditSession = function(text, mode) { }; this.doc2Screen = function(docRow, docColumn) { + var wrapData; + if (this.$useWrapMode) { + wrapData = this.$wrapData; + if (docRow > wrapData.length - 1) { + return [ + this.getScreenLength(), + wrapData.length == 0 ? 0 : (wrapData[wrapData.length - 1].length - 1) + ]; + } + } + var screenRow = 0, screenColumn = 0, foldStartRow = null, @@ -1331,30 +1338,19 @@ var EditSession = function(text, mode) { textLine = this.getLine(docRow).substring(0, docColumn); foldStartRow = docRow; } else { - // Build the textline using the FoldLine walker. - var line; - foldLine.walk(function(placeholder, row, column, lastColumn, isNewRow) { - if (placeholder) { - textLine += placeholder; - } else { - if (isNewRow) { - line = this.getLine(row); - } - textLine += line.substring(lastColumn, column); - } - }.bind(this), docRow, docColumn) + textLine = this.getFoldDisplayLine(foldLine, docRow, docColumn); } // Clamp textLine if in wrapMode. if (this.$useWrapMode) { - var wrapData = this.$wrapData[foldStartRow]; + var wrapRow = wrapData[foldStartRow]; var screenRowOffset = 0; - while (docColumn >= wrapData[screenRowOffset]) { + while (docColumn >= wrapRow[screenRowOffset]) { screenRow ++; screenRowOffset++; } textLine = textLine.substring( - wrapData[screenRowOffset - 1] || 0, textLine.length); + wrapRow[screenRowOffset - 1] || 0, textLine.length); } return [screenRow, this.$getStringScreenWidth(textLine)[0], textLine]; @@ -1868,10 +1864,9 @@ var EditSession = function(text, mode) { } // TODO: Recalculate wrapData - // TODO: Recalculate width etc. - // TODO: Mark as dirty etc. // Notify that fold data has changed. + this.$modified = true; this._dispatchEvent("changeFold"); }; @@ -1917,6 +1912,7 @@ var EditSession = function(text, mode) { // TODO: Update wrapData. // Notify that fold data has changed. + this.$modified = true; this._dispatchEvent("changeFold"); } @@ -1932,6 +1928,7 @@ var EditSession = function(text, mode) { cloneFolds.forEach(function(fold) { this.removeFold(fold); }, this); + this.$modified = true; } /** @@ -1949,6 +1946,24 @@ var EditSession = function(text, mode) { : docRow) }; + this.getFoldDisplayLine = function(foldLine, endRow, endColumn) { + // Build the textline using the FoldLine walker. + var line = "", + textLine = ""; + + foldLine.walk(function(placeholder, row, column, lastColumn, isNewRow) { + if (placeholder) { + textLine += placeholder; + } else { + if (isNewRow) { + line = this.getLine(row); + } + textLine += line.substring(lastColumn, column); + } + }.bind(this), endRow, endColumn); + return textLine; + } + }).call(EditSession.prototype); exports.EditSession = EditSession;