Skip to content

Commit 1983108

Browse files
committed
Better annotation splitting
1 parent 6b0fd8f commit 1983108

File tree

6 files changed

+70
-15
lines changed

6 files changed

+70
-15
lines changed

.changeset/new-feet-sell.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@code-hike/lighter": patch
3+
---
4+
5+
Better annotation splitting

lib/dist/index.cjs.js

+19-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/dist/index.esm.mjs

+19-4
Original file line numberDiff line numberDiff line change
@@ -2637,14 +2637,22 @@ function reannotateLine(annotatedLine, annotation) {
26372637
// we need to split the first group in two
26382638
const [firstHalf, secondHalf] = splitGroup$1(firstGroup, fromColumn);
26392639
newAnnotatedLine.push(firstHalf);
2640-
newGroup.tokens.push(secondHalf);
2640+
newAnnotatedLine.push(newGroup);
2641+
if (secondHalf.toColumn > toColumn) {
2642+
// we need to split the second half in two
2643+
const [secondFirstHalf, secondSecondHalf] = splitGroup$1(secondHalf, toColumn + 1);
2644+
newGroup.tokens.push(secondFirstHalf);
2645+
newAnnotatedLine.push(secondSecondHalf);
2646+
}
2647+
else {
2648+
newGroup.tokens.push(secondHalf);
2649+
}
26412650
i++;
26422651
}
26432652
while (i < annotatedLine.length && annotatedLine[i].toColumn < toColumn) {
26442653
newGroup.tokens.push(annotatedLine[i]);
26452654
i++;
26462655
}
2647-
newAnnotatedLine.push(newGroup);
26482656
if (i === annotatedLine.length) {
26492657
return newAnnotatedLine;
26502658
}
@@ -2744,15 +2752,22 @@ function reannotateLines(annotatedLines, annotation) {
27442752
if (firstGroup.fromLineNumber < fromLineNumber) {
27452753
const [firstHalf, secondHalf] = splitGroup(firstGroup, fromLineNumber);
27462754
newAnnotatedLines.push(firstHalf);
2747-
newGroup.lines.push(secondHalf);
2755+
newAnnotatedLines.push(newGroup);
2756+
if (secondHalf.toLineNumber > toLineNumber) {
2757+
const [secondFirstHalf, secondSecondHalf] = splitGroup(secondHalf, toLineNumber + 1);
2758+
newGroup.lines.push(secondFirstHalf);
2759+
newAnnotatedLines.push(secondSecondHalf);
2760+
}
2761+
else {
2762+
newGroup.lines.push(secondHalf);
2763+
}
27482764
i++;
27492765
}
27502766
while (i < annotatedLines.length &&
27512767
annotatedLines[i].toLineNumber <= toLineNumber) {
27522768
newGroup.lines.push(annotatedLines[i]);
27532769
i++;
27542770
}
2755-
newAnnotatedLines.push(newGroup);
27562771
if (i === annotatedLines.length) {
27572772
return newAnnotatedLines;
27582773
}

lib/src/annotations.inline.ts

+14-3
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,20 @@ function reannotateLine(
8282
const [firstHalf, secondHalf] = splitGroup(firstGroup, fromColumn);
8383

8484
newAnnotatedLine.push(firstHalf);
85-
newGroup.tokens.push(secondHalf);
85+
newAnnotatedLine.push(newGroup);
86+
87+
if (secondHalf.toColumn > toColumn) {
88+
// we need to split the second half in two
89+
const [secondFirstHalf, secondSecondHalf] = splitGroup(
90+
secondHalf,
91+
toColumn + 1
92+
);
93+
94+
newGroup.tokens.push(secondFirstHalf);
95+
newAnnotatedLine.push(secondSecondHalf);
96+
} else {
97+
newGroup.tokens.push(secondHalf);
98+
}
8699
i++;
87100
}
88101

@@ -91,8 +104,6 @@ function reannotateLine(
91104
i++;
92105
}
93106

94-
newAnnotatedLine.push(newGroup);
95-
96107
if (i === annotatedLine.length) {
97108
return newAnnotatedLine;
98109
}

lib/src/annotations.multiline.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,18 @@ function reannotateLines(
8585
if (firstGroup.fromLineNumber < fromLineNumber) {
8686
const [firstHalf, secondHalf] = splitGroup(firstGroup, fromLineNumber);
8787
newAnnotatedLines.push(firstHalf);
88-
newGroup.lines.push(secondHalf);
88+
newAnnotatedLines.push(newGroup);
89+
90+
if (secondHalf.toLineNumber > toLineNumber) {
91+
const [secondFirstHalf, secondSecondHalf] = splitGroup(
92+
secondHalf,
93+
toLineNumber + 1
94+
);
95+
newGroup.lines.push(secondFirstHalf);
96+
newAnnotatedLines.push(secondSecondHalf);
97+
} else {
98+
newGroup.lines.push(secondHalf);
99+
}
89100
i++;
90101
}
91102

@@ -97,8 +108,6 @@ function reannotateLines(
97108
i++;
98109
}
99110

100-
newAnnotatedLines.push(newGroup);
101-
102111
if (i === annotatedLines.length) {
103112
return newAnnotatedLines;
104113
}

test/test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const x = 1; /* bar x */ more /* notannotated */ code`;
99

1010
const code = `
1111
// mark[4:7]
12-
console.log(1)
12+
consolessssssssss
1313
`.trim();
1414
// const code = `// foo `;
1515
const alias = "js";

0 commit comments

Comments
 (0)