Skip to content
Browse files

Add LineRange.

  • Loading branch information...
1 parent ee7ecd6 commit 89aadd4606ebbbda18b14c23978d32b710697a34 @georgebrock georgebrock committed Dec 9, 2012
Showing with 119 additions and 9 deletions.
  1. +24 −7 js/range.js
  2. +95 −2 tests/unit/range.js
View
31 js/range.js
@@ -1,14 +1,17 @@
(function () {
- Vimulator.CharacterRange = function (start, end, options) {
- this.inclusive = options.inclusive;
-
+ function assignOrdered(subject, start, end) {
if (start.row < end.row || start.row == end.row && start.col < end.col) {
- this.start = start;
- this.end = end;
+ subject.start = start;
+ subject.end = end;
} else {
- this.start = end;
- this.end = start;
+ subject.start = end;
+ subject.end = start;
}
+ }
+
+ Vimulator.CharacterRange = function (start, end, options) {
+ this.inclusive = options.inclusive;
+ assignOrdered(this, start, end);
};
Vimulator.CharacterRange.prototype.removeFrom = function (buffer) {
@@ -25,4 +28,18 @@
buffer.lines.splice(this.start.row + 1, this.end.row - this.start.row);
};
+
+ Vimulator.LineRange = function (start, end) {
+ assignOrdered(this, start, end);
+ };
+
+ Vimulator.LineRange.prototype.removeFrom = function (buffer) {
+ var count = this.end.row - this.start.row + 1;
+ buffer.lines.splice(this.start.row, count);
+ };
+
+ Vimulator.LineRange.prototype.replaceIn = function (buffer, str) {
+ var count = this.end.row - this.start.row + 1;
+ buffer.lines.splice(this.start.row, count, str);
+ };
}());
View
97 tests/unit/range.js
@@ -282,10 +282,103 @@ describe("CharacterRange set to exclusive", function () {
});
});
-describe("InclusiveLineRange", function () {
+describe("LineRange", function () {
+ var buffer, range;
+
+ beforeEach(function () {
+ buffer = {
+ lines: [
+ "The first line",
+ "The second line",
+ "The third line"
+ ]
+ };
+ });
+
describe(".removeFrom", function () {
+ it("can remove a single line", function () {
+ range = new Vimulator.LineRange(
+ {row: 0, col: 0},
+ {row: 0, col: 3}
+ );
+
+ range.removeFrom(buffer);
+
+ expect(buffer.lines).toEqual([
+ "The second line",
+ "The third line"
+ ]);
+ });
+
+ it("can remove multiple lines", function () {
+ range = new Vimulator.LineRange(
+ {row: 0, col: 7},
+ {row: 1, col: 5}
+ );
+
+ range.removeFrom(buffer);
+
+ expect(buffer.lines).toEqual([
+ "The third line"
+ ]);
+ });
+
+ it("can accept an end row before the start row", function () {
+ range = new Vimulator.LineRange(
+ {row: 1, col: 7},
+ {row: 0, col: 5}
+ );
+
+ range.removeFrom(buffer);
+
+ expect(buffer.lines).toEqual([
+ "The third line"
+ ]);
+ });
});
- describe(".replace", function () {
+ describe(".replaceIn", function () {
+ it("can replace a single line", function () {
+ range = new Vimulator.LineRange(
+ {row: 0, col: 0},
+ {row: 0, col: 3}
+ );
+
+ range.replaceIn(buffer, "Line 1");
+
+ expect(buffer.lines).toEqual([
+ "Line 1",
+ "The second line",
+ "The third line"
+ ]);
+ });
+
+ it("can replace multiple lines", function () {
+ range = new Vimulator.LineRange(
+ {row: 0, col: 7},
+ {row: 1, col: 5}
+ );
+
+ range.replaceIn(buffer, "1 & 2");
+
+ expect(buffer.lines).toEqual([
+ "1 & 2",
+ "The third line"
+ ]);
+ });
+
+ it("can accept an end row before the start row", function () {
+ range = new Vimulator.LineRange(
+ {row: 1, col: 7},
+ {row: 0, col: 5}
+ );
+
+ range.replaceIn(buffer, "Replacement");
+
+ expect(buffer.lines).toEqual([
+ "Replacement",
+ "The third line"
+ ]);
+ });
});
});

0 comments on commit 89aadd4

Please sign in to comment.
Something went wrong with that request. Please try again.