Skip to content

Commit 9eaaed3

Browse files
committed
refactor: Move decorations related helpers to /decorations
1 parent 3f44f13 commit 9eaaed3

10 files changed

+75
-13
lines changed

packages/test-case-component/src/helpers/addContentRangeDecorations.ts renamed to packages/test-case-component/src/helpers/decorations/addContentRangeDecorations.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { TargetPlainObject } from "@cursorless/common";
22
import type { DecorationItem } from "shiki";
3-
import { getDecorationClass, type classesMap } from "./classesMap";
3+
import { getDecorationClass, type classesMap } from "../classesMap";
44

55
function addContentRangeDecorations({
66
marks,
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import type { PlainSpyIDERecordedValues } from "@cursorless/common";
2+
import type { DecorationItem } from "shiki";
3+
import { isLineRange, isPositionRange } from "../typeGuards";
4+
import { getDecorationClass } from "../classesMap";
5+
6+
function getIdeFlashDecorations({
7+
ide,
8+
lines,
9+
}: {
10+
ide?: PlainSpyIDERecordedValues;
11+
lines?: string[];
12+
} = {}): DecorationItem[] {
13+
if (!lines) {
14+
console.warn("Lines are undefined. Skipping line decorations.");
15+
return [];
16+
}
17+
18+
if (!ide?.flashes || !Array.isArray(ide.flashes)) {
19+
console.warn("No flashes found in IDE. Skipping line decorations.");
20+
return [];
21+
}
22+
23+
const decorations: DecorationItem[] = [];
24+
25+
const { flashes } = ide
26+
27+
flashes.forEach(({ style, range }) => {
28+
const { type } = range;
29+
30+
if (isLineRange(range)) {
31+
const { start: lineStart, end: lineEnd } = range
32+
for (let line = lineStart; line <= lineEnd; line++) {
33+
if (line >= lines.length) {
34+
continue
35+
}
36+
const contentLine = lines[line];
37+
const startPosition = { line, character: 0 };
38+
const endPosition = { line, character: contentLine.length };
39+
const decorationItem = {
40+
start: startPosition,
41+
end: endPosition,
42+
properties: {
43+
class: `${getDecorationClass(style)} full`,
44+
},
45+
alwaysWrap: true,
46+
};
47+
decorations.push(decorationItem);
48+
}
49+
} else if (isPositionRange(range)) {
50+
const { start: rangeStart, end: rangeEnd } = range;
51+
const decorationItem = {
52+
start: rangeStart,
53+
end: rangeEnd,
54+
properties: {
55+
class: getDecorationClass(style),
56+
},
57+
alwaysWrap: true,
58+
}
59+
decorations.push(decorationItem);
60+
} else {
61+
console.warn(`Unknown range type "${type}". Skipping this flash.`);
62+
}
63+
});
64+
65+
return decorations;
66+
}
67+
68+
export { getIdeFlashDecorations }

packages/test-case-component/src/helpers/getMarkDecorations.ts renamed to packages/test-case-component/src/helpers/decorations/getMarkDecorations.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { SerializedMarks } from "@cursorless/common";
22
import type { DecorationItem } from "shiki";
3-
import type { classesMap } from "./classesMap";
4-
import { getDecorationClass } from "./classesMap";
3+
import type { classesMap } from "../classesMap";
4+
import { getDecorationClass } from "../classesMap";
55

66
function getMarkDecorations({
77
marks,

packages/test-case-component/src/helpers/getSelections.ts renamed to packages/test-case-component/src/helpers/decorations/getSelections.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { SelectionPlainObject } from "@cursorless/common";
22
import type { DecorationItem } from "shiki";
3-
import { getDecorationClass } from "./classesMap";
3+
import { getDecorationClass } from "../classesMap";
44

55
function getSelections(
66
{

packages/test-case-component/src/helpers/getSourceMarks.ts renamed to packages/test-case-component/src/helpers/decorations/getSourceMarks.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ function getSourceMarks({ sourceMark }: { sourceMark?: TargetPlainObject[] }): D
1313
highlightClass: "sourceMark", decorations
1414
});
1515
}
16-
1716
return decorations
1817
}
1918

packages/test-case-component/src/helpers/mergeOverlappingDecorations.test.ts renamed to packages/test-case-component/src/helpers/decorations/mergeOverlappingDecorations.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { mergeOverlappingDecorations } from "./mergeOverlappingDecorations.ts";
1+
import { mergeOverlappingDecorations } from "./mergeOverlappingDecorations";
22

33
describe("mergeOverlappingDecorations", () => {
44
it("merges overlapping decorations and adds 'overlap' to class", () => {
Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
export * from "./splitDocumentWithOffsets";
2-
export * from "./createDecorations";
3-
export * from "./getMarkDecorations";
4-
export * from "./getIdeFlashDecorations";
5-
export * from "./getSelections";
6-
export * from "./getThatMarks";
7-
export * from "./getSourceMarks";
8-
export * from "./addContentRangeDecorations";
2+
export * from "./decorations/createDecorations";
3+
export * from "./decorations/mergeOverlappingDecorations";
94
export * from "./typeGuards";
105
export * from "./classesMap";

0 commit comments

Comments
 (0)