Skip to content

Commit 9ba3d31

Browse files
committed
more clean up
1 parent f4c3bbe commit 9ba3d31

File tree

2 files changed

+41
-19
lines changed

2 files changed

+41
-19
lines changed

packages/cspell-lib/src/lib/Transform/SourceMap.ts

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -482,35 +482,37 @@ function advanceCursors(cursor1: SourceMapMergeCursor, cursor2: SourceMapMergeCu
482482
export function sliceSourceMapToSourceRange(map: SourceMap | undefined, range: Range): SourceMap | undefined {
483483
if (!map?.length) return map;
484484

485+
const cursor = createSourceMapCursor(map);
485486
const [start, end] = range;
486487

487-
let idx = 0;
488-
let p = 0;
489-
for (; idx < map.length && p + map[idx] < start; idx += 2) {
490-
p += map[idx];
491-
}
488+
const startDst = cursor.mapOffsetToDest(start);
489+
if (cursor.done) return [];
492490

493-
if (idx >= map.length) {
494-
return [];
495-
}
491+
const startIdx = cursor.idx;
492+
const startOffsetSrc = cursor.begin0;
493+
const startOffsetDst = cursor.begin1;
494+
const startLinear = cursor.linear;
496495

497-
const startIdx = idx;
498-
const startOffset = start - p;
496+
cursor.mapOffsetToDest(end);
499497

500-
for (; idx < map.length && p + map[idx] <= end; idx += 2) {
501-
p += map[idx];
498+
let endIdx = cursor.idx;
499+
if (startIdx === endIdx && startLinear) {
500+
return [];
502501
}
503502

504-
const endIdx = idx;
505-
if (startIdx === endIdx) {
506-
return undefined;
503+
if (!cursor.linear || cursor.begin0 < end) {
504+
endIdx += 2;
507505
}
508506

509507
const newMap = map.slice(startIdx, endIdx);
510508

511-
if (startOffset !== 0) {
512-
newMap[0] -= startOffset;
513-
newMap[1] -= Math.min(startOffset, newMap[1]);
509+
// Only adjust the first pair of offsets, the rest are relative to the first pair.
510+
newMap[0] -= start - startOffsetSrc;
511+
newMap[1] -= startDst - startOffsetDst;
512+
513+
if (newMap[0] === newMap[1] && !startLinear) {
514+
// Force a non-linear map when the deltas are the same but the segment is non-linear.
515+
newMap.unshift(0, 0);
514516
}
515517

516518
return newMap;

packages/cspell-lib/src/lib/Transform/TextMap.test.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,37 @@ describe('TextMap', () => {
1515
${'fine café'} | ${[200, 209]} | ${undefined} | ${[205, 209]} | ${tm('café', [205, 209])}
1616
${'fine café coffee'} | ${[200, 219]} | ${[8, 8, 4, 1, 7, 7]} | ${[205, 212]} | ${tm('café', [205, 212], [3, 3, 4, 1])}
1717
${'fine café coffee'} | ${[200, 219]} | ${[8, 8, 4, 1, 7, 7]} | ${[200, 212]} | ${tm('fine café', [200, 212], [8, 8, 4, 1])}
18-
${'fine café coffee'} | ${[200, 219]} | ${[8, 8, 4, 1, 7, 7]} | ${[200, 213]} | ${tm('fine café ', [200, 213], [8, 8, 4, 1])}
18+
${'fine café coffee'} | ${[200, 219]} | ${[8, 8, 4, 1, 7, 7]} | ${[200, 213]} | ${tm('fine café ', [200, 213], [8, 8, 4, 1, 7, 7])}
1919
${'fine café coffee'} | ${[200, 219]} | ${[8, 8, 4, 1, 7, 7]} | ${[200, 219]} | ${tm('fine café coffee', [200, 219], [8, 8, 4, 1, 7, 7])}
2020
${'fine café coffee'} | ${[200, 219]} | ${[8, 8, 4, 1]} | ${[200, 219]} | ${tm('fine café coffee', [200, 219], [8, 8, 4, 1])}
2121
${'fine café coffee'} | ${[200, 219]} | ${[8, 8, 4, 1]} | ${[205, 212]} | ${tm('café', [205, 212], [3, 3, 4, 1])}
2222
${'fine café coffee'} | ${[200, 219]} | ${[8, 8, 4, 1]} | ${[205, 219]} | ${tm('café coffee', [205, 219], [3, 3, 4, 1])}
23+
${'fine café coffee'} | ${[200, 219]} | ${[8, 8, 4, 1]} | ${[201, 202]} | ${tm('i', [201, 202])}
24+
${'fine café coffee'} | ${[200, 219]} | ${[8, 8, 4, 1]} | ${[201, 202]} | ${tm('i', [201, 202])}
2325
`('extractTextMap $text $range $map $extRange', ({ text, range, map, extRange, expected }) => {
2426
const tm = { text, range, map };
2527
const r = extractTextMapRangeOrigin(tm, extRange);
2628
expect(r).toEqual(expected);
2729
});
2830

31+
test.each`
32+
text | range | map | extRange | expected
33+
${'fine café coffee'} | ${[200, 219]} | ${[8, 8, 4, 1]} | ${[208, 208]} | ${tm('', [208, 208], [4, 1])}
34+
${'fine café coffee'} | ${[200, 219]} | ${[8, 8, 4, 1]} | ${[208, 209]} | ${tm('', [208, 209], [4, 1])}
35+
${'fine café coffee'} | ${[200, 219]} | ${[8, 8, 4, 1, 7, 7]} | ${[208, 210]} | ${tm('', [208, 210], [4, 1])}
36+
${'fine café coffee'} | ${[200, 219]} | ${[8, 8, 4, 1, 7, 7]} | ${[208, 211]} | ${tm('', [208, 211], [4, 1])}
37+
${'fine café coffee'} | ${[200, 219]} | ${[8, 8, 4, 1, 7, 7]} | ${[208, 212]} | ${tm('é', [208, 212], [4, 1])}
38+
${'fine café coffee'} | ${[200, 219]} | ${[8, 8, 4, 1, 7, 7]} | ${[208, 213]} | ${tm('é ', [208, 213], [4, 1, 7, 7])}
39+
${'fine café coffee'} | ${[200, 219]} | ${[8, 8, 4, 1, 7, 7]} | ${[209, 212]} | ${tm('é', [209, 212], [3, 1])}
40+
${'fine café coffee'} | ${[200, 219]} | ${[8, 8, 4, 1, 7, 7]} | ${[210, 212]} | ${tm('é', [210, 212], [2, 1])}
41+
${'fine café coffee'} | ${[200, 219]} | ${[8, 8, 4, 1, 7, 7]} | ${[211, 212]} | ${tm('é', [211, 212], [0, 0, 1, 1])}
42+
${'fine café coffee'} | ${[200, 219]} | ${[8, 8, 4, 1, 7, 7]} | ${[212, 212]} | ${tm('', [212, 212])}
43+
`('extractTextMap edge cases $text $range $map $extRange', ({ text, range, map, extRange, expected }) => {
44+
const tm = { text, range, map };
45+
const r = extractTextMapRangeOrigin(tm, extRange);
46+
expect(r).toEqual(expected);
47+
});
48+
2949
test.each`
3050
range | intersectRange | expected
3151
${[0, 100]} | ${[50, 200]} | ${true}

0 commit comments

Comments
 (0)