@decimalturn/toml-patch 3.1.2 → 3.2.0-dev
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/dist/dev/comment-alignment.js +372 -0
- package/dist/dev/comment-alignment.js.map +1 -0
- package/dist/dev/comment-ownership.js +660 -0
- package/dist/dev/comment-ownership.js.map +1 -0
- package/dist/dev/cst-source.js +118 -0
- package/dist/dev/cst-source.js.map +1 -0
- package/dist/dev/cst.js +68 -0
- package/dist/dev/cst.js.map +1 -0
- package/dist/dev/cursor.js +66 -0
- package/dist/dev/cursor.js.map +1 -0
- package/dist/dev/date-format.js +196 -0
- package/dist/dev/date-format.js.map +1 -0
- package/dist/dev/decode-utf8.js +27 -0
- package/dist/dev/decode-utf8.js.map +1 -0
- package/dist/dev/diff-lite.js +123 -0
- package/dist/dev/diff-lite.js.map +1 -0
- package/dist/dev/diff.js +265 -0
- package/dist/dev/diff.js.map +1 -0
- package/dist/dev/encode-lite.js +193 -0
- package/dist/dev/encode-lite.js.map +1 -0
- package/dist/dev/escape-preference.js +166 -0
- package/dist/dev/escape-preference.js.map +1 -0
- package/dist/dev/find-by-path.js +111 -0
- package/dist/dev/find-by-path.js.map +1 -0
- package/dist/dev/formatter.js +298 -0
- package/dist/dev/formatter.js.map +1 -0
- package/dist/dev/generate.js +465 -0
- package/dist/dev/generate.js.map +1 -0
- package/dist/dev/inline-comma-space.js +13 -0
- package/dist/dev/inline-comma-space.js.map +1 -0
- package/dist/dev/inline-format.js +106 -0
- package/dist/dev/inline-format.js.map +1 -0
- package/dist/dev/inline-layout.js +107 -0
- package/dist/dev/inline-layout.js.map +1 -0
- package/dist/dev/line-ending-backslash.js +320 -0
- package/dist/dev/line-ending-backslash.js.map +1 -0
- package/dist/dev/literal-string.js +46 -0
- package/dist/dev/literal-string.js.map +1 -0
- package/dist/dev/location.js +104 -0
- package/dist/dev/location.js.map +1 -0
- package/dist/dev/parse-error.js +27 -0
- package/dist/dev/parse-error.js.map +1 -0
- package/dist/dev/parse-js.js +150 -0
- package/dist/dev/parse-js.js.map +1 -0
- package/dist/dev/parse-string.js +122 -0
- package/dist/dev/parse-string.js.map +1 -0
- package/dist/dev/parse-toml.js +714 -0
- package/dist/dev/parse-toml.js.map +1 -0
- package/dist/dev/patch-lite.js +4 -0
- package/dist/dev/patch-lite2.js +96 -0
- package/dist/dev/patch-lite2.js.map +1 -0
- package/dist/dev/patch.js +2652 -0
- package/dist/dev/patch.js.map +1 -0
- package/dist/dev/smol-toml-date.js +61 -0
- package/dist/dev/smol-toml-date.js.map +1 -0
- package/dist/dev/to-js.js +303 -0
- package/dist/dev/to-js.js.map +1 -0
- package/dist/dev/to-toml.js +874 -0
- package/dist/dev/to-toml.js.map +1 -0
- package/dist/dev/tokenizer.js +225 -0
- package/dist/dev/tokenizer.js.map +1 -0
- package/dist/dev/toml-document.js +220 -0
- package/dist/dev/toml-document.js.map +1 -0
- package/dist/dev/toml-format.js +528 -0
- package/dist/dev/toml-format.js.map +1 -0
- package/dist/dev/toml-patch.js +94 -0
- package/dist/dev/toml-patch.js.map +1 -0
- package/dist/dev/traverse.js +61 -0
- package/dist/dev/traverse.js.map +1 -0
- package/dist/dev/truncate.js +64 -0
- package/dist/dev/truncate.js.map +1 -0
- package/dist/dev/update-order.js +235 -0
- package/dist/dev/update-order.js.map +1 -0
- package/dist/dev/utils.js +244 -0
- package/dist/dev/utils.js.map +1 -0
- package/dist/dev/writer.js +758 -0
- package/dist/dev/writer.js.map +1 -0
- package/dist/patch-lite.d.ts +18 -0
- package/dist/toml-patch.d.ts +7 -1
- package/package.json +16 -93
- package/LICENSE +0 -22
- package/README.md +0 -169
- package/dist/toml-patch.js +0 -56
|
@@ -0,0 +1,874 @@
|
|
|
1
|
+
//! @decimalturn/toml-patch v3.2.0 - https://github.com/DecimalTurn/toml-patch - @license: MIT
|
|
2
|
+
import { hasItem, hasItems, isInlineArray, isInlineItem, isInlineTable, isKeyValue, isTable, isTableArray } from "./cst.js";
|
|
3
|
+
import { getCommaSpace } from "./inline-comma-space.js";
|
|
4
|
+
import { isIterable } from "./utils.js";
|
|
5
|
+
import { getNodeSource, originalFirstChildStartedAfterOpener, sourceStructureUnchanged } from "./cst-source.js";
|
|
6
|
+
import { isGeneratedNestedTable, isGeneratedNestedTableHost, isPreservedMultilineEmpty } from "./inline-format.js";
|
|
7
|
+
|
|
8
|
+
//#region src/to-toml.ts
|
|
9
|
+
const BY_NEW_LINE = /(\r\n|\n)/g;
|
|
10
|
+
/**
|
|
11
|
+
* Converts a Concrete Syntax Tree (CST) back to TOML format string.
|
|
12
|
+
*
|
|
13
|
+
* This function traverses the CST and reconstructs the original TOML document
|
|
14
|
+
* by writing each node's raw content to the appropriate location coordinates.
|
|
15
|
+
* It preserves the original formatting, spacing, and structure of the TOML file.
|
|
16
|
+
*
|
|
17
|
+
* @param cst - The Concrete Syntax Tree representing the parsed TOML document
|
|
18
|
+
* @param format - The formatting options to use for the output
|
|
19
|
+
* @returns The reconstructed TOML document as a string
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```typescript
|
|
23
|
+
* const tomlString = toTOML(cst, TomlFormat.default());
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
function toTOML(cst, format) {
|
|
27
|
+
const lines = [];
|
|
28
|
+
const closingPosition = (container) => {
|
|
29
|
+
const position = {
|
|
30
|
+
line: container.loc.end.line,
|
|
31
|
+
column: container.loc.end.column - 1
|
|
32
|
+
};
|
|
33
|
+
const last = container.items[container.items.length - 1];
|
|
34
|
+
if (last && (last.loc.end.line > position.line || last.loc.end.line === position.line && last.loc.end.column > position.column)) {
|
|
35
|
+
position.line = last.loc.end.line;
|
|
36
|
+
position.column = last.loc.end.column + (last.comma ? 1 : 0);
|
|
37
|
+
}
|
|
38
|
+
return position;
|
|
39
|
+
};
|
|
40
|
+
function emitNode(node) {
|
|
41
|
+
switch (node.type) {
|
|
42
|
+
case "Document":
|
|
43
|
+
for (let i = 0; i < node.items.length; i++) emitNode(node.items[i]);
|
|
44
|
+
break;
|
|
45
|
+
case "Table": {
|
|
46
|
+
const tbl = node;
|
|
47
|
+
emitNode(tbl.key);
|
|
48
|
+
for (let i = 0; i < tbl.items.length; i++) emitNode(tbl.items[i]);
|
|
49
|
+
break;
|
|
50
|
+
}
|
|
51
|
+
case "TableKey": {
|
|
52
|
+
const tk = node;
|
|
53
|
+
write(lines, tk.loc, "[" + tk.item.raw + "]");
|
|
54
|
+
break;
|
|
55
|
+
}
|
|
56
|
+
case "TableArray": {
|
|
57
|
+
const ta = node;
|
|
58
|
+
emitNode(ta.key);
|
|
59
|
+
for (let i = 0; i < ta.items.length; i++) emitNode(ta.items[i]);
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
case "TableArrayKey": {
|
|
63
|
+
const tak = node;
|
|
64
|
+
write(lines, tak.loc, "[[" + tak.item.raw + "]]");
|
|
65
|
+
break;
|
|
66
|
+
}
|
|
67
|
+
case "KeyValue": {
|
|
68
|
+
const kv = node;
|
|
69
|
+
const line = kv.loc.start.line;
|
|
70
|
+
writeSingle(lines, line, kv.equals, "=");
|
|
71
|
+
emitNode(kv.key);
|
|
72
|
+
emitNode(kv.value);
|
|
73
|
+
break;
|
|
74
|
+
}
|
|
75
|
+
case "Key":
|
|
76
|
+
write(lines, node.loc, node.raw);
|
|
77
|
+
break;
|
|
78
|
+
case "String":
|
|
79
|
+
write(lines, node.loc, node.raw);
|
|
80
|
+
break;
|
|
81
|
+
case "Integer":
|
|
82
|
+
write(lines, node.loc, node.raw);
|
|
83
|
+
break;
|
|
84
|
+
case "Float":
|
|
85
|
+
write(lines, node.loc, node.raw);
|
|
86
|
+
break;
|
|
87
|
+
case "Boolean":
|
|
88
|
+
write(lines, node.loc, node.value.toString());
|
|
89
|
+
break;
|
|
90
|
+
case "DateTime":
|
|
91
|
+
write(lines, node.loc, node.raw);
|
|
92
|
+
break;
|
|
93
|
+
case "InlineArray": {
|
|
94
|
+
const ia = node;
|
|
95
|
+
const { start } = ia.loc;
|
|
96
|
+
const closing = closingPosition(ia);
|
|
97
|
+
writeSingle(lines, start.line, start.column, "[");
|
|
98
|
+
writeSingle(lines, closing.line, closing.column, "]");
|
|
99
|
+
for (let i = 0; i < ia.items.length; i++) emitNode(ia.items[i]);
|
|
100
|
+
break;
|
|
101
|
+
}
|
|
102
|
+
case "InlineTable": {
|
|
103
|
+
const it = node;
|
|
104
|
+
const { start } = it.loc;
|
|
105
|
+
const closing = closingPosition(it);
|
|
106
|
+
writeSingle(lines, start.line, start.column, "{");
|
|
107
|
+
writeSingle(lines, closing.line, closing.column, "}");
|
|
108
|
+
for (let i = 0; i < it.items.length; i++) emitNode(it.items[i]);
|
|
109
|
+
break;
|
|
110
|
+
}
|
|
111
|
+
case "InlineItem": {
|
|
112
|
+
const ii = node;
|
|
113
|
+
emitNode(ii.item);
|
|
114
|
+
if (ii.comma) {
|
|
115
|
+
const end = ii.loc.end;
|
|
116
|
+
writeSingle(lines, end.line, end.column, ",");
|
|
117
|
+
}
|
|
118
|
+
break;
|
|
119
|
+
}
|
|
120
|
+
case "Comment":
|
|
121
|
+
write(lines, node.loc, node.raw);
|
|
122
|
+
break;
|
|
123
|
+
default: {
|
|
124
|
+
const type = node.type;
|
|
125
|
+
throw new Error(`toTOML: Unrecognized node type: ${String(type)}`);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
if (isIterable(cst)) for (const item of cst) emitNode(item);
|
|
130
|
+
else emitNode(cst);
|
|
131
|
+
if (format.useTabsForIndentation) {
|
|
132
|
+
const contentLines = /* @__PURE__ */ new Set();
|
|
133
|
+
const collectStringLines = (node) => {
|
|
134
|
+
if (node.type === "String" && node.loc.end.line > node.loc.start.line) for (let l = node.loc.start.line + 1; l <= node.loc.end.line; l++) contentLines.add(l);
|
|
135
|
+
if (hasItems(node)) for (const item of node.items) collectStringLines(item);
|
|
136
|
+
if (isKeyValue(node)) collectStringLines(node.value);
|
|
137
|
+
if (isInlineItem(node)) collectStringLines(node.item);
|
|
138
|
+
};
|
|
139
|
+
if (isIterable(cst)) for (const item of cst) collectStringLines(item);
|
|
140
|
+
else collectStringLines(cst);
|
|
141
|
+
for (let i = 0; i < lines.length; i++) {
|
|
142
|
+
if (contentLines.has(i + 1)) continue;
|
|
143
|
+
const line = lines[i];
|
|
144
|
+
const match = line.match(/^( +)/);
|
|
145
|
+
if (match) {
|
|
146
|
+
const leadingSpaces = match[1];
|
|
147
|
+
const leadingTabs = " ".repeat(leadingSpaces.length);
|
|
148
|
+
lines[i] = leadingTabs + line.substring(leadingSpaces.length);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return lines.join(format.newLine) + format.newLine.repeat(format.trailingNewline);
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Source-aware emitter used while CST ranges replace coordinate painting.
|
|
156
|
+
* Clean nodes copy their original text. Dirty nodes emit in structural order,
|
|
157
|
+
* so a stale coordinate can add whitespace but cannot overwrite later text.
|
|
158
|
+
*/
|
|
159
|
+
function toTOMLCursor(cst, format) {
|
|
160
|
+
const roots = isIterable(cst) ? Array.from(cst) : [cst];
|
|
161
|
+
const chunks = [];
|
|
162
|
+
const copiedRanges = [];
|
|
163
|
+
const hostedComments = [];
|
|
164
|
+
const consumedComments = /* @__PURE__ */ new WeakSet();
|
|
165
|
+
let line = 1;
|
|
166
|
+
let column = 0;
|
|
167
|
+
const collectComments = (node) => {
|
|
168
|
+
if (node.type === "Comment") hostedComments.push(node);
|
|
169
|
+
if (isKeyValue(node)) collectComments(node.value);
|
|
170
|
+
else if (isInlineItem(node)) collectComments(node.item);
|
|
171
|
+
else if (node.type === "Table" || node.type === "TableArray") for (const item of node.items) collectComments(item);
|
|
172
|
+
else if (hasItems(node)) for (const item of node.items) collectComments(item);
|
|
173
|
+
};
|
|
174
|
+
for (const root of roots) collectComments(root);
|
|
175
|
+
const blockLevelInlineContainers = /* @__PURE__ */ new WeakSet();
|
|
176
|
+
const markBlockLevelInlineContainers = (items) => {
|
|
177
|
+
for (const item of items) if (isKeyValue(item)) {
|
|
178
|
+
const value = item.value;
|
|
179
|
+
if (isInlineTable(value) || isInlineArray(value)) blockLevelInlineContainers.add(value);
|
|
180
|
+
} else if (isTable(item) || isTableArray(item)) markBlockLevelInlineContainers(item.items);
|
|
181
|
+
};
|
|
182
|
+
markBlockLevelInlineContainers(roots);
|
|
183
|
+
const indentation = (width) => (format.useTabsForIndentation ? " " : " ").repeat(width);
|
|
184
|
+
const append = (text) => {
|
|
185
|
+
if (text.length === 0) return;
|
|
186
|
+
chunks.push(text);
|
|
187
|
+
const parts = text.split(/\r\n|\n/);
|
|
188
|
+
if (parts.length === 1) column += text.length;
|
|
189
|
+
else {
|
|
190
|
+
line += parts.length - 1;
|
|
191
|
+
column = parts[parts.length - 1].length;
|
|
192
|
+
}
|
|
193
|
+
};
|
|
194
|
+
const advanceTo = (targetLine, targetColumn) => {
|
|
195
|
+
if (targetLine > line) {
|
|
196
|
+
append(format.newLine.repeat(targetLine - line));
|
|
197
|
+
if (targetColumn > 0) append(indentation(targetColumn));
|
|
198
|
+
} else if (targetLine === line && targetColumn > column) append(column === 0 ? indentation(targetColumn) : " ".repeat(targetColumn - column));
|
|
199
|
+
else if (chunks.length > 0 && (targetLine < line || targetLine === line && targetColumn < column)) {
|
|
200
|
+
append(format.newLine);
|
|
201
|
+
if (targetColumn > 0) append(indentation(targetColumn));
|
|
202
|
+
}
|
|
203
|
+
};
|
|
204
|
+
const leafText = (node) => {
|
|
205
|
+
switch (node.type) {
|
|
206
|
+
case "Key": return node.raw;
|
|
207
|
+
case "String": return node.raw;
|
|
208
|
+
case "Integer": return node.raw;
|
|
209
|
+
case "Float": return node.raw;
|
|
210
|
+
case "Boolean": return node.value.toString();
|
|
211
|
+
case "DateTime": return node.raw;
|
|
212
|
+
case "Comment": return node.raw;
|
|
213
|
+
default: return;
|
|
214
|
+
}
|
|
215
|
+
};
|
|
216
|
+
const directChildren = (node) => {
|
|
217
|
+
if (isKeyValue(node)) return [node.key, node.value];
|
|
218
|
+
if (isInlineItem(node)) return [node.item];
|
|
219
|
+
if (hasItem(node)) return [node.item];
|
|
220
|
+
if (node.type === "Table" || node.type === "TableArray") {
|
|
221
|
+
const table = node;
|
|
222
|
+
return [table.key, ...table.items];
|
|
223
|
+
}
|
|
224
|
+
return hasItems(node) ? node.items : [];
|
|
225
|
+
};
|
|
226
|
+
const sourceSubtreeReusable = (node) => {
|
|
227
|
+
const pending = [node];
|
|
228
|
+
while (pending.length > 0) {
|
|
229
|
+
const current = pending.pop();
|
|
230
|
+
if (current.dirty) return false;
|
|
231
|
+
const text = leafText(current);
|
|
232
|
+
if (text !== void 0) {
|
|
233
|
+
const source = getNodeSource(current);
|
|
234
|
+
if (!source || !current.range || source.slice(current.range[0], current.range[1]) !== text) return false;
|
|
235
|
+
} else if (!sourceStructureUnchanged(current)) return false;
|
|
236
|
+
pending.push(...directChildren(current));
|
|
237
|
+
}
|
|
238
|
+
return true;
|
|
239
|
+
};
|
|
240
|
+
const canCopyWholeNode = (node) => node.type !== "Document" && node.type !== "InlineItem" && node.type !== "TableKey" && node.type !== "TableArrayKey" && (leafText(node) !== void 0 || sourceStructureUnchanged(node));
|
|
241
|
+
const appendRelativeGap = (from, to, absoluteIndent = false) => {
|
|
242
|
+
if (to.line > from.line) {
|
|
243
|
+
append(format.newLine.repeat(to.line - from.line));
|
|
244
|
+
if (to.column > 0) append(absoluteIndent ? " ".repeat(to.column) : indentation(to.column));
|
|
245
|
+
} else if (to.line === from.line && to.column > from.column) append(" ".repeat(to.column - from.column));
|
|
246
|
+
};
|
|
247
|
+
const sourceGap = (container, previous, current) => {
|
|
248
|
+
var _previous$range, _current$range;
|
|
249
|
+
const source = getNodeSource(container);
|
|
250
|
+
if (!source || !container.range) return void 0;
|
|
251
|
+
if (isInlineArray(container) && isGeneratedNestedTableHost(container) && (!current || previous && !previous.range || current && !current.range)) return void 0;
|
|
252
|
+
if (previous && getNodeSource(previous) !== source) return void 0;
|
|
253
|
+
if (current && getNodeSource(current) !== source) return void 0;
|
|
254
|
+
const start = (previous === null || previous === void 0 || (_previous$range = previous.range) === null || _previous$range === void 0 ? void 0 : _previous$range[1]) ?? container.range[0] + 1;
|
|
255
|
+
const end = (current === null || current === void 0 || (_current$range = current.range) === null || _current$range === void 0 ? void 0 : _current$range[0]) ?? container.range[1] - 1;
|
|
256
|
+
if (start === void 0 || end === void 0 || end < start) return void 0;
|
|
257
|
+
let gap = source.slice(start, end);
|
|
258
|
+
if ((previous === null || previous === void 0 ? void 0 : previous.comma) && gap.startsWith(",")) gap = gap.slice(1);
|
|
259
|
+
if (!/^[\t \r\n]*$/.test(gap)) return void 0;
|
|
260
|
+
return gap;
|
|
261
|
+
};
|
|
262
|
+
const commentBelongsTo = (comment, container) => {
|
|
263
|
+
const source = getNodeSource(container);
|
|
264
|
+
return source !== void 0 && getNodeSource(comment) === source && container.range !== void 0 && comment.range !== void 0 && comment.range[0] >= container.range[0] && comment.range[1] <= container.range[1];
|
|
265
|
+
};
|
|
266
|
+
const emitInlineItems = (container) => {
|
|
267
|
+
const generatedMultilineTable = isInlineTable(container) && isGeneratedNestedTable(container);
|
|
268
|
+
let previous;
|
|
269
|
+
for (const current of container.items) {
|
|
270
|
+
let from = previous ? {
|
|
271
|
+
line: previous.loc.end.line,
|
|
272
|
+
column: previous.loc.end.column + (previous.comma ? 1 : 0)
|
|
273
|
+
} : {
|
|
274
|
+
line: container.loc.start.line,
|
|
275
|
+
column: container.loc.start.column + 1
|
|
276
|
+
};
|
|
277
|
+
const commentsBefore = hostedComments.filter((comment) => !consumedComments.has(comment) && commentBelongsTo(comment, container) && comment.loc.start.line >= from.line && comment.loc.end.line <= current.loc.start.line && (comment.loc.start.line > from.line || comment.loc.start.column >= from.column) && (comment.loc.end.line < current.loc.start.line || comment.loc.end.column <= current.loc.start.column) && comment.loc.start.line >= container.loc.start.line && comment.loc.end.line <= container.loc.end.line).sort((left, right) => left.loc.start.line - right.loc.start.line || left.loc.start.column - right.loc.start.column);
|
|
278
|
+
if (commentsBefore.length > 0) {
|
|
279
|
+
for (const comment of commentsBefore) {
|
|
280
|
+
appendRelativeGap(from, comment.loc.start);
|
|
281
|
+
append(comment.raw);
|
|
282
|
+
consumedComments.add(comment);
|
|
283
|
+
from = comment.loc.end;
|
|
284
|
+
}
|
|
285
|
+
appendRelativeGap(from, current.loc.start);
|
|
286
|
+
} else {
|
|
287
|
+
const gap = sourceGap(container, previous, current);
|
|
288
|
+
if (gap !== void 0) append(gap);
|
|
289
|
+
else {
|
|
290
|
+
const crossSource = getNodeSource(current) !== getNodeSource(container);
|
|
291
|
+
if (previous && crossSource && current.loc.start.line === previous.loc.end.line) {
|
|
292
|
+
const commaGap = getCommaSpace(container) ?? 2;
|
|
293
|
+
const wantedSpaces = Math.max(0, commaGap - 1);
|
|
294
|
+
if (wantedSpaces > 0) append(" ".repeat(wantedSpaces));
|
|
295
|
+
} else appendRelativeGap(from, current.loc.start, generatedMultilineTable);
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
emitNode(current, false);
|
|
299
|
+
for (const comment of hostedComments) {
|
|
300
|
+
if (consumedComments.has(comment)) continue;
|
|
301
|
+
if (!commentBelongsTo(comment, container)) continue;
|
|
302
|
+
if (comment.loc.start.line !== current.loc.end.line) continue;
|
|
303
|
+
if (comment.loc.start.column < current.loc.end.column) continue;
|
|
304
|
+
if (comment.loc.start.line < container.loc.start.line || comment.loc.end.line > container.loc.end.line) continue;
|
|
305
|
+
const separatorEnd = current.loc.end.column + (current.comma ? 1 : 0);
|
|
306
|
+
if (comment.loc.start.column > separatorEnd) append(" ".repeat(comment.loc.start.column - separatorEnd));
|
|
307
|
+
append(comment.raw);
|
|
308
|
+
consumedComments.add(comment);
|
|
309
|
+
}
|
|
310
|
+
previous = current;
|
|
311
|
+
}
|
|
312
|
+
let trailingFrom = previous ? {
|
|
313
|
+
line: previous.loc.end.line,
|
|
314
|
+
column: previous.loc.end.column + (previous.comma ? 1 : 0)
|
|
315
|
+
} : {
|
|
316
|
+
line: container.loc.start.line,
|
|
317
|
+
column: container.loc.start.column + 1
|
|
318
|
+
};
|
|
319
|
+
const trailingComments = hostedComments.filter((comment) => !consumedComments.has(comment) && commentBelongsTo(comment, container) && comment.loc.start.line >= trailingFrom.line && comment.loc.end.line <= container.loc.end.line).sort((left, right) => left.loc.start.line - right.loc.start.line || left.loc.start.column - right.loc.start.column);
|
|
320
|
+
if (trailingComments.length > 0) {
|
|
321
|
+
for (const comment of trailingComments) {
|
|
322
|
+
appendRelativeGap(trailingFrom, comment.loc.start);
|
|
323
|
+
append(comment.raw);
|
|
324
|
+
consumedComments.add(comment);
|
|
325
|
+
trailingFrom = comment.loc.end;
|
|
326
|
+
}
|
|
327
|
+
appendRelativeGap(trailingFrom, {
|
|
328
|
+
line: container.loc.end.line,
|
|
329
|
+
column: container.loc.end.column - 1
|
|
330
|
+
});
|
|
331
|
+
} else {
|
|
332
|
+
const trailingGap = sourceGap(container, previous, void 0);
|
|
333
|
+
if (trailingGap !== void 0) append(trailingGap);
|
|
334
|
+
else if (previous) {
|
|
335
|
+
if (generatedMultilineTable && previous.comma) append(" ");
|
|
336
|
+
appendRelativeGap(trailingFrom, {
|
|
337
|
+
line: container.loc.end.line,
|
|
338
|
+
column: container.loc.end.column - 1
|
|
339
|
+
}, generatedMultilineTable);
|
|
340
|
+
} else {
|
|
341
|
+
const source = getNodeSource(container);
|
|
342
|
+
if (source && container.range) {
|
|
343
|
+
const original = source.slice(container.range[0], container.range[1]);
|
|
344
|
+
const lastNewline = Math.max(original.lastIndexOf("\n"), original.lastIndexOf("\r"));
|
|
345
|
+
const keepClosingRow = blockLevelInlineContainers.has(container) || isPreservedMultilineEmpty(container);
|
|
346
|
+
if (lastNewline !== -1 && keepClosingRow && originalFirstChildStartedAfterOpener(container)) {
|
|
347
|
+
var _original$slice$match;
|
|
348
|
+
const closingIndent = ((_original$slice$match = original.slice(lastNewline + 1, -1).match(/^[\t ]*/)) === null || _original$slice$match === void 0 ? void 0 : _original$slice$match[0]) ?? "";
|
|
349
|
+
append(format.newLine + closingIndent);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
};
|
|
355
|
+
const emitNode = (node, place = true) => {
|
|
356
|
+
if (node.type === "Comment" && consumedComments.has(node)) return;
|
|
357
|
+
const source = getNodeSource(node);
|
|
358
|
+
if (source !== void 0 && node.range && copiedRanges.some((copied) => copied.source === source && copied.start <= node.range[0] && copied.end >= node.range[1])) return;
|
|
359
|
+
if (canCopyWholeNode(node) && sourceSubtreeReusable(node) && node.range && source !== void 0) {
|
|
360
|
+
const slice = source.slice(node.range[0], node.range[1]);
|
|
361
|
+
const expectedLeaf = leafText(node);
|
|
362
|
+
if (expectedLeaf === void 0 || expectedLeaf === slice) {
|
|
363
|
+
if (place) advanceTo(node.loc.start.line, node.loc.start.column);
|
|
364
|
+
append(slice);
|
|
365
|
+
copiedRanges.push({
|
|
366
|
+
source,
|
|
367
|
+
start: node.range[0],
|
|
368
|
+
end: node.range[1]
|
|
369
|
+
});
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
switch (node.type) {
|
|
374
|
+
case "Document":
|
|
375
|
+
for (const item of node.items) emitNode(item);
|
|
376
|
+
break;
|
|
377
|
+
case "Table": {
|
|
378
|
+
const table = node;
|
|
379
|
+
emitNode(table.key, place);
|
|
380
|
+
for (const item of table.items) emitNode(item);
|
|
381
|
+
break;
|
|
382
|
+
}
|
|
383
|
+
case "TableKey": {
|
|
384
|
+
const tableKey = node;
|
|
385
|
+
if (place) advanceTo(tableKey.loc.start.line, tableKey.loc.start.column);
|
|
386
|
+
append(`[${tableKey.item.raw}]`);
|
|
387
|
+
break;
|
|
388
|
+
}
|
|
389
|
+
case "TableArray": {
|
|
390
|
+
const tableArray = node;
|
|
391
|
+
emitNode(tableArray.key, place);
|
|
392
|
+
for (const item of tableArray.items) emitNode(item);
|
|
393
|
+
break;
|
|
394
|
+
}
|
|
395
|
+
case "TableArrayKey": {
|
|
396
|
+
const tableArrayKey = node;
|
|
397
|
+
if (place) advanceTo(tableArrayKey.loc.start.line, tableArrayKey.loc.start.column);
|
|
398
|
+
append(`[[${tableArrayKey.item.raw}]]`);
|
|
399
|
+
break;
|
|
400
|
+
}
|
|
401
|
+
case "KeyValue": {
|
|
402
|
+
const keyValue = node;
|
|
403
|
+
if (place) advanceTo(keyValue.loc.start.line, keyValue.loc.start.column);
|
|
404
|
+
emitNode(keyValue.key, false);
|
|
405
|
+
appendRelativeGap(keyValue.key.loc.end, {
|
|
406
|
+
line: keyValue.loc.start.line,
|
|
407
|
+
column: keyValue.equals
|
|
408
|
+
});
|
|
409
|
+
append("=");
|
|
410
|
+
appendRelativeGap({
|
|
411
|
+
line: keyValue.loc.start.line,
|
|
412
|
+
column: keyValue.equals + 1
|
|
413
|
+
}, keyValue.value.loc.start);
|
|
414
|
+
emitNode(keyValue.value, false);
|
|
415
|
+
break;
|
|
416
|
+
}
|
|
417
|
+
case "Key":
|
|
418
|
+
if (place) advanceTo(node.loc.start.line, node.loc.start.column);
|
|
419
|
+
append(node.raw);
|
|
420
|
+
break;
|
|
421
|
+
case "String":
|
|
422
|
+
if (place) advanceTo(node.loc.start.line, node.loc.start.column);
|
|
423
|
+
append(node.raw);
|
|
424
|
+
break;
|
|
425
|
+
case "Integer":
|
|
426
|
+
if (place) advanceTo(node.loc.start.line, node.loc.start.column);
|
|
427
|
+
append(node.raw);
|
|
428
|
+
break;
|
|
429
|
+
case "Float":
|
|
430
|
+
if (place) advanceTo(node.loc.start.line, node.loc.start.column);
|
|
431
|
+
append(node.raw);
|
|
432
|
+
break;
|
|
433
|
+
case "Boolean":
|
|
434
|
+
if (place) advanceTo(node.loc.start.line, node.loc.start.column);
|
|
435
|
+
append(node.value.toString());
|
|
436
|
+
break;
|
|
437
|
+
case "DateTime":
|
|
438
|
+
if (place) advanceTo(node.loc.start.line, node.loc.start.column);
|
|
439
|
+
append(node.raw);
|
|
440
|
+
break;
|
|
441
|
+
case "InlineArray": {
|
|
442
|
+
const array = node;
|
|
443
|
+
if (place) advanceTo(array.loc.start.line, array.loc.start.column);
|
|
444
|
+
append("[");
|
|
445
|
+
emitInlineItems(array);
|
|
446
|
+
append("]");
|
|
447
|
+
break;
|
|
448
|
+
}
|
|
449
|
+
case "InlineTable": {
|
|
450
|
+
const table = node;
|
|
451
|
+
if (place) advanceTo(table.loc.start.line, table.loc.start.column);
|
|
452
|
+
append("{");
|
|
453
|
+
emitInlineItems(table);
|
|
454
|
+
append("}");
|
|
455
|
+
break;
|
|
456
|
+
}
|
|
457
|
+
case "InlineItem": {
|
|
458
|
+
const item = node;
|
|
459
|
+
emitNode(item.item, false);
|
|
460
|
+
if (item.comma) {
|
|
461
|
+
const source = getNodeSource(item);
|
|
462
|
+
if (source && item.range && item.item.range && getNodeSource(item.item) === source) {
|
|
463
|
+
const beforeComma = source.slice(item.item.range[1], item.range[1]);
|
|
464
|
+
if (/^[\t ]*$/.test(beforeComma)) append(beforeComma);
|
|
465
|
+
}
|
|
466
|
+
append(",");
|
|
467
|
+
}
|
|
468
|
+
break;
|
|
469
|
+
}
|
|
470
|
+
case "Comment":
|
|
471
|
+
if (place) advanceTo(node.loc.start.line, node.loc.start.column);
|
|
472
|
+
append(node.raw);
|
|
473
|
+
break;
|
|
474
|
+
default: throw new Error(`toTOMLCursor: Unrecognized node type: ${String(node.type)}`);
|
|
475
|
+
}
|
|
476
|
+
};
|
|
477
|
+
for (const root of roots) emitNode(root);
|
|
478
|
+
return chunks.join("").replace(/\r\n|\n/g, format.newLine) + format.newLine.repeat(format.trailingNewline);
|
|
479
|
+
}
|
|
480
|
+
/**
|
|
481
|
+
* Sequential emitter for generated CSTs. Generated nodes carry no source and
|
|
482
|
+
* already have consistent positions, so text can be appended in structural
|
|
483
|
+
* order and gaps filled with newlines and indentation instead of painting text
|
|
484
|
+
* at recorded coordinates. Produces byte-identical output to `toTOML` for CSTs
|
|
485
|
+
* produced by `parseJS`.
|
|
486
|
+
*/
|
|
487
|
+
function toTOMLSequential(cst, format) {
|
|
488
|
+
const roots = Array.isArray(cst) ? cst : isIterable(cst) ? Array.from(cst) : [cst];
|
|
489
|
+
const chunks = [];
|
|
490
|
+
let line = 1;
|
|
491
|
+
let column = 0;
|
|
492
|
+
const indentation = (width) => (format.useTabsForIndentation ? " " : " ").repeat(width);
|
|
493
|
+
const append = (text) => {
|
|
494
|
+
if (text.length === 0) return;
|
|
495
|
+
chunks.push(text);
|
|
496
|
+
let count = 0;
|
|
497
|
+
let lastIndex = -1;
|
|
498
|
+
let index = text.indexOf("\n");
|
|
499
|
+
while (index !== -1) {
|
|
500
|
+
count++;
|
|
501
|
+
lastIndex = index;
|
|
502
|
+
index = text.indexOf("\n", index + 1);
|
|
503
|
+
}
|
|
504
|
+
if (count === 0) {
|
|
505
|
+
column += text.length;
|
|
506
|
+
return;
|
|
507
|
+
}
|
|
508
|
+
line += count;
|
|
509
|
+
column = text.length - lastIndex - 1;
|
|
510
|
+
};
|
|
511
|
+
const advanceTo = (targetLine, targetColumn) => {
|
|
512
|
+
if (targetLine > line) {
|
|
513
|
+
append(format.newLine.repeat(targetLine - line));
|
|
514
|
+
if (targetColumn > 0) append(indentation(targetColumn));
|
|
515
|
+
} else if (targetLine === line && targetColumn > column) append(column === 0 ? indentation(targetColumn) : " ".repeat(targetColumn - column));
|
|
516
|
+
else if (chunks.length > 0 && (targetLine < line || targetLine === line && targetColumn < column)) {
|
|
517
|
+
append(format.newLine);
|
|
518
|
+
if (targetColumn > 0) append(indentation(targetColumn));
|
|
519
|
+
}
|
|
520
|
+
};
|
|
521
|
+
const leafRaw = (node) => {
|
|
522
|
+
switch (node.type) {
|
|
523
|
+
case "Key": return node.raw;
|
|
524
|
+
case "String": return node.raw;
|
|
525
|
+
case "Integer": return node.raw;
|
|
526
|
+
case "Float": return node.raw;
|
|
527
|
+
case "Boolean": return node.value.toString();
|
|
528
|
+
case "DateTime": return node.raw;
|
|
529
|
+
case "Comment": return node.raw;
|
|
530
|
+
default: return "";
|
|
531
|
+
}
|
|
532
|
+
};
|
|
533
|
+
const stack = [];
|
|
534
|
+
const pushNode = (node, place) => {
|
|
535
|
+
switch (node.type) {
|
|
536
|
+
case "Document": {
|
|
537
|
+
const items = node.items;
|
|
538
|
+
for (let i = items.length - 1; i >= 0; i--) stack.push([
|
|
539
|
+
0,
|
|
540
|
+
items[i],
|
|
541
|
+
true
|
|
542
|
+
]);
|
|
543
|
+
break;
|
|
544
|
+
}
|
|
545
|
+
case "Table": {
|
|
546
|
+
const table = node;
|
|
547
|
+
const items = table.items;
|
|
548
|
+
for (let i = items.length - 1; i >= 0; i--) stack.push([
|
|
549
|
+
0,
|
|
550
|
+
items[i],
|
|
551
|
+
true
|
|
552
|
+
]);
|
|
553
|
+
stack.push([
|
|
554
|
+
0,
|
|
555
|
+
table.key,
|
|
556
|
+
place
|
|
557
|
+
]);
|
|
558
|
+
break;
|
|
559
|
+
}
|
|
560
|
+
case "TableKey": {
|
|
561
|
+
const tableKey = node;
|
|
562
|
+
stack.push([1, `[${tableKey.item.raw}]`]);
|
|
563
|
+
if (place) stack.push([
|
|
564
|
+
3,
|
|
565
|
+
tableKey.loc.start.line,
|
|
566
|
+
tableKey.loc.start.column
|
|
567
|
+
]);
|
|
568
|
+
break;
|
|
569
|
+
}
|
|
570
|
+
case "TableArray": {
|
|
571
|
+
const tableArray = node;
|
|
572
|
+
const items = tableArray.items;
|
|
573
|
+
for (let i = items.length - 1; i >= 0; i--) stack.push([
|
|
574
|
+
0,
|
|
575
|
+
items[i],
|
|
576
|
+
true
|
|
577
|
+
]);
|
|
578
|
+
stack.push([
|
|
579
|
+
0,
|
|
580
|
+
tableArray.key,
|
|
581
|
+
place
|
|
582
|
+
]);
|
|
583
|
+
break;
|
|
584
|
+
}
|
|
585
|
+
case "TableArrayKey": {
|
|
586
|
+
const tableArrayKey = node;
|
|
587
|
+
stack.push([1, `[[${tableArrayKey.item.raw}]]`]);
|
|
588
|
+
if (place) stack.push([
|
|
589
|
+
3,
|
|
590
|
+
tableArrayKey.loc.start.line,
|
|
591
|
+
tableArrayKey.loc.start.column
|
|
592
|
+
]);
|
|
593
|
+
break;
|
|
594
|
+
}
|
|
595
|
+
case "KeyValue": {
|
|
596
|
+
const keyValue = node;
|
|
597
|
+
stack.push([
|
|
598
|
+
0,
|
|
599
|
+
keyValue.value,
|
|
600
|
+
false
|
|
601
|
+
]);
|
|
602
|
+
stack.push([
|
|
603
|
+
2,
|
|
604
|
+
keyValue.loc.start.line,
|
|
605
|
+
keyValue.equals + 1,
|
|
606
|
+
keyValue.value.loc.start.line,
|
|
607
|
+
keyValue.value.loc.start.column,
|
|
608
|
+
false
|
|
609
|
+
]);
|
|
610
|
+
stack.push([1, "="]);
|
|
611
|
+
stack.push([
|
|
612
|
+
2,
|
|
613
|
+
keyValue.key.loc.end.line,
|
|
614
|
+
keyValue.key.loc.end.column,
|
|
615
|
+
keyValue.loc.start.line,
|
|
616
|
+
keyValue.equals,
|
|
617
|
+
false
|
|
618
|
+
]);
|
|
619
|
+
stack.push([
|
|
620
|
+
0,
|
|
621
|
+
keyValue.key,
|
|
622
|
+
false
|
|
623
|
+
]);
|
|
624
|
+
if (place) stack.push([
|
|
625
|
+
3,
|
|
626
|
+
keyValue.loc.start.line,
|
|
627
|
+
keyValue.loc.start.column
|
|
628
|
+
]);
|
|
629
|
+
break;
|
|
630
|
+
}
|
|
631
|
+
case "Key":
|
|
632
|
+
case "String":
|
|
633
|
+
case "Integer":
|
|
634
|
+
case "Float":
|
|
635
|
+
case "Boolean":
|
|
636
|
+
case "DateTime":
|
|
637
|
+
case "Comment":
|
|
638
|
+
stack.push([1, leafRaw(node)]);
|
|
639
|
+
if (place) stack.push([
|
|
640
|
+
3,
|
|
641
|
+
node.loc.start.line,
|
|
642
|
+
node.loc.start.column
|
|
643
|
+
]);
|
|
644
|
+
break;
|
|
645
|
+
case "InlineArray": {
|
|
646
|
+
const array = node;
|
|
647
|
+
const items = array.items;
|
|
648
|
+
stack.push([1, "]"]);
|
|
649
|
+
if (items.length > 0) {
|
|
650
|
+
const last = items[items.length - 1];
|
|
651
|
+
stack.push([
|
|
652
|
+
2,
|
|
653
|
+
last.loc.end.line,
|
|
654
|
+
last.loc.end.column + (last.comma ? 1 : 0),
|
|
655
|
+
array.loc.end.line,
|
|
656
|
+
array.loc.end.column - 1,
|
|
657
|
+
false
|
|
658
|
+
]);
|
|
659
|
+
}
|
|
660
|
+
for (let i = items.length - 1; i >= 0; i--) {
|
|
661
|
+
const current = items[i];
|
|
662
|
+
const previous = i > 0 ? items[i - 1] : void 0;
|
|
663
|
+
stack.push([
|
|
664
|
+
0,
|
|
665
|
+
current,
|
|
666
|
+
false
|
|
667
|
+
]);
|
|
668
|
+
stack.push(previous ? [
|
|
669
|
+
2,
|
|
670
|
+
previous.loc.end.line,
|
|
671
|
+
previous.loc.end.column + (previous.comma ? 1 : 0),
|
|
672
|
+
current.loc.start.line,
|
|
673
|
+
current.loc.start.column,
|
|
674
|
+
false
|
|
675
|
+
] : [
|
|
676
|
+
2,
|
|
677
|
+
array.loc.start.line,
|
|
678
|
+
array.loc.start.column + 1,
|
|
679
|
+
current.loc.start.line,
|
|
680
|
+
current.loc.start.column,
|
|
681
|
+
false
|
|
682
|
+
]);
|
|
683
|
+
}
|
|
684
|
+
stack.push([1, "["]);
|
|
685
|
+
if (place) stack.push([
|
|
686
|
+
3,
|
|
687
|
+
array.loc.start.line,
|
|
688
|
+
array.loc.start.column
|
|
689
|
+
]);
|
|
690
|
+
break;
|
|
691
|
+
}
|
|
692
|
+
case "InlineTable": {
|
|
693
|
+
const table = node;
|
|
694
|
+
const items = table.items;
|
|
695
|
+
const generatedMultilineTable = isGeneratedNestedTable(table);
|
|
696
|
+
stack.push([1, "}"]);
|
|
697
|
+
if (items.length > 0) {
|
|
698
|
+
const last = items[items.length - 1];
|
|
699
|
+
if (generatedMultilineTable && last.comma) stack.push([1, " "]);
|
|
700
|
+
stack.push([
|
|
701
|
+
2,
|
|
702
|
+
last.loc.end.line,
|
|
703
|
+
last.loc.end.column + (last.comma ? 1 : 0),
|
|
704
|
+
table.loc.end.line,
|
|
705
|
+
table.loc.end.column - 1,
|
|
706
|
+
generatedMultilineTable
|
|
707
|
+
]);
|
|
708
|
+
}
|
|
709
|
+
for (let i = items.length - 1; i >= 0; i--) {
|
|
710
|
+
const current = items[i];
|
|
711
|
+
const previous = i > 0 ? items[i - 1] : void 0;
|
|
712
|
+
stack.push([
|
|
713
|
+
0,
|
|
714
|
+
current,
|
|
715
|
+
false
|
|
716
|
+
]);
|
|
717
|
+
stack.push(previous ? [
|
|
718
|
+
2,
|
|
719
|
+
previous.loc.end.line,
|
|
720
|
+
previous.loc.end.column + (previous.comma ? 1 : 0),
|
|
721
|
+
current.loc.start.line,
|
|
722
|
+
current.loc.start.column,
|
|
723
|
+
generatedMultilineTable
|
|
724
|
+
] : [
|
|
725
|
+
2,
|
|
726
|
+
table.loc.start.line,
|
|
727
|
+
table.loc.start.column + 1,
|
|
728
|
+
current.loc.start.line,
|
|
729
|
+
current.loc.start.column,
|
|
730
|
+
generatedMultilineTable
|
|
731
|
+
]);
|
|
732
|
+
}
|
|
733
|
+
stack.push([1, "{"]);
|
|
734
|
+
if (place) stack.push([
|
|
735
|
+
3,
|
|
736
|
+
table.loc.start.line,
|
|
737
|
+
table.loc.start.column
|
|
738
|
+
]);
|
|
739
|
+
break;
|
|
740
|
+
}
|
|
741
|
+
case "InlineItem": {
|
|
742
|
+
const item = node;
|
|
743
|
+
if (item.comma) stack.push([1, ","]);
|
|
744
|
+
stack.push([
|
|
745
|
+
0,
|
|
746
|
+
item.item,
|
|
747
|
+
false
|
|
748
|
+
]);
|
|
749
|
+
break;
|
|
750
|
+
}
|
|
751
|
+
default: throw new Error(`toTOMLSequential: Unrecognized node type: ${String(node.type)}`);
|
|
752
|
+
}
|
|
753
|
+
};
|
|
754
|
+
for (let i = roots.length - 1; i >= 0; i--) stack.push([
|
|
755
|
+
0,
|
|
756
|
+
roots[i],
|
|
757
|
+
true
|
|
758
|
+
]);
|
|
759
|
+
while (stack.length > 0) {
|
|
760
|
+
const command = stack.pop();
|
|
761
|
+
switch (command[0]) {
|
|
762
|
+
case 0:
|
|
763
|
+
pushNode(command[1], command[2]);
|
|
764
|
+
break;
|
|
765
|
+
case 1:
|
|
766
|
+
append(command[1]);
|
|
767
|
+
break;
|
|
768
|
+
case 2: {
|
|
769
|
+
const fromLine = command[1];
|
|
770
|
+
const fromColumn = command[2];
|
|
771
|
+
const toLine = command[3];
|
|
772
|
+
const toColumn = command[4];
|
|
773
|
+
const absoluteIndent = command[5];
|
|
774
|
+
if (toLine > fromLine) {
|
|
775
|
+
append(format.newLine.repeat(toLine - fromLine));
|
|
776
|
+
if (toColumn > 0) append(absoluteIndent ? " ".repeat(toColumn) : indentation(toColumn));
|
|
777
|
+
} else if (toLine === fromLine && toColumn > fromColumn) append(" ".repeat(toColumn - fromColumn));
|
|
778
|
+
break;
|
|
779
|
+
}
|
|
780
|
+
case 3: advanceTo(command[1], command[2]);
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
return chunks.join("").replace(/\r\n|\n/g, format.newLine) + format.newLine.repeat(format.trailingNewline);
|
|
784
|
+
}
|
|
785
|
+
/**
|
|
786
|
+
* Writes raw string content to specific location coordinates within a lines array.
|
|
787
|
+
*
|
|
788
|
+
* This function is responsible for placing TOML content at precise positions within
|
|
789
|
+
* the output lines, handling multi-line content and preserving existing content
|
|
790
|
+
* around the target location.
|
|
791
|
+
*
|
|
792
|
+
* @param lines - Array of string lines representing the TOML document being built.
|
|
793
|
+
* Lines are 1-indexed but stored in 0-indexed array.
|
|
794
|
+
* @param loc - Location object specifying where to write the content, containing:
|
|
795
|
+
* - start: { line: number, column: number } - Starting position (1-indexed line, 0-indexed column)
|
|
796
|
+
* - end: { line: number, column: number } - Ending position (1-indexed line, 0-indexed column)
|
|
797
|
+
* @param raw - The raw string content to write at the specified location.
|
|
798
|
+
* Can contain multiple lines separated by \n or \r\n.
|
|
799
|
+
*
|
|
800
|
+
* @throws {Error} When there's a mismatch between location span and raw string line count
|
|
801
|
+
* @throws {Error} When attempting to write to an uninitialized line
|
|
802
|
+
*
|
|
803
|
+
* @example
|
|
804
|
+
* ```typescript
|
|
805
|
+
* const lines = ['', ''];
|
|
806
|
+
* const location = { start: { line: 1, column: 0 }, end: { line: 1, column: 3 } };
|
|
807
|
+
* write(lines, location, 'key');
|
|
808
|
+
* // Result: lines[0] becomes 'key'
|
|
809
|
+
* ```
|
|
810
|
+
*/
|
|
811
|
+
function write(lines, loc, raw) {
|
|
812
|
+
if (loc.start.line === loc.end.line) {
|
|
813
|
+
const line = getLine(lines, loc.start.line);
|
|
814
|
+
const existingBefore = line.substring(0, loc.start.column);
|
|
815
|
+
const before = existingBefore.length < loc.start.column ? existingBefore.padEnd(loc.start.column, " ") : existingBefore;
|
|
816
|
+
const after = line.substring(loc.end.column);
|
|
817
|
+
lines[loc.start.line - 1] = before + raw + after;
|
|
818
|
+
return;
|
|
819
|
+
}
|
|
820
|
+
const raw_lines = raw.split(BY_NEW_LINE).filter((line) => line !== "\n" && line !== "\r\n");
|
|
821
|
+
const expected_lines = loc.end.line - loc.start.line + 1;
|
|
822
|
+
if (raw_lines.length !== expected_lines) throw new Error(`Mismatch between location and raw string, expected ${expected_lines} lines for "${raw}"`);
|
|
823
|
+
for (let i = loc.start.line; i <= loc.end.line; i++) {
|
|
824
|
+
const line = getLine(lines, i);
|
|
825
|
+
const is_start_line = i === loc.start.line;
|
|
826
|
+
const is_end_line = i === loc.end.line;
|
|
827
|
+
let before = "";
|
|
828
|
+
if (is_start_line) {
|
|
829
|
+
const existingBefore = line.substring(0, loc.start.column);
|
|
830
|
+
if (existingBefore.length < loc.start.column) before = existingBefore.padEnd(loc.start.column, " ");
|
|
831
|
+
else before = existingBefore;
|
|
832
|
+
}
|
|
833
|
+
const after = is_end_line ? line.substring(loc.end.column) : "";
|
|
834
|
+
lines[i - 1] = before + raw_lines[i - loc.start.line] + after;
|
|
835
|
+
}
|
|
836
|
+
}
|
|
837
|
+
/**
|
|
838
|
+
* Fast path for writing a single character at a specific position.
|
|
839
|
+
* Avoids creating temporary Location objects.
|
|
840
|
+
*/
|
|
841
|
+
function writeSingle(lines, lineNum, column, char) {
|
|
842
|
+
const line = getLine(lines, lineNum);
|
|
843
|
+
const before = line.length < column ? line.padEnd(column, " ") : line.substring(0, column);
|
|
844
|
+
const after = line.substring(column + 1);
|
|
845
|
+
lines[lineNum - 1] = before + char + after;
|
|
846
|
+
}
|
|
847
|
+
/**
|
|
848
|
+
* Safely retrieves a line from the lines array, initializing empty lines as needed.
|
|
849
|
+
*
|
|
850
|
+
* This helper function handles the conversion between 1-indexed line numbers (used in locations)
|
|
851
|
+
* and 0-indexed array positions. It ensures that accessing a line that doesn't exist yet
|
|
852
|
+
* will initialize all preceding lines with empty strings.
|
|
853
|
+
*
|
|
854
|
+
* @param lines - Array of string lines representing the document
|
|
855
|
+
* @param index - 1-indexed line number to retrieve
|
|
856
|
+
* @returns The line content as a string, or empty string for new lines
|
|
857
|
+
*
|
|
858
|
+
* @example
|
|
859
|
+
* ```typescript
|
|
860
|
+
* const lines = ['first line'];
|
|
861
|
+
* const line = getLine(lines, 3); // Initializes lines[1] and lines[2] as empty strings
|
|
862
|
+
* // lines becomes ['first line', '', '']
|
|
863
|
+
* ```
|
|
864
|
+
*/
|
|
865
|
+
function getLine(lines, index) {
|
|
866
|
+
if (!lines[index - 1]) {
|
|
867
|
+
for (let i = 0; i < index; i++) if (!lines[i]) lines[i] = "";
|
|
868
|
+
}
|
|
869
|
+
return lines[index - 1];
|
|
870
|
+
}
|
|
871
|
+
|
|
872
|
+
//#endregion
|
|
873
|
+
export { toTOML as default, toTOMLCursor, toTOMLSequential };
|
|
874
|
+
//# sourceMappingURL=to-toml.js.map
|