Skip to content

Commit 0a5f73a

Browse files
More improvements to empty ranges
1 parent 63877b3 commit 0a5f73a

File tree

1 file changed

+31
-26
lines changed

1 file changed

+31
-26
lines changed

packages/cursorless-org-docs/src/docs/components/flattenHighlights.ts

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,14 @@ import type { BorderRadius, Highlight, Style } from "./types";
1010
export function flattenHighlights(highlights: Highlight[]): Highlight[] {
1111
const positions = getUniquePositions(highlights);
1212
const results: Highlight[] = [];
13+
1314
for (let i = 0; i < positions.length - 1; i++) {
1415
const range = new Range(positions[i], positions[i + 1]);
1516

16-
const matchingHighlights = range.isEmpty
17-
? highlights.filter((h) => h.range.contains(range))
18-
: highlights.filter((h) => {
19-
const intersection = h.range.intersection(range);
20-
return intersection && !intersection.isEmpty;
21-
});
17+
const matchingHighlights = highlights.filter((h) => {
18+
const intersection = h.range.intersection(range);
19+
return intersection && !intersection.isEmpty;
20+
});
2221

2322
// This range could be between two scopes.
2423
if (matchingHighlights.length === 0) {
@@ -34,8 +33,14 @@ export function flattenHighlights(highlights: Highlight[]): Highlight[] {
3433

3534
if (emptyHighlights.length > 0) {
3635
for (const emptyHighlight of emptyHighlights) {
37-
if (!results.some((h) => h.range.isRangeEqual(emptyHighlight.range))) {
38-
results.push(emptyHighlight);
36+
const { range } = emptyHighlight;
37+
if (!results.some((h) => h.range.isRangeEqual(range))) {
38+
const matchingHighlights = highlights.filter((h) =>
39+
h.range.contains(range),
40+
);
41+
const style = combineHighlightStyles(range, matchingHighlights);
42+
43+
results.push({ range, style });
3944
}
4045
}
4146

@@ -45,24 +50,6 @@ export function flattenHighlights(highlights: Highlight[]): Highlight[] {
4550
return results;
4651
}
4752

48-
function sortHighlights(highlights: Highlight[]) {
49-
highlights.sort((a, b) => {
50-
if (a.range.start.isBefore(b.range.start)) {
51-
return -1;
52-
}
53-
if (a.range.start.isAfter(b.range.start)) {
54-
return 1;
55-
}
56-
if (a.range.end.isBefore(b.range.end)) {
57-
return -1;
58-
}
59-
if (a.range.end.isAfter(b.range.end)) {
60-
return 1;
61-
}
62-
return 0;
63-
});
64-
}
65-
6653
function getUniquePositions(highlights: Highlight[]): Position[] {
6754
const result: Position[] = [];
6855
const positions = highlights
@@ -125,3 +112,21 @@ function combineHighlightStyles(range: Range, highlights: Highlight[]): Style {
125112
borderColorPorous: lastHighlight.style.borderColorPorous,
126113
};
127114
}
115+
116+
function sortHighlights(highlights: Highlight[]) {
117+
highlights.sort((a, b) => {
118+
if (a.range.start.isBefore(b.range.start)) {
119+
return -1;
120+
}
121+
if (a.range.start.isAfter(b.range.start)) {
122+
return 1;
123+
}
124+
if (a.range.end.isBefore(b.range.end)) {
125+
return -1;
126+
}
127+
if (a.range.end.isAfter(b.range.end)) {
128+
return 1;
129+
}
130+
return 0;
131+
});
132+
}

0 commit comments

Comments
 (0)