|
| 1 | +import type { DecorationItem } from "shiki"; |
| 2 | +import type { Position } from "@cursorless/common"; |
| 3 | + |
| 4 | +/** |
| 5 | + * Merges overlapping decorations. If two decorations overlap, merges their classes and adds "overlap" to the class list. |
| 6 | + * @param decorations Array of decoration objects |
| 7 | + * @returns Array of merged decorations |
| 8 | + */ |
| 9 | +export function mergeOverlappingDecorations(decorations: DecorationItem[]): DecorationItem[] { |
| 10 | + if (decorations.length === 0) { return []; } |
| 11 | + |
| 12 | + // Helper to normalize positions (in case shiki uses offset numbers) |
| 13 | + function isPosition(obj: any): obj is Position { |
| 14 | + return obj && typeof obj.line === "number" && typeof obj.character === "number"; |
| 15 | + } |
| 16 | + |
| 17 | + // Collect all unique boundary points |
| 18 | + const points: Position[] = []; |
| 19 | + for (const deco of decorations) { |
| 20 | + if (isPosition(deco.start) && isPosition(deco.end)) { |
| 21 | + points.push(deco.start, deco.end); |
| 22 | + } |
| 23 | + } |
| 24 | + points.sort((a, b) => a.line !== b.line ? a.line - b.line : a.character - b.character); |
| 25 | + // Remove duplicates |
| 26 | + const uniquePoints: Position[] = []; |
| 27 | + for (const p of points) { |
| 28 | + if (!uniquePoints.length || uniquePoints[uniquePoints.length - 1].line !== p.line || uniquePoints[uniquePoints.length - 1].character !== p.character) { |
| 29 | + uniquePoints.push(p); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + const result: DecorationItem[] = []; |
| 34 | + for (let i = 0; i < uniquePoints.length - 1; ++i) { |
| 35 | + const segStart = uniquePoints[i]; |
| 36 | + const segEnd = uniquePoints[i + 1]; |
| 37 | + // Find all decorations covering this segment |
| 38 | + const covering = decorations.filter(d => |
| 39 | + isPosition(d.start) && isPosition(d.end) && |
| 40 | + (d.start.line < segEnd.line || (d.start.line === segEnd.line && d.start.character < segEnd.character)) && |
| 41 | + (d.end.line > segStart.line || (d.end.line === segStart.line && d.end.character > segStart.character)) |
| 42 | + ); |
| 43 | + if (covering.length === 0) { continue; } |
| 44 | + if (covering.length === 1) { |
| 45 | + const c = covering[0]; |
| 46 | + result.push({ |
| 47 | + start: segStart, |
| 48 | + end: segEnd, |
| 49 | + properties: { class: c && c.properties ? c.properties.class : "" }, |
| 50 | + alwaysWrap: c && c.alwaysWrap, |
| 51 | + }); |
| 52 | + } else { |
| 53 | + // Merge classes and add overlap |
| 54 | + let classNames = covering |
| 55 | + .map(d => (d && d.properties ? d.properties.class : "")) |
| 56 | + .join(" ") |
| 57 | + .split(" ") |
| 58 | + .filter(Boolean); |
| 59 | + classNames = Array.from(new Set(classNames)); |
| 60 | + classNames.push("overlap"); |
| 61 | + result.push({ |
| 62 | + start: segStart, |
| 63 | + end: segEnd, |
| 64 | + properties: { class: classNames.join(" ") }, |
| 65 | + alwaysWrap: covering.some(d => d && d.alwaysWrap), |
| 66 | + }); |
| 67 | + } |
| 68 | + } |
| 69 | + return result; |
| 70 | +} |
0 commit comments