@tsplc/compiler 0.2.17 → 0.2.18
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 +2 -2
- package/dist/cli.js +920 -943
- package/dist/cli.js.map +13 -10
- package/dist/codesys.js +3893 -3352
- package/dist/codesys.js.map +29 -20
- package/dist/elaborate-capture.js +33 -17
- package/dist/elaborate-capture.js.map +3 -3
- package/dist/index.js +8787 -6855
- package/dist/index.js.map +40 -20
- package/dist/ir.js +1575 -1072
- package/dist/ir.js.map +21 -17
- package/dist/math.js.map +2 -2
- package/dist/project.js +154 -60
- package/dist/project.js.map +5 -5
- package/dist/telemetry.js +27 -9
- package/dist/telemetry.js.map +5 -5
- package/package.json +112 -112
- package/src/codesys/config.ts +50 -0
- package/src/codesys/debug.ts +670 -0
- package/src/codesys/emit.ts +142 -0
- package/src/codesys/engineering.ts +109 -0
- package/src/codesys/expressions.ts +92 -0
- package/src/codesys/global-lists.ts +250 -0
- package/src/codesys/global-names.ts +68 -0
- package/src/codesys/http.ts +890 -0
- package/src/codesys/index.ts +30 -0
- package/src/codesys/instrumentation.ts +430 -0
- package/src/codesys/io-paths.ts +140 -0
- package/src/codesys/manifest.ts +643 -0
- package/src/codesys/names.ts +81 -0
- package/src/codesys/numeric.ts +409 -0
- package/src/codesys/operands.ts +62 -0
- package/src/codesys/plan.ts +605 -0
- package/src/codesys/programs.ts +53 -0
- package/src/codesys/publication.ts +171 -0
- package/src/codesys/routines.ts +603 -0
- package/src/codesys/sources.ts +71 -0
- package/src/codesys/st-files.ts +115 -0
- package/src/codesys/storage.ts +97 -0
- package/src/codesys/symbols.ts +220 -0
- package/src/codesys/task-access.ts +320 -0
- package/src/codesys/telemetry.ts +732 -0
- package/src/codesys/types.ts +250 -0
- package/src/codesys/vendor.ts +150 -0
- package/src/frontend/analyzer.ts +794 -0
- package/src/frontend/async.ts +317 -1137
- package/src/frontend/blocks.ts +537 -403
- package/src/frontend/configuration.ts +176 -144
- package/src/frontend/const-functions.ts +183 -148
- package/src/frontend/cooperative.ts +73 -43
- package/src/frontend/coroutines.ts +1032 -0
- package/src/frontend/elaborate/calls.ts +535 -0
- package/src/frontend/elaborate/capture.ts +157 -101
- package/src/frontend/elaborate/dialect.ts +257 -0
- package/src/frontend/elaborate/expressions.ts +992 -0
- package/src/frontend/elaborate/frame.ts +53 -0
- package/src/frontend/elaborate/graph.ts +116 -71
- package/src/frontend/elaborate/imports.ts +70 -0
- package/src/frontend/elaborate/index.ts +435 -331
- package/src/frontend/elaborate/initializers.ts +198 -0
- package/src/frontend/elaborate/layouts.ts +591 -384
- package/src/frontend/elaborate/model.ts +264 -169
- package/src/frontend/elaborate/specialize.ts +343 -2586
- package/src/frontend/elaborate/statements.ts +606 -0
- package/src/frontend/elaborate/storage.ts +546 -0
- package/src/frontend/elaborate/values.ts +58 -0
- package/src/frontend/elaborate/writes.ts +100 -0
- package/src/frontend/exposure.ts +172 -140
- package/src/frontend/expressions.ts +1383 -0
- package/src/frontend/intrinsics.ts +337 -0
- package/src/frontend/module-graph.ts +89 -59
- package/src/frontend/positions.ts +271 -200
- package/src/frontend/promise-chains.ts +897 -0
- package/src/frontend/promises.ts +390 -747
- package/src/frontend/routines.ts +181 -0
- package/src/frontend/sdk.ts +82 -0
- package/src/frontend/services.ts +506 -372
- package/src/frontend/statements.ts +855 -0
- package/src/frontend/tasks.ts +471 -0
- package/src/frontend/telemetry.ts +174 -0
- package/src/frontend/types.ts +845 -551
- package/src/frontend/typescript.ts +323 -2543
- package/src/index.ts +5 -5
- package/src/ir/aggregates.ts +275 -0
- package/src/ir/allocation.ts +84 -0
- package/src/ir/core-model.ts +146 -0
- package/src/ir/declarations.ts +295 -0
- package/src/ir/diagnostics.ts +61 -0
- package/src/ir/domains.ts +152 -0
- package/src/ir/fold.ts +342 -0
- package/src/ir/index.ts +90 -0
- package/{ir/errors.ts → src/ir/job-errors.ts} +12 -10
- package/src/ir/layouts.ts +181 -0
- package/src/ir/link.ts +354 -0
- package/src/ir/model.ts +386 -0
- package/{ir → src/ir}/normalization-error.ts +4 -4
- package/src/ir/normalize.ts +70 -0
- package/src/ir/numeric.ts +171 -0
- package/src/ir/ranges.ts +256 -0
- package/src/ir/routine.ts +1163 -0
- package/src/ir/scalars.ts +321 -0
- package/src/ir/types.ts +127 -0
- package/src/project/artifacts.ts +342 -0
- package/src/project/cli.ts +177 -0
- package/src/project/debugger.ts +82 -0
- package/src/project/index.ts +6 -0
- package/src/project/plant.ts +467 -0
- package/src/project/project-commands.ts +372 -0
- package/src/project/project.ts +457 -0
- package/src/project/release.ts +153 -0
- package/src/project/server.ts +150 -0
- package/src/project/terminal.ts +32 -0
- package/stdlib/math.ts +14 -10
- package/stdlib/telemetry.ts +71 -47
- package/types/async.d.ts +46 -40
- package/types/data-types.d.ts +26 -26
- package/types/globals.d.ts +41 -38
- package/types/index.d.ts +96 -51
- package/types/plc.d.ts +3 -4
- package/types/telemetry.d.ts +14 -14
- package/backends/codesys/config.ts +0 -54
- package/backends/codesys/debug.ts +0 -509
- package/backends/codesys/emit.ts +0 -1738
- package/backends/codesys/engineering.ts +0 -88
- package/backends/codesys/global-names.ts +0 -49
- package/backends/codesys/http.ts +0 -675
- package/backends/codesys/index.ts +0 -13
- package/backends/codesys/io-paths.ts +0 -105
- package/backends/codesys/manifest.ts +0 -312
- package/backends/codesys/names.ts +0 -76
- package/backends/codesys/numeric.ts +0 -356
- package/backends/codesys/plan.ts +0 -457
- package/backends/codesys/publication.ts +0 -59
- package/backends/codesys/storage.ts +0 -70
- package/backends/codesys/task-access.ts +0 -293
- package/backends/codesys/telemetry.ts +0 -593
- package/backends/codesys/types.ts +0 -179
- package/backends/codesys/vendor.ts +0 -108
- package/ir/aggregates.ts +0 -333
- package/ir/allocation.ts +0 -74
- package/ir/core-model.ts +0 -129
- package/ir/core.ts +0 -64
- package/ir/declarations.ts +0 -208
- package/ir/diagnostics.ts +0 -43
- package/ir/domains.ts +0 -96
- package/ir/index.ts +0 -8
- package/ir/link.ts +0 -185
- package/ir/model.ts +0 -346
- package/ir/routine.ts +0 -859
- package/ir/scalars.ts +0 -675
- package/ir/types.ts +0 -76
- package/project/artifacts.ts +0 -277
- package/project/cli.ts +0 -395
- package/project/index.ts +0 -1
- package/project/plant.ts +0 -509
- package/project/project.ts +0 -358
- package/project/server.ts +0 -113
|
@@ -6,196 +6,238 @@ import type { SourceSpan } from "@tsplc/compiler/ir";
|
|
|
6
6
|
* onto `from`..`to` of `file`; generated text maps as a whole to the node it was generated from.
|
|
7
7
|
*/
|
|
8
8
|
interface Segment {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
9
|
+
start: number;
|
|
10
|
+
end: number;
|
|
11
|
+
file: string;
|
|
12
|
+
from: number;
|
|
13
|
+
to: number;
|
|
14
|
+
copied: boolean;
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
/** Text a lowering pass writes, with the source each part of it came from. Segments are in order. */
|
|
18
18
|
export class Mapped {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
19
|
+
constructor(
|
|
20
|
+
readonly text: string,
|
|
21
|
+
readonly segments: readonly Segment[] = [],
|
|
22
|
+
) {}
|
|
23
23
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
24
|
+
toString(): string {
|
|
25
|
+
return this.text;
|
|
26
|
+
}
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
export type Part = string | number | Mapped;
|
|
30
30
|
|
|
31
31
|
const append = (segments: Segment[], text: Part, offset: number): void => {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
32
|
+
if (text instanceof Mapped) {
|
|
33
|
+
for (const segment of text.segments) {
|
|
34
|
+
segments.push({
|
|
35
|
+
...segment,
|
|
36
|
+
start: segment.start + offset,
|
|
37
|
+
end: segment.end + offset,
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
}
|
|
35
41
|
};
|
|
36
42
|
|
|
37
43
|
/** A template tag that concatenates like a template literal and keeps the origins of `Mapped` parts. */
|
|
38
|
-
export const mapped = (
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
44
|
+
export const mapped = (
|
|
45
|
+
strings: TemplateStringsArray,
|
|
46
|
+
...parts: readonly Part[]
|
|
47
|
+
): Mapped => {
|
|
48
|
+
let text = strings[0]!;
|
|
49
|
+
const segments: Segment[] = [];
|
|
50
|
+
parts.forEach((part, index) => {
|
|
51
|
+
append(segments, part, text.length);
|
|
52
|
+
text += `${part}${strings[index + 1]}`;
|
|
53
|
+
});
|
|
54
|
+
return new Mapped(text, segments);
|
|
46
55
|
};
|
|
47
56
|
|
|
48
57
|
export const joined = (parts: readonly Part[], separator: string): Mapped => {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
58
|
+
let text = "";
|
|
59
|
+
const segments: Segment[] = [];
|
|
60
|
+
parts.forEach((part, index) => {
|
|
61
|
+
if (index) {
|
|
62
|
+
text += separator;
|
|
63
|
+
}
|
|
64
|
+
append(segments, part, text.length);
|
|
65
|
+
text += String(part);
|
|
66
|
+
});
|
|
67
|
+
return new Mapped(text, segments);
|
|
57
68
|
};
|
|
58
69
|
|
|
59
70
|
const fill = (text: Part, file: string, from: number, to: number): Mapped => {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
71
|
+
const value = text instanceof Mapped ? text : new Mapped(String(text));
|
|
72
|
+
const segments: Segment[] = [];
|
|
73
|
+
let cursor = 0;
|
|
74
|
+
const gap = (end: number) => {
|
|
75
|
+
if (end > cursor) {
|
|
76
|
+
segments.push({ start: cursor, end, file, from, to, copied: false });
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
for (const segment of value.segments) {
|
|
80
|
+
gap(segment.start);
|
|
81
|
+
segments.push(segment);
|
|
82
|
+
cursor = segment.end;
|
|
83
|
+
}
|
|
84
|
+
gap(value.text.length);
|
|
85
|
+
return new Mapped(value.text, segments);
|
|
73
86
|
};
|
|
74
87
|
|
|
75
88
|
/** Text generated from a node: the parts without an origin of their own map to the node. */
|
|
76
89
|
export const from = (node: ts.Node, text: Part): Mapped => {
|
|
77
|
-
|
|
78
|
-
|
|
90
|
+
const source = node.getSourceFile();
|
|
91
|
+
return fill(text, source.fileName, node.getStart(source), node.end);
|
|
79
92
|
};
|
|
80
93
|
|
|
81
94
|
/** A node's source text, copied. */
|
|
82
95
|
export const copied = (node: ts.Node): Mapped => {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
96
|
+
const source = node.getSourceFile();
|
|
97
|
+
const start = node.getStart(source);
|
|
98
|
+
return new Mapped(source.text.slice(start, node.end), [
|
|
99
|
+
{
|
|
100
|
+
start: 0,
|
|
101
|
+
end: node.end - start,
|
|
102
|
+
file: source.fileName,
|
|
103
|
+
from: start,
|
|
104
|
+
to: node.end,
|
|
105
|
+
copied: true,
|
|
106
|
+
},
|
|
107
|
+
]);
|
|
95
108
|
};
|
|
96
109
|
|
|
97
110
|
export const slice = (text: Mapped, start: number, end: number): Mapped => {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
111
|
+
const segments: Segment[] = [];
|
|
112
|
+
for (const segment of text.segments) {
|
|
113
|
+
if (segment.end <= start || segment.start >= end) {
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
const first = Math.max(segment.start, start);
|
|
117
|
+
const last = Math.min(segment.end, end);
|
|
118
|
+
segments.push({
|
|
119
|
+
...segment,
|
|
120
|
+
start: first - start,
|
|
121
|
+
end: last - start,
|
|
122
|
+
...(segment.copied && {
|
|
123
|
+
from: segment.from + first - segment.start,
|
|
124
|
+
to: segment.from + last - segment.start,
|
|
125
|
+
}),
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
return new Mapped(text.text.slice(start, end), segments);
|
|
114
129
|
};
|
|
115
130
|
|
|
116
131
|
export interface Edit {
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
132
|
+
start: number;
|
|
133
|
+
end: number;
|
|
134
|
+
text: Part;
|
|
120
135
|
}
|
|
121
136
|
|
|
122
137
|
/**
|
|
123
138
|
* Text with edits applied. Unedited text keeps its origin; the parts of an edit's text without an
|
|
124
139
|
* origin of their own map to the text the edit replaces. Edits at one position apply last first.
|
|
125
140
|
*/
|
|
126
|
-
export const edited = (
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
141
|
+
export const edited = (
|
|
142
|
+
base: ts.SourceFile | Mapped,
|
|
143
|
+
edits: readonly Edit[],
|
|
144
|
+
): Mapped => {
|
|
145
|
+
const text =
|
|
146
|
+
base instanceof Mapped
|
|
147
|
+
? base
|
|
148
|
+
: new Mapped(base.text, [
|
|
149
|
+
{
|
|
150
|
+
start: 0,
|
|
151
|
+
end: base.text.length,
|
|
152
|
+
file: base.fileName,
|
|
153
|
+
from: 0,
|
|
154
|
+
to: base.text.length,
|
|
155
|
+
copied: true,
|
|
156
|
+
},
|
|
157
|
+
]);
|
|
158
|
+
const ordered = edits
|
|
159
|
+
.map((edit, index) => ({ edit, index }))
|
|
160
|
+
.sort(
|
|
161
|
+
(left, right) =>
|
|
162
|
+
left.edit.start - right.edit.start || right.index - left.index,
|
|
163
|
+
);
|
|
164
|
+
const parts: Mapped[] = [];
|
|
165
|
+
let cursor = 0;
|
|
166
|
+
for (const { edit } of ordered) {
|
|
167
|
+
if (edit.start > cursor) {
|
|
168
|
+
parts.push(slice(text, cursor, edit.start));
|
|
169
|
+
}
|
|
170
|
+
const origin = resolve(text.segments, edit.start, edit.end);
|
|
171
|
+
parts.push(
|
|
172
|
+
origin
|
|
173
|
+
? fill(edit.text, origin.file, origin.from, origin.to)
|
|
174
|
+
: mapped`${edit.text}`,
|
|
175
|
+
);
|
|
176
|
+
cursor = Math.max(cursor, edit.end);
|
|
177
|
+
}
|
|
178
|
+
parts.push(slice(text, cursor, text.text.length));
|
|
179
|
+
return joined(parts, "");
|
|
155
180
|
};
|
|
156
181
|
|
|
157
182
|
/** The first segment that ends after `offset`. */
|
|
158
183
|
const search = (segments: readonly Segment[], offset: number): number => {
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
184
|
+
let low = 0;
|
|
185
|
+
let high = segments.length;
|
|
186
|
+
while (low < high) {
|
|
187
|
+
const middle = (low + high) >> 1;
|
|
188
|
+
if (segments[middle]!.end > offset) {
|
|
189
|
+
high = middle;
|
|
190
|
+
} else {
|
|
191
|
+
low = middle + 1;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
return low;
|
|
167
195
|
};
|
|
168
196
|
|
|
169
197
|
/** Where `start`..`end` of mapped text came from; a range across origins maps to its start's. */
|
|
170
198
|
const resolve = (
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
199
|
+
segments: readonly Segment[],
|
|
200
|
+
start: number,
|
|
201
|
+
end: number,
|
|
174
202
|
): { file: string; from: number; to: number } | undefined => {
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
203
|
+
const first = search(segments, start);
|
|
204
|
+
const head = segments[first];
|
|
205
|
+
if (!head || head.start >= Math.max(end, start + 1)) {
|
|
206
|
+
return undefined;
|
|
207
|
+
}
|
|
208
|
+
let last = end > start ? search(segments, end - 1) : first;
|
|
209
|
+
if (!segments[last] || segments[last]!.start >= end) {
|
|
210
|
+
last--;
|
|
211
|
+
}
|
|
212
|
+
const tail = last > first ? segments[last]! : head;
|
|
213
|
+
const from = head.copied
|
|
214
|
+
? head.from + Math.max(0, start - head.start)
|
|
215
|
+
: head.from;
|
|
216
|
+
const until = (segment: Segment) =>
|
|
217
|
+
segment.copied
|
|
218
|
+
? segment.from + Math.min(end, segment.end) - segment.start
|
|
219
|
+
: segment.to;
|
|
220
|
+
const to = until(tail);
|
|
221
|
+
if (tail.file === head.file && to >= from) {
|
|
222
|
+
return { file: head.file, from, to };
|
|
223
|
+
}
|
|
224
|
+
return { file: head.file, from, to: Math.max(from, until(head)) };
|
|
187
225
|
};
|
|
188
226
|
|
|
189
227
|
/** A span of a source as its text reads. */
|
|
190
|
-
export const sourceSpan = (
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
228
|
+
export const sourceSpan = (
|
|
229
|
+
source: ts.SourceFile,
|
|
230
|
+
start: number,
|
|
231
|
+
end: number,
|
|
232
|
+
): SourceSpan => {
|
|
233
|
+
const position = source.getLineAndCharacterOfPosition(start);
|
|
234
|
+
return {
|
|
235
|
+
file: source.fileName,
|
|
236
|
+
start,
|
|
237
|
+
end,
|
|
238
|
+
line: position.line + 1,
|
|
239
|
+
column: position.character + 1,
|
|
240
|
+
};
|
|
199
241
|
};
|
|
200
242
|
|
|
201
243
|
/**
|
|
@@ -203,70 +245,99 @@ export const sourceSpan = (source: ts.SourceFile, start: number, end: number): S
|
|
|
203
245
|
* files the user wrote. Each lowering's output is composed with the maps of the text it read.
|
|
204
246
|
*/
|
|
205
247
|
export class Positions {
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
248
|
+
private readonly maps = new Map<string, readonly Segment[]>();
|
|
249
|
+
/** The user's files as written, for positions in files lowering has since rewritten. */
|
|
250
|
+
private readonly originals = new Map<string, ts.SourceFile>();
|
|
209
251
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
252
|
+
/** Records a lowering's rewritten and generated sources; `program` is the program it read. */
|
|
253
|
+
record(sources: ReadonlyMap<string, Mapped>, program: ts.Program): void {
|
|
254
|
+
// Every source of one lowering maps into the text it read, so compose them all before replacing any map.
|
|
255
|
+
const composed = new Map<string, Segment[]>();
|
|
256
|
+
for (const [file, text] of sources) {
|
|
257
|
+
const segments: Segment[] = [];
|
|
258
|
+
for (const segment of text.segments) {
|
|
259
|
+
const previous = this.maps.get(segment.file);
|
|
260
|
+
if (!previous) {
|
|
261
|
+
if (!this.originals.has(segment.file)) {
|
|
262
|
+
this.originals.set(
|
|
263
|
+
segment.file,
|
|
264
|
+
program.getSourceFile(segment.file)!,
|
|
265
|
+
);
|
|
266
|
+
}
|
|
267
|
+
segments.push(segment);
|
|
268
|
+
} else if (segment.copied) {
|
|
269
|
+
for (
|
|
270
|
+
let index = search(previous, segment.from);
|
|
271
|
+
index < previous.length;
|
|
272
|
+
index++
|
|
273
|
+
) {
|
|
274
|
+
const inner = previous[index]!;
|
|
275
|
+
if (inner.start >= segment.to) {
|
|
276
|
+
break;
|
|
277
|
+
}
|
|
278
|
+
const first = Math.max(inner.start, segment.from);
|
|
279
|
+
const last = Math.min(inner.end, segment.to);
|
|
280
|
+
segments.push({
|
|
281
|
+
start: segment.start + first - segment.from,
|
|
282
|
+
end: segment.start + last - segment.from,
|
|
283
|
+
file: inner.file,
|
|
284
|
+
from: inner.copied
|
|
285
|
+
? inner.from + first - inner.start
|
|
286
|
+
: inner.from,
|
|
287
|
+
to: inner.copied ? inner.from + last - inner.start : inner.to,
|
|
288
|
+
copied: inner.copied,
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
} else {
|
|
292
|
+
const origin = resolve(previous, segment.from, segment.to);
|
|
293
|
+
if (origin) {
|
|
294
|
+
segments.push({ ...segment, ...origin });
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
composed.set(file, segments);
|
|
299
|
+
}
|
|
300
|
+
for (const [file, segments] of composed) {
|
|
301
|
+
this.maps.set(file, segments);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
246
304
|
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
305
|
+
/** The user's source and offsets that `start`..`end` of a source the frontend reads came from. */
|
|
306
|
+
private origin(source: ts.SourceFile, start: number, end: number) {
|
|
307
|
+
const segments = this.maps.get(source.fileName);
|
|
308
|
+
const origin = segments && resolve(segments, start, end);
|
|
309
|
+
if (!origin) {
|
|
310
|
+
return { source, from: start, to: end };
|
|
311
|
+
}
|
|
312
|
+
return {
|
|
313
|
+
source: this.originals.get(origin.file)!,
|
|
314
|
+
from: origin.from,
|
|
315
|
+
to: origin.to,
|
|
316
|
+
};
|
|
317
|
+
}
|
|
254
318
|
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
319
|
+
readonly span = (
|
|
320
|
+
source: ts.SourceFile,
|
|
321
|
+
start: number,
|
|
322
|
+
end: number,
|
|
323
|
+
): SourceSpan => {
|
|
324
|
+
const origin = this.origin(source, start, end);
|
|
325
|
+
return sourceSpan(origin.source, origin.from, origin.to);
|
|
326
|
+
};
|
|
259
327
|
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
328
|
+
readonly node = (node: ts.Node): SourceSpan => {
|
|
329
|
+
const source = node.getSourceFile();
|
|
330
|
+
return this.span(source, node.getStart(source), node.end);
|
|
331
|
+
};
|
|
264
332
|
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
333
|
+
/** The user's text a node came from, on one line and at most `limit` characters. */
|
|
334
|
+
excerpt(node: ts.Node, limit = 120): string {
|
|
335
|
+
const source = node.getSourceFile();
|
|
336
|
+
const origin = this.origin(source, node.getStart(source), node.end);
|
|
337
|
+
const text = origin.source.text
|
|
338
|
+
.slice(origin.from, origin.to)
|
|
339
|
+
.replace(/\s+/g, " ")
|
|
340
|
+
.trim();
|
|
341
|
+
return text.length > limit ? `${text.slice(0, limit - 1)}…` : text;
|
|
342
|
+
}
|
|
272
343
|
}
|