Skip to content

Commit

Permalink
fix: range
Browse files Browse the repository at this point in the history
  • Loading branch information
shaokeyibb committed Aug 26, 2024
1 parent 7111842 commit 3c718e9
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions cloudflare-workers/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type OffsetDelta = {
export function calcOffsetModification(offsets: Offset[], diff: ModifiedCommentBody['diff']): Replacement[] {
const offsetsDelta: OffsetDelta[] = Array.from({ length: offsets.length }, () => ({ startDelta: 0, endDelta: 0 }));

// i1, i2, j1, j2, start, end 均为左闭右开
for (const { tag, i1, i2, j1, j2 } of diff) {
for (let i = 0; i < offsets.length; i++) {
const { start, end } = offsets[i];
Expand All @@ -33,7 +34,7 @@ export function calcOffsetModification(offsets: Offset[], diff: ModifiedCommentB
case 'delete':
case 'replace': {
// 替换点在该区间前
if (i2 < start) {
if (i2 <= start) {
const deltaLength = j2 - j1 - (i2 - i1);
delta.startDelta += deltaLength;
delta.endDelta += deltaLength;
Expand All @@ -44,17 +45,17 @@ export function calcOffsetModification(offsets: Offset[], diff: ModifiedCommentB
delta.endDelta += deltaLength;
}
// 替换点右半边在该区间内,左半边在该区间外
else if (i1 < start && i2 >= start && i2 <= end) {
else if (i1 < start && i2 > start && i2 <= end) {
const deltaLength = i1 - start; // 获取替换部分
delta.startDelta += deltaLength;
delta.endDelta += deltaLength - (i2 - start);
}
// 替换点右半边在该区间外,左半边在该区间内
else if (i2 > end && i1 >= start && i1 < end) {
else if (i2 >= end && i1 >= start && i1 < end) {
delta.endDelta += i1 - end;
}
// 替换点包括整个区间
else if (i1 < start && i2 > end) {
else if (i1 < start && i2 >= end) {
delta.invalid = true;
}
// 替换点在该区间后不需要变动
Expand Down

0 comments on commit 3c718e9

Please sign in to comment.