Skip to content

Commit e435c42

Browse files
Simplify calculate highlights
1 parent 2980d43 commit e435c42

File tree

1 file changed

+21
-26
lines changed

1 file changed

+21
-26
lines changed

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

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,55 +14,50 @@ export function generateDecorations(
1414
fixture: Fixture,
1515
rangeType: RangeType,
1616
): DecorationItem[] {
17-
const { domainRanges, allNestedRanges } = getRanges(fixture, rangeType);
17+
const { domainRanges, targetRanges } = getRanges(fixture, rangeType);
1818

1919
const codeLineRanges = getCodeLineRanges(fixture.code);
2020
const domainDecorations = getDecorations(codeLineRanges, domainRanges);
21-
const nestedRangeDecorations = getDecorations(
22-
codeLineRanges,
23-
allNestedRanges,
24-
);
25-
26-
const domainColors = highlightColors.domain;
27-
const nestedRangeColors =
28-
rangeType === "content" ? highlightColors.content : highlightColors.removal;
21+
const targetRangeDecorations = getDecorations(codeLineRanges, targetRanges);
2922

3023
const domainHighlights = domainDecorations.map((d) =>
31-
getHighlights(domainColors, d.range, d.style),
24+
getHighlights(highlightColors.domain, d.range, d.style),
3225
);
33-
const nestedRangeHighlights = nestedRangeDecorations.map((d) =>
34-
getHighlights(nestedRangeColors, d.range, d.style),
26+
const targetRangeHighlights = targetRangeDecorations.map((d) =>
27+
getHighlights(getTargetRangeColor(rangeType), d.range, d.style),
3528
);
3629

3730
const highlights = flattenHighlights([
3831
...domainHighlights,
39-
...nestedRangeHighlights,
32+
...targetRangeHighlights,
4033
]);
4134

4235
return highlightsToDecorations(highlights);
4336
}
4437

38+
function getTargetRangeColor(rangeType: RangeType): RangeTypeColors {
39+
return rangeType === "content"
40+
? highlightColors.content
41+
: highlightColors.removal;
42+
}
43+
4544
function getRanges(fixture: Fixture, rangeType: RangeType) {
4645
const domainRanges: Range[] = [];
47-
const allNestedRanges: Range[] = [];
46+
const targetRanges: Range[] = [];
4847

4948
for (const { domain, targets } of fixture.scopes) {
50-
const nestedRanges = targets
51-
.map((t) =>
52-
rangeType === "content" ? t.content : (t.removal ?? t.content),
53-
)
54-
.map((r) => Range.fromConcise(r));
55-
56-
const domainRange = domain != null ? Range.fromConcise(domain) : null;
57-
58-
if (domainRange != null) {
59-
domainRanges.push(domainRange);
49+
if (domain != null) {
50+
domainRanges.push(Range.fromConcise(domain));
6051
}
6152

62-
allNestedRanges.push(...nestedRanges);
53+
for (const t of targets) {
54+
const range =
55+
rangeType === "content" ? t.content : (t.removal ?? t.content);
56+
targetRanges.push(Range.fromConcise(range));
57+
}
6358
}
6459

65-
return { domainRanges, allNestedRanges };
60+
return { domainRanges, targetRanges };
6661
}
6762

6863
function getHighlights(

0 commit comments

Comments
 (0)