@volar/source-map 2.4.29 → 3.0.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +45 -36
- package/index.d.ts +1 -2
- package/index.js +1 -2
- package/lib/spanMap.d.ts +80 -0
- package/lib/spanMap.js +170 -0
- package/package.json +1 -1
- package/lib/sourceMap.d.ts +0 -23
- package/lib/sourceMap.js +0 -105
- package/lib/translateOffset.d.ts +0 -1
- package/lib/translateOffset.js +0 -57
package/README.md
CHANGED
|
@@ -1,52 +1,61 @@
|
|
|
1
1
|
# @volar/source-map
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Maps positions and ranges between virtual (generated) and original (source) text, in the span map model of
|
|
4
|
+
TypeScript 7.1: `SpanMapSegment`, `SpanMapKind`, `SpanMapFeature` and `SpanMapFidelity` have the shape and values
|
|
5
|
+
of their counterparts in `typescript/ast`.
|
|
4
6
|
|
|
5
|
-
##
|
|
7
|
+
## `SpanMapSegment`
|
|
6
8
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
- `toGeneratedRange(sourceStart: number, sourceEnd: number, fallbackToAnyMatch: boolean, filter?: (data: Data) => boolean) `: Returns all generated start and end offsets for the given source start and end offsets.
|
|
19
|
-
|
|
20
|
-
- `toSourceLocation(generatedOffset: number, filter?: (data: Data) => boolean)`: Returns all source offsets for a given generated offset.
|
|
21
|
-
|
|
22
|
-
- `toGeneratedLocation(sourceOffset: number, filter?: (data: Data) => boolean)`: Returns all generated offsets for a given source offset.
|
|
23
|
-
|
|
24
|
-
## Data Structures
|
|
9
|
+
```ts
|
|
10
|
+
interface SpanMapSegment {
|
|
11
|
+
virtualStart: number;
|
|
12
|
+
virtualEnd: number;
|
|
13
|
+
originalStart: number;
|
|
14
|
+
originalEnd: number;
|
|
15
|
+
kind: SpanMapKind;
|
|
16
|
+
features?: SpanMapFeature; // SpanMapFeature.All if omitted
|
|
17
|
+
}
|
|
18
|
+
```
|
|
25
19
|
|
|
26
|
-
|
|
20
|
+
Ranges are half-open (`[start, end)`).
|
|
27
21
|
|
|
28
|
-
|
|
22
|
+
- `SpanMapKind.Verbatim`: offsets inside the spans map 1:1, clamped to the other span.
|
|
23
|
+
- `SpanMapKind.Atom`: the spans map as a whole. An edge maps to the edge; an offset inside maps to the start, or to
|
|
24
|
+
the end for the end of a range; a range inside one maps to the whole other span.
|
|
25
|
+
- `SpanMapKind.Alias`: maps as `Atom`. It exists for parity with TS.
|
|
26
|
+
- `features`: the `SpanMapFeature` bits of the language features that the segment takes part in.
|
|
29
27
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
28
|
+
```ts
|
|
29
|
+
const segment: SpanMapSegment = {
|
|
30
|
+
virtualStart: 30,
|
|
31
|
+
virtualEnd: 40,
|
|
32
|
+
originalStart: 10,
|
|
33
|
+
originalEnd: 20,
|
|
34
|
+
kind: SpanMapKind.Verbatim,
|
|
35
|
+
features: SpanMapFeature.Hover | SpanMapFeature.Completion,
|
|
36
|
+
};
|
|
37
|
+
```
|
|
34
38
|
|
|
35
|
-
|
|
39
|
+
## `SpanMap`
|
|
36
40
|
|
|
37
41
|
```ts
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
data: myData,
|
|
44
|
-
};
|
|
42
|
+
const map = new SpanMap(segments);
|
|
43
|
+
map.virtualToOriginalPositions(position, feature?): MappedPosition[];
|
|
44
|
+
map.originalToVirtualPositions(position, feature?): MappedPosition[];
|
|
45
|
+
map.virtualToOriginalSpans(start, end, feature?): MappedRange[];
|
|
46
|
+
map.originalToVirtualSpans(start, end, feature?): MappedRange[];
|
|
45
47
|
```
|
|
46
48
|
|
|
47
|
-
|
|
49
|
+
- Every method returns every candidate, in segment order. Text outside every segment maps to nothing.
|
|
50
|
+
- A position maps through each segment that contains it, its end included.
|
|
51
|
+
- A range maps through each segment that contains it, with the fidelity `Exact` (verbatim) or `Atom`. If no segment
|
|
52
|
+
contains it, it maps from a segment of its start to the nearest segment of its end, with the fidelity
|
|
53
|
+
`Approximate`; callers that edit text (rename) reject `Approximate`.
|
|
54
|
+
- `feature` selects the segments that share one of its bits; every segment if it is omitted.
|
|
55
|
+
- `map.segments` are the segments with omitted `features` as `SpanMapFeature.All`.
|
|
48
56
|
|
|
49
|
-
|
|
57
|
+
Unlike `SpanMap` of TS, `virtualToOriginal*` return every candidate rather than one, and a gap maps to nothing
|
|
58
|
+
rather than to an insertion point.
|
|
50
59
|
|
|
51
60
|
## License
|
|
52
61
|
|
package/index.d.ts
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
export * from './lib/
|
|
2
|
-
export * from './lib/translateOffset';
|
|
1
|
+
export * from './lib/spanMap';
|
package/index.js
CHANGED
|
@@ -14,6 +14,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./lib/
|
|
18
|
-
__exportStar(require("./lib/translateOffset"), exports);
|
|
17
|
+
__exportStar(require("./lib/spanMap"), exports);
|
|
19
18
|
//# sourceMappingURL=index.js.map
|
package/lib/spanMap.d.ts
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
export declare enum SpanMapKind {
|
|
2
|
+
Verbatim = 0,
|
|
3
|
+
Atom = 1,
|
|
4
|
+
Alias = 2
|
|
5
|
+
}
|
|
6
|
+
export declare enum SpanMapFeature {
|
|
7
|
+
Hover = 1,
|
|
8
|
+
SignatureHelp = 2,
|
|
9
|
+
Completion = 4,
|
|
10
|
+
Definition = 8,
|
|
11
|
+
TypeDefinition = 16,
|
|
12
|
+
Implementation = 32,
|
|
13
|
+
References = 64,
|
|
14
|
+
DocumentHighlights = 128,
|
|
15
|
+
Rename = 256,
|
|
16
|
+
CallHierarchy = 512,
|
|
17
|
+
CodeActions = 1024,
|
|
18
|
+
Formatting = 2048,
|
|
19
|
+
InlayHints = 4096,
|
|
20
|
+
SemanticTokens = 8192,
|
|
21
|
+
FoldingRanges = 16384,
|
|
22
|
+
SelectionRanges = 32768,
|
|
23
|
+
LinkedEditing = 65536,
|
|
24
|
+
AutoInsert = 131072,
|
|
25
|
+
DocumentSymbols = 262144,
|
|
26
|
+
CodeLens = 524288,
|
|
27
|
+
None = 0,
|
|
28
|
+
All = 1048575
|
|
29
|
+
}
|
|
30
|
+
export declare enum SpanMapFidelity {
|
|
31
|
+
Exact = 0,
|
|
32
|
+
Atom = 1,
|
|
33
|
+
Approximate = 2,
|
|
34
|
+
None = 3
|
|
35
|
+
}
|
|
36
|
+
/** A half-open virtual range mapped to a half-open original range, as `SpanMapSegment` of TS 7.1. */
|
|
37
|
+
export interface SpanMapSegment {
|
|
38
|
+
readonly virtualStart: number;
|
|
39
|
+
readonly virtualEnd: number;
|
|
40
|
+
readonly originalStart: number;
|
|
41
|
+
readonly originalEnd: number;
|
|
42
|
+
readonly kind: SpanMapKind;
|
|
43
|
+
/** `SpanMapFeature.All` if omitted. */
|
|
44
|
+
readonly features?: SpanMapFeature;
|
|
45
|
+
}
|
|
46
|
+
/** A segment of a `SpanMap`, its omitted `features` as `SpanMapFeature.All`. */
|
|
47
|
+
export type NormalizedSpanMapSegment = SpanMapSegment & {
|
|
48
|
+
readonly features: SpanMapFeature;
|
|
49
|
+
};
|
|
50
|
+
export interface MappedPosition {
|
|
51
|
+
readonly position: number;
|
|
52
|
+
readonly fidelity: SpanMapFidelity;
|
|
53
|
+
readonly segment: NormalizedSpanMapSegment;
|
|
54
|
+
}
|
|
55
|
+
export interface MappedRange {
|
|
56
|
+
readonly start: number;
|
|
57
|
+
readonly end: number;
|
|
58
|
+
/** `Approximate` if `start` and `end` map through different segments. */
|
|
59
|
+
readonly fidelity: SpanMapFidelity;
|
|
60
|
+
readonly segments: readonly [start: NormalizedSpanMapSegment, end: NormalizedSpanMapSegment];
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Maps positions and ranges between virtual and original text, every candidate of each. A position maps through
|
|
64
|
+
* every segment that contains it, its end included; a range maps through one segment that contains it or, if none
|
|
65
|
+
* does, from a segment of its start to a segment of its end, as `Approximate`. Text outside every segment maps to
|
|
66
|
+
* nothing. `feature` selects the segments that share one of its bits; all segments if omitted.
|
|
67
|
+
*/
|
|
68
|
+
export declare class SpanMap {
|
|
69
|
+
readonly segments: readonly NormalizedSpanMapSegment[];
|
|
70
|
+
private virtualIndex;
|
|
71
|
+
private originalIndex;
|
|
72
|
+
constructor(segments: readonly SpanMapSegment[]);
|
|
73
|
+
virtualToOriginalPositions(position: number, feature?: SpanMapFeature): MappedPosition[];
|
|
74
|
+
originalToVirtualPositions(position: number, feature?: SpanMapFeature): MappedPosition[];
|
|
75
|
+
virtualToOriginalSpans(start: number, end: number, feature?: SpanMapFeature): MappedRange[];
|
|
76
|
+
originalToVirtualSpans(start: number, end: number, feature?: SpanMapFeature): MappedRange[];
|
|
77
|
+
private mapPosition;
|
|
78
|
+
private mapRange;
|
|
79
|
+
private getIndex;
|
|
80
|
+
}
|
package/lib/spanMap.js
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SpanMap = exports.SpanMapFidelity = exports.SpanMapFeature = exports.SpanMapKind = void 0;
|
|
4
|
+
const binarySearch_1 = require("./binarySearch");
|
|
5
|
+
var SpanMapKind;
|
|
6
|
+
(function (SpanMapKind) {
|
|
7
|
+
SpanMapKind[SpanMapKind["Verbatim"] = 0] = "Verbatim";
|
|
8
|
+
SpanMapKind[SpanMapKind["Atom"] = 1] = "Atom";
|
|
9
|
+
SpanMapKind[SpanMapKind["Alias"] = 2] = "Alias";
|
|
10
|
+
})(SpanMapKind || (exports.SpanMapKind = SpanMapKind = {}));
|
|
11
|
+
var SpanMapFeature;
|
|
12
|
+
(function (SpanMapFeature) {
|
|
13
|
+
SpanMapFeature[SpanMapFeature["Hover"] = 1] = "Hover";
|
|
14
|
+
SpanMapFeature[SpanMapFeature["SignatureHelp"] = 2] = "SignatureHelp";
|
|
15
|
+
SpanMapFeature[SpanMapFeature["Completion"] = 4] = "Completion";
|
|
16
|
+
SpanMapFeature[SpanMapFeature["Definition"] = 8] = "Definition";
|
|
17
|
+
SpanMapFeature[SpanMapFeature["TypeDefinition"] = 16] = "TypeDefinition";
|
|
18
|
+
SpanMapFeature[SpanMapFeature["Implementation"] = 32] = "Implementation";
|
|
19
|
+
SpanMapFeature[SpanMapFeature["References"] = 64] = "References";
|
|
20
|
+
SpanMapFeature[SpanMapFeature["DocumentHighlights"] = 128] = "DocumentHighlights";
|
|
21
|
+
SpanMapFeature[SpanMapFeature["Rename"] = 256] = "Rename";
|
|
22
|
+
SpanMapFeature[SpanMapFeature["CallHierarchy"] = 512] = "CallHierarchy";
|
|
23
|
+
SpanMapFeature[SpanMapFeature["CodeActions"] = 1024] = "CodeActions";
|
|
24
|
+
SpanMapFeature[SpanMapFeature["Formatting"] = 2048] = "Formatting";
|
|
25
|
+
SpanMapFeature[SpanMapFeature["InlayHints"] = 4096] = "InlayHints";
|
|
26
|
+
SpanMapFeature[SpanMapFeature["SemanticTokens"] = 8192] = "SemanticTokens";
|
|
27
|
+
SpanMapFeature[SpanMapFeature["FoldingRanges"] = 16384] = "FoldingRanges";
|
|
28
|
+
SpanMapFeature[SpanMapFeature["SelectionRanges"] = 32768] = "SelectionRanges";
|
|
29
|
+
SpanMapFeature[SpanMapFeature["LinkedEditing"] = 65536] = "LinkedEditing";
|
|
30
|
+
SpanMapFeature[SpanMapFeature["AutoInsert"] = 131072] = "AutoInsert";
|
|
31
|
+
SpanMapFeature[SpanMapFeature["DocumentSymbols"] = 262144] = "DocumentSymbols";
|
|
32
|
+
SpanMapFeature[SpanMapFeature["CodeLens"] = 524288] = "CodeLens";
|
|
33
|
+
SpanMapFeature[SpanMapFeature["None"] = 0] = "None";
|
|
34
|
+
SpanMapFeature[SpanMapFeature["All"] = 1048575] = "All";
|
|
35
|
+
})(SpanMapFeature || (exports.SpanMapFeature = SpanMapFeature = {}));
|
|
36
|
+
var SpanMapFidelity;
|
|
37
|
+
(function (SpanMapFidelity) {
|
|
38
|
+
SpanMapFidelity[SpanMapFidelity["Exact"] = 0] = "Exact";
|
|
39
|
+
SpanMapFidelity[SpanMapFidelity["Atom"] = 1] = "Atom";
|
|
40
|
+
SpanMapFidelity[SpanMapFidelity["Approximate"] = 2] = "Approximate";
|
|
41
|
+
SpanMapFidelity[SpanMapFidelity["None"] = 3] = "None";
|
|
42
|
+
})(SpanMapFidelity || (exports.SpanMapFidelity = SpanMapFidelity = {}));
|
|
43
|
+
/**
|
|
44
|
+
* Maps positions and ranges between virtual and original text, every candidate of each. A position maps through
|
|
45
|
+
* every segment that contains it, its end included; a range maps through one segment that contains it or, if none
|
|
46
|
+
* does, from a segment of its start to a segment of its end, as `Approximate`. Text outside every segment maps to
|
|
47
|
+
* nothing. `feature` selects the segments that share one of its bits; all segments if omitted.
|
|
48
|
+
*/
|
|
49
|
+
class SpanMap {
|
|
50
|
+
constructor(segments) {
|
|
51
|
+
this.segments = segments.map(segment => ({ ...segment, features: segment.features ?? SpanMapFeature.All }));
|
|
52
|
+
}
|
|
53
|
+
virtualToOriginalPositions(position, feature) {
|
|
54
|
+
return this.mapPosition(position, 'virtual', feature, false);
|
|
55
|
+
}
|
|
56
|
+
originalToVirtualPositions(position, feature) {
|
|
57
|
+
return this.mapPosition(position, 'original', feature, false);
|
|
58
|
+
}
|
|
59
|
+
virtualToOriginalSpans(start, end, feature) {
|
|
60
|
+
return this.mapRange(start, end, 'virtual', feature);
|
|
61
|
+
}
|
|
62
|
+
originalToVirtualSpans(start, end, feature) {
|
|
63
|
+
return this.mapRange(start, end, 'original', feature);
|
|
64
|
+
}
|
|
65
|
+
mapPosition(position, from, feature, preferEnd) {
|
|
66
|
+
const index = this.getIndex(from);
|
|
67
|
+
const results = [];
|
|
68
|
+
if (!index.offsets.length) {
|
|
69
|
+
return results;
|
|
70
|
+
}
|
|
71
|
+
const { low, high } = (0, binarySearch_1.binarySearch)(index.offsets, position);
|
|
72
|
+
const visited = new Set();
|
|
73
|
+
for (let i = low; i <= high; i++) {
|
|
74
|
+
for (const segment of index.segments[i]) {
|
|
75
|
+
if (visited.has(segment)) {
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
visited.add(segment);
|
|
79
|
+
if (feature !== undefined && !(segment.features & feature)) {
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
const mapped = translate(position, segment, from, preferEnd);
|
|
83
|
+
if (mapped !== undefined) {
|
|
84
|
+
results.push({
|
|
85
|
+
position: mapped,
|
|
86
|
+
fidelity: segment.kind === SpanMapKind.Verbatim ? SpanMapFidelity.Exact : SpanMapFidelity.Atom,
|
|
87
|
+
segment,
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return results;
|
|
93
|
+
}
|
|
94
|
+
mapRange(start, end, from, feature) {
|
|
95
|
+
const starts = this.mapPosition(start, from, feature, false);
|
|
96
|
+
const results = [];
|
|
97
|
+
for (const { position, fidelity, segment } of starts) {
|
|
98
|
+
const mappedEnd = translate(end, segment, from, true);
|
|
99
|
+
if (mappedEnd !== undefined) {
|
|
100
|
+
results.push({ start: position, end: mappedEnd, fidelity, segments: [segment, segment] });
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
if (results.length) {
|
|
104
|
+
return results;
|
|
105
|
+
}
|
|
106
|
+
const ends = this.mapPosition(end, from, feature, true);
|
|
107
|
+
for (const mappedStart of starts) {
|
|
108
|
+
const mappedEnd = ends.find(mappedEnd => mappedEnd.position >= mappedStart.position);
|
|
109
|
+
if (mappedEnd) {
|
|
110
|
+
results.push({
|
|
111
|
+
start: mappedStart.position,
|
|
112
|
+
end: mappedEnd.position,
|
|
113
|
+
fidelity: SpanMapFidelity.Approximate,
|
|
114
|
+
segments: [mappedStart.segment, mappedEnd.segment],
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return results;
|
|
119
|
+
}
|
|
120
|
+
getIndex(side) {
|
|
121
|
+
return side === 'virtual'
|
|
122
|
+
? this.virtualIndex ??= createIndex(this.segments, 'virtual')
|
|
123
|
+
: this.originalIndex ??= createIndex(this.segments, 'original');
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
exports.SpanMap = SpanMap;
|
|
127
|
+
function createIndex(segments, side) {
|
|
128
|
+
const offsetSet = new Set();
|
|
129
|
+
for (const segment of segments) {
|
|
130
|
+
const [start, end] = rangeOf(segment, side);
|
|
131
|
+
offsetSet.add(start);
|
|
132
|
+
offsetSet.add(end);
|
|
133
|
+
}
|
|
134
|
+
const offsets = [...offsetSet].sort((a, b) => a - b);
|
|
135
|
+
const buckets = offsets.map(() => new Set());
|
|
136
|
+
for (const segment of segments) {
|
|
137
|
+
const [start, end] = rangeOf(segment, side);
|
|
138
|
+
const startIndex = (0, binarySearch_1.binarySearch)(offsets, start).match;
|
|
139
|
+
const endIndex = (0, binarySearch_1.binarySearch)(offsets, end).match;
|
|
140
|
+
for (let i = startIndex; i <= endIndex; i++) {
|
|
141
|
+
buckets[i].add(segment);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return { offsets, segments: buckets };
|
|
145
|
+
}
|
|
146
|
+
function rangeOf(segment, side) {
|
|
147
|
+
return side === 'virtual'
|
|
148
|
+
? [segment.virtualStart, segment.virtualEnd]
|
|
149
|
+
: [segment.originalStart, segment.originalEnd];
|
|
150
|
+
}
|
|
151
|
+
function translate(position, segment, from, preferEnd) {
|
|
152
|
+
const [fromStart, fromEnd] = rangeOf(segment, from);
|
|
153
|
+
const [toStart, toEnd] = rangeOf(segment, from === 'virtual' ? 'original' : 'virtual');
|
|
154
|
+
const relative = position - fromStart;
|
|
155
|
+
if (relative < 0 || position > fromEnd) {
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
if (segment.kind === SpanMapKind.Verbatim) {
|
|
159
|
+
return Math.min(toStart + relative, toEnd);
|
|
160
|
+
}
|
|
161
|
+
// an atom, or an alias, maps as a whole: its edges to the edges, its inside to the start or, for an end, the end
|
|
162
|
+
if (relative === 0 && (fromEnd > fromStart || !preferEnd)) {
|
|
163
|
+
return toStart;
|
|
164
|
+
}
|
|
165
|
+
if (position === fromEnd || preferEnd) {
|
|
166
|
+
return toEnd;
|
|
167
|
+
}
|
|
168
|
+
return toStart;
|
|
169
|
+
}
|
|
170
|
+
//# sourceMappingURL=spanMap.js.map
|
package/package.json
CHANGED
package/lib/sourceMap.d.ts
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
type CodeRangeKey = 'sourceOffsets' | 'generatedOffsets';
|
|
2
|
-
export interface Mapping<Data = unknown> {
|
|
3
|
-
sourceOffsets: number[];
|
|
4
|
-
generatedOffsets: number[];
|
|
5
|
-
lengths: number[];
|
|
6
|
-
generatedLengths?: number[];
|
|
7
|
-
data: Data;
|
|
8
|
-
}
|
|
9
|
-
export declare class SourceMap<Data = unknown> {
|
|
10
|
-
readonly mappings: Mapping<Data>[];
|
|
11
|
-
private sourceCodeOffsetsMemo;
|
|
12
|
-
private generatedCodeOffsetsMemo;
|
|
13
|
-
constructor(mappings: Mapping<Data>[]);
|
|
14
|
-
toSourceRange(generatedStart: number, generatedEnd: number, fallbackToAnyMatch: boolean, filter?: (data: Data) => boolean): Generator<[mappedStart: number, mappedEnd: number, startMapping: Mapping<Data>, endMapping: Mapping<Data>], any, any>;
|
|
15
|
-
toGeneratedRange(sourceStart: number, sourceEnd: number, fallbackToAnyMatch: boolean, filter?: (data: Data) => boolean): Generator<[mappedStart: number, mappedEnd: number, startMapping: Mapping<Data>, endMapping: Mapping<Data>], any, any>;
|
|
16
|
-
toSourceLocation(generatedOffset: number, filter?: (data: Data) => boolean): Generator<readonly [number, Mapping<Data>], void, unknown>;
|
|
17
|
-
toGeneratedLocation(sourceOffset: number, filter?: (data: Data) => boolean): Generator<readonly [number, Mapping<Data>], void, unknown>;
|
|
18
|
-
findMatchingOffsets(offset: number, fromRange: CodeRangeKey, filter?: (data: Data) => boolean, preferEnd?: boolean): Generator<readonly [number, Mapping<Data>], void, unknown>;
|
|
19
|
-
findMatchingStartEnd(start: number, end: number, fallbackToAnyMatch: boolean, fromRange: CodeRangeKey, filter?: (data: Data) => boolean): Generator<[mappedStart: number, mappedEnd: number, startMapping: Mapping<Data>, endMapping: Mapping<Data>]>;
|
|
20
|
-
private getMemoBasedOnRange;
|
|
21
|
-
private createMemo;
|
|
22
|
-
}
|
|
23
|
-
export {};
|
package/lib/sourceMap.js
DELETED
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.SourceMap = void 0;
|
|
4
|
-
const binarySearch_1 = require("./binarySearch");
|
|
5
|
-
const translateOffset_1 = require("./translateOffset");
|
|
6
|
-
class SourceMap {
|
|
7
|
-
constructor(mappings) {
|
|
8
|
-
this.mappings = mappings;
|
|
9
|
-
}
|
|
10
|
-
toSourceRange(generatedStart, generatedEnd, fallbackToAnyMatch, filter) {
|
|
11
|
-
return this.findMatchingStartEnd(generatedStart, generatedEnd, fallbackToAnyMatch, 'generatedOffsets', filter);
|
|
12
|
-
}
|
|
13
|
-
toGeneratedRange(sourceStart, sourceEnd, fallbackToAnyMatch, filter) {
|
|
14
|
-
return this.findMatchingStartEnd(sourceStart, sourceEnd, fallbackToAnyMatch, 'sourceOffsets', filter);
|
|
15
|
-
}
|
|
16
|
-
toSourceLocation(generatedOffset, filter) {
|
|
17
|
-
return this.findMatchingOffsets(generatedOffset, 'generatedOffsets', filter);
|
|
18
|
-
}
|
|
19
|
-
toGeneratedLocation(sourceOffset, filter) {
|
|
20
|
-
return this.findMatchingOffsets(sourceOffset, 'sourceOffsets', filter);
|
|
21
|
-
}
|
|
22
|
-
*findMatchingOffsets(offset, fromRange, filter, preferEnd = false) {
|
|
23
|
-
const memo = this.getMemoBasedOnRange(fromRange);
|
|
24
|
-
if (memo.offsets.length === 0) {
|
|
25
|
-
return;
|
|
26
|
-
}
|
|
27
|
-
const { low: start, high: end } = (0, binarySearch_1.binarySearch)(memo.offsets, offset);
|
|
28
|
-
const skip = new Set();
|
|
29
|
-
const toRange = fromRange === 'sourceOffsets' ? 'generatedOffsets' : 'sourceOffsets';
|
|
30
|
-
for (let i = start; i <= end; i++) {
|
|
31
|
-
for (const mapping of memo.mappings[i]) {
|
|
32
|
-
if (skip.has(mapping)) {
|
|
33
|
-
continue;
|
|
34
|
-
}
|
|
35
|
-
skip.add(mapping);
|
|
36
|
-
if (filter && !filter(mapping.data)) {
|
|
37
|
-
continue;
|
|
38
|
-
}
|
|
39
|
-
const mapped = (0, translateOffset_1.translateOffset)(offset, mapping[fromRange], mapping[toRange], getLengths(mapping, fromRange), getLengths(mapping, toRange), preferEnd);
|
|
40
|
-
if (mapped !== undefined) {
|
|
41
|
-
yield [mapped, mapping];
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
*findMatchingStartEnd(start, end, fallbackToAnyMatch, fromRange, filter) {
|
|
47
|
-
const toRange = fromRange === 'sourceOffsets' ? 'generatedOffsets' : 'sourceOffsets';
|
|
48
|
-
const preferEnd = toRange === 'sourceOffsets';
|
|
49
|
-
const mappedStarts = [];
|
|
50
|
-
let hadMatch = false;
|
|
51
|
-
for (const [mappedStart, mapping] of this.findMatchingOffsets(start, fromRange)) {
|
|
52
|
-
if (filter && !filter(mapping.data)) {
|
|
53
|
-
continue;
|
|
54
|
-
}
|
|
55
|
-
mappedStarts.push([mappedStart, mapping]);
|
|
56
|
-
const mappedEnd = (0, translateOffset_1.translateOffset)(end, mapping[fromRange], mapping[toRange], getLengths(mapping, fromRange), getLengths(mapping, toRange), preferEnd);
|
|
57
|
-
if (mappedEnd !== undefined) {
|
|
58
|
-
hadMatch = true;
|
|
59
|
-
yield [mappedStart, mappedEnd, mapping, mapping];
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
if (!hadMatch && fallbackToAnyMatch) {
|
|
63
|
-
for (const [mappedStart, mappingStart] of mappedStarts) {
|
|
64
|
-
for (const [mappedEnd, mappingEnd] of this.findMatchingOffsets(end, fromRange, undefined, preferEnd)) {
|
|
65
|
-
if (filter && !filter(mappingEnd.data) || mappedEnd < mappedStart) {
|
|
66
|
-
continue;
|
|
67
|
-
}
|
|
68
|
-
yield [mappedStart, mappedEnd, mappingStart, mappingEnd];
|
|
69
|
-
break;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
getMemoBasedOnRange(fromRange) {
|
|
75
|
-
return fromRange === 'sourceOffsets'
|
|
76
|
-
? this.sourceCodeOffsetsMemo ??= this.createMemo('sourceOffsets')
|
|
77
|
-
: this.generatedCodeOffsetsMemo ??= this.createMemo('generatedOffsets');
|
|
78
|
-
}
|
|
79
|
-
createMemo(key) {
|
|
80
|
-
const offsetsSet = new Set();
|
|
81
|
-
for (const mapping of this.mappings) {
|
|
82
|
-
for (let i = 0; i < mapping[key].length; i++) {
|
|
83
|
-
offsetsSet.add(mapping[key][i]);
|
|
84
|
-
offsetsSet.add(mapping[key][i] + getLengths(mapping, key)[i]);
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
const offsets = [...offsetsSet].sort((a, b) => a - b);
|
|
88
|
-
const mappings = offsets.map(() => new Set());
|
|
89
|
-
for (const mapping of this.mappings) {
|
|
90
|
-
for (let i = 0; i < mapping[key].length; i++) {
|
|
91
|
-
const startIndex = (0, binarySearch_1.binarySearch)(offsets, mapping[key][i]).match;
|
|
92
|
-
const endIndex = (0, binarySearch_1.binarySearch)(offsets, mapping[key][i] + getLengths(mapping, key)[i]).match;
|
|
93
|
-
for (let i = startIndex; i <= endIndex; i++) {
|
|
94
|
-
mappings[i].add(mapping);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
return { offsets, mappings };
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
exports.SourceMap = SourceMap;
|
|
102
|
-
function getLengths(mapping, key) {
|
|
103
|
-
return key === 'sourceOffsets' ? mapping.lengths : mapping.generatedLengths ?? mapping.lengths;
|
|
104
|
-
}
|
|
105
|
-
//# sourceMappingURL=sourceMap.js.map
|
package/lib/translateOffset.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function translateOffset(start: number, fromOffsets: number[], toOffsets: number[], fromLengths: number[], toLengths?: number[], preferEnd?: boolean): number | undefined;
|
package/lib/translateOffset.js
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.translateOffset = translateOffset;
|
|
4
|
-
let warned = false;
|
|
5
|
-
function translateOffset(start, fromOffsets, toOffsets, fromLengths, toLengths = fromLengths, preferEnd = false) {
|
|
6
|
-
const isSorted = fromOffsets.every((value, index) => index === 0 || fromOffsets[index - 1] <= value);
|
|
7
|
-
if (!isSorted) {
|
|
8
|
-
for (let i = 0; i < fromOffsets.length; i++) {
|
|
9
|
-
const fromOffset = fromOffsets[i];
|
|
10
|
-
const fromLength = fromLengths[i];
|
|
11
|
-
if (start >= fromOffset && start <= fromOffset + fromLength) {
|
|
12
|
-
const toLength = toLengths[i];
|
|
13
|
-
const toOffset = toOffsets[i];
|
|
14
|
-
let rangeOffset;
|
|
15
|
-
const relativePos = start - fromOffset;
|
|
16
|
-
if (preferEnd && toLength > fromLength && relativePos === fromLength) {
|
|
17
|
-
rangeOffset = toLength;
|
|
18
|
-
}
|
|
19
|
-
else {
|
|
20
|
-
rangeOffset = Math.min(relativePos, toLength);
|
|
21
|
-
}
|
|
22
|
-
return toOffset + rangeOffset;
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
if (!warned) {
|
|
26
|
-
warned = true;
|
|
27
|
-
console.warn('fromOffsets should be sorted in ascending order');
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
let low = 0;
|
|
31
|
-
let high = fromOffsets.length - 1;
|
|
32
|
-
while (low <= high) {
|
|
33
|
-
const mid = Math.floor((low + high) / 2);
|
|
34
|
-
const fromOffset = fromOffsets[mid];
|
|
35
|
-
const fromLength = fromLengths[mid];
|
|
36
|
-
if (start >= fromOffset && start <= fromOffset + fromLength) {
|
|
37
|
-
const toLength = toLengths[mid];
|
|
38
|
-
const toOffset = toOffsets[mid];
|
|
39
|
-
let rangeOffset;
|
|
40
|
-
const relativePos = start - fromOffset;
|
|
41
|
-
if (preferEnd && toLength > fromLength && relativePos === fromLength) {
|
|
42
|
-
rangeOffset = toLength;
|
|
43
|
-
}
|
|
44
|
-
else {
|
|
45
|
-
rangeOffset = Math.min(relativePos, toLength);
|
|
46
|
-
}
|
|
47
|
-
return toOffset + rangeOffset;
|
|
48
|
-
}
|
|
49
|
-
else if (start < fromOffset) {
|
|
50
|
-
high = mid - 1;
|
|
51
|
-
}
|
|
52
|
-
else {
|
|
53
|
-
low = mid + 1;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
//# sourceMappingURL=translateOffset.js.map
|