2021-11-09 03:53:39 +01:00
|
|
|
// Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
|
|
|
|
export interface CaretLocation {
|
|
|
|
coordinates: number[];
|
|
|
|
offset: number;
|
|
|
|
}
|
|
|
|
|
2021-11-18 10:18:39 +01:00
|
|
|
export enum Position {
|
|
|
|
Before = -1,
|
2021-11-09 03:53:39 +01:00
|
|
|
Equal,
|
2021-11-18 10:18:39 +01:00
|
|
|
After,
|
2021-11-09 03:53:39 +01:00
|
|
|
}
|
|
|
|
|
2021-11-18 10:18:39 +01:00
|
|
|
/* first is positioned {} second */
|
|
|
|
export function compareLocations(
|
|
|
|
first: CaretLocation,
|
|
|
|
second: CaretLocation,
|
|
|
|
): Position {
|
2021-11-09 03:53:39 +01:00
|
|
|
const smallerLength = Math.min(first.coordinates.length, second.coordinates.length);
|
|
|
|
|
|
|
|
for (let i = 0; i <= smallerLength; i++) {
|
|
|
|
if (first.coordinates.length === i) {
|
|
|
|
if (second.coordinates.length === i) {
|
|
|
|
if (first.offset < second.offset) {
|
2021-11-18 10:18:39 +01:00
|
|
|
return Position.Before;
|
2021-11-09 03:53:39 +01:00
|
|
|
} else if (first.offset > second.offset) {
|
2021-11-18 10:18:39 +01:00
|
|
|
return Position.After;
|
2021-11-09 03:53:39 +01:00
|
|
|
} else {
|
2021-11-18 10:18:39 +01:00
|
|
|
return Position.Equal;
|
2021-11-09 03:53:39 +01:00
|
|
|
}
|
|
|
|
}
|
2021-11-18 10:18:39 +01:00
|
|
|
return Position.Before;
|
2021-11-09 03:53:39 +01:00
|
|
|
} else if (second.coordinates.length === i) {
|
2021-11-18 10:18:39 +01:00
|
|
|
return Position.After;
|
2021-11-09 03:53:39 +01:00
|
|
|
} else if (first.coordinates[i] < second.coordinates[i]) {
|
2021-11-18 10:18:39 +01:00
|
|
|
return Position.Before;
|
2021-11-09 03:53:39 +01:00
|
|
|
} else if (first.coordinates[i] > second.coordinates[i]) {
|
2021-11-18 10:18:39 +01:00
|
|
|
return Position.After;
|
2021-11-09 03:53:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error("compareLocations: Should never happen");
|
|
|
|
}
|