231 lines
6.7 KiB
JavaScript
231 lines
6.7 KiB
JavaScript
/* ***** BEGIN LICENSE BLOCK *****
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
*
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
* the License. You may obtain a copy of the License at
|
|
* http://www.mozilla.org/MPL/
|
|
*
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
* for the specific language governing rights and limitations under the
|
|
* License.
|
|
*
|
|
* The Original Code is Ajax.org Code Editor (ACE).
|
|
*
|
|
* The Initial Developer of the Original Code is
|
|
* Ajax.org B.V.
|
|
* Portions created by the Initial Developer are Copyright (C) 2010
|
|
* the Initial Developer. All Rights Reserved.
|
|
*
|
|
* Contributor(s):
|
|
* Harutyun Amirjanyan <amirjanyan AT gmail DOT com>
|
|
*
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
* the provisions above, a recipient may use your version of this file under
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
*
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
define(function(require, exports, module) {
|
|
"use strict";
|
|
|
|
|
|
var RangeList = function() {
|
|
this.ranges = [];
|
|
};
|
|
|
|
(function() {
|
|
this.comparePoints = function(p1, p2) {
|
|
return p1.row - p2.row || p1.column - p2.column;
|
|
};
|
|
|
|
this.pointIndex = function(pos, startIndex) {
|
|
var list = this.ranges;
|
|
|
|
for (var i = startIndex || 0; i < list.length; i++) {
|
|
var range = list[i];
|
|
var cmp = this.comparePoints(pos, range.end);
|
|
|
|
if (cmp > 0)
|
|
continue;
|
|
if (cmp == 0)
|
|
return i;
|
|
cmp = this.comparePoints(pos, range.start);
|
|
if (cmp >= 0)
|
|
return i;
|
|
|
|
return -i-1;
|
|
}
|
|
return -i - 1;
|
|
};
|
|
|
|
this.add = function(range) {
|
|
var startIndex = this.pointIndex(range.start);
|
|
if (startIndex < 0)
|
|
startIndex = -startIndex - 1;
|
|
|
|
var endIndex = this.pointIndex(range.end, startIndex);
|
|
|
|
if (endIndex < 0)
|
|
endIndex = -endIndex - 1;
|
|
else
|
|
endIndex++;
|
|
|
|
return this.ranges.splice(startIndex, endIndex - startIndex, range);
|
|
};
|
|
|
|
this.addList = function(list) {
|
|
var removed = [];
|
|
for (var i = list.length; i--; ) {
|
|
removed.push.call(removed, this.add(list[i]));
|
|
}
|
|
return removed;
|
|
};
|
|
|
|
this.substractPoint = function(pos) {
|
|
var i = this.pointIndex(pos);
|
|
|
|
if (i >= 0)
|
|
return this.ranges.splice(i, 1);
|
|
};
|
|
|
|
// merge overlapping ranges
|
|
this.merge = function() {
|
|
var removed = [];
|
|
var list = this.ranges;
|
|
var next = list[0], range;
|
|
for (var i = 1; i < list.length; i++) {
|
|
range = next;
|
|
next = list[i];
|
|
var cmp = this.comparePoints(range.end, next.start);
|
|
if (cmp < 0)
|
|
continue;
|
|
|
|
if (cmp == 0 && !(range.isEmpty() || next.isEmpty()))
|
|
continue;
|
|
|
|
if (this.comparePoints(range.end, next.end) < 0) {
|
|
range.end.row = next.end.row;
|
|
range.end.column = next.end.column;
|
|
}
|
|
|
|
list.splice(i, 1);
|
|
removed.push(next);
|
|
next = range;
|
|
i--;
|
|
}
|
|
|
|
return removed;
|
|
};
|
|
|
|
this.contains = function(row, column) {
|
|
return this.pointIndex({row: row, column: column}) >= 0;
|
|
};
|
|
|
|
this.containsPoint = function(pos) {
|
|
return this.pointIndex(pos) >= 0;
|
|
};
|
|
|
|
this.rangeAtPoint = function(pos) {
|
|
var i = this.pointIndex(pos);
|
|
if (i >= 0)
|
|
return this.ranges[i];
|
|
};
|
|
|
|
|
|
this.clipRows = function(startRow, endRow) {
|
|
var list = this.ranges;
|
|
if (list[0].start.row > endRow || list[list.length - 1].start.row < startRow)
|
|
return [];
|
|
|
|
var startIndex = this.pointIndex({row: startRow, column: 0});
|
|
if (startIndex < 0)
|
|
startIndex = -startIndex - 1;
|
|
var endIndex = this.pointIndex({row: endRow, column: 0}, startIndex);
|
|
if (endIndex < 0)
|
|
endIndex = -endIndex - 1;
|
|
|
|
var clipped = [];
|
|
for (var i = startIndex; i < endIndex; i++) {
|
|
clipped.push(list[i]);
|
|
}
|
|
return clipped;
|
|
};
|
|
|
|
this.removeAll = function() {
|
|
return this.ranges.splice(0, this.ranges.length);
|
|
};
|
|
|
|
this.attach = function(session) {
|
|
if (this.session)
|
|
this.detach();
|
|
|
|
this.session = session;
|
|
this.onChange = this.$onChange.bind(this);
|
|
|
|
this.session.on('change', this.onChange);
|
|
};
|
|
|
|
this.detach = function() {
|
|
if (!this.session)
|
|
return;
|
|
this.session.removeListener('change', this.onChange);
|
|
this.session = null;
|
|
};
|
|
|
|
this.$onChange = function(e) {
|
|
var changeRange = e.data.range;
|
|
if (e.data.action[0] == "i"){
|
|
var start = changeRange.start;
|
|
var end = changeRange.end;
|
|
} else {
|
|
var end = changeRange.start;
|
|
var start = changeRange.end;
|
|
}
|
|
var startRow = start.row;
|
|
var endRow = end.row;
|
|
var lineDif = endRow - startRow;
|
|
|
|
var colDiff = -start.column + end.column;
|
|
|
|
var ranges = this.ranges;
|
|
|
|
for (var i=0, n = ranges.length; i < n; i++) {
|
|
var r = ranges[i];
|
|
if (r.end.row < startRow)
|
|
continue;
|
|
if (r.start.row > startRow)
|
|
break;
|
|
|
|
if (r.start.row == startRow && r.start.column >= start.column ) {
|
|
r.start.column += colDiff;
|
|
r.start.row += lineDif;
|
|
}
|
|
if (r.end.row == startRow && r.end.column >= start.column) {
|
|
r.end.column += colDiff;
|
|
r.end.row += lineDif;
|
|
}
|
|
}
|
|
|
|
if (lineDif != 0 && i < n) {
|
|
for (; i < n; i++) {
|
|
var r = ranges[i];
|
|
r.start.row += lineDif;
|
|
r.end.row += lineDif;
|
|
}
|
|
}
|
|
};
|
|
|
|
}).call(RangeList.prototype);
|
|
|
|
exports.RangeList = RangeList;
|
|
});
|