Skip to content

Add generalized range utility functions #1645

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions packages/common/src/types/GeneralizedRange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,91 @@ export function toLineRange(range: Range): LineRange {
export function toCharacterRange({ start, end }: Range): CharacterRange {
return { type: "character", start, end };
}

export function isGeneralizedRangeEqual(
a: GeneralizedRange,
b: GeneralizedRange,
): boolean {
if (a.type === "character" && b.type === "character") {
return a.start.isEqual(b.start) && a.end.isEqual(b.end);
}

if (a.type === "line" && b.type === "line") {
return a.start === b.start && a.end === b.end;
}

return false;
}

/**
* Determines whether {@link a} contains {@link b}. This is true if {@link a}
* starts before or equal to the start of {@link b} and ends after or equal to
* the end of {@link b}.
*
* Note that if {@link a} is a {@link CharacterRange} and {@link b} is a
* {@link LineRange}, we require that the {@link LineRange} is fully contained
* in the {@link CharacterRange}, because otherwise it visually looks like the
* {@link LineRange} is not contained because the line range extends to the edge
* of the screen.
* @param a A generalized range
* @param b A generalized range
* @returns `true` if `a` contains `b`, `false` otherwise
*/
export function generalizedRangeContains(
a: GeneralizedRange,
b: GeneralizedRange,
): boolean {
if (a.type === "character") {
if (b.type === "character") {
// a.type === "character" && b.type === "character"
return a.start.isBeforeOrEqual(b.start) && a.end.isAfterOrEqual(b.end);
}

// a.type === "character" && b.type === "line"
// Require that the line range is fully contained in the character range
// because otherwise it visually looks like the line range is not contained
return a.start.line < b.start && a.end.line > b.end;
}

if (b.type === "line") {
// a.type === "line" && b.type === "line"
return a.start <= b.start && a.end >= b.end;
}

// a.type === "line" && b.type === "character"
return a.start <= b.start.line && a.end >= b.end.line;
}

/**
* Determines whether {@link a} touches {@link b}. This is true if {@link a}
* has any intersection with {@link b}, even if the intersection is empty.
*
* In the case where one range is a {@link CharacterRange} and the other is a
* {@link LineRange}, we return `true` if they both include at least one line
* in common.
* @param a A generalized range
* @param b A generalized range
* @returns `true` if `a` touches `b`, `false` otherwise
*/
export function generalizedRangeTouches(
a: GeneralizedRange,
b: GeneralizedRange,
): boolean {
if (a.type === "character") {
if (b.type === "character") {
// a.type === "character" && b.type === "character"
return a.start.isBeforeOrEqual(b.end) && a.end.isAfterOrEqual(b.start);
}

// a.type === "character" && b.type === "line"
return a.start.line <= b.end && a.end.line >= b.start;
}

if (b.type === "line") {
// a.type === "line" && b.type === "line"
return a.start <= b.end && a.end >= b.start;
}

// a.type === "line" && b.type === "character"
return a.start <= b.end.line && a.end >= b.start.line;
}
163 changes: 163 additions & 0 deletions packages/common/src/types/generalizedRangeContains.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
import assert = require("assert");
import { generalizedRangeContains, Position } from "..";

suite("generalizedRangeContains", () => {
test("character", () => {
assert.strictEqual(
generalizedRangeContains(
{
type: "character",
start: new Position(0, 0),
end: new Position(0, 0),
},
{
type: "character",
start: new Position(0, 0),
end: new Position(0, 0),
},
),
true,
);
assert.strictEqual(
generalizedRangeContains(
{
type: "character",
start: new Position(0, 0),
end: new Position(0, 1),
},
{
type: "character",
start: new Position(0, 0),
end: new Position(0, 0),
},
),
true,
);
assert.strictEqual(
generalizedRangeContains(
{
type: "character",
start: new Position(0, 0),
end: new Position(0, 0),
},
{
type: "character",
start: new Position(0, 0),
end: new Position(0, 1),
},
),
false,
);
});

test("line", () => {
assert.strictEqual(
generalizedRangeContains(
{
type: "line",
start: 0,
end: 0,
},
{
type: "line",
start: 0,
end: 0,
},
),
true,
);
assert.strictEqual(
generalizedRangeContains(
{
type: "line",
start: 0,
end: 1,
},
{
type: "line",
start: 0,
end: 0,
},
),
true,
);
assert.strictEqual(
generalizedRangeContains(
{
type: "line",
start: 0,
end: 0,
},
{
type: "line",
start: 1,
end: 1,
},
),
false,
);
});

test("mixed", () => {
assert.strictEqual(
generalizedRangeContains(
{
type: "line",
start: 0,
end: 1,
},
{
type: "character",
start: new Position(0, 0),
end: new Position(1, 1),
},
),
true,
);
assert.strictEqual(
generalizedRangeContains(
{
type: "line",
start: 0,
end: 0,
},
{
type: "character",
start: new Position(0, 0),
end: new Position(1, 0),
},
),
false,
);
assert.strictEqual(
generalizedRangeContains(
{
type: "character",
start: new Position(0, 0),
end: new Position(2, 0),
},
{
type: "line",
start: 1,
end: 1,
},
),
true,
);
assert.strictEqual(
generalizedRangeContains(
{
type: "character",
start: new Position(0, 0),
end: new Position(1, 0),
},
{
type: "line",
start: 1,
end: 1,
},
),
false,
);
});
});
139 changes: 139 additions & 0 deletions packages/common/src/types/generalizedRangeTouches.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import assert = require("assert");
import { GeneralizedRange, generalizedRangeTouches, Position } from "..";

suite("generalizedRangeTouches", () => {
test("character", () => {
testRangePair(
{
type: "character",
start: new Position(0, 0),
end: new Position(0, 0),
},
{
type: "character",
start: new Position(0, 0),
end: new Position(0, 0),
},
true,
);
testRangePair(
{
type: "character",
start: new Position(0, 0),
end: new Position(0, 1),
},
{
type: "character",
start: new Position(0, 0),
end: new Position(0, 0),
},
true,
);
testRangePair(
{
type: "character",
start: new Position(0, 0),
end: new Position(0, 1),
},
{
type: "character",
start: new Position(0, 1),
end: new Position(0, 2),
},
true,
);
testRangePair(
{
type: "character",
start: new Position(0, 0),
end: new Position(0, 0),
},
{
type: "character",
start: new Position(0, 1),
end: new Position(0, 1),
},
false,
);
});

test("line", () => {
testRangePair(
{
type: "line",
start: 0,
end: 0,
},
{
type: "line",
start: 0,
end: 0,
},
true,
);
testRangePair(
{
type: "line",
start: 0,
end: 1,
},
{
type: "line",
start: 0,
end: 0,
},
true,
);
testRangePair(
{
type: "line",
start: 0,
end: 0,
},
{
type: "line",
start: 1,
end: 1,
},
false,
);
});

test("mixed", () => {
testRangePair(
{
type: "line",
start: 0,
end: 0,
},
{
type: "character",
start: new Position(0, 0),
end: new Position(1, 1),
},
true,
);
testRangePair(
{
type: "line",
start: 0,
end: 0,
},
{
type: "character",
start: new Position(1, 0),
end: new Position(1, 1),
},
false,
);
});
});

function testRangePair(
a: GeneralizedRange,
b: GeneralizedRange,
expected: boolean,
) {
assert.strictEqual(generalizedRangeTouches(a, b), expected);
assert.strictEqual(generalizedRangeTouches(b, a), expected);
}
Loading