Skip to content
This repository has been archived by the owner on Jun 8, 2019. It is now read-only.

Commit

Permalink
Inclusive/Exclusive ranges.
Browse files Browse the repository at this point in the history
  • Loading branch information
georgebrock committed Dec 9, 2012
1 parent b0f6603 commit ee7ecd6
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 23 deletions.
12 changes: 8 additions & 4 deletions js/range.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
(function () {
Vimulator.InclusiveCharacterRange = function (start, end) {
Vimulator.CharacterRange = function (start, end, options) {
this.inclusive = options.inclusive;

if (start.row < end.row || start.row == end.row && start.col < end.col) {
this.start = start;
this.end = end;
Expand All @@ -9,15 +11,17 @@
}
};

Vimulator.InclusiveCharacterRange.prototype.removeFrom = function (buffer) {
Vimulator.CharacterRange.prototype.removeFrom = function (buffer) {
this.replaceIn(buffer, "");
};

Vimulator.InclusiveCharacterRange.prototype.replaceIn = function (buffer, str) {
Vimulator.CharacterRange.prototype.replaceIn = function (buffer, str) {
var endOffset = this.inclusive ? 1 : 0;

buffer.lines[this.start.row] =
buffer.lines[this.start.row].substr(0, this.start.col) +
str +
buffer.lines[this.end.row].substr(this.end.col + 1);
buffer.lines[this.end.row].substr(this.end.col + endOffset);

buffer.lines.splice(this.start.row + 1, this.end.row - this.start.row);
};
Expand Down
180 changes: 161 additions & 19 deletions tests/unit/range.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
describe("InclusiveCharacterRange", function () {
describe("CharacterRange set to inclusive", function () {
var buffer, range;

beforeEach(function () {
Expand All @@ -13,9 +13,10 @@ describe("InclusiveCharacterRange", function () {

describe(".removeFrom", function () {
it("can remove characters in a single line", function () {
range = new Vimulator.InclusiveCharacterRange(
range = new Vimulator.CharacterRange(
{row: 0, col: 4},
{row: 0, col: 9}
{row: 0, col: 9},
{inclusive: true}
);

range.removeFrom(buffer);
Expand All @@ -28,9 +29,10 @@ describe("InclusiveCharacterRange", function () {
});

it("can remove chracters over multiple lines", function () {
range = new Vimulator.InclusiveCharacterRange(
range = new Vimulator.CharacterRange(
{row: 0, col: 7},
{row: 2, col: 4}
{row: 2, col: 4},
{inclusive: true}
);

range.removeFrom(buffer);
Expand All @@ -41,9 +43,10 @@ describe("InclusiveCharacterRange", function () {
});

it("can remove characters to the end of a line", function () {
range = new Vimulator.InclusiveCharacterRange(
range = new Vimulator.CharacterRange(
{row: 0, col: 3},
{row: 0, col: 13}
{row: 0, col: 13},
{inclusive: true}
);

range.removeFrom(buffer);
Expand All @@ -56,9 +59,10 @@ describe("InclusiveCharacterRange", function () {
});

it("can accept a start position before the end position", function () {
range = new Vimulator.InclusiveCharacterRange(
range = new Vimulator.CharacterRange(
{row: 0, col: 9},
{row: 0, col: 4}
{row: 0, col: 4},
{inclusive: true}
);

range.removeFrom(buffer);
Expand All @@ -73,9 +77,10 @@ describe("InclusiveCharacterRange", function () {

describe(".replaceIn", function () {
it("can replace characters in a single line", function () {
range = new Vimulator.InclusiveCharacterRange(
range = new Vimulator.CharacterRange(
{row: 0, col: 4},
{row: 0, col: 9}
{row: 0, col: 9},
{inclusive: true}
);

range.replaceIn(buffer, "l-l-");
Expand All @@ -88,9 +93,10 @@ describe("InclusiveCharacterRange", function () {
});

it("can replace chracters over multiple lines", function () {
range = new Vimulator.InclusiveCharacterRange(
range = new Vimulator.CharacterRange(
{row: 0, col: 7},
{row: 2, col: 4}
{row: 2, col: 4},
{inclusive: true}
);

range.replaceIn(buffer, "-");
Expand All @@ -101,9 +107,10 @@ describe("InclusiveCharacterRange", function () {
});

it("can replace characters to the end of a line", function () {
range = new Vimulator.InclusiveCharacterRange(
range = new Vimulator.CharacterRange(
{row: 0, col: 3},
{row: 0, col: 13}
{row: 0, col: 13},
{inclusive: true}
);

range.replaceIn(buffer, "...");
Expand All @@ -116,9 +123,10 @@ describe("InclusiveCharacterRange", function () {
});

it("can accept a start position before the end position", function () {
range = new Vimulator.InclusiveCharacterRange(
range = new Vimulator.CharacterRange(
{row: 0, col: 9},
{row: 0, col: 4}
{row: 0, col: 4},
{inclusive: true}
);

range.replaceIn(buffer, "bee-");
Expand All @@ -132,11 +140,145 @@ describe("InclusiveCharacterRange", function () {
});
});

describe("ExclusiveCharacterRange", function () {
describe("CharacterRange set to exclusive", function () {
var buffer, range;

beforeEach(function () {
buffer = {
lines: [
"The first line",
"The second line",
"The third line"
]
};
});

describe(".removeFrom", function () {
it("can remove characters in a single line", function () {
range = new Vimulator.CharacterRange(
{row: 0, col: 4},
{row: 0, col: 9},
{inclusive: false}
);

range.removeFrom(buffer);

expect(buffer.lines).toEqual([
"The line",
"The second line",
"The third line"
]);
});

it("can remove chracters over multiple lines", function () {
range = new Vimulator.CharacterRange(
{row: 0, col: 7},
{row: 2, col: 4},
{inclusive: false}
);

range.removeFrom(buffer);

expect(buffer.lines).toEqual([
"The firthird line"
]);
});

it("cannot remove characters to the end of a line", function () {
range = new Vimulator.CharacterRange(
{row: 0, col: 3},
{row: 0, col: 13},
{inclusive: false}
);

range.removeFrom(buffer);

expect(buffer.lines).toEqual([
"Thee",
"The second line",
"The third line"
]);
});

it("can accept a start position before the end position", function () {
range = new Vimulator.CharacterRange(
{row: 0, col: 9},
{row: 0, col: 4},
{inclusive: false}
);

range.removeFrom(buffer);

expect(buffer.lines).toEqual([
"The line",
"The second line",
"The third line"
]);
});
});

describe(".replace", function () {
describe(".replaceIn", function () {
it("can replace characters in a single line", function () {
range = new Vimulator.CharacterRange(
{row: 0, col: 4},
{row: 0, col: 9},
{inclusive: false}
);

range.replaceIn(buffer, "l-l-");

expect(buffer.lines).toEqual([
"The l-l- line",
"The second line",
"The third line"
]);
});

it("can replace chracters over multiple lines", function () {
range = new Vimulator.CharacterRange(
{row: 0, col: 7},
{row: 2, col: 4},
{inclusive: false}
);

range.replaceIn(buffer, "-");

expect(buffer.lines).toEqual([
"The fir-third line"
]);
});

it("cannot replace characters to the end of a line", function () {
range = new Vimulator.CharacterRange(
{row: 0, col: 3},
{row: 0, col: 13},
{inclusive: false}
);

range.replaceIn(buffer, "...");

expect(buffer.lines).toEqual([
"The...e",
"The second line",
"The third line"
]);
});

it("can accept a start position before the end position", function () {
range = new Vimulator.CharacterRange(
{row: 0, col: 9},
{row: 0, col: 4},
{inclusive: false}
);

range.replaceIn(buffer, "bee-");

expect(buffer.lines).toEqual([
"The bee- line",
"The second line",
"The third line"
]);
});
});
});

Expand Down

0 comments on commit ee7ecd6

Please sign in to comment.