From 4e82a96353a0decfd430fa3b3b3d1d7df053173d Mon Sep 17 00:00:00 2001 From: nightwing Date: Mon, 25 Feb 2013 14:30:09 +0400 Subject: [PATCH] fix nested folds --- lib/ace/edit_session/fold.js | 38 ++++++++++++++++++++++++++++++--- lib/ace/edit_session/folding.js | 14 ++++++------ 2 files changed, 41 insertions(+), 11 deletions(-) diff --git a/lib/ace/edit_session/fold.js b/lib/ace/edit_session/fold.js index 3a66f9c5..0f898021 100644 --- a/lib/ace/edit_session/fold.js +++ b/lib/ace/edit_session/fold.js @@ -31,6 +31,9 @@ define(function(require, exports, module) { "use strict"; +var Range = require("../range").Range; +var RangeList = require("../range_list").RangeList; +var oop = require("../lib/oop") /* * Simple fold-data struct. **/ @@ -42,9 +45,11 @@ var Fold = exports.Fold = function(range, placeholder) { this.end = range.end; this.sameRow = range.start.row == range.end.row; - this.subFolds = []; + this.subFolds = this.ranges = []; }; +oop.inherits(Fold, RangeList); + (function() { this.toString = function() { @@ -64,17 +69,21 @@ var Fold = exports.Fold = function(range, placeholder) { this.subFolds.forEach(function(subFold) { fold.subFolds.push(subFold.clone()); }); + fold.collapseChildren = this.collapseChildren; return fold; }; this.addSubFold = function(fold) { if (this.range.isEqual(fold)) - return this; + return; if (!this.range.containsRange(fold)) throw "A fold can't intersect already existing fold" + fold.range + this.range; - var row = fold.range.start.row, column = fold.range.start.column; + // transform fold to local coordinates + consumeRange(fold, this.start); + + var row = fold.start.row, column = fold.start.column; for (var i = 0, cmp = -1; i < this.subFolds.length; i++) { cmp = this.subFolds[i].range.compare(row, column); if (cmp != 1) @@ -102,7 +111,30 @@ var Fold = exports.Fold = function(range, placeholder) { return fold; }; + + this.restoreRange = function(range) { + return restoreRange(range, this.start); + }; }).call(Fold.prototype); +function consumePoint(point, anchor) { + point.row -= anchor.row; + if (point.row == 0) + point.column -= anchor.column; +} +function consumeRange(range, anchor) { + consumePoint(range.start, anchor); + consumePoint(range.end, anchor); +} +function restorePoint(point, anchor) { + if (point.row == 0) + point.column += anchor.column; + point.row += anchor.row; +} +function restoreRange(range, anchor) { + restorePoint(range.start, anchor); + restorePoint(range.end, anchor); +} + }); diff --git a/lib/ace/edit_session/folding.js b/lib/ace/edit_session/folding.js index 174d4e41..c9176cfe 100644 --- a/lib/ace/edit_session/folding.js +++ b/lib/ace/edit_session/folding.js @@ -118,11 +118,6 @@ function Folding() { function addFold(fold) { folds.push(fold); - if (!fold.subFolds) - return; - - for (var i = 0; i < fold.subFolds.length; i++) - addFold(fold.subFolds[i]); } for (var i = 0; i < foldLines.length; i++) @@ -297,7 +292,9 @@ function Folding() { // Remove the folds from fold data. this.removeFolds(folds); // Add the removed folds as subfolds on the new fold. - fold.subFolds = folds; + folds.forEach(function(subFold) { + fold.addSubFold(subFold); + }); } for (var i = 0; i < foldData.length; i++) { @@ -415,8 +412,9 @@ function Folding() { this.expandFold = function(fold) { this.removeFold(fold); - fold.subFolds.forEach(function(fold) { - this.addFold(fold); + fold.subFolds.forEach(function(subFold) { + fold.restoreRange(subFold); + this.addFold(subFold); }, this); fold.subFolds = []; };