fold.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Distributed under the BSD license:
  3. *
  4. * Copyright (c) 2010, Ajax.org B.V.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * * Neither the name of Ajax.org B.V. nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * ***** END LICENSE BLOCK ***** */
  30. define(function(require, exports, module) {
  31. "use strict";
  32. var Range = require("../range").Range;
  33. var RangeList = require("../range_list").RangeList;
  34. var oop = require("../lib/oop")
  35. /*
  36. * Simple fold-data struct.
  37. **/
  38. var Fold = exports.Fold = function(range, placeholder) {
  39. this.foldLine = null;
  40. this.placeholder = placeholder;
  41. this.range = range;
  42. this.start = range.start;
  43. this.end = range.end;
  44. this.sameRow = range.start.row == range.end.row;
  45. this.subFolds = this.ranges = [];
  46. };
  47. oop.inherits(Fold, RangeList);
  48. (function() {
  49. this.toString = function() {
  50. return '"' + this.placeholder + '" ' + this.range.toString();
  51. };
  52. this.setFoldLine = function(foldLine) {
  53. this.foldLine = foldLine;
  54. this.subFolds.forEach(function(fold) {
  55. fold.setFoldLine(foldLine);
  56. });
  57. };
  58. this.clone = function() {
  59. var range = this.range.clone();
  60. var fold = new Fold(range, this.placeholder);
  61. this.subFolds.forEach(function(subFold) {
  62. fold.subFolds.push(subFold.clone());
  63. });
  64. fold.collapseChildren = this.collapseChildren;
  65. return fold;
  66. };
  67. this.addSubFold = function(fold) {
  68. if (this.range.isEqual(fold))
  69. return;
  70. if (!this.range.containsRange(fold))
  71. throw new Error("A fold can't intersect already existing fold" + fold.range + this.range);
  72. // transform fold to local coordinates
  73. consumeRange(fold, this.start);
  74. var row = fold.start.row, column = fold.start.column;
  75. for (var i = 0, cmp = -1; i < this.subFolds.length; i++) {
  76. cmp = this.subFolds[i].range.compare(row, column);
  77. if (cmp != 1)
  78. break;
  79. }
  80. var afterStart = this.subFolds[i];
  81. if (cmp == 0)
  82. return afterStart.addSubFold(fold);
  83. // cmp == -1
  84. var row = fold.range.end.row, column = fold.range.end.column;
  85. for (var j = i, cmp = -1; j < this.subFolds.length; j++) {
  86. cmp = this.subFolds[j].range.compare(row, column);
  87. if (cmp != 1)
  88. break;
  89. }
  90. var afterEnd = this.subFolds[j];
  91. if (cmp == 0)
  92. throw new Error("A fold can't intersect already existing fold" + fold.range + this.range);
  93. var consumedFolds = this.subFolds.splice(i, j - i, fold);
  94. fold.setFoldLine(this.foldLine);
  95. return fold;
  96. };
  97. this.restoreRange = function(range) {
  98. return restoreRange(range, this.start);
  99. };
  100. }).call(Fold.prototype);
  101. function consumePoint(point, anchor) {
  102. point.row -= anchor.row;
  103. if (point.row == 0)
  104. point.column -= anchor.column;
  105. }
  106. function consumeRange(range, anchor) {
  107. consumePoint(range.start, anchor);
  108. consumePoint(range.end, anchor);
  109. }
  110. function restorePoint(point, anchor) {
  111. if (point.row == 0)
  112. point.column += anchor.column;
  113. point.row += anchor.row;
  114. }
  115. function restoreRange(range, anchor) {
  116. restorePoint(range.start, anchor);
  117. restorePoint(range.end, anchor);
  118. }
  119. });