Skip to content

Commit ae9fe50

Browse files
Added blended range view to scope visualizer component (#3158)
<img width="389" height="150" alt="image" src="https://github.com/user-attachments/assets/fb2f550c-3b35-4814-b7ab-e0986037fcda" />
1 parent 1b724e8 commit ae9fe50

3 files changed

Lines changed: 55 additions & 36 deletions

File tree

‎packages/cursorless-org-docs/src/docs/components/ScopeVisualizer.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ export function ScopeVisualizer({ languageId, scopeTypeType }: Props) {
7979
>
8080
<option value="content">Content range</option>
8181
<option value="removal">Removal range</option>
82+
<option value="blend">Blended ranges</option>
8283
</select>
8384

8485
<label className="ml-2">

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

Lines changed: 53 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -15,62 +15,80 @@ export function generateDecorations(
1515
rangeType: RangeType,
1616
isIteration: boolean,
1717
): DecorationItem[] {
18-
const { domainRanges, targetRanges } = getRanges(fixture, rangeType);
18+
const { domainRanges, removalRanges, contentRanges } = getRanges(
19+
fixture,
20+
rangeType,
21+
);
1922

2023
const codeLineRanges = getCodeLineRanges(fixture.code);
21-
const colors = getColors(rangeType, isIteration);
22-
23-
const domainDecorations = getDecorations(codeLineRanges, domainRanges);
24-
const targetRangeDecorations = getDecorations(codeLineRanges, targetRanges);
2524

26-
const domainHighlights = domainDecorations.map((d) =>
27-
getHighlights(colors.domain, d.range, d.style),
28-
);
29-
const targetRangeHighlights = targetRangeDecorations.map((d) =>
30-
getHighlights(colors.target, d.range, d.style),
25+
const highlights = getDecorations(codeLineRanges, domainRanges).map((d) =>
26+
getHighlights(highlightColors.domain, d.range, d.style),
3127
);
3228

33-
const highlights = flattenHighlights([
34-
...domainHighlights,
35-
...targetRangeHighlights,
36-
]);
29+
if (rangeType === "blend" && !isIteration) {
30+
const removalHighlights = getDecorations(codeLineRanges, removalRanges).map(
31+
(d) => getHighlights(highlightColors.removal, d.range, d.style),
32+
);
33+
const contentHighlights = getDecorations(codeLineRanges, contentRanges).map(
34+
(d) => getHighlights(highlightColors.content, d.range, d.style),
35+
);
36+
37+
highlights.push(...removalHighlights, ...contentHighlights);
38+
} else {
39+
const colors = getColors(rangeType, isIteration);
40+
const targetRangeHighlights = getDecorations(
41+
codeLineRanges,
42+
rangeType === "removal" ? removalRanges : contentRanges,
43+
).map((d) => getHighlights(colors, d.range, d.style));
3744

38-
return highlightsToDecorations(highlights);
45+
highlights.push(...targetRangeHighlights);
46+
}
47+
48+
const flattenedHighlights = flattenHighlights(highlights);
49+
50+
return highlightsToDecorations(flattenedHighlights);
3951
}
4052

4153
function getColors(rangeType: RangeType, isIteration: boolean) {
42-
const target = (() => {
43-
if (isIteration) {
44-
return highlightColors.iteration;
45-
}
46-
if (rangeType === "content") {
47-
return highlightColors.content;
48-
}
49-
return highlightColors.removal;
50-
})();
51-
return {
52-
domain: highlightColors.domain,
53-
target,
54-
};
54+
if (isIteration) {
55+
return highlightColors.iteration;
56+
}
57+
if (rangeType === "content") {
58+
return highlightColors.content;
59+
}
60+
return highlightColors.removal;
5561
}
5662

5763
function getRanges(fixture: Fixture, rangeType: RangeType) {
58-
const domainRanges: Range[] = [];
59-
const targetRanges: Range[] = [];
64+
const domainRanges: string[] = [];
65+
const removalRanges: string[] = [];
66+
const contentRanges: string[] = [];
6067

6168
for (const { domain, targets } of fixture.scopes) {
6269
if (domain != null) {
63-
domainRanges.push(Range.fromConcise(domain));
70+
domainRanges.push(domain);
6471
}
6572

6673
for (const t of targets) {
67-
const range =
68-
rangeType === "content" ? t.content : (t.removal ?? t.content);
69-
targetRanges.push(Range.fromConcise(range));
74+
if (rangeType === "blend") {
75+
if (t.removal != null) {
76+
removalRanges.push(t.removal);
77+
}
78+
contentRanges.push(t.content);
79+
} else if (rangeType === "removal") {
80+
removalRanges.push(t.removal ?? t.content);
81+
} else {
82+
contentRanges.push(t.content);
83+
}
7084
}
7185
}
7286

73-
return { domainRanges, targetRanges };
87+
return {
88+
domainRanges: domainRanges.map(Range.fromConcise),
89+
removalRanges: removalRanges.map(Range.fromConcise),
90+
contentRanges: contentRanges.map(Range.fromConcise),
91+
};
7492
}
7593

7694
function getHighlights(

‎packages/cursorless-org-docs/src/docs/components/types.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type {
55
Range,
66
} from "@cursorless/common";
77

8-
export type RangeType = "content" | "removal";
8+
export type RangeType = "content" | "removal" | "blend";
99
export type FacetValue = ScopeSupportFacet | PlaintextScopeSupportFacet;
1010

1111
export interface ScopeTests {

0 commit comments

Comments
 (0)