|
1 | 1 | import { |
2 | | - blendMultipleColors, |
3 | | - BorderStyle, |
4 | 2 | generateDecorationsForCharacterRange, |
5 | 3 | Range, |
6 | 4 | type DecorationStyle, |
7 | | - type Position, |
8 | 5 | } from "@cursorless/common"; |
9 | 6 | import { blendRangeTypeColors } from "./blendRangeColors"; |
10 | | -import { type Highlight, type Style } from "./Code"; |
| 7 | +import { type Highlight } from "./Code"; |
| 8 | +import { flattenHighlights } from "./combineHighlightStyles"; |
11 | 9 | import { highlightColors } from "./highlightColors"; |
12 | 10 | import type { Fixture, RangeType, RangeTypeColors } from "./types"; |
13 | 11 |
|
@@ -56,33 +54,6 @@ export function calculateHighlights( |
56 | 54 | ]); |
57 | 55 | } |
58 | 56 |
|
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 | | - |
86 | 57 | function getRanges(fixture: Fixture, rangeType: RangeType) { |
87 | 58 | const domainRanges: Range[] = []; |
88 | 59 | const allNestedRanges: Range[] = []; |
@@ -115,68 +86,6 @@ function getRanges(fixture: Fixture, rangeType: RangeType) { |
115 | 86 | return { domainRanges, allNestedRanges, domainEqualsNestedRanges }; |
116 | 87 | } |
117 | 88 |
|
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 | | - |
180 | 89 | function getHighlights( |
181 | 90 | colors: RangeTypeColors, |
182 | 91 | range: Range, |
|
0 commit comments