Skip to content

Commit 827eac7

Browse files
Refactor
1 parent ab70df3 commit 827eac7

File tree

2 files changed

+99
-93
lines changed

2 files changed

+99
-93
lines changed

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

Lines changed: 2 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import {
2-
blendMultipleColors,
3-
BorderStyle,
42
generateDecorationsForCharacterRange,
53
Range,
64
type DecorationStyle,
7-
type Position,
85
} from "@cursorless/common";
96
import { blendRangeTypeColors } from "./blendRangeColors";
10-
import { type Highlight, type Style } from "./Code";
7+
import { type Highlight } from "./Code";
8+
import { flattenHighlights } from "./combineHighlightStyles";
119
import { highlightColors } from "./highlightColors";
1210
import type { Fixture, RangeType, RangeTypeColors } from "./types";
1311

@@ -56,33 +54,6 @@ export function calculateHighlights(
5654
]);
5755
}
5856

59-
function flattenHighlights(highlights: Highlight[]): Highlight[] {
60-
const positions = getUniquePositions(highlights);
61-
const results: Highlight[] = [];
62-
63-
for (let i = 0; i < positions.length - 1; i++) {
64-
const subRange: Range = new Range(positions[i], positions[i + 1]);
65-
66-
const matchingHighlights = highlights.filter(({ range }) =>
67-
range.contains(subRange),
68-
);
69-
70-
// This sub range could be between two scopes.
71-
if (matchingHighlights.length === 0) {
72-
continue;
73-
}
74-
75-
const style = combineHighlightStyles(subRange, matchingHighlights);
76-
77-
results.push({
78-
range: subRange,
79-
style,
80-
});
81-
}
82-
83-
return results;
84-
}
85-
8657
function getRanges(fixture: Fixture, rangeType: RangeType) {
8758
const domainRanges: Range[] = [];
8859
const allNestedRanges: Range[] = [];
@@ -115,68 +86,6 @@ function getRanges(fixture: Fixture, rangeType: RangeType) {
11586
return { domainRanges, allNestedRanges, domainEqualsNestedRanges };
11687
}
11788

118-
function combineHighlightStyles(range: Range, highlights: Highlight[]): Style {
119-
if (highlights.length === 1) {
120-
return highlights[0].style;
121-
}
122-
123-
const lastHighlight = highlights[highlights.length - 1];
124-
125-
const borderStyle: DecorationStyle = {
126-
left: BorderStyle.none,
127-
right: BorderStyle.none,
128-
top: BorderStyle.none,
129-
bottom: BorderStyle.none,
130-
};
131-
132-
borderStyle.top = lastHighlight.style.borderStyle.top;
133-
borderStyle.bottom = lastHighlight.style.borderStyle.bottom;
134-
135-
const matchingStart = highlights.filter((h) =>
136-
h.range.start.isEqual(range.start),
137-
);
138-
const matchingEnd = highlights.filter((h) => h.range.end.isEqual(range.end));
139-
140-
if (matchingStart.length > 0) {
141-
borderStyle.left = matchingStart.at(-1)!.style.borderStyle.left;
142-
}
143-
144-
if (matchingEnd.length > 0) {
145-
borderStyle.right = matchingEnd.at(-1)!.style.borderStyle.right;
146-
}
147-
148-
const backgroundColor = blendMultipleColors(
149-
highlights.map((h) => h.style.backgroundColor),
150-
);
151-
152-
return {
153-
backgroundColor,
154-
borderStyle,
155-
borderColorSolid: lastHighlight.style.borderColorSolid,
156-
borderColorPorous: lastHighlight.style.borderColorPorous,
157-
};
158-
}
159-
160-
function getUniquePositions(highlights: Highlight[]): Position[] {
161-
const result: Position[] = [];
162-
const emptyHighlights = highlights.filter((h) => h.range.isEmpty);
163-
const positions = highlights
164-
.flatMap((h) => [h.range.start, h.range.end])
165-
.sort((a, b) =>
166-
a.line === b.line ? a.character - b.character : a.line - b.line,
167-
);
168-
for (let i = 0; i < positions.length; i++) {
169-
if (
170-
i === 0 ||
171-
!positions[i].isEqual(positions[i - 1]) ||
172-
emptyHighlights.some((h) => h.range.start.isEqual(positions[i]))
173-
) {
174-
result.push(positions[i]);
175-
}
176-
}
177-
return result;
178-
}
179-
18089
function getHighlights(
18190
colors: RangeTypeColors,
18291
range: Range,
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import {
2+
type DecorationStyle,
3+
type Position,
4+
blendMultipleColors,
5+
BorderStyle,
6+
Range,
7+
} from "@cursorless/common";
8+
import { type Highlight, type Style } from "./Code";
9+
10+
export function flattenHighlights(highlights: Highlight[]): Highlight[] {
11+
const positions = getUniquePositions(highlights);
12+
const results: Highlight[] = [];
13+
14+
for (let i = 0; i < positions.length - 1; i++) {
15+
const subRange: Range = new Range(positions[i], positions[i + 1]);
16+
17+
const matchingHighlights = highlights.filter(({ range }) =>
18+
range.contains(subRange),
19+
);
20+
21+
// This sub range could be between two scopes.
22+
if (matchingHighlights.length === 0) {
23+
continue;
24+
}
25+
26+
const style = combineHighlightStyles(subRange, matchingHighlights);
27+
28+
results.push({
29+
range: subRange,
30+
style,
31+
});
32+
}
33+
34+
return results;
35+
}
36+
37+
function getUniquePositions(highlights: Highlight[]): Position[] {
38+
const result: Position[] = [];
39+
const emptyHighlights = highlights.filter((h) => h.range.isEmpty);
40+
const positions = highlights
41+
.flatMap((h) => [h.range.start, h.range.end])
42+
.sort((a, b) =>
43+
a.line === b.line ? a.character - b.character : a.line - b.line,
44+
);
45+
for (let i = 0; i < positions.length; i++) {
46+
if (
47+
i === 0 ||
48+
!positions[i].isEqual(positions[i - 1]) ||
49+
emptyHighlights.some((h) => h.range.start.isEqual(positions[i]))
50+
) {
51+
result.push(positions[i]);
52+
}
53+
}
54+
return result;
55+
}
56+
57+
function combineHighlightStyles(range: Range, highlights: Highlight[]): Style {
58+
if (highlights.length === 1) {
59+
return highlights[0].style;
60+
}
61+
62+
const lastHighlight = highlights[highlights.length - 1];
63+
64+
const borderStyle: DecorationStyle = {
65+
left: BorderStyle.none,
66+
right: BorderStyle.none,
67+
top: BorderStyle.none,
68+
bottom: BorderStyle.none,
69+
};
70+
71+
borderStyle.top = lastHighlight.style.borderStyle.top;
72+
borderStyle.bottom = lastHighlight.style.borderStyle.bottom;
73+
74+
const matchingStart = highlights.filter((h) =>
75+
h.range.start.isEqual(range.start),
76+
);
77+
const matchingEnd = highlights.filter((h) => h.range.end.isEqual(range.end));
78+
79+
if (matchingStart.length > 0) {
80+
borderStyle.left = matchingStart.at(-1)!.style.borderStyle.left;
81+
}
82+
83+
if (matchingEnd.length > 0) {
84+
borderStyle.right = matchingEnd.at(-1)!.style.borderStyle.right;
85+
}
86+
87+
const backgroundColor = blendMultipleColors(
88+
highlights.map((h) => h.style.backgroundColor),
89+
);
90+
91+
return {
92+
backgroundColor,
93+
borderStyle,
94+
borderColorSolid: lastHighlight.style.borderColorSolid,
95+
borderColorPorous: lastHighlight.style.borderColorPorous,
96+
};
97+
}

0 commit comments

Comments
 (0)