Skip to content

Commit beb38fb

Browse files
Sort ranges before calculating highlights
1 parent ee69491 commit beb38fb

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

packages/common/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ export * from "./util/regex";
110110
export * from "./util/selectionsEqual";
111111
export * from "./util/serializedMarksToTokenHats";
112112
export * from "./util/serializeScopeType";
113+
export * from "./util/sortRanges";
113114
export * from "./util/splitKey";
114115
export * from "./util/toPlainObject";
115116
export * from "./util/type";
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import type { Range } from "../types/Range";
2+
3+
/**
4+
* Sorts an array of ranges in ascending order based on their start and end
5+
* positions.
6+
*
7+
* @param ranges The array of ranges to sort
8+
* @returns The sorted array of ranges
9+
*/
10+
export function sortRanges(ranges: Range[]) {
11+
return ranges.sort((a, b) => {
12+
if (a.start.isBefore(b.start)) {
13+
return -1;
14+
}
15+
if (a.start.isAfter(b.start)) {
16+
return 1;
17+
}
18+
if (a.end.isBefore(b.end)) {
19+
return -1;
20+
}
21+
if (a.end.isAfter(b.end)) {
22+
return 1;
23+
}
24+
return 0;
25+
});
26+
}

packages/cursorless-org-docs/src/docs/user/languages/components/ScopeSupport.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
Range,
33
serializeScopeType,
4+
sortRanges,
45
type ScopeSupportFacetInfo,
56
type ScopeTypeType,
67
} from "@cursorless/common";
@@ -155,7 +156,7 @@ function getHighlights(fixture: Fixture, rangeType: RangeType): Highlight[] {
155156
rangeType === "content"
156157
? scope.targets.map((t) => t.content)
157158
: scope.targets.map((t) => t.removal ?? t.content);
158-
const ranges = conciseRanges.map((r) => Range.fromConcise(r));
159+
const ranges = sortRanges(conciseRanges.map((r) => Range.fromConcise(r)));
159160

160161
if (scope.domain != null && !conciseRanges.includes(scope.domain)) {
161162
domainRanges.push(Range.fromConcise(scope.domain));
@@ -196,7 +197,7 @@ function getHighlights(fixture: Fixture, rangeType: RangeType): Highlight[] {
196197
highlights.some((h) => highlights.some((o) => hasOverlap(h.range, o.range)))
197198
) {
198199
console.error("Overlapping highlights detected:");
199-
console.error(fixture.name);
200+
console.error(fixture);
200201
console.error(highlights);
201202
}
202203

0 commit comments

Comments
 (0)