From c22d80398ad3ef298af1b77f17dbe1f8e2ff45e6 Mon Sep 17 00:00:00 2001 From: Julian Viereck Date: Mon, 25 Apr 2011 00:51:04 +0200 Subject: [PATCH] Add handling for simple adding/removing of content to realign the folds --- lib/ace/edit_session.js | 79 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 78 insertions(+), 1 deletion(-) diff --git a/lib/ace/edit_session.js b/lib/ace/edit_session.js index 24f773d4..c1005e7b 100644 --- a/lib/ace/edit_session.js +++ b/lib/ace/edit_session.js @@ -879,6 +879,15 @@ var EditSession = function(text, mode) { useWrapMode && this.$wrapData.splice(firstRow, len); // TODO: Remove no longer needed folds here. // TODO: Update row data on folds. + + var foldLines = this.$foldData; + for (var i = 0; i < foldLines.length; i++) { + var foldLine = foldLines[i]; + if (foldLine.start.row >= firstRow + len) { + foldLine.shiftRow(-len); + } + } + lastRow = firstRow; } else { var args; @@ -889,7 +898,45 @@ var EditSession = function(text, mode) { } // TODO: Expand folds here if needed. - // TODO: Update row data on folds. + // TODO: Split foldLine in case there are new lines added in + // between of a foldLine. + var foldLines = this.$foldData; + for (var i = 0; i < foldLines.length; i++) { + var foldLine = foldLines[i]; + if (foldLine.start.row >= firstRow) { + foldLine.shiftRow(len); + } + } + } + } else { + len = Math.abs(e.data.range.start.column - e.data.range.end.column); + var column; + if (action.indexOf("insert") != -1) { + column = e.data.range.start.column; + } else { + column = e.data.range.end.column; + len = -len; + } + var foldLine = this.getFoldLine(firstRow); + if (foldLine) { + // TODO: Adding new characters into a fold range should + // expand/remove the fold. + // for (var i = 0; i <= ) + var ret = foldLine.getNextFoldTo(firstRow, column); + if (ret) { + if (ret.kind == "inside") { + // TODO + } else if (ret.fold.start.row == firstRow){ + var folds = foldLine.folds; + for (var i = folds.indexOf(ret.fold); i < folds.length; i++) { + folds[i].start.column += len; + if (!folds[i].sameLine) { + break; + } + folds[i].end.column += len; + } + } + } } } @@ -1410,6 +1457,15 @@ var EditSession = function(text, mode) { } (function() { + this.shiftRow = function(shift) { + this.start.row += shift; + this.end.row += shift; + this.folds.forEach(function(fold) { + fold.start.row += shift; + fold.end.row += shift; + }); + } + this.addFold = function(fold) { if (fold.sameLine) { if (fold.start.row < this.startRow || fold.endRow > this.endRow) { @@ -1483,11 +1539,32 @@ var EditSession = function(text, mode) { callback(null, endRow, endColumn, lastEnd, isNewRow); } + this.getNextFoldTo = function(row, column) { + var fold; + for (var i = 0; i < this.folds.length; i++) { + fold = this.folds[i]; + cmp = fold.compare(row, column); + if (cmp == -1) { + return { + fold: fold, + kind: "after" + }; + } else if (cmp == 0) { + return { + fold: fold, + kind: "inside" + } + } + } + return null; + } + this.getStringAt = function(session, row, column, trim) { var fold, lastFold, cmp, str; lastFold = { end: { column: 0 } }; + // TODO: Refactor to use getNextFoldTo function. for (var i = 0; i < this.folds.length; i++) { fold = this.folds[i]; cmp = fold.compare(row, column);