add draft implementation of rangeList to test it's usefulness

This commit is contained in:
nightwing 2012-03-14 14:57:58 +04:00
commit a4f82e70f6
3 changed files with 241 additions and 0 deletions

142
lib/ace/range_list.js Normal file
View file

@ -0,0 +1,142 @@
/* ***** 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):
* Fabian Jakobs <fabian AT ajax DOT org>
*
* 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(startRow, startColumn, endRow, endColumn) {
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-2;
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++;
this.ranges.splice(startIndex, endIndex - startIndex, range);
return startIndex;
};
this.addList = function(list) {
list.forEach(this.add, this);
};
this.substractRange = function(range) {
var i = this.pointIndex(range.start);
if (i > 0)
this.splice(i, 1);
i = this.pointIndex(range.end);
if (i > 0)
this.splice(i, 1);
};
this.substractPoint = function(pos) {
var i = this.pointIndex(pos);
if (i > 0)
this.splice(i, 1);
};
this.contains = function(row, column) {
return this.pointIndex({row: row, column: column}) >= 0;
};
this.containsRange = function(row, column) {
//todo
};
this.containsPoint = function(pos) {
return this.pointIndex(pos) >= 0;
};
this.attach = function(session) {
if (this.session)
this.dettach();
this.session = session;
this.onChange = this.$onChange.bind(this);
this.session.on('change', this.onChange);
};
this.dettach = function() {
if (!this.session)
return;
this.session.removeListener('change', this.onChange)
this.session = null
};
this.$onChange = function(e) {
// todo
};
}).call(RangeList.prototype);
exports.RangeList = RangeList;
});

View file

@ -0,0 +1,98 @@
/* ***** 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):
* Fabian Jakobs <fabian AT ajax DOT org>
*
* 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 ***** */
if (typeof process !== "undefined") {
require("amd-loader");
}
define(function(require, exports, module) {
"use strict";
var Range = require("./range").Range;
var RangeList = require("./range_list").RangeList;
var EditSession = require("./edit_session").EditSession;
var assert = require("./test/assertions");
module.exports = {
name: "ACE range_list.js",
"test: rangeList pointIndex": function() {
var rangeList = new RangeList();
rangeList.ranges = [
new Range(1,2,3,4),
new Range(4,2,5,4),
new Range(8,8,9,9)
];
assert.equal(rangeList.pointIndex({row: 0, column: 1}), -1);
assert.equal(rangeList.pointIndex({row: 1, column: 2}), -1);
assert.equal(rangeList.pointIndex({row: 1, column: 3}), 0);
assert.equal(rangeList.pointIndex({row: 3, column: 4}), -2);
assert.equal(rangeList.pointIndex({row: 4, column: 1}), -2);
assert.equal(rangeList.pointIndex({row: 5, column: 1}), 1);
assert.equal(rangeList.pointIndex({row: 8, column: 9}), 2);
assert.equal(rangeList.pointIndex({row: 18, column: 9}), -4);
},
"test: rangeList add": function() {
var rangeList = new RangeList();
rangeList.addList([
new Range(9,0,9,1),
new Range(1,2,3,4),
new Range(8,8,9,9),
new Range(4,2,5,4),
new Range(3,20,3,24),
new Range(6,6,7,7)
]);
assert.equal(rangeList.ranges.length, 5);
rangeList.add(new Range(1,2,3,5));
assert.range(rangeList.ranges[0], 1,2,3,5);
assert.equal(rangeList.ranges.length, 5);
rangeList.add(new Range(7,7,7,7));
assert.range(rangeList.ranges[4], 7,7,7,7);
}
};
});
if (typeof module !== "undefined" && module === require.main) {
require("asyncjs").test.testcase(module.exports).exec()
}

View file

@ -42,6 +42,7 @@ var testNames = [
"ace/mode/folding/pythonic_test",
"ace/mode/folding/xml_test",
"ace/range_test",
"ace/range_list_test",
"ace/search_test",
"ace/selection_test",
"ace/token_iterator_test",