@decimalturn/toml-patch 3.3.2-dev → 3.3.2
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/LICENSE +22 -0
- package/README.md +203 -0
- package/dist/toml-patch.js +84 -0
- package/package.json +103 -16
- package/dist/dev/comment-alignment.js +0 -372
- package/dist/dev/comment-alignment.js.map +0 -1
- package/dist/dev/comment-ownership.js +0 -673
- package/dist/dev/comment-ownership.js.map +0 -1
- package/dist/dev/cst-source.js +0 -118
- package/dist/dev/cst-source.js.map +0 -1
- package/dist/dev/cst.js +0 -68
- package/dist/dev/cst.js.map +0 -1
- package/dist/dev/cursor.js +0 -66
- package/dist/dev/cursor.js.map +0 -1
- package/dist/dev/date-format.js +0 -287
- package/dist/dev/date-format.js.map +0 -1
- package/dist/dev/decode-utf8.js +0 -27
- package/dist/dev/decode-utf8.js.map +0 -1
- package/dist/dev/diff-lite.js +0 -108
- package/dist/dev/diff-lite.js.map +0 -1
- package/dist/dev/diff.js +0 -265
- package/dist/dev/diff.js.map +0 -1
- package/dist/dev/encode-lite.js +0 -210
- package/dist/dev/encode-lite.js.map +0 -1
- package/dist/dev/escape-preference.js +0 -166
- package/dist/dev/escape-preference.js.map +0 -1
- package/dist/dev/find-by-path.js +0 -115
- package/dist/dev/find-by-path.js.map +0 -1
- package/dist/dev/formatter.js +0 -298
- package/dist/dev/formatter.js.map +0 -1
- package/dist/dev/generate.js +0 -480
- package/dist/dev/generate.js.map +0 -1
- package/dist/dev/inline-comma-space.js +0 -13
- package/dist/dev/inline-comma-space.js.map +0 -1
- package/dist/dev/inline-format.js +0 -106
- package/dist/dev/inline-format.js.map +0 -1
- package/dist/dev/inline-layout.js +0 -107
- package/dist/dev/inline-layout.js.map +0 -1
- package/dist/dev/line-ending-backslash.js +0 -320
- package/dist/dev/line-ending-backslash.js.map +0 -1
- package/dist/dev/literal-string.js +0 -46
- package/dist/dev/literal-string.js.map +0 -1
- package/dist/dev/location.js +0 -104
- package/dist/dev/location.js.map +0 -1
- package/dist/dev/parse-error.js +0 -27
- package/dist/dev/parse-error.js.map +0 -1
- package/dist/dev/parse-js.js +0 -150
- package/dist/dev/parse-js.js.map +0 -1
- package/dist/dev/parse-string.js +0 -120
- package/dist/dev/parse-string.js.map +0 -1
- package/dist/dev/parse-toml.js +0 -714
- package/dist/dev/parse-toml.js.map +0 -1
- package/dist/dev/patch-lite.js +0 -4
- package/dist/dev/patch-lite2.js +0 -97
- package/dist/dev/patch-lite2.js.map +0 -1
- package/dist/dev/patch.js +0 -3128
- package/dist/dev/patch.js.map +0 -1
- package/dist/dev/smol-toml-date.js +0 -61
- package/dist/dev/smol-toml-date.js.map +0 -1
- package/dist/dev/to-js.js +0 -308
- package/dist/dev/to-js.js.map +0 -1
- package/dist/dev/to-toml.js +0 -874
- package/dist/dev/to-toml.js.map +0 -1
- package/dist/dev/tokenizer.js +0 -225
- package/dist/dev/tokenizer.js.map +0 -1
- package/dist/dev/toml-document.js +0 -221
- package/dist/dev/toml-document.js.map +0 -1
- package/dist/dev/toml-format.js +0 -549
- package/dist/dev/toml-format.js.map +0 -1
- package/dist/dev/toml-patch.js +0 -94
- package/dist/dev/toml-patch.js.map +0 -1
- package/dist/dev/traverse.js +0 -61
- package/dist/dev/traverse.js.map +0 -1
- package/dist/dev/truncate.js +0 -64
- package/dist/dev/truncate.js.map +0 -1
- package/dist/dev/update-order.js +0 -235
- package/dist/dev/update-order.js.map +0 -1
- package/dist/dev/utils.js +0 -296
- package/dist/dev/utils.js.map +0 -1
- package/dist/dev/writer.js +0 -758
- package/dist/dev/writer.js.map +0 -1
- package/dist/patch-lite.d.ts +0 -18
package/dist/dev/patch.js
DELETED
|
@@ -1,3128 +0,0 @@
|
|
|
1
|
-
//! @decimalturn/toml-patch v3.3.2 - https://github.com/DecimalTurn/toml-patch - @license: MIT
|
|
2
|
-
import { hasItem, hasItems, isComment, isDateTime, isDocument, isFloat, isInlineArray, isInlineItem, isInlineTable, isInteger, isKeyValue, isString, isTable, isTableArray } from "./cst.js";
|
|
3
|
-
import { getSpan } from "./location.js";
|
|
4
|
-
import { IS_BARE_KEY, createNewlineScanState } from "./tokenizer.js";
|
|
5
|
-
import { DateFormatHelper, padFractionalSeconds } from "./date-format.js";
|
|
6
|
-
import parseTOML from "./parse-toml.js";
|
|
7
|
-
import { arraysEqual, datesEqual, hasTemporal, isDate, isInteger as isInteger$1, isObject, isTemporal, last, stableStringify, temporalToTomlString } from "./utils.js";
|
|
8
|
-
import traverse from "./traverse.js";
|
|
9
|
-
import { stripLeadingBom } from "./decode-utf8.js";
|
|
10
|
-
import { TomlFormat, resolveTomlFormat } from "./toml-format.js";
|
|
11
|
-
import { attachSources } from "./cst-source.js";
|
|
12
|
-
import { findInlineContainerDepth, findInlineContainerParent, hasStructuralMultilineRows, resolveInlineContainerLayout } from "./inline-format.js";
|
|
13
|
-
import { addExitOffset, applyBracketSpacing, applyWrites, deleteInlineContainerNeedingTighten, getExitOffsets, getPendingEnterOffsets, hasInlineContainerNeedingTighten, insert, markDirty, perLine, recalcContainerEnd, remove, replace, setInlineIndentColumn, setRootIndentWidth, shiftNode } from "./writer.js";
|
|
14
|
-
import { escapeStringContent } from "./escape-preference.js";
|
|
15
|
-
import { generateComment, generateInlineItem, generateKey, generateKeyValue, generateString, generateTable, generateTableArray } from "./generate.js";
|
|
16
|
-
import { arrayHadTrailingCommas, calculateTableDepth, normalizeGeneratedInlineContainerRows, normalizeInlineContainerRows, postInlineItemRemovalAdjustment, tableHadTrailingCommas } from "./formatter.js";
|
|
17
|
-
import { positionGeneratedNestedInlineTables, prepareInsertedNestedInlineContainer } from "./inline-layout.js";
|
|
18
|
-
import parseJS from "./parse-js.js";
|
|
19
|
-
import toTOML, { toTOMLCursor } from "./to-toml.js";
|
|
20
|
-
import toJS from "./to-js.js";
|
|
21
|
-
import diff, { isAdd, isEdit, isMove, isRemove, isRename } from "./diff.js";
|
|
22
|
-
import findByPath, { findParent, tryFindByPath } from "./find-by-path.js";
|
|
23
|
-
import { findHostContainer, moveInlineElement, removeMember, resolveGroups } from "./comment-ownership.js";
|
|
24
|
-
import { applyKeyOrderMoves } from "./update-order.js";
|
|
25
|
-
import { getInlineInsertColumnDelta, normalizeInlineCommentAlignmentInString, preserveAlignedInlineCommentColumn, preserveAlignedInlineCommentForDelta, recordInlineTableCommentDelta } from "./comment-alignment.js";
|
|
26
|
-
|
|
27
|
-
//#region src/patch.ts
|
|
28
|
-
/**
|
|
29
|
-
* Applies modifications to a TOML document by comparing an existing TOML string with updated JavaScript data.
|
|
30
|
-
*
|
|
31
|
-
* This function preserves formatting and comments from the existing TOML document while
|
|
32
|
-
* applying changes from the updated data structure. It performs a diff between the existing
|
|
33
|
-
* and updated data, then strategically applies only the necessary changes to maintain the
|
|
34
|
-
* original document structure as much as possible.
|
|
35
|
-
*
|
|
36
|
-
* @param existing - The original TOML document as a string
|
|
37
|
-
* @param updated - The updated JavaScript object with desired changes
|
|
38
|
-
* @param format - Optional formatting options to apply to new or modified sections
|
|
39
|
-
* @returns A new TOML string with the changes applied
|
|
40
|
-
*/
|
|
41
|
-
function patch(existing, updated, format) {
|
|
42
|
-
const newlineState = createNewlineScanState();
|
|
43
|
-
const existingToml = stripLeadingBom(existing);
|
|
44
|
-
const existing_cst = Array.from(parseTOML(existingToml, newlineState));
|
|
45
|
-
attachSources(existing_cst, existingToml);
|
|
46
|
-
const autoDetectedFormat = TomlFormat.autoDetectFormatWithCst(existing, existing_cst);
|
|
47
|
-
const fmt = resolveTomlFormat(format, autoDetectedFormat);
|
|
48
|
-
if (newlineState.mixed) {
|
|
49
|
-
const normalized = fmt.newLine === "\r\n" ? "CRLF" : "LF";
|
|
50
|
-
console.warn(`toml-patch: Mixed line endings detected. Line endings in the output will be normalized to ${normalized}`);
|
|
51
|
-
}
|
|
52
|
-
const patchedToml = patchCst(existing_cst, updated, fmt, format instanceof TomlFormat || format != null && Object.prototype.hasOwnProperty.call(format, "indentWidth"), format instanceof TomlFormat || format != null && Object.prototype.hasOwnProperty.call(format, "trailingComma"), format instanceof TomlFormat || format != null && Object.prototype.hasOwnProperty.call(format, "inlineTableStart")).tomlString;
|
|
53
|
-
const withBom = (toml) => fmt.leadingBom ? `${""}${toml}` : toml;
|
|
54
|
-
return withBom(patchedToml);
|
|
55
|
-
}
|
|
56
|
-
/**
|
|
57
|
-
* Length of the existing dotted key's prefix that the edit path matches.
|
|
58
|
-
* Only the path's RELATIVE tail (past the key's container) is eligible:
|
|
59
|
-
* the absolute path carries the container's own key segments (e.g. the
|
|
60
|
-
* [mbe.""] header consumes ['mbe',''] before the row key), and matching
|
|
61
|
-
* against those keeps extra key segments when the container's key echoes
|
|
62
|
-
* the row's — editing `mbe[""][""]` once truncated the row
|
|
63
|
-
* `"".""."mfn31vru"` to `"".""` instead of `""` (fuzz seed 13057).
|
|
64
|
-
*/
|
|
65
|
-
function truncationMatchLen(changePath, existingKey, containerAbsLen) {
|
|
66
|
-
const relPath = changePath.slice(containerAbsLen);
|
|
67
|
-
let matchLen = 0;
|
|
68
|
-
const max = Math.min(relPath.length, existingKey.length);
|
|
69
|
-
for (let i = 1; i <= max; i++) if (arraysEqual(relPath.slice(relPath.length - i), existingKey.slice(0, i))) matchLen = i;
|
|
70
|
-
return matchLen;
|
|
71
|
-
}
|
|
72
|
-
function inlineTableAddIndex(parent, child, beforeKey) {
|
|
73
|
-
if (beforeKey === void 0) return void 0;
|
|
74
|
-
const childKey = child.key.value;
|
|
75
|
-
if (childKey.length < 2) return void 0;
|
|
76
|
-
const prefix = childKey.slice(0, -1);
|
|
77
|
-
for (let index = 0; index < parent.items.length; index++) {
|
|
78
|
-
const item = parent.items[index];
|
|
79
|
-
if (!isInlineItem(item) || !isKeyValue(item.item)) continue;
|
|
80
|
-
const existingKey = item.item.key.value;
|
|
81
|
-
if (existingKey.length === childKey.length && arraysEqual(existingKey.slice(0, -1), prefix) && existingKey[existingKey.length - 1] === beforeKey) return index;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
/** Every node currently in `document`, seeding updateOrder's isEligibleForLeading guard. */
|
|
85
|
-
function collectPrePatchNodes(document) {
|
|
86
|
-
const nodes = /* @__PURE__ */ new WeakSet();
|
|
87
|
-
const visit = (node) => {
|
|
88
|
-
nodes.add(node);
|
|
89
|
-
};
|
|
90
|
-
traverse(document, {
|
|
91
|
-
Document: visit,
|
|
92
|
-
Table: visit,
|
|
93
|
-
TableKey: visit,
|
|
94
|
-
TableArray: visit,
|
|
95
|
-
TableArrayKey: visit,
|
|
96
|
-
KeyValue: visit,
|
|
97
|
-
Key: visit,
|
|
98
|
-
String: visit,
|
|
99
|
-
Integer: visit,
|
|
100
|
-
Float: visit,
|
|
101
|
-
Boolean: visit,
|
|
102
|
-
DateTime: visit,
|
|
103
|
-
InlineArray: visit,
|
|
104
|
-
InlineItem: visit,
|
|
105
|
-
InlineTable: visit,
|
|
106
|
-
Comment: visit
|
|
107
|
-
});
|
|
108
|
-
return nodes;
|
|
109
|
-
}
|
|
110
|
-
/**
|
|
111
|
-
* updateOrder needs `updated_js`'s TOP-level key order to reflect exactly what the caller
|
|
112
|
-
* requested — but parseJS -> formatTopLevel unconditionally hoists any inline-table/AOT-shaped
|
|
113
|
-
* root key into its own [section]/[[array]] block via remove-then-APPEND (src/formatter.ts).
|
|
114
|
-
* That's a genuine TOML requirement (root scalars must precede section headers) when the
|
|
115
|
-
* value becomes an actual section, but formatTopLevel applies it to ANY nested object at the
|
|
116
|
-
* default inlineTableStart, even when the existing document represents it as a plain inline
|
|
117
|
-
* value with no such constraint — silently reordering it after every scalar key, regardless
|
|
118
|
-
* of the caller's request or updateOrder.
|
|
119
|
-
*
|
|
120
|
-
* Fixes this by re-deriving the root key order from a throwaway, un-hoisted parse
|
|
121
|
-
* (inlineTableStart: 0 disables the section conversion entirely) and re-keying `updated_js`'s
|
|
122
|
-
* top level to match. Only the top-level key ORDER is affected — values, nested levels, and
|
|
123
|
-
* the actual output format of newly-added content (still driven by `format`/`diffing_fmt`,
|
|
124
|
-
* unrelated to this) are untouched. Only called when `format.updateOrder` is set, so it costs
|
|
125
|
-
* nothing when the option is off.
|
|
126
|
-
*/
|
|
127
|
-
function applyRequestedRootKeyOrder(updated, updated_js, diffing_fmt, useTemporal) {
|
|
128
|
-
if (!isObject(updated_js)) return updated_js;
|
|
129
|
-
const unhoisted_fmt = resolveTomlFormat({
|
|
130
|
-
...diffing_fmt,
|
|
131
|
-
inlineTableStart: 0
|
|
132
|
-
}, diffing_fmt);
|
|
133
|
-
const unhoisted_document = parseJS(updated, unhoisted_fmt);
|
|
134
|
-
const unhoisted_js = toJS(unhoisted_document.items, "", { temporal: useTemporal });
|
|
135
|
-
if (!isObject(unhoisted_js)) return updated_js;
|
|
136
|
-
const reordered = {};
|
|
137
|
-
for (const key of Object.keys(unhoisted_js)) if (Object.prototype.hasOwnProperty.call(updated_js, key)) reordered[key] = updated_js[key];
|
|
138
|
-
for (const key of Object.keys(updated_js)) if (!Object.prototype.hasOwnProperty.call(reordered, key)) reordered[key] = updated_js[key];
|
|
139
|
-
return reordered;
|
|
140
|
-
}
|
|
141
|
-
/**
|
|
142
|
-
* The parser's table-body loop consumes comments between consecutive [[x]] entries
|
|
143
|
-
* as trailing children of the first entry (it sees `# comment` before the next
|
|
144
|
-
* `[[x]]` header). This function promotes those trailing comments to Document-level
|
|
145
|
-
* siblings so that resolveGroups can assign them to the correct entry.
|
|
146
|
-
*/
|
|
147
|
-
function normalizeAotEntryComments(doc, ancestorBoundariesOnly = false) {
|
|
148
|
-
const items = doc.items;
|
|
149
|
-
for (let i = 0; i < items.length; i++) {
|
|
150
|
-
const item = items[i];
|
|
151
|
-
if (!((isTable(item) || isTableArray(item)) && hasItems(item))) continue;
|
|
152
|
-
const containerItems = item.items;
|
|
153
|
-
let commentStart = containerItems.length;
|
|
154
|
-
while (commentStart > 0 && isComment(containerItems[commentStart - 1])) commentStart--;
|
|
155
|
-
if (commentStart < containerItems.length) {
|
|
156
|
-
const nextItem = items[i + 1];
|
|
157
|
-
if (nextItem && isTableArray(nextItem)) {
|
|
158
|
-
const thisKey = item.key.item.value;
|
|
159
|
-
const nextKey = nextItem.key.item.value;
|
|
160
|
-
const isDescendantBoundary = arraysEqual(thisKey, nextKey.slice(0, thisKey.length));
|
|
161
|
-
const isAncestorBoundary = arraysEqual(nextKey, thisKey.slice(0, nextKey.length));
|
|
162
|
-
if (ancestorBoundariesOnly ? isAncestorBoundary : isDescendantBoundary) {
|
|
163
|
-
const comments = containerItems.splice(commentStart);
|
|
164
|
-
const nextIdx = items.indexOf(nextItem);
|
|
165
|
-
for (const comment of [...comments].reverse()) items.splice(nextIdx, 0, comment);
|
|
166
|
-
recalcContainerEnd(item);
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
function patchCst(existing_cst, updated, format, indentWidthExplicit = false, trailingCommaExplicit = true, inlineTableStartExplicit = false) {
|
|
173
|
-
const items = [...existing_cst];
|
|
174
|
-
updated = compactSparseArrays(updated);
|
|
175
|
-
const useTemporal = hasTemporal(updated);
|
|
176
|
-
let endLine = 1;
|
|
177
|
-
let endColumn = 0;
|
|
178
|
-
for (const item of items) {
|
|
179
|
-
const e = item.loc.end;
|
|
180
|
-
if (e.line > endLine || e.line === endLine && e.column > endColumn) {
|
|
181
|
-
endLine = e.line;
|
|
182
|
-
endColumn = e.column;
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
const existing_js = toJS(items, "", { temporal: useTemporal });
|
|
186
|
-
const existing_document = {
|
|
187
|
-
type: "Document",
|
|
188
|
-
loc: {
|
|
189
|
-
start: {
|
|
190
|
-
line: 1,
|
|
191
|
-
column: 0
|
|
192
|
-
},
|
|
193
|
-
end: {
|
|
194
|
-
line: endLine,
|
|
195
|
-
column: endColumn
|
|
196
|
-
}
|
|
197
|
-
},
|
|
198
|
-
items
|
|
199
|
-
};
|
|
200
|
-
setRootIndentWidth(existing_document, format.indentWidth);
|
|
201
|
-
const diffing_inlineTableStart = format.inlineTableStart != null && format.inlineTableStart > 1 ? 1 : format.inlineTableStart;
|
|
202
|
-
const diffing_fmt = resolveTomlFormat({
|
|
203
|
-
...format,
|
|
204
|
-
inlineTableStart: diffing_inlineTableStart
|
|
205
|
-
}, format);
|
|
206
|
-
const updated_document = parseJS(updated, diffing_fmt);
|
|
207
|
-
const updated_js_raw = toJS(updated_document.items, "", { temporal: useTemporal });
|
|
208
|
-
const updated_js = format.updateOrder ? applyRequestedRootKeyOrder(updated, updated_js_raw, diffing_fmt, useTemporal) : updated_js_raw;
|
|
209
|
-
if (!format.updateOrder && valuesEqualIterative(existing_js, updated_js)) return {
|
|
210
|
-
tomlString: toTOML(items, format),
|
|
211
|
-
document: existing_document
|
|
212
|
-
};
|
|
213
|
-
const changes = reorder(coalesceStructuralReplacements(existing_document, updated_js, diff(existing_js, updated_js, [], {
|
|
214
|
-
updateOrder: format.updateOrder,
|
|
215
|
-
orderSource: updated
|
|
216
|
-
})));
|
|
217
|
-
if (changes.length === 0) return {
|
|
218
|
-
tomlString: toTOML(items, format),
|
|
219
|
-
document: existing_document
|
|
220
|
-
};
|
|
221
|
-
const patched_document = applyChanges(existing_document, updated_document, changes, format, useTemporal, format.updateOrder ? collectPrePatchNodes(existing_document) : /* @__PURE__ */ new WeakSet(), updated, indentWidthExplicit, trailingCommaExplicit, inlineTableStartExplicit, updated_js_raw);
|
|
222
|
-
return {
|
|
223
|
-
tomlString: normalizeInlineCommentAlignmentInString(patched_document, toTOMLCursor(patched_document.items, format), format),
|
|
224
|
-
document: patched_document
|
|
225
|
-
};
|
|
226
|
-
}
|
|
227
|
-
function valuesEqualIterative(left, right) {
|
|
228
|
-
const pending = [[left, right]];
|
|
229
|
-
while (pending.length > 0) {
|
|
230
|
-
const [a, b] = pending.pop();
|
|
231
|
-
if (typeof a === "number" && typeof b === "number" && (!Number.isFinite(a) || !Number.isFinite(b))) return false;
|
|
232
|
-
if (typeof a === "number" && typeof b === "number" && Number.isNaN(a) && Number.isNaN(b)) return false;
|
|
233
|
-
if (Object.is(a, b)) continue;
|
|
234
|
-
if (isDate(a) && isDate(b)) {
|
|
235
|
-
if (!datesEqual(a, b)) return false;
|
|
236
|
-
continue;
|
|
237
|
-
}
|
|
238
|
-
if (!a || !b || typeof a !== "object" || typeof b !== "object") return false;
|
|
239
|
-
if (Array.isArray(a) !== Array.isArray(b)) return false;
|
|
240
|
-
if (!Array.isArray(a)) {
|
|
241
|
-
const aPrototype = Object.getPrototypeOf(a);
|
|
242
|
-
const bPrototype = Object.getPrototypeOf(b);
|
|
243
|
-
if (aPrototype !== Object.prototype && aPrototype !== null || bPrototype !== Object.prototype && bPrototype !== null) return false;
|
|
244
|
-
}
|
|
245
|
-
const aKeys = Object.keys(a);
|
|
246
|
-
const bKeys = Object.keys(b);
|
|
247
|
-
if (aKeys.length !== bKeys.length) return false;
|
|
248
|
-
for (const key of aKeys) {
|
|
249
|
-
if (!Object.prototype.hasOwnProperty.call(b, key)) return false;
|
|
250
|
-
pending.push([a[key], b[key]]);
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
return true;
|
|
254
|
-
}
|
|
255
|
-
function compactSparseArrays(value) {
|
|
256
|
-
if (Array.isArray(value)) {
|
|
257
|
-
let changed = false;
|
|
258
|
-
const compacted = [];
|
|
259
|
-
for (let index = 0; index < value.length; index++) {
|
|
260
|
-
if (!Object.prototype.hasOwnProperty.call(value, index)) {
|
|
261
|
-
changed = true;
|
|
262
|
-
continue;
|
|
263
|
-
}
|
|
264
|
-
const child = compactSparseArrays(value[index]);
|
|
265
|
-
changed ||= child !== value[index];
|
|
266
|
-
compacted.push(child);
|
|
267
|
-
}
|
|
268
|
-
return changed ? compacted : value;
|
|
269
|
-
}
|
|
270
|
-
if (!isObject(value)) return value;
|
|
271
|
-
let normalized = value;
|
|
272
|
-
for (const key of Object.keys(value)) {
|
|
273
|
-
const child = compactSparseArrays(value[key]);
|
|
274
|
-
if (child !== value[key]) {
|
|
275
|
-
if (normalized === value) normalized = { ...value };
|
|
276
|
-
normalized[key] = child;
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
return normalized;
|
|
280
|
-
}
|
|
281
|
-
function reorder(changes) {
|
|
282
|
-
for (let i = 0; i < changes.length; i++) {
|
|
283
|
-
const change = changes[i];
|
|
284
|
-
if (isRemove(change)) {
|
|
285
|
-
const aIdx = last(change.path);
|
|
286
|
-
const aPrefix = change.path.slice(0, -1);
|
|
287
|
-
let j = i + 1;
|
|
288
|
-
while (j < changes.length) {
|
|
289
|
-
const next_change = changes[j];
|
|
290
|
-
if (isAdd(next_change)) {
|
|
291
|
-
if (typeof last(next_change.path) === "number" && arraysEqual(aPrefix, next_change.path.slice(0, -1))) {
|
|
292
|
-
if (change.coordinate === "source") {
|
|
293
|
-
let sourceRemove = -1;
|
|
294
|
-
for (let k = j + 1; k < changes.length; k++) {
|
|
295
|
-
const candidate = changes[k];
|
|
296
|
-
if (isRemove(candidate) && candidate.coordinate === "source" && arraysEqual(aPrefix, candidate.path.slice(0, -1))) {
|
|
297
|
-
sourceRemove = k;
|
|
298
|
-
break;
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
if (sourceRemove !== -1) {
|
|
302
|
-
const sourceChange = changes.splice(sourceRemove, 1)[0];
|
|
303
|
-
changes.splice(i, 0, sourceChange);
|
|
304
|
-
i = -1;
|
|
305
|
-
break;
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
break;
|
|
309
|
-
}
|
|
310
|
-
j++;
|
|
311
|
-
continue;
|
|
312
|
-
}
|
|
313
|
-
if (isMove(next_change) && next_change.key === void 0) {
|
|
314
|
-
if (arraysEqual(aPrefix, next_change.path)) break;
|
|
315
|
-
j++;
|
|
316
|
-
continue;
|
|
317
|
-
}
|
|
318
|
-
if (!isRemove(next_change)) {
|
|
319
|
-
j++;
|
|
320
|
-
continue;
|
|
321
|
-
}
|
|
322
|
-
const bIdx = last(next_change.path);
|
|
323
|
-
const bPrefix = next_change.path.slice(0, -1);
|
|
324
|
-
if (typeof aIdx === "number" && typeof bIdx === "number" && arraysEqual(aPrefix, bPrefix) && bIdx > aIdx) {
|
|
325
|
-
changes.splice(j, 1);
|
|
326
|
-
changes.splice(i, 0, next_change);
|
|
327
|
-
i = -1;
|
|
328
|
-
break;
|
|
329
|
-
}
|
|
330
|
-
j++;
|
|
331
|
-
}
|
|
332
|
-
}
|
|
333
|
-
}
|
|
334
|
-
return changes;
|
|
335
|
-
}
|
|
336
|
-
/**
|
|
337
|
-
* Merges sibling `Remove(prefix.oldKey)` + `Add(prefix.newKey)` changes that
|
|
338
|
-
* share a common parent path into a single `Edit(prefix)` change.
|
|
339
|
-
*
|
|
340
|
-
* When a table/AOT is replaced by an incompatible type at a shallower path
|
|
341
|
-
* (e.g. `[a.b]` → `a = { x = 1 }`), the object-diff sees the whole `a`
|
|
342
|
-
* subtree disappear and a differently-shaped `a` appear, and reports it as
|
|
343
|
-
* a `Remove` of the old child (`a.b`) plus one `Add` per key of the new
|
|
344
|
-
* value (`a.x`) — never a single `Edit` on `a` itself, since `a` never
|
|
345
|
-
* existed as its own JS-diff node (it was only implicit via the dotted
|
|
346
|
-
* table key). Left alone, the generic Add handling resolves the missing
|
|
347
|
-
* `a` container by walking up to the document root (`findParent`), silently
|
|
348
|
-
* dropping the `a` nesting. Coalescing back into one `Edit(prefix)` routes
|
|
349
|
-
* it through `handleStructuralEdit`, which already rebuilds the correct
|
|
350
|
-
* nested structure from the full updated value at that path.
|
|
351
|
-
*
|
|
352
|
-
* Only applies when `prefix` has no literal node of its own in `original`
|
|
353
|
-
* (it was purely implicit) — if `prefix` is an actual existing table, the
|
|
354
|
-
* normal per-key Add/Remove handling already does the right thing.
|
|
355
|
-
*
|
|
356
|
-
* Also merges per-index Edit/Add/Remove changes under an existing
|
|
357
|
-
* array-of-tables (AOT) whose updated value is still a JS array but no
|
|
358
|
-
* longer made up entirely of plain objects (e.g. `[[i]]` → `i = [1, 2, 3]`
|
|
359
|
-
* or `i = [9]`). An AOT entry is a Table living at the document level, not
|
|
360
|
-
* an item inside an array node, so it can't be edited/appended/removed in
|
|
361
|
-
* place the way the diff assumes — the whole array needs to be rebuilt via
|
|
362
|
-
* `handleStructuralEdit`. When every element of the updated array is still
|
|
363
|
-
* a plain object, the AOT stays an AOT and the normal per-entry handling is
|
|
364
|
-
* left untouched.
|
|
365
|
-
*/
|
|
366
|
-
function coalesceStructuralReplacements(original, updated_js, changes) {
|
|
367
|
-
const consumed = /* @__PURE__ */ new Set();
|
|
368
|
-
const coalescedEdits = [];
|
|
369
|
-
const groups = /* @__PURE__ */ new Map();
|
|
370
|
-
for (const change of changes) {
|
|
371
|
-
if (!isRemove(change) && !isAdd(change)) continue;
|
|
372
|
-
const parentPath = change.path.slice(0, -1);
|
|
373
|
-
if (parentPath.length === 0) continue;
|
|
374
|
-
const key = JSON.stringify(parentPath);
|
|
375
|
-
let group = groups.get(key);
|
|
376
|
-
if (!group) {
|
|
377
|
-
group = {
|
|
378
|
-
path: parentPath,
|
|
379
|
-
removes: [],
|
|
380
|
-
adds: []
|
|
381
|
-
};
|
|
382
|
-
groups.set(key, group);
|
|
383
|
-
}
|
|
384
|
-
(isRemove(change) ? group.removes : group.adds).push(change);
|
|
385
|
-
}
|
|
386
|
-
for (const group of groups.values()) {
|
|
387
|
-
if (group.removes.length === 0 || group.adds.length === 0) continue;
|
|
388
|
-
if (tryFindByPath(original, group.path)) continue;
|
|
389
|
-
group.removes.forEach((change) => consumed.add(change));
|
|
390
|
-
group.adds.forEach((change) => consumed.add(change));
|
|
391
|
-
coalescedEdits.push({
|
|
392
|
-
type: "Edit",
|
|
393
|
-
path: group.path
|
|
394
|
-
});
|
|
395
|
-
}
|
|
396
|
-
for (const change of changes) {
|
|
397
|
-
if (consumed.has(change) || !isRemove(change) && !isAdd(change)) continue;
|
|
398
|
-
const elementPath = change.path.slice(0, -1);
|
|
399
|
-
if (elementPath.length === 0 || typeof last(elementPath) !== "number") continue;
|
|
400
|
-
const element = tryFindByPath(original, elementPath);
|
|
401
|
-
if (!element || !isInlineItem(element) || !isInlineTable(element.item)) continue;
|
|
402
|
-
const siblings = changes.filter((candidate) => !consumed.has(candidate) && (isRemove(candidate) || isAdd(candidate)) && arraysEqual(candidate.path.slice(0, -1), elementPath));
|
|
403
|
-
if (siblings.length === 0 || siblings.some((candidate) => candidate.path.length !== elementPath.length + 1)) continue;
|
|
404
|
-
const existingKeys = new Set(element.item.items.filter(isInlineItem).map((item) => {
|
|
405
|
-
const keyValue = isKeyValue(item.item) ? item.item : void 0;
|
|
406
|
-
return (keyValue === null || keyValue === void 0 ? void 0 : keyValue.key.value.length) === 1 ? keyValue.key.value[0] : void 0;
|
|
407
|
-
}).filter((key) => key !== void 0));
|
|
408
|
-
const removedKeys = new Set(siblings.filter(isRemove).map((candidate) => last(candidate.path)));
|
|
409
|
-
if (!siblings.some(isRemove) || !siblings.some(isAdd) || existingKeys.size !== removedKeys.size || [...existingKeys].some((key) => !removedKeys.has(key))) continue;
|
|
410
|
-
for (const sibling of siblings) consumed.add(sibling);
|
|
411
|
-
coalescedEdits.push({
|
|
412
|
-
type: "Edit",
|
|
413
|
-
path: elementPath
|
|
414
|
-
});
|
|
415
|
-
}
|
|
416
|
-
const arrayPrefixes = /* @__PURE__ */ new Map();
|
|
417
|
-
for (const change of changes) {
|
|
418
|
-
if (consumed.has(change)) continue;
|
|
419
|
-
if (!isEdit(change) && !isAdd(change) && !isRemove(change)) continue;
|
|
420
|
-
if (typeof last(change.path) !== "number") continue;
|
|
421
|
-
const prefix = change.path.slice(0, -1);
|
|
422
|
-
if (prefix.length === 0) continue;
|
|
423
|
-
const firstEntry = tryFindByPath(original, prefix.concat(0));
|
|
424
|
-
if (!firstEntry || !isTableArray(firstEntry)) continue;
|
|
425
|
-
arrayPrefixes.set(JSON.stringify(prefix), prefix);
|
|
426
|
-
}
|
|
427
|
-
for (const prefix of arrayPrefixes.values()) {
|
|
428
|
-
let value = updated_js;
|
|
429
|
-
for (const key of prefix) value = value === null || value === void 0 ? void 0 : value[key];
|
|
430
|
-
if (!Array.isArray(value)) continue;
|
|
431
|
-
if (value.every((el) => isObject(el))) continue;
|
|
432
|
-
const elementChanges = changes.filter((c) => !consumed.has(c) && (isEdit(c) || isAdd(c) || isRemove(c)) && c.path.length > prefix.length && arraysEqual(c.path.slice(0, prefix.length), prefix));
|
|
433
|
-
if (elementChanges.length < 2) continue;
|
|
434
|
-
for (const change of elementChanges) consumed.add(change);
|
|
435
|
-
coalescedEdits.push({
|
|
436
|
-
type: "Edit",
|
|
437
|
-
path: prefix
|
|
438
|
-
});
|
|
439
|
-
}
|
|
440
|
-
if (consumed.size === 0) return changes;
|
|
441
|
-
const result = changes.filter((change) => !consumed.has(change));
|
|
442
|
-
result.push(...coalescedEdits);
|
|
443
|
-
return result;
|
|
444
|
-
}
|
|
445
|
-
function preserveEscapedKeyRaw(existingRaw, keyParts, escapeSequenceUpperCase = true) {
|
|
446
|
-
return keyParts.map((part) => IS_BARE_KEY.test(part) ? part : `"${escapeStringContent(part, existingRaw, "singleline-basic", escapeSequenceUpperCase)}"`).join(".");
|
|
447
|
-
}
|
|
448
|
-
function dottedKeySeparators(raw) {
|
|
449
|
-
const separators = [];
|
|
450
|
-
let quote;
|
|
451
|
-
let escaped = false;
|
|
452
|
-
for (let index = 0; index < raw.length; index++) {
|
|
453
|
-
const character = raw[index];
|
|
454
|
-
if (quote) {
|
|
455
|
-
if (quote === "\"" && escaped) escaped = false;
|
|
456
|
-
else if (quote === "\"" && character === "\\") escaped = true;
|
|
457
|
-
else if (character === quote) quote = void 0;
|
|
458
|
-
continue;
|
|
459
|
-
}
|
|
460
|
-
if (character === "\"" || character === "'") {
|
|
461
|
-
quote = character;
|
|
462
|
-
continue;
|
|
463
|
-
}
|
|
464
|
-
if (character !== ".") continue;
|
|
465
|
-
let start = index;
|
|
466
|
-
while (start > 0 && (raw[start - 1] === " " || raw[start - 1] === " ")) start--;
|
|
467
|
-
let end = index + 1;
|
|
468
|
-
while (end < raw.length && (raw[end] === " " || raw[end] === " ")) end++;
|
|
469
|
-
separators.push(raw.slice(start, end));
|
|
470
|
-
}
|
|
471
|
-
return separators;
|
|
472
|
-
}
|
|
473
|
-
function dottedKeyParts(raw) {
|
|
474
|
-
const parts = [];
|
|
475
|
-
let start = 0;
|
|
476
|
-
let quote;
|
|
477
|
-
let escaped = false;
|
|
478
|
-
for (let index = 0; index < raw.length; index++) {
|
|
479
|
-
const character = raw[index];
|
|
480
|
-
if (quote) {
|
|
481
|
-
if (quote === "\"" && escaped) escaped = false;
|
|
482
|
-
else if (quote === "\"" && character === "\\") escaped = true;
|
|
483
|
-
else if (character === quote) quote = void 0;
|
|
484
|
-
continue;
|
|
485
|
-
}
|
|
486
|
-
if (character === "\"" || character === "'") quote = character;
|
|
487
|
-
else if (character === ".") {
|
|
488
|
-
parts.push(raw.slice(start, index));
|
|
489
|
-
start = index + 1;
|
|
490
|
-
}
|
|
491
|
-
}
|
|
492
|
-
parts.push(raw.slice(start));
|
|
493
|
-
return parts;
|
|
494
|
-
}
|
|
495
|
-
function preserveDottedKeySpacing(key, styleRaw) {
|
|
496
|
-
const separators = dottedKeySeparators(styleRaw);
|
|
497
|
-
if (separators.length === 0) return;
|
|
498
|
-
key.raw = dottedKeyParts(key.raw).reduce((raw, part, index) => {
|
|
499
|
-
if (index === 0) return part;
|
|
500
|
-
return `${raw}${separators[Math.min(index - 1, separators.length - 1)]}${part}`;
|
|
501
|
-
}, "");
|
|
502
|
-
key.loc.end.column = key.loc.start.column + key.raw.length;
|
|
503
|
-
}
|
|
504
|
-
function findDottedKeyStyle(root, prefix) {
|
|
505
|
-
let style;
|
|
506
|
-
traverse(root, { KeyValue: (node) => {
|
|
507
|
-
if (!style && node.key.value.length > prefix.length && arraysEqual(node.key.value.slice(0, prefix.length), prefix) && dottedKeySeparators(node.key.raw).length > 0) style = node;
|
|
508
|
-
} });
|
|
509
|
-
return style;
|
|
510
|
-
}
|
|
511
|
-
/**
|
|
512
|
-
* Analyzes an existing float's raw text: whether it uses exponent notation and
|
|
513
|
-
* whether its fractional part is all zeros (a "round" float such as `1.0`).
|
|
514
|
-
*/
|
|
515
|
-
function analyzeFloatStyle(raw) {
|
|
516
|
-
const expIndex = raw.search(/[eE]/);
|
|
517
|
-
const mantissa = expIndex === -1 ? raw : raw.slice(0, expIndex);
|
|
518
|
-
const dotIndex = mantissa.indexOf(".");
|
|
519
|
-
const fraction = dotIndex === -1 ? "" : mantissa.slice(dotIndex + 1);
|
|
520
|
-
return {
|
|
521
|
-
hasExponent: expIndex !== -1,
|
|
522
|
-
fractionLength: fraction.length,
|
|
523
|
-
isRound: !/[1-9]/.test(fraction)
|
|
524
|
-
};
|
|
525
|
-
}
|
|
526
|
-
/** Counts the decimal places written in a float's raw text (before any exponent). */
|
|
527
|
-
function decimalPlacesOfRaw(raw) {
|
|
528
|
-
const expIndex = raw.search(/[eE]/);
|
|
529
|
-
const mantissa = expIndex === -1 ? raw : raw.slice(0, expIndex);
|
|
530
|
-
const dotIndex = mantissa.indexOf(".");
|
|
531
|
-
return dotIndex === -1 ? 0 : mantissa.length - dotIndex - 1;
|
|
532
|
-
}
|
|
533
|
-
/** Renders a value in TOML exponent notation (e.g. `1.0e11`) with at least `minimumDecimals` decimals. */
|
|
534
|
-
function renderExponentNotation(value, minimumDecimals, uppercaseExponent = false) {
|
|
535
|
-
const [mantissa, exponent] = value.toExponential().split(/[eE]/);
|
|
536
|
-
const decimals = Math.max(minimumDecimals, decimalPlacesOfRaw(mantissa));
|
|
537
|
-
return `${decimals > 0 ? Number(mantissa).toFixed(decimals) : String(Number(mantissa))}${uppercaseExponent ? "E" : "e"}${exponent.replace(/^\+/, "")}`;
|
|
538
|
-
}
|
|
539
|
-
/** Detects the radix and prefix of a prefixed integer literal (`0x`/`0o`/`0b`). */
|
|
540
|
-
function integerRadixOf(raw) {
|
|
541
|
-
if (raw.startsWith("0x")) return {
|
|
542
|
-
radix: 16,
|
|
543
|
-
prefix: "0x"
|
|
544
|
-
};
|
|
545
|
-
if (raw.startsWith("0o")) return {
|
|
546
|
-
radix: 8,
|
|
547
|
-
prefix: "0o"
|
|
548
|
-
};
|
|
549
|
-
if (raw.startsWith("0b")) return {
|
|
550
|
-
radix: 2,
|
|
551
|
-
prefix: "0b"
|
|
552
|
-
};
|
|
553
|
-
}
|
|
554
|
-
/** Extracts the underscore grouping period: the digit count of the rightmost group. */
|
|
555
|
-
function underscorePeriodOf(raw) {
|
|
556
|
-
let body = raw;
|
|
557
|
-
if (body.startsWith("+") || body.startsWith("-")) body = body.slice(1);
|
|
558
|
-
if (body.startsWith("0x") || body.startsWith("0o") || body.startsWith("0b")) body = body.slice(2);
|
|
559
|
-
const lastUnderscore = body.lastIndexOf("_");
|
|
560
|
-
return lastUnderscore === -1 ? void 0 : body.length - lastUnderscore - 1;
|
|
561
|
-
}
|
|
562
|
-
/** Inserts an underscore every `period` digits, counted from the right. */
|
|
563
|
-
function groupDigits(digits, period) {
|
|
564
|
-
if (digits.length <= period) return digits;
|
|
565
|
-
let result = "";
|
|
566
|
-
let count = 0;
|
|
567
|
-
for (let i = digits.length - 1; i >= 0; i--) {
|
|
568
|
-
result = digits[i] + result;
|
|
569
|
-
count++;
|
|
570
|
-
if (count === period && i > 0) {
|
|
571
|
-
result = "_" + result;
|
|
572
|
-
count = 0;
|
|
573
|
-
}
|
|
574
|
-
}
|
|
575
|
-
return result;
|
|
576
|
-
}
|
|
577
|
-
/** Renders an integer in the style of an existing integer literal (radix, digit case, grouping). */
|
|
578
|
-
function renderIntegerLike(value, existingRaw) {
|
|
579
|
-
const radixInfo = integerRadixOf(existingRaw);
|
|
580
|
-
const period = underscorePeriodOf(existingRaw);
|
|
581
|
-
if (radixInfo) {
|
|
582
|
-
if (typeof value === "bigint" ? value < 0n : value < 0) return void 0;
|
|
583
|
-
const uppercaseHex = radixInfo.radix === 16 && /[A-F]/.test(existingRaw);
|
|
584
|
-
let digits = value.toString(radixInfo.radix);
|
|
585
|
-
if (uppercaseHex) digits = digits.toUpperCase();
|
|
586
|
-
return radixInfo.prefix + (period ? groupDigits(digits, period) : digits);
|
|
587
|
-
}
|
|
588
|
-
if (period) {
|
|
589
|
-
const negative = typeof value === "bigint" ? value < 0n : value < 0;
|
|
590
|
-
const abs = negative ? value.toString().slice(1) : value.toString();
|
|
591
|
-
return (negative ? "-" : "") + groupDigits(abs, period);
|
|
592
|
-
}
|
|
593
|
-
}
|
|
594
|
-
/**
|
|
595
|
-
* Preserves formatting from the existing node when applying it to the replacement node.
|
|
596
|
-
* This includes multiline string formats, trailing commas, DateTime formats, etc.
|
|
597
|
-
*
|
|
598
|
-
* @param existing - The existing node with formatting to preserve
|
|
599
|
-
* @param replacement - The replacement node to apply formatting to
|
|
600
|
-
*/
|
|
601
|
-
function preserveFormatting(existing, replacement, escapeSequenceUpperCase = true, minimumTimeDecimals = 0) {
|
|
602
|
-
if (isString(existing) && isString(replacement)) {
|
|
603
|
-
const newString = generateString(replacement.value, existing.raw, escapeSequenceUpperCase);
|
|
604
|
-
replacement.raw = newString.raw;
|
|
605
|
-
replacement.loc = newString.loc;
|
|
606
|
-
}
|
|
607
|
-
if (isDateTime(existing) && isDateTime(replacement)) {
|
|
608
|
-
const originalRaw = existing.raw;
|
|
609
|
-
const newValue = replacement.value;
|
|
610
|
-
if (isTemporal(newValue)) {
|
|
611
|
-
let raw = temporalToTomlString(newValue);
|
|
612
|
-
if (originalRaw.includes(" ") && raw.includes("T")) raw = raw.replace("T", " ");
|
|
613
|
-
if (/(?:\+00:00|-00:00)/.test(originalRaw) && raw.endsWith("Z")) {
|
|
614
|
-
const offset = originalRaw.match(/([+-]00:00)/)[1];
|
|
615
|
-
raw = raw.replace(/Z$/, offset);
|
|
616
|
-
}
|
|
617
|
-
raw = padFractionalSeconds(raw, minimumTimeDecimals);
|
|
618
|
-
replacement.raw = raw;
|
|
619
|
-
replacement.loc.end.column = replacement.loc.start.column + replacement.raw.length;
|
|
620
|
-
} else if (DateFormatHelper.IS_TIME_ONLY.test(originalRaw) && newValue instanceof Date && newValue.getUTCFullYear() > 1) {} else {
|
|
621
|
-
const formattedDate = DateFormatHelper.createDateWithOriginalFormat(newValue, originalRaw);
|
|
622
|
-
replacement.value = formattedDate;
|
|
623
|
-
replacement.raw = padFractionalSeconds(formattedDate.toISOString(), minimumTimeDecimals);
|
|
624
|
-
replacement.loc.end.column = replacement.loc.start.column + replacement.raw.length;
|
|
625
|
-
}
|
|
626
|
-
}
|
|
627
|
-
if (isFloat(existing) && isFloat(replacement) && Number.isNaN(existing.value) && Number.isNaN(replacement.value)) {
|
|
628
|
-
const existingFloat = existing;
|
|
629
|
-
const replacementFloat = replacement;
|
|
630
|
-
if (existingFloat.nanSign) {
|
|
631
|
-
replacementFloat.nanSign = "+";
|
|
632
|
-
replacementFloat.raw = "+nan";
|
|
633
|
-
}
|
|
634
|
-
}
|
|
635
|
-
if (isInteger(existing) && replacement.type === "Integer") {
|
|
636
|
-
const existingInt = existing;
|
|
637
|
-
const newRaw = renderIntegerLike(replacement.value, existingInt.raw);
|
|
638
|
-
if (newRaw !== void 0) {
|
|
639
|
-
const replacementInt = replacement;
|
|
640
|
-
replacementInt.raw = newRaw;
|
|
641
|
-
replacementInt.loc.end.column = replacementInt.loc.start.column + newRaw.length;
|
|
642
|
-
}
|
|
643
|
-
}
|
|
644
|
-
if (isFloat(existing) && (replacement.type === "Integer" || replacement.type === "Float")) {
|
|
645
|
-
const existingFloat = existing;
|
|
646
|
-
const value = replacement.value;
|
|
647
|
-
if (typeof value === "number") {
|
|
648
|
-
const { hasExponent, fractionLength, isRound } = analyzeFloatStyle(existingFloat.raw);
|
|
649
|
-
const valueIsIntegral = Number.isInteger(value) && !Object.is(value, -0);
|
|
650
|
-
const replacementDecimals = decimalPlacesOfRaw(replacement.raw);
|
|
651
|
-
let newRaw;
|
|
652
|
-
if (hasExponent && Number.isFinite(value) && !Object.is(value, -0)) {
|
|
653
|
-
const uppercaseExponent = existingFloat.raw.includes("E");
|
|
654
|
-
newRaw = renderExponentNotation(value, valueIsIntegral ? Math.max(isRound ? fractionLength : 0, replacementDecimals) : replacementDecimals, uppercaseExponent);
|
|
655
|
-
} else if (valueIsIntegral && isRound) newRaw = value.toFixed(Math.max(fractionLength, replacementDecimals));
|
|
656
|
-
if (newRaw !== void 0) {
|
|
657
|
-
const replacementFloat = replacement;
|
|
658
|
-
replacementFloat.type = "Float";
|
|
659
|
-
replacementFloat.raw = newRaw;
|
|
660
|
-
replacementFloat.loc.end.column = replacementFloat.loc.start.column + newRaw.length;
|
|
661
|
-
}
|
|
662
|
-
}
|
|
663
|
-
}
|
|
664
|
-
if (isInlineArray(existing) && isInlineArray(replacement)) {
|
|
665
|
-
const originalHadTrailingCommas = arrayHadTrailingCommas(existing);
|
|
666
|
-
if (replacement.items.length > 0) {
|
|
667
|
-
const lastItem = replacement.items[replacement.items.length - 1];
|
|
668
|
-
if (lastItem.comma !== originalHadTrailingCommas) {
|
|
669
|
-
lastItem.comma = originalHadTrailingCommas;
|
|
670
|
-
if (originalHadTrailingCommas) replacement.loc.end.column += 1;
|
|
671
|
-
}
|
|
672
|
-
}
|
|
673
|
-
}
|
|
674
|
-
if (isInlineTable(existing) && isInlineTable(replacement)) {
|
|
675
|
-
const originalHadTrailingCommas = tableHadTrailingCommas(existing);
|
|
676
|
-
if (replacement.items.length > 0) {
|
|
677
|
-
const lastItem = replacement.items[replacement.items.length - 1];
|
|
678
|
-
if (lastItem.comma !== originalHadTrailingCommas) {
|
|
679
|
-
lastItem.comma = originalHadTrailingCommas;
|
|
680
|
-
if (originalHadTrailingCommas) replacement.loc.end.column += 1;
|
|
681
|
-
}
|
|
682
|
-
}
|
|
683
|
-
}
|
|
684
|
-
}
|
|
685
|
-
/**
|
|
686
|
-
* Applies a list of changes to the original TOML document CST while preserving formatting and structure.
|
|
687
|
-
*
|
|
688
|
-
* This function processes different types of changes (Add, Edit, Remove, Move, Rename) and applies them
|
|
689
|
-
* to the original document in a way that maintains the existing formatting preferences, comments, and
|
|
690
|
-
* structural elements as much as possible. Special handling is provided for different node types like
|
|
691
|
-
* inline tables, arrays, and table arrays to ensure proper formatting consistency.
|
|
692
|
-
*
|
|
693
|
-
* @param original - The original TOML document CST to be modified
|
|
694
|
-
* @param updated - The updated document CST containing new values for changes
|
|
695
|
-
* @param changes - Array of change objects describing what modifications to apply
|
|
696
|
-
* @param format - Formatting preferences to use for newly added elements
|
|
697
|
-
* @returns The modified original document with all changes applied
|
|
698
|
-
*
|
|
699
|
-
* @example
|
|
700
|
-
* ```typescript
|
|
701
|
-
* const changes = [
|
|
702
|
-
* { type: 'add', path: ['newKey'], value: 'newValue' },
|
|
703
|
-
* { type: 'edit', path: ['existingKey'], value: 'updatedValue' }
|
|
704
|
-
* ];
|
|
705
|
-
* const result = applyChanges(originalDoc, updatedDoc, changes, format);
|
|
706
|
-
* ```
|
|
707
|
-
*/
|
|
708
|
-
function applyChanges(original, updated, changes, format, temporal = false, commentEligibleNodes = /* @__PURE__ */ new WeakSet(), rawUpdated = void 0, indentWidthExplicit = false, trailingCommaExplicit = true, inlineTableStartExplicit = false, updatedJs = void 0) {
|
|
709
|
-
const convertedUpdated = updatedJs ?? toJS(updated.items, "", { temporal });
|
|
710
|
-
const emptiedAotKeys = /* @__PURE__ */ new Map();
|
|
711
|
-
const materialisedTables = /* @__PURE__ */ new Set();
|
|
712
|
-
function trackAdjacentComment(table) {
|
|
713
|
-
const items = original.items;
|
|
714
|
-
const idx = items.indexOf(table);
|
|
715
|
-
if (idx > 0) {
|
|
716
|
-
const prev = items[idx - 1];
|
|
717
|
-
if (isComment(prev) && prev.loc.end.line + 1 === table.loc.start.line) materialisedTables.add(table);
|
|
718
|
-
}
|
|
719
|
-
}
|
|
720
|
-
const absolutePathCache = /* @__PURE__ */ new WeakMap();
|
|
721
|
-
function absolutePathOf(target) {
|
|
722
|
-
const cached = absolutePathCache.get(target);
|
|
723
|
-
if (cached !== void 0) return cached;
|
|
724
|
-
const indexes = {};
|
|
725
|
-
function walk(node, path) {
|
|
726
|
-
if (node === target) return path;
|
|
727
|
-
if (isKeyValue(node)) return walk(node.value, path);
|
|
728
|
-
if (isInlineItem(node)) return walk(node.item, path);
|
|
729
|
-
if (!hasItems(node)) return void 0;
|
|
730
|
-
for (let i = 0; i < node.items.length; i++) {
|
|
731
|
-
const item = node.items[i];
|
|
732
|
-
let key = [];
|
|
733
|
-
if (isKeyValue(item)) key = item.key.value;
|
|
734
|
-
else if (isTable(item) || isTableArray(item)) {
|
|
735
|
-
key = item.key.item.value;
|
|
736
|
-
if (isTableArray(item)) {
|
|
737
|
-
const ks = stableStringify(key);
|
|
738
|
-
indexes[ks] = (indexes[ks] ?? -1) + 1;
|
|
739
|
-
key = key.concat(indexes[ks]);
|
|
740
|
-
}
|
|
741
|
-
} else if (isInlineItem(item)) {
|
|
742
|
-
if (isKeyValue(item.item)) key = item.item.key.value;
|
|
743
|
-
else key = [i];
|
|
744
|
-
}
|
|
745
|
-
const found = walk(item, key.length ? path.concat(key) : path);
|
|
746
|
-
if (found) return found;
|
|
747
|
-
}
|
|
748
|
-
}
|
|
749
|
-
const result = walk(original, []);
|
|
750
|
-
if (result !== void 0) absolutePathCache.set(target, result);
|
|
751
|
-
return result;
|
|
752
|
-
}
|
|
753
|
-
function isPrefixMatchedNode(candidate, probe) {
|
|
754
|
-
const kv = isKeyValue(candidate) ? candidate : isInlineItem(candidate) && isKeyValue(candidate.item) ? candidate.item : void 0;
|
|
755
|
-
if (!kv) return false;
|
|
756
|
-
const keyLen = kv.key.value.length;
|
|
757
|
-
if (keyLen > probe.length) return true;
|
|
758
|
-
return !arraysEqual(kv.key.value, probe.slice(probe.length - keyLen));
|
|
759
|
-
}
|
|
760
|
-
function aotEntryUsesDottedKey(styleEntry, relativeKey) {
|
|
761
|
-
const fullKey = styleEntry.key.item.value.concat(relativeKey);
|
|
762
|
-
if (styleEntry.items.some((item) => isKeyValue(item) && item.key.value.length > relativeKey.length && arraysEqual(item.key.value.slice(0, relativeKey.length), relativeKey))) return true;
|
|
763
|
-
return original.items.some((item) => {
|
|
764
|
-
if (!isTable(item) && !isTableArray(item)) return false;
|
|
765
|
-
const key = item.key.item.value;
|
|
766
|
-
return key.length > fullKey.length && arraysEqual(key.slice(0, fullKey.length), fullKey);
|
|
767
|
-
});
|
|
768
|
-
}
|
|
769
|
-
function preserveAotEntryDottedKeys(entry, styleEntry) {
|
|
770
|
-
const rows = [];
|
|
771
|
-
let changed = false;
|
|
772
|
-
for (const item of entry.items) {
|
|
773
|
-
if (!isKeyValue(item) || !isInlineTable(item.value) || !aotEntryUsesDottedKey(styleEntry, item.key.value)) {
|
|
774
|
-
rows.push(item);
|
|
775
|
-
continue;
|
|
776
|
-
}
|
|
777
|
-
const replacementRows = [];
|
|
778
|
-
for (const nestedItem of item.value.items) {
|
|
779
|
-
if (!isInlineItem(nestedItem) || !isKeyValue(nestedItem.item)) continue;
|
|
780
|
-
let nestedValue = toJS([nestedItem.item], "", { temporal });
|
|
781
|
-
for (const key of nestedItem.item.key.value) nestedValue = nestedValue === null || nestedValue === void 0 ? void 0 : nestedValue[key];
|
|
782
|
-
const freshValue = regenerateValue(nestedValue, format);
|
|
783
|
-
if (freshValue !== void 0) replacementRows.push(generateKeyValue(item.key.value.concat(nestedItem.item.key.value), freshValue));
|
|
784
|
-
}
|
|
785
|
-
if (replacementRows.length > 0) {
|
|
786
|
-
rows.push(...replacementRows);
|
|
787
|
-
changed = true;
|
|
788
|
-
} else rows.push(item);
|
|
789
|
-
}
|
|
790
|
-
if (!changed) return entry;
|
|
791
|
-
const rebuilt = generateTableArray(entry.key.item.value, format.escapeSequenceUpperCase);
|
|
792
|
-
for (const row of rows) insert(rebuilt, rebuilt, row);
|
|
793
|
-
applyWrites(rebuilt);
|
|
794
|
-
return rebuilt;
|
|
795
|
-
}
|
|
796
|
-
function findStructuralParent(root, target) {
|
|
797
|
-
function walk(node) {
|
|
798
|
-
if (isKeyValue(node)) return walk(node.value);
|
|
799
|
-
if (isInlineItem(node)) return walk(node.item);
|
|
800
|
-
if (!hasItems(node)) return void 0;
|
|
801
|
-
const items = node.items;
|
|
802
|
-
for (const item of items) {
|
|
803
|
-
if (item === target) return node;
|
|
804
|
-
if (isInlineItem(item) && item.item === target) return node;
|
|
805
|
-
}
|
|
806
|
-
for (const item of items) {
|
|
807
|
-
const found = walk(isInlineItem(item) ? item.item : item);
|
|
808
|
-
if (found) return found;
|
|
809
|
-
}
|
|
810
|
-
}
|
|
811
|
-
return walk(root);
|
|
812
|
-
}
|
|
813
|
-
function extendKeyWithParentAndReplace(freshKV, parentKey, existing, tableParent) {
|
|
814
|
-
const dottedKey = parentKey.concat(freshKV.key.value);
|
|
815
|
-
const oldRaw = freshKV.key.raw;
|
|
816
|
-
freshKV.key.value = dottedKey;
|
|
817
|
-
freshKV.key.raw = dottedKey.map((part) => IS_BARE_KEY.test(part) ? part : JSON.stringify(part).replace(/\x7f/g, "\\u007f")).join(".");
|
|
818
|
-
const delta = freshKV.key.raw.length - oldRaw.length;
|
|
819
|
-
freshKV.key.loc.end.column = freshKV.key.loc.start.column + freshKV.key.raw.length;
|
|
820
|
-
freshKV.equals += delta;
|
|
821
|
-
shiftNode(freshKV.value, {
|
|
822
|
-
lines: 0,
|
|
823
|
-
columns: delta
|
|
824
|
-
}, { first_line_only: true });
|
|
825
|
-
if (freshKV.loc.end.line === freshKV.loc.start.line) freshKV.loc.end.column += delta;
|
|
826
|
-
replace(original, tableParent, existing, freshKV);
|
|
827
|
-
hoistRootKeyValueAboveTables(original, freshKV);
|
|
828
|
-
}
|
|
829
|
-
function extendSectionKeyInPlace(section, parentKey) {
|
|
830
|
-
const keyNode = section.key.item;
|
|
831
|
-
const oldRaw = keyNode.raw;
|
|
832
|
-
const dottedKey = parentKey.concat(keyNode.value);
|
|
833
|
-
const newRaw = dottedKey.map((part) => IS_BARE_KEY.test(part) ? part : JSON.stringify(part).replace(/\x7f/g, "\\u007f")).join(".");
|
|
834
|
-
const delta = newRaw.length - oldRaw.length;
|
|
835
|
-
keyNode.value = dottedKey;
|
|
836
|
-
keyNode.raw = newRaw;
|
|
837
|
-
keyNode.loc.end.column = keyNode.loc.start.column + newRaw.length;
|
|
838
|
-
if (section.key.loc.end.line === section.key.loc.start.line) section.key.loc.end.column += delta;
|
|
839
|
-
}
|
|
840
|
-
function extendSectionKeyWithParentAndReplace(section, parentKey, existing, tableParent) {
|
|
841
|
-
extendSectionKeyInPlace(section, parentKey);
|
|
842
|
-
replace(original, tableParent, existing, section);
|
|
843
|
-
}
|
|
844
|
-
function restoreMissingKeySegments(parent_path, child, change_path) {
|
|
845
|
-
const direct = tryFindByPath(original, parent_path);
|
|
846
|
-
if (direct && !isPrefixMatchedNode(direct, parent_path)) return null;
|
|
847
|
-
let prefixLen = parent_path.length;
|
|
848
|
-
while (prefixLen > 0) {
|
|
849
|
-
const node = tryFindByPath(original, parent_path.slice(0, prefixLen));
|
|
850
|
-
if (node && !isPrefixMatchedNode(node, parent_path.slice(0, prefixLen))) break;
|
|
851
|
-
prefixLen--;
|
|
852
|
-
}
|
|
853
|
-
const missing = parent_path.slice(prefixLen);
|
|
854
|
-
if (missing.length === 0 || !missing.every((seg) => typeof seg === "string")) return null;
|
|
855
|
-
if (rawUpdated !== void 0) {
|
|
856
|
-
let jsValue = rawUpdated;
|
|
857
|
-
for (const k of change_path) jsValue = jsValue === null || jsValue === void 0 ? void 0 : jsValue[k];
|
|
858
|
-
if (jsValue !== void 0) {
|
|
859
|
-
const freshValue = regenerateValue(jsValue, format);
|
|
860
|
-
if (freshValue !== void 0) return generateKeyValue([...missing, ...child.key.value], freshValue, false, format.escapeSequenceUpperCase);
|
|
861
|
-
}
|
|
862
|
-
}
|
|
863
|
-
const oldKey = child.key;
|
|
864
|
-
const newRaw = [...missing, ...oldKey.value].map((part) => IS_BARE_KEY.test(part) ? part : JSON.stringify(part).replace(/\x7f/g, "\\u007f")).join(".");
|
|
865
|
-
const colDelta = newRaw.length - oldKey.raw.length;
|
|
866
|
-
oldKey.raw = newRaw;
|
|
867
|
-
oldKey.value = [...missing, ...oldKey.value];
|
|
868
|
-
oldKey.loc.end.column = oldKey.loc.start.column + newRaw.length;
|
|
869
|
-
child.equals += colDelta;
|
|
870
|
-
shiftNode(child.value, {
|
|
871
|
-
lines: 0,
|
|
872
|
-
columns: colDelta
|
|
873
|
-
}, { first_line_only: true });
|
|
874
|
-
if (child.loc.end.line === child.loc.start.line) child.loc.end.column += colDelta;
|
|
875
|
-
return child;
|
|
876
|
-
}
|
|
877
|
-
const restoredInsertContainers = /* @__PURE__ */ new Set();
|
|
878
|
-
const insertedInlineContainers = /* @__PURE__ */ new Set();
|
|
879
|
-
const removedInlineTrailingCommas = /* @__PURE__ */ new WeakMap();
|
|
880
|
-
function trailingCommaForAddedItem(container, detected) {
|
|
881
|
-
return removedInlineTrailingCommas.get(container) ?? detected;
|
|
882
|
-
}
|
|
883
|
-
const objectMoves = [];
|
|
884
|
-
const replacedInlineArrays = /* @__PURE__ */ new Set();
|
|
885
|
-
const replacedInlineContainers = [];
|
|
886
|
-
function regenerateInlineChildForParent(parent, changePath, child) {
|
|
887
|
-
if (!isInlineArray(parent) && !isInlineTable(parent) || !isInlineItem(child)) return child;
|
|
888
|
-
if (isInlineTable(parent) && !isKeyValue(child.item)) return child;
|
|
889
|
-
let jsValue = rawUpdated;
|
|
890
|
-
for (const key of changePath) jsValue = jsValue === null || jsValue === void 0 ? void 0 : jsValue[key];
|
|
891
|
-
if (!Array.isArray(jsValue) && !isObject(jsValue)) return child;
|
|
892
|
-
const parentDepth = findInlineContainerDepth(original, parent) ?? 0;
|
|
893
|
-
const parentIsMultiline = perLine(parent) || parent.items.length === 0 && parent.loc.end.line > parent.loc.start.line;
|
|
894
|
-
const generatedIndentWidth = indentWidthExplicit ? format.indentWidth : isInlineArray(parent) && parent.items.length === 0 ? Math.max(format.indentWidth, 4) : format.indentWidth;
|
|
895
|
-
if (isInlineArray(parent) && parent.items.length === 0 && !format.useTabsForIndentation) setInlineIndentColumn(parent, Math.max(generatedIndentWidth, 4));
|
|
896
|
-
const kind = Array.isArray(jsValue) ? "array" : "table";
|
|
897
|
-
const sibling = parent.items.map((item) => isInlineTable(parent) && isKeyValue(item.item) ? item.item.value : item.item).find((item) => kind === "array" ? isInlineArray(item) : isInlineTable(item));
|
|
898
|
-
const siblingTrailingComma = sibling && kind === "array" ? arrayHadTrailingCommas(sibling) : sibling && kind === "table" ? tableHadTrailingCommas(sibling) : false;
|
|
899
|
-
const siblingContainer = sibling;
|
|
900
|
-
const siblingBracketSpacing = siblingContainer && siblingContainer.items.length > 0 ? siblingContainer.items[0].loc.start.column - siblingContainer.loc.start.column > 1 : void 0;
|
|
901
|
-
const multiline = resolveInlineContainerLayout(kind, parentDepth + 1, parentIsMultiline, format);
|
|
902
|
-
const modeProperty = kind === "array" ? "multilineArray" : "multilineTable";
|
|
903
|
-
const contextualFormat = resolveTomlFormat({
|
|
904
|
-
...format,
|
|
905
|
-
inlineTableStart: 0,
|
|
906
|
-
[modeProperty]: multiline,
|
|
907
|
-
trailingComma: trailingCommaExplicit ? format.trailingComma : siblingTrailingComma,
|
|
908
|
-
bracketSpacing: siblingBracketSpacing ?? format.bracketSpacing,
|
|
909
|
-
indentWidth: generatedIndentWidth
|
|
910
|
-
}, format);
|
|
911
|
-
const wrapper = parseJS({ tmp: jsValue }, contextualFormat, parentDepth + 1, parentIsMultiline).items[0];
|
|
912
|
-
if (!wrapper || !isKeyValue(wrapper)) return child;
|
|
913
|
-
const value = wrapper.value;
|
|
914
|
-
if (multiline && (isInlineArray(value) || isInlineTable(value))) {
|
|
915
|
-
const startColumn = value.loc.start.column;
|
|
916
|
-
value.loc.start.column = 0;
|
|
917
|
-
if (value.loc.end.line === value.loc.start.line) value.loc.end.column -= startColumn;
|
|
918
|
-
else if (resolveInlineContainerLayout(isInlineArray(value) ? "array" : "table", parentDepth + 1, parentIsMultiline, contextualFormat)) value.loc.end.column = 1;
|
|
919
|
-
}
|
|
920
|
-
if (isInlineTable(parent)) {
|
|
921
|
-
const keyValue = generateKeyValue(child.item.key.value, value, false, format.escapeSequenceUpperCase);
|
|
922
|
-
const regenerated = generateInlineItem(keyValue);
|
|
923
|
-
regenerated.comma = child.comma;
|
|
924
|
-
return regenerated;
|
|
925
|
-
}
|
|
926
|
-
const regenerated = generateInlineItem(value);
|
|
927
|
-
if (parent.items.length === 0 && isInlineArray(parent) && isInlineTable(value)) regenerated.comma = true;
|
|
928
|
-
return regenerated;
|
|
929
|
-
}
|
|
930
|
-
function regenerateInlineReplacement(existing, replacement, changePath) {
|
|
931
|
-
const replacementValue = isInlineItem(replacement) ? replacement.item : replacement;
|
|
932
|
-
if (!isInlineArray(replacementValue) && !isInlineTable(replacementValue)) return replacement;
|
|
933
|
-
let jsValue = rawUpdated;
|
|
934
|
-
for (const key of changePath) jsValue = jsValue === null || jsValue === void 0 ? void 0 : jsValue[key];
|
|
935
|
-
if (!Array.isArray(jsValue) && !isObject(jsValue)) return replacement;
|
|
936
|
-
const parent = findInlineContainerParent(original, existing);
|
|
937
|
-
const parentDepth = parent ? findInlineContainerDepth(original, parent) ?? 0 : -1;
|
|
938
|
-
const parentIsMultiline = parent ? perLine(parent) || parent.items.length === 0 && parent.loc.end.line > parent.loc.start.line : false;
|
|
939
|
-
const kind = Array.isArray(jsValue) ? "array" : "table";
|
|
940
|
-
const mode = kind === "array" ? format.multilineArray : format.multilineTable;
|
|
941
|
-
const selected = resolveInlineContainerLayout(kind, parentDepth + 1, parentIsMultiline, format);
|
|
942
|
-
const existingValue = isInlineItem(existing) ? existing.item : existing;
|
|
943
|
-
const existingInlineValue = isInlineArray(existingValue) || isInlineTable(existingValue) ? existingValue : void 0;
|
|
944
|
-
const existingKind = isInlineArray(existingValue) ? "array" : isInlineTable(existingValue) ? "table" : void 0;
|
|
945
|
-
const preserveSourceLayout = existingInlineValue !== void 0 && existingKind !== kind && hasStructuralMultilineRows(existingInlineValue) && mode === "auto";
|
|
946
|
-
if (!(selected || preserveSourceLayout)) return replacement;
|
|
947
|
-
const modeProperty = kind === "array" ? "multilineArray" : "multilineTable";
|
|
948
|
-
const contextualFormat = resolveTomlFormat({
|
|
949
|
-
...format,
|
|
950
|
-
inlineTableStart: 0,
|
|
951
|
-
[modeProperty]: true,
|
|
952
|
-
trailingComma: trailingCommaExplicit ? format.trailingComma : false
|
|
953
|
-
}, format);
|
|
954
|
-
const wrapper = parseJS({ tmp: jsValue }, contextualFormat, parentDepth + 1, parentIsMultiline).items[0];
|
|
955
|
-
if (!wrapper || !isKeyValue(wrapper)) return replacement;
|
|
956
|
-
const value = wrapper.value;
|
|
957
|
-
if (isInlineArray(value) || isInlineTable(value)) {
|
|
958
|
-
const targetColumn = isInlineItem(existing) ? existing.item.loc.start.column : existing.loc.start.column;
|
|
959
|
-
if (value.loc.end.line === value.loc.start.line) value.loc.end.column += targetColumn - value.loc.start.column;
|
|
960
|
-
else value.loc.end.column = 1;
|
|
961
|
-
value.loc.start.column = targetColumn;
|
|
962
|
-
}
|
|
963
|
-
if (isInlineItem(replacement)) {
|
|
964
|
-
const regenerated = generateInlineItem(value);
|
|
965
|
-
regenerated.comma = replacement.comma;
|
|
966
|
-
return regenerated;
|
|
967
|
-
}
|
|
968
|
-
return value;
|
|
969
|
-
}
|
|
970
|
-
const originalAotItems = changes.some((change) => {
|
|
971
|
-
if (!isAdd(change)) return false;
|
|
972
|
-
const child = findByPath(updated, change.path);
|
|
973
|
-
if (child && isTableArray(child)) return true;
|
|
974
|
-
const parentPath = change.path.slice(0, -1);
|
|
975
|
-
const index = last(change.path);
|
|
976
|
-
const sibling = tryFindByPath(original, parentPath.concat(0));
|
|
977
|
-
return isInteger$1(index) && !isInteger$1(last(parentPath)) && !!sibling && isTableArray(sibling);
|
|
978
|
-
}) ? [...original.items] : void 0;
|
|
979
|
-
const aotStylePathsByKey = /* @__PURE__ */ new Map();
|
|
980
|
-
const getAotStylePaths = (entryKey, owner) => {
|
|
981
|
-
let stylePathsByKey = aotStylePathsByKey.get(owner);
|
|
982
|
-
if (!stylePathsByKey) {
|
|
983
|
-
stylePathsByKey = /* @__PURE__ */ new Map();
|
|
984
|
-
aotStylePathsByKey.set(owner, stylePathsByKey);
|
|
985
|
-
}
|
|
986
|
-
const cacheKey = JSON.stringify(entryKey);
|
|
987
|
-
if (stylePathsByKey.has(cacheKey)) return stylePathsByKey.get(cacheKey);
|
|
988
|
-
const paths = {
|
|
989
|
-
tablePaths: /* @__PURE__ */ new Set(),
|
|
990
|
-
tableArrayPaths: /* @__PURE__ */ new Set(),
|
|
991
|
-
inlineTablePaths: /* @__PURE__ */ new Set()
|
|
992
|
-
};
|
|
993
|
-
const items = originalAotItems ?? original.items;
|
|
994
|
-
const ownerScope = owner ? new Set(findAotEntryScopeItems(original, owner, items.indexOf(owner), items)) : void 0;
|
|
995
|
-
const collectNestedInlineTableStyles = (table, basePath) => {
|
|
996
|
-
for (const item of table.items) {
|
|
997
|
-
if (!isInlineItem(item) || !isKeyValue(item.item) || !isInlineTable(item.item.value)) continue;
|
|
998
|
-
const path = basePath.concat(item.item.key.value);
|
|
999
|
-
paths.inlineTablePaths.add(JSON.stringify(path));
|
|
1000
|
-
collectNestedInlineTableStyles(item.item.value, path);
|
|
1001
|
-
}
|
|
1002
|
-
};
|
|
1003
|
-
const collectInlineTableStyles = (container, basePath) => {
|
|
1004
|
-
for (const item of container.items) {
|
|
1005
|
-
if (!isKeyValue(item) || !isInlineTable(item.value)) continue;
|
|
1006
|
-
const path = basePath.concat(item.key.value);
|
|
1007
|
-
paths.inlineTablePaths.add(JSON.stringify(path));
|
|
1008
|
-
collectNestedInlineTableStyles(item.value, path);
|
|
1009
|
-
}
|
|
1010
|
-
};
|
|
1011
|
-
for (let index = 0; index < items.length; index++) {
|
|
1012
|
-
const entry = items[index];
|
|
1013
|
-
if (!isTableArray(entry) || !arraysEqual(entry.key.item.value, entryKey) || ownerScope && !ownerScope.has(entry)) continue;
|
|
1014
|
-
collectInlineTableStyles(entry, entryKey);
|
|
1015
|
-
const sections = findAotEntryScopeItems(original, entry, index, items);
|
|
1016
|
-
for (const section of sections) {
|
|
1017
|
-
if (!isTable(section) && !isTableArray(section)) continue;
|
|
1018
|
-
const sectionKey = itemKey(section);
|
|
1019
|
-
if (!sectionKey || sectionKey.length <= entryKey.length || !keyStartsWith(sectionKey, entryKey)) continue;
|
|
1020
|
-
collectInlineTableStyles(section, sectionKey);
|
|
1021
|
-
if (isTable(section)) paths.tablePaths.add(JSON.stringify(sectionKey));
|
|
1022
|
-
else paths.tableArrayPaths.add(JSON.stringify(sectionKey));
|
|
1023
|
-
}
|
|
1024
|
-
}
|
|
1025
|
-
const result = paths.tablePaths.size > 0 || paths.tableArrayPaths.size > 0 || paths.inlineTablePaths.size > 0 ? paths : void 0;
|
|
1026
|
-
stylePathsByKey.set(cacheKey, result);
|
|
1027
|
-
return result;
|
|
1028
|
-
};
|
|
1029
|
-
changes.forEach((change) => {
|
|
1030
|
-
if (isAdd(change)) {
|
|
1031
|
-
var _findHostContainer;
|
|
1032
|
-
let child = findByPath(updated, change.path);
|
|
1033
|
-
const parent_path = change.path.slice(0, -1);
|
|
1034
|
-
let index = last(change.path);
|
|
1035
|
-
let is_table_array = isTableArray(child);
|
|
1036
|
-
if (isInteger$1(index) && !is_table_array && !isInteger$1(last(parent_path))) {
|
|
1037
|
-
const sibling = tryFindByPath(original, parent_path.concat(0));
|
|
1038
|
-
if (sibling && isTableArray(sibling)) is_table_array = true;
|
|
1039
|
-
}
|
|
1040
|
-
const owner = findAotEntryForPath(original, parent_path);
|
|
1041
|
-
if (is_table_array && isInteger$1(index) && !isInteger$1(last(parent_path))) {
|
|
1042
|
-
let updatedArray = convertedUpdated;
|
|
1043
|
-
for (const segment of parent_path) updatedArray = updatedArray === null || updatedArray === void 0 ? void 0 : updatedArray[segment];
|
|
1044
|
-
if (Array.isArray(updatedArray) && !updatedArray.every(isObject)) {
|
|
1045
|
-
const arrayKey = parent_path.filter((segment) => typeof segment === "string");
|
|
1046
|
-
const explicitParent = findDeepestExplicitTable(original, arrayKey, (owner === null || owner === void 0 ? void 0 : owner.key.item.value.length) ?? 0, owner);
|
|
1047
|
-
const parentKey = (explicitParent === null || explicitParent === void 0 ? void 0 : explicitParent.key.item.value) ?? (owner === null || owner === void 0 ? void 0 : owner.key.item.value) ?? [];
|
|
1048
|
-
const replacementRow = generateExplicitParentRow(arrayKey.slice(parentKey.length), updatedArray, format);
|
|
1049
|
-
if (replacementRow) {
|
|
1050
|
-
const oldSections = findAotScopedDocumentItems(original, arrayKey, owner).filter((item) => (isTable(item) || isTableArray(item)) && keyStartsWith(itemKey(item), arrayKey));
|
|
1051
|
-
const oldSectionSet = new Set(oldSections);
|
|
1052
|
-
const commentsToRelocate = /* @__PURE__ */ new Set();
|
|
1053
|
-
for (const section of oldSections) traverse(section, { Comment: (comment) => commentsToRelocate.add(comment) });
|
|
1054
|
-
for (const group of resolveGroups(original)) {
|
|
1055
|
-
if (!group.member || !oldSectionSet.has(group.member)) continue;
|
|
1056
|
-
for (const item of group.items) if (isComment(item)) commentsToRelocate.add(item);
|
|
1057
|
-
}
|
|
1058
|
-
const comments = [...commentsToRelocate].sort((a, b) => a.loc.start.line - b.loc.start.line || a.loc.start.column - b.loc.start.column).map((comment) => comment.raw);
|
|
1059
|
-
for (const section of oldSections.reverse()) removeMember(original, original, section);
|
|
1060
|
-
const destination = explicitParent ?? owner ?? original;
|
|
1061
|
-
let insertionIndex = isDocument(destination) ? rootKeyValueInsertIndex(original) : void 0;
|
|
1062
|
-
for (const comment of comments) {
|
|
1063
|
-
insert(original, destination, generateComment(comment), insertionIndex);
|
|
1064
|
-
if (insertionIndex !== void 0) insertionIndex++;
|
|
1065
|
-
}
|
|
1066
|
-
insert(original, destination, replacementRow, insertionIndex);
|
|
1067
|
-
commentEligibleNodes.add(replacementRow);
|
|
1068
|
-
return;
|
|
1069
|
-
}
|
|
1070
|
-
}
|
|
1071
|
-
}
|
|
1072
|
-
if (is_table_array && isInteger$1(index) && !isInteger$1(last(parent_path))) {
|
|
1073
|
-
const existingParent = tryFindByPath(original, parent_path);
|
|
1074
|
-
const existingArray = existingParent && isKeyValue(existingParent) ? existingParent.value : existingParent;
|
|
1075
|
-
if (existingArray && isInlineArray(existingArray)) {
|
|
1076
|
-
let jsValue = convertedUpdated;
|
|
1077
|
-
for (const k of change.path) jsValue = jsValue === null || jsValue === void 0 ? void 0 : jsValue[k];
|
|
1078
|
-
const inlineFmt = resolveTomlFormat({
|
|
1079
|
-
...format,
|
|
1080
|
-
inlineTableStart: 0
|
|
1081
|
-
}, format);
|
|
1082
|
-
const valueDoc = jsValue === void 0 ? void 0 : parseJS({ tmp: jsValue }, inlineFmt);
|
|
1083
|
-
const wrapper = valueDoc === null || valueDoc === void 0 ? void 0 : valueDoc.items[0];
|
|
1084
|
-
if (wrapper && isKeyValue(wrapper)) {
|
|
1085
|
-
child = generateInlineItem(wrapper.value);
|
|
1086
|
-
is_table_array = false;
|
|
1087
|
-
if (isInlineItem(child) && isInlineTable(child.item) && child.item.items.length > 0) {
|
|
1088
|
-
const table = child.item;
|
|
1089
|
-
if (!(table.items[0].loc.start.column - table.loc.start.column > 1)) {
|
|
1090
|
-
const sibling = existingArray.items.map((item) => item.item).find((item) => isInlineTable(item) && item.items.length > 0);
|
|
1091
|
-
if (sibling && sibling.items[0].loc.start.column - sibling.loc.start.column > 1) applyBracketSpacing(original, table, true);
|
|
1092
|
-
}
|
|
1093
|
-
}
|
|
1094
|
-
}
|
|
1095
|
-
}
|
|
1096
|
-
}
|
|
1097
|
-
const tableArrayKey = parent_path.filter((part) => typeof part === "string");
|
|
1098
|
-
const aotStyle = is_table_array && !inlineTableStartExplicit ? getAotStylePaths(tableArrayKey, owner) : void 0;
|
|
1099
|
-
const preserveScopedSections = is_table_array && !inlineTableStartExplicit && !!aotStyle;
|
|
1100
|
-
let jsValue = convertedUpdated;
|
|
1101
|
-
for (const k of change.path) jsValue = jsValue === null || jsValue === void 0 ? void 0 : jsValue[k];
|
|
1102
|
-
const hasMatchingInlineStyle = (value, relativePath = []) => {
|
|
1103
|
-
if (!value || typeof value !== "object") return false;
|
|
1104
|
-
if (Array.isArray(value)) return value.some((item) => hasMatchingInlineStyle(item, relativePath));
|
|
1105
|
-
return Object.entries(value).some(([key, nestedValue]) => {
|
|
1106
|
-
const fullPath = JSON.stringify(tableArrayKey.concat(relativePath, key));
|
|
1107
|
-
if ((aotStyle === null || aotStyle === void 0 ? void 0 : aotStyle.inlineTablePaths.has(fullPath)) && isObject(nestedValue)) return true;
|
|
1108
|
-
return hasMatchingInlineStyle(nestedValue, relativePath.concat(key));
|
|
1109
|
-
});
|
|
1110
|
-
};
|
|
1111
|
-
const hasMatchingSectionStyle = (value, relativePath = []) => {
|
|
1112
|
-
if (!value || typeof value !== "object") return false;
|
|
1113
|
-
if (Array.isArray(value)) return value.some((item) => hasMatchingSectionStyle(item, relativePath));
|
|
1114
|
-
return Object.entries(value).some(([key, nestedValue]) => {
|
|
1115
|
-
const fullPath = JSON.stringify(tableArrayKey.concat(relativePath, key));
|
|
1116
|
-
if ((aotStyle === null || aotStyle === void 0 ? void 0 : aotStyle.tablePaths.has(fullPath)) && !Array.isArray(nestedValue) || (aotStyle === null || aotStyle === void 0 ? void 0 : aotStyle.tableArrayPaths.has(fullPath)) && Array.isArray(nestedValue)) return true;
|
|
1117
|
-
return hasMatchingSectionStyle(nestedValue, relativePath.concat(key));
|
|
1118
|
-
});
|
|
1119
|
-
};
|
|
1120
|
-
const hasMatchingInlineStyleForEntry = preserveScopedSections && hasMatchingInlineStyle(jsValue);
|
|
1121
|
-
const hasMatchingStyle = preserveScopedSections && (hasMatchingInlineStyleForEntry || hasMatchingSectionStyle(jsValue));
|
|
1122
|
-
if (is_table_array && (!isTableArray(child) || hasMatchingStyle)) {
|
|
1123
|
-
if (jsValue !== void 0) {
|
|
1124
|
-
const freshTableArray = generateTableArray(tableArrayKey, format.escapeSequenceUpperCase);
|
|
1125
|
-
const entryFormat = hasMatchingInlineStyleForEntry ? resolveTomlFormat({
|
|
1126
|
-
...format,
|
|
1127
|
-
inlineTableStart: 0
|
|
1128
|
-
}, format) : format;
|
|
1129
|
-
const entryDoc = parseJS(jsValue, entryFormat);
|
|
1130
|
-
for (const item of entryDoc.items) insert(freshTableArray, freshTableArray, item, void 0);
|
|
1131
|
-
if (preserveScopedSections && aotStyle) {
|
|
1132
|
-
const preserveNestedAotArrays = (container, relativePath) => {
|
|
1133
|
-
for (const item of [...container.items]) {
|
|
1134
|
-
if (isKeyValue(item) && isInlineTable(item.value)) {
|
|
1135
|
-
const fullPath = tableArrayKey.concat(relativePath, item.key.value);
|
|
1136
|
-
if (aotStyle.tablePaths.has(JSON.stringify(fullPath))) {
|
|
1137
|
-
remove(freshTableArray, container, item);
|
|
1138
|
-
const table = generateTable(relativePath.concat(item.key.value), format.escapeSequenceUpperCase);
|
|
1139
|
-
for (const row of item.value.items) insert(freshTableArray, table, row.item, void 0, void 0, void 0, void 0, true);
|
|
1140
|
-
insert(freshTableArray, container, table);
|
|
1141
|
-
preserveNestedAotArrays(table, relativePath.concat(item.key.value));
|
|
1142
|
-
continue;
|
|
1143
|
-
}
|
|
1144
|
-
}
|
|
1145
|
-
if (isKeyValue(item) && isInlineArray(item.value)) {
|
|
1146
|
-
const fullPath = tableArrayKey.concat(relativePath, item.key.value);
|
|
1147
|
-
if (aotStyle.tableArrayPaths.has(JSON.stringify(fullPath)) && item.value.items.length > 0 && item.value.items.every((value) => isInlineItem(value) && isInlineTable(value.item))) {
|
|
1148
|
-
remove(freshTableArray, container, item);
|
|
1149
|
-
for (const value of item.value.items) {
|
|
1150
|
-
const tableArray = generateTableArray(relativePath.concat(item.key.value), format.escapeSequenceUpperCase);
|
|
1151
|
-
for (const row of value.item.items) insert(freshTableArray, tableArray, row.item, void 0, void 0, void 0, void 0, true);
|
|
1152
|
-
insert(freshTableArray, container, tableArray);
|
|
1153
|
-
preserveNestedAotArrays(tableArray, relativePath.concat(item.key.value));
|
|
1154
|
-
}
|
|
1155
|
-
continue;
|
|
1156
|
-
}
|
|
1157
|
-
}
|
|
1158
|
-
if (isTable(item) || isTableArray(item)) preserveNestedAotArrays(item, relativePath.concat(item.key.item.value));
|
|
1159
|
-
}
|
|
1160
|
-
};
|
|
1161
|
-
preserveNestedAotArrays(freshTableArray, []);
|
|
1162
|
-
}
|
|
1163
|
-
const prefixNestedSectionKeys = (node) => {
|
|
1164
|
-
if (!hasItems(node)) return;
|
|
1165
|
-
for (const item of node.items) {
|
|
1166
|
-
if (!isTable(item) && !isTableArray(item)) continue;
|
|
1167
|
-
const holder = item.key;
|
|
1168
|
-
const keyNode = holder.item;
|
|
1169
|
-
const fullKey = tableArrayKey.concat(keyNode.value);
|
|
1170
|
-
keyNode.value = fullKey;
|
|
1171
|
-
keyNode.raw = generateKey(fullKey, format.escapeSequenceUpperCase).raw;
|
|
1172
|
-
keyNode.loc.start.column = holder.loc.start.column + 1;
|
|
1173
|
-
keyNode.loc.end.column = keyNode.loc.start.column + keyNode.raw.length;
|
|
1174
|
-
holder.loc.end.column = keyNode.loc.start.column + keyNode.raw.length + 1;
|
|
1175
|
-
prefixNestedSectionKeys(item);
|
|
1176
|
-
}
|
|
1177
|
-
};
|
|
1178
|
-
prefixNestedSectionKeys(freshTableArray);
|
|
1179
|
-
applyWrites(freshTableArray);
|
|
1180
|
-
child = freshTableArray;
|
|
1181
|
-
}
|
|
1182
|
-
}
|
|
1183
|
-
let parent;
|
|
1184
|
-
if (isTable(child)) parent = original;
|
|
1185
|
-
else if (is_table_array) {
|
|
1186
|
-
parent = original;
|
|
1187
|
-
const document = original;
|
|
1188
|
-
const before = tryFindByPath(document, parent_path.concat(index - 1));
|
|
1189
|
-
const after = tryFindByPath(document, parent_path.concat(index));
|
|
1190
|
-
if (after) index = document.items.indexOf(after);
|
|
1191
|
-
else if (before) {
|
|
1192
|
-
index = document.items.indexOf(before) + 1;
|
|
1193
|
-
if (isTableArray(before)) {
|
|
1194
|
-
const aotKey = before.key.item.value;
|
|
1195
|
-
let lastSubTable = index - 1;
|
|
1196
|
-
for (let i = index; i < document.items.length; i++) {
|
|
1197
|
-
const item = document.items[i];
|
|
1198
|
-
if (isTableArray(item)) {
|
|
1199
|
-
const itemKey = item.key.item.value;
|
|
1200
|
-
if (itemKey.length < aotKey.length && arraysEqual(aotKey.slice(0, itemKey.length), itemKey)) break;
|
|
1201
|
-
}
|
|
1202
|
-
const key = isTable(item) || isTableArray(item) ? item.key.item.value : void 0;
|
|
1203
|
-
if (key && key.length > aotKey.length && arraysEqual(key.slice(0, aotKey.length), aotKey)) lastSubTable = i;
|
|
1204
|
-
}
|
|
1205
|
-
index = lastSubTable + 1;
|
|
1206
|
-
if (isTableArray(child)) child = preserveAotEntryDottedKeys(child, before);
|
|
1207
|
-
}
|
|
1208
|
-
} else index = document.items.length;
|
|
1209
|
-
} else {
|
|
1210
|
-
parent = original;
|
|
1211
|
-
for (let pLen = change.path.length - 1; pLen >= 0; pLen--) {
|
|
1212
|
-
const candidate = tryFindByPath(original, change.path.slice(0, pLen));
|
|
1213
|
-
if (!candidate) continue;
|
|
1214
|
-
if (isPrefixMatchedNode(candidate, change.path.slice(0, pLen))) continue;
|
|
1215
|
-
parent = candidate;
|
|
1216
|
-
break;
|
|
1217
|
-
}
|
|
1218
|
-
if (isKeyValue(parent)) parent = parent.value;
|
|
1219
|
-
else if (isInlineItem(parent) && isKeyValue(parent.item)) parent = parent.item.value;
|
|
1220
|
-
else if (isInlineItem(parent) && isInlineTable(parent.item)) parent = parent.item;
|
|
1221
|
-
else if (isInlineItem(parent) && isInlineArray(parent.item)) parent = parent.item;
|
|
1222
|
-
}
|
|
1223
|
-
if (isInlineArray(parent) || isInlineTable(parent)) {
|
|
1224
|
-
child = regenerateInlineChildForParent(parent, change.path, child);
|
|
1225
|
-
prepareInsertedNestedInlineContainer(parent, child, format.indentWidth);
|
|
1226
|
-
}
|
|
1227
|
-
if (isInlineArray(parent)) {
|
|
1228
|
-
const rowNode = tryFindByPath(original, parent_path);
|
|
1229
|
-
const rowContainer = tryFindByPath(original, parent_path.slice(0, -1));
|
|
1230
|
-
if (rowNode && isKeyValue(rowNode) && rowContainer) {
|
|
1231
|
-
const deltaColumns = getInlineInsertColumnDelta(parent, child, index);
|
|
1232
|
-
if (deltaColumns !== 0) preserveAlignedInlineCommentForDelta(rowContainer, rowNode, deltaColumns);
|
|
1233
|
-
}
|
|
1234
|
-
}
|
|
1235
|
-
const multilineInlineParent = (isInlineArray(parent) || isInlineTable(parent)) && parent.loc.end.line > parent.loc.start.line;
|
|
1236
|
-
if (multilineInlineParent && insertedInlineContainers.has(parent)) applyWrites(original);
|
|
1237
|
-
const inlineHostItems = isInlineArray(parent) || isInlineTable(parent) ? (_findHostContainer = findHostContainer(original, parent)) === null || _findHostContainer === void 0 ? void 0 : _findHostContainer.items : void 0;
|
|
1238
|
-
if (isTableArray(parent) || isInlineArray(parent) || isDocument(parent)) {
|
|
1239
|
-
if (is_table_array && !isInteger$1(last(parent_path))) normalizeAotEntryComments(original, true);
|
|
1240
|
-
if (isInlineArray(parent)) {
|
|
1241
|
-
const originalHadTrailingCommas = trailingCommaForAddedItem(parent, arrayHadTrailingCommas(parent));
|
|
1242
|
-
if (isInlineItem(child)) {
|
|
1243
|
-
const generatedMultilineTable = parent.items.length === 0 && isInlineTable(child.item) && perLine(child.item);
|
|
1244
|
-
child.comma = generatedMultilineTable || originalHadTrailingCommas;
|
|
1245
|
-
const addedItem = child;
|
|
1246
|
-
const addedTable = isInlineTable(addedItem.item) ? addedItem.item : void 0;
|
|
1247
|
-
const siblingTable = addedTable && addedTable.items.length > 0 ? parent.items.map((item) => item.item).find((item) => item !== addedItem.item && isInlineTable(item)) : void 0;
|
|
1248
|
-
if (addedTable && siblingTable) {
|
|
1249
|
-
const wanted = tableHadTrailingCommas(siblingTable);
|
|
1250
|
-
if (wanted !== tableHadTrailingCommas(addedTable)) {
|
|
1251
|
-
let jsValue = convertedUpdated;
|
|
1252
|
-
for (const k of change.path) jsValue = jsValue === null || jsValue === void 0 ? void 0 : jsValue[k];
|
|
1253
|
-
const matchedFmt = resolveTomlFormat({
|
|
1254
|
-
...format,
|
|
1255
|
-
inlineTableStart: 0,
|
|
1256
|
-
trailingComma: wanted
|
|
1257
|
-
}, format);
|
|
1258
|
-
const rebuilt = jsValue === void 0 ? void 0 : parseJS({ tmp: jsValue }, matchedFmt).items[0];
|
|
1259
|
-
if (rebuilt && isKeyValue(rebuilt)) {
|
|
1260
|
-
const replacementItem = generateInlineItem(rebuilt.value);
|
|
1261
|
-
replacementItem.comma = addedItem.comma;
|
|
1262
|
-
child = replacementItem;
|
|
1263
|
-
}
|
|
1264
|
-
}
|
|
1265
|
-
}
|
|
1266
|
-
}
|
|
1267
|
-
}
|
|
1268
|
-
if (format.inlineTableStart !== void 0 && format.inlineTableStart > 0 && isDocument(parent) && isTable(child)) {
|
|
1269
|
-
const additionalTables = convertNestedInlineTablesToMultiline(child, original, format);
|
|
1270
|
-
insert(original, parent, child, index);
|
|
1271
|
-
for (const table of additionalTables) insert(original, original, table, void 0);
|
|
1272
|
-
} else {
|
|
1273
|
-
let childToInsert = child;
|
|
1274
|
-
if (isInlineItem(child) && (isTableArray(parent) || isDocument(parent))) childToInsert = child.item;
|
|
1275
|
-
let resolvedIndex = index;
|
|
1276
|
-
if (isDocument(parent) && isKeyValue(childToInsert)) {
|
|
1277
|
-
const rootTableEnd = rootKeyValueInsertIndex(parent);
|
|
1278
|
-
if (rootTableEnd !== void 0) resolvedIndex = rootTableEnd;
|
|
1279
|
-
}
|
|
1280
|
-
let restoredKeySegments = false;
|
|
1281
|
-
if ((isTableArray(parent) || isDocument(parent)) && isKeyValue(childToInsert)) {
|
|
1282
|
-
const restored = restoreMissingKeySegments(parent_path, childToInsert, change.path);
|
|
1283
|
-
if (restored) {
|
|
1284
|
-
if (restoredInsertContainers.has(parent)) applyWrites(original);
|
|
1285
|
-
childToInsert = restored;
|
|
1286
|
-
restoredKeySegments = true;
|
|
1287
|
-
}
|
|
1288
|
-
}
|
|
1289
|
-
if (isInlineArray(parent) && parent.items.length === 0) applyWrites(original);
|
|
1290
|
-
if (resolvedIndex === 0 && isInlineArray(parent) && getPendingEnterOffsets(original).has(parent)) applyWrites(original);
|
|
1291
|
-
if (resolvedIndex === 0 && (isDocument(parent) || isTable(parent) || isTableArray(parent)) && getPendingEnterOffsets(original).has(parent)) applyWrites(original);
|
|
1292
|
-
if (isKeyValue(childToInsert) && childToInsert.key.value.length > 1) {
|
|
1293
|
-
const style = findDottedKeyStyle(original, childToInsert.key.value.slice(0, -1));
|
|
1294
|
-
if (style) {
|
|
1295
|
-
const oldEquals = childToInsert.equals;
|
|
1296
|
-
preserveDottedKeySpacing(childToInsert.key, style.key.raw);
|
|
1297
|
-
childToInsert.equals = childToInsert.key.loc.end.column + style.equals - style.key.loc.end.column;
|
|
1298
|
-
const columnDelta = childToInsert.equals - oldEquals;
|
|
1299
|
-
shiftNode(childToInsert.value, {
|
|
1300
|
-
lines: 0,
|
|
1301
|
-
columns: columnDelta
|
|
1302
|
-
}, { first_line_only: true });
|
|
1303
|
-
if (childToInsert.loc.end.line === childToInsert.loc.start.line) childToInsert.loc.end.column += columnDelta;
|
|
1304
|
-
}
|
|
1305
|
-
}
|
|
1306
|
-
insert(original, parent, childToInsert, resolvedIndex, void 0, inlineHostItems);
|
|
1307
|
-
if (restoredKeySegments) restoredInsertContainers.add(parent);
|
|
1308
|
-
}
|
|
1309
|
-
} else if (isInlineTable(parent)) {
|
|
1310
|
-
if (parent.items.length === 0) {
|
|
1311
|
-
applyWrites(original);
|
|
1312
|
-
if (hasInlineContainerNeedingTighten(parent) && parent.loc.end.line > parent.loc.start.line) parent.loc.end.line = parent.loc.start.line;
|
|
1313
|
-
}
|
|
1314
|
-
const originalHadTrailingCommas = trailingCommaForAddedItem(parent, tableHadTrailingCommas(parent));
|
|
1315
|
-
if (isKeyValue(child)) {
|
|
1316
|
-
const inlineItem = generateInlineItem(child);
|
|
1317
|
-
inlineItem.comma = originalHadTrailingCommas;
|
|
1318
|
-
const insertIndex = inlineTableAddIndex(parent, child, change.before);
|
|
1319
|
-
insert(original, parent, inlineItem, insertIndex, void 0, inlineHostItems);
|
|
1320
|
-
} else if (isInlineItem(child) && isKeyValue(child.item)) {
|
|
1321
|
-
let kv = child.item;
|
|
1322
|
-
const restored = restoreMissingKeySegments(parent_path, kv, change.path);
|
|
1323
|
-
if (restored) {
|
|
1324
|
-
if (restoredInsertContainers.has(parent)) applyWrites(original);
|
|
1325
|
-
kv = restored;
|
|
1326
|
-
}
|
|
1327
|
-
const inlineItem = generateInlineItem(kv);
|
|
1328
|
-
inlineItem.comma = originalHadTrailingCommas;
|
|
1329
|
-
const insertIndex = inlineTableAddIndex(parent, kv, change.before);
|
|
1330
|
-
insert(original, parent, inlineItem, insertIndex, void 0, inlineHostItems);
|
|
1331
|
-
if (restored) restoredInsertContainers.add(parent);
|
|
1332
|
-
} else insert(original, parent, child, void 0, void 0, inlineHostItems);
|
|
1333
|
-
} else if (format.inlineTableStart !== void 0 && format.inlineTableStart > 0 && isKeyValue(child) && isInlineTable(child.value) && isTable(parent)) {
|
|
1334
|
-
const nestedTableKey = [...parent.key.item.value, ...child.key.value];
|
|
1335
|
-
if (calculateTableDepth(nestedTableKey) < format.inlineTableStart) convertInlineTableToSeparateSection(child, parent, original, format);
|
|
1336
|
-
else insert(original, parent, child);
|
|
1337
|
-
} else if (format.inlineTableStart === 0 && isKeyValue(child) && isInlineTable(child.value) && isDocument(parent)) insert(original, parent, child, void 0, true);
|
|
1338
|
-
else {
|
|
1339
|
-
let childToInsert = child;
|
|
1340
|
-
if (isInlineItem(child) && (isTable(parent) || isTableArray(parent) || isDocument(parent))) childToInsert = child.item;
|
|
1341
|
-
let restoredKeySegments = false;
|
|
1342
|
-
if (isTable(parent) && isKeyValue(childToInsert)) {
|
|
1343
|
-
const restored = restoreMissingKeySegments(parent_path, childToInsert, change.path);
|
|
1344
|
-
if (restored) {
|
|
1345
|
-
if (restoredInsertContainers.has(parent)) applyWrites(original);
|
|
1346
|
-
childToInsert = restored;
|
|
1347
|
-
restoredKeySegments = true;
|
|
1348
|
-
}
|
|
1349
|
-
}
|
|
1350
|
-
const leadingLines = (isTable(parent) || isTableArray(parent)) && parent.items.length > 0 && parent.items.every(isComment) ? 2 : void 0;
|
|
1351
|
-
insert(original, parent, childToInsert, void 0, void 0, void 0, leadingLines);
|
|
1352
|
-
if (restoredKeySegments) restoredInsertContainers.add(parent);
|
|
1353
|
-
}
|
|
1354
|
-
if (multilineInlineParent) insertedInlineContainers.add(parent);
|
|
1355
|
-
} else if (isEdit(change)) {
|
|
1356
|
-
let existing = tryFindByPath(original, change.path);
|
|
1357
|
-
let replacement;
|
|
1358
|
-
try {
|
|
1359
|
-
replacement = findByPath(updated, change.path);
|
|
1360
|
-
} catch {
|
|
1361
|
-
replacement = void 0;
|
|
1362
|
-
}
|
|
1363
|
-
if (!existing || !replacement) {
|
|
1364
|
-
handleStructuralEdit(original, updated, change, format, temporal, commentEligibleNodes, materialisedTables);
|
|
1365
|
-
return;
|
|
1366
|
-
}
|
|
1367
|
-
if (isKeyValue(existing) && isTable(replacement)) {
|
|
1368
|
-
const tableKey = replacement.key.item.value;
|
|
1369
|
-
if (findDocumentItemsByKeyPrefix(original, tableKey).some((n) => n !== existing)) {
|
|
1370
|
-
handleStructuralEdit(original, updated, change, format, temporal, commentEligibleNodes, materialisedTables);
|
|
1371
|
-
return;
|
|
1372
|
-
}
|
|
1373
|
-
}
|
|
1374
|
-
if (isInlineItem(existing) && (isTable(replacement) || isTableArray(replacement))) {
|
|
1375
|
-
const parentArr = tryFindByPath(original, change.path.slice(0, -1));
|
|
1376
|
-
const parentArray = parentArr && isKeyValue(parentArr) ? parentArr.value : parentArr;
|
|
1377
|
-
if (parentArray && isInlineArray(parentArray)) {
|
|
1378
|
-
let jsValue = rawUpdated;
|
|
1379
|
-
for (const k of change.path) jsValue = jsValue === null || jsValue === void 0 ? void 0 : jsValue[k];
|
|
1380
|
-
if (jsValue !== void 0) {
|
|
1381
|
-
const inlineFmt = resolveTomlFormat({
|
|
1382
|
-
...format,
|
|
1383
|
-
inlineTableStart: 0
|
|
1384
|
-
}, format);
|
|
1385
|
-
const valueDoc = parseJS({ tmp: jsValue }, inlineFmt);
|
|
1386
|
-
const wrapper = valueDoc === null || valueDoc === void 0 ? void 0 : valueDoc.items[0];
|
|
1387
|
-
if (wrapper && isKeyValue(wrapper)) replacement = generateInlineItem(wrapper.value);
|
|
1388
|
-
}
|
|
1389
|
-
}
|
|
1390
|
-
}
|
|
1391
|
-
let parent;
|
|
1392
|
-
let containerParent = tryFindByPath(original, change.path.slice(0, -1));
|
|
1393
|
-
if (containerParent && isKeyValue(containerParent) && isPrefixMatchedNode(containerParent, change.path.slice(0, -1))) containerParent = findStructuralParent(original, containerParent);
|
|
1394
|
-
const inlineTableRowContext = findEnclosingInlineTableRowContext(original, change.path);
|
|
1395
|
-
if (isKeyValue(existing) && isKeyValue(replacement)) {
|
|
1396
|
-
let keyTruncated = false;
|
|
1397
|
-
if (existing.key.value.length > 1) {
|
|
1398
|
-
const absPath = absolutePathOf(existing);
|
|
1399
|
-
const matchLen = truncationMatchLen(change.path, existing.key.value, absPath ? absPath.length - existing.key.value.length : 0);
|
|
1400
|
-
if (matchLen > 0 && matchLen < existing.key.value.length) {
|
|
1401
|
-
existing.key.value = existing.key.value.slice(0, matchLen);
|
|
1402
|
-
existing.key.raw = generateKey(existing.key.value).raw;
|
|
1403
|
-
const oldEndCol = existing.key.loc.end.column;
|
|
1404
|
-
const newEndCol = existing.key.loc.start.column + existing.key.raw.length;
|
|
1405
|
-
const delta = newEndCol - oldEndCol;
|
|
1406
|
-
existing.key.loc.end.column = newEndCol;
|
|
1407
|
-
existing.equals += delta;
|
|
1408
|
-
existing.value.loc.start.column += delta;
|
|
1409
|
-
if (existing.value.loc.end.line === existing.value.loc.start.line) existing.value.loc.end.column += delta;
|
|
1410
|
-
if (existing.loc.end.line === existing.loc.start.line) existing.loc.end.column += delta;
|
|
1411
|
-
keyTruncated = true;
|
|
1412
|
-
const truncatedPrefix = existing.key.value;
|
|
1413
|
-
if (containerParent && (isTable(containerParent) || isDocument(containerParent) || isTableArray(containerParent))) {
|
|
1414
|
-
const parentItems = [...containerParent.items];
|
|
1415
|
-
for (let si = parentItems.length - 1; si >= 0; si--) {
|
|
1416
|
-
const sibling = parentItems[si];
|
|
1417
|
-
if (sibling === existing) continue;
|
|
1418
|
-
const siblingKey = isKeyValue(sibling) ? sibling.key.value : isTable(sibling) || isTableArray(sibling) ? sibling.key.item.value : void 0;
|
|
1419
|
-
if (siblingKey && siblingKey.length >= truncatedPrefix.length && arraysEqual(siblingKey.slice(0, truncatedPrefix.length), truncatedPrefix)) removeMember(original, containerParent, sibling);
|
|
1420
|
-
}
|
|
1421
|
-
}
|
|
1422
|
-
removeDocumentItemsExtendingPath(original, change.path, existing);
|
|
1423
|
-
}
|
|
1424
|
-
}
|
|
1425
|
-
if (!keyTruncated && isKeyValue(replacement)) {
|
|
1426
|
-
if (containerParent && (isTable(containerParent) || isDocument(containerParent) || isTableArray(containerParent))) removeSiblingsExtendingPrefix(original, containerParent, existing.key.value, existing);
|
|
1427
|
-
}
|
|
1428
|
-
preserveFormatting(existing.value, replacement.value, format.escapeSequenceUpperCase, format.minimumTimeDecimals ?? 0);
|
|
1429
|
-
if (containerParent) preserveAlignedInlineCommentColumn(containerParent, existing, existing.value, replacement.value);
|
|
1430
|
-
parent = existing;
|
|
1431
|
-
existing = existing.value;
|
|
1432
|
-
replacement = replacement.value;
|
|
1433
|
-
if (keyTruncated && rawUpdated !== void 0) {
|
|
1434
|
-
let jsValue = rawUpdated;
|
|
1435
|
-
for (const k of change.path) jsValue = jsValue === null || jsValue === void 0 ? void 0 : jsValue[k];
|
|
1436
|
-
if (jsValue !== void 0) {
|
|
1437
|
-
const freshValue = regenerateValue(jsValue, format);
|
|
1438
|
-
if (freshValue !== void 0) {
|
|
1439
|
-
replacement = freshValue;
|
|
1440
|
-
preserveFormatting(existing, replacement, format.escapeSequenceUpperCase, format.minimumTimeDecimals ?? 0);
|
|
1441
|
-
}
|
|
1442
|
-
}
|
|
1443
|
-
}
|
|
1444
|
-
} else if (isKeyValue(existing) && isInlineItem(replacement) && isKeyValue(replacement.item)) {
|
|
1445
|
-
let keyTruncated = false;
|
|
1446
|
-
if (existing.key.value.length > 1) {
|
|
1447
|
-
const absPath = absolutePathOf(existing);
|
|
1448
|
-
const matchLen = truncationMatchLen(change.path, existing.key.value, absPath ? absPath.length - existing.key.value.length : 0);
|
|
1449
|
-
if (matchLen > 0 && matchLen < existing.key.value.length) {
|
|
1450
|
-
existing.key.value = existing.key.value.slice(0, matchLen);
|
|
1451
|
-
existing.key.raw = generateKey(existing.key.value).raw;
|
|
1452
|
-
const oldEndCol = existing.key.loc.end.column;
|
|
1453
|
-
const newEndCol = existing.key.loc.start.column + existing.key.raw.length;
|
|
1454
|
-
const delta = newEndCol - oldEndCol;
|
|
1455
|
-
existing.key.loc.end.column = newEndCol;
|
|
1456
|
-
existing.equals += delta;
|
|
1457
|
-
existing.value.loc.start.column += delta;
|
|
1458
|
-
if (existing.value.loc.end.line === existing.value.loc.start.line) existing.value.loc.end.column += delta;
|
|
1459
|
-
if (existing.loc.end.line === existing.loc.start.line) existing.loc.end.column += delta;
|
|
1460
|
-
keyTruncated = true;
|
|
1461
|
-
const truncatedPrefix = existing.key.value;
|
|
1462
|
-
if (containerParent && (isTable(containerParent) || isDocument(containerParent) || isTableArray(containerParent))) {
|
|
1463
|
-
const parentItems = [...containerParent.items];
|
|
1464
|
-
for (let si = parentItems.length - 1; si >= 0; si--) {
|
|
1465
|
-
const sibling = parentItems[si];
|
|
1466
|
-
if (sibling === existing) continue;
|
|
1467
|
-
const siblingKey = isKeyValue(sibling) ? sibling.key.value : isTable(sibling) || isTableArray(sibling) ? sibling.key.item.value : void 0;
|
|
1468
|
-
if (siblingKey && siblingKey.length >= truncatedPrefix.length && arraysEqual(siblingKey.slice(0, truncatedPrefix.length), truncatedPrefix)) removeMember(original, containerParent, sibling);
|
|
1469
|
-
}
|
|
1470
|
-
}
|
|
1471
|
-
removeDocumentItemsExtendingPath(original, change.path, existing);
|
|
1472
|
-
}
|
|
1473
|
-
}
|
|
1474
|
-
if (!keyTruncated && isKeyValue(replacement.item)) {
|
|
1475
|
-
if (containerParent && (isTable(containerParent) || isDocument(containerParent) || isTableArray(containerParent))) removeSiblingsExtendingPrefix(original, containerParent, existing.key.value, existing);
|
|
1476
|
-
}
|
|
1477
|
-
parent = existing;
|
|
1478
|
-
existing = existing.value;
|
|
1479
|
-
replacement = replacement.item.value;
|
|
1480
|
-
if (keyTruncated && rawUpdated !== void 0) {
|
|
1481
|
-
let jsValue = rawUpdated;
|
|
1482
|
-
for (const k of change.path) jsValue = jsValue === null || jsValue === void 0 ? void 0 : jsValue[k];
|
|
1483
|
-
if (jsValue !== void 0) {
|
|
1484
|
-
const freshValue = regenerateValue(jsValue, format);
|
|
1485
|
-
if (freshValue !== void 0) replacement = freshValue;
|
|
1486
|
-
}
|
|
1487
|
-
}
|
|
1488
|
-
} else if (isInlineItem(existing) && isKeyValue(existing.item) && isKeyValue(replacement)) {
|
|
1489
|
-
const existingKeyValue = existing.item;
|
|
1490
|
-
let keyTruncated = false;
|
|
1491
|
-
if (existingKeyValue.key.value.length > 1) {
|
|
1492
|
-
const absPath = absolutePathOf(existingKeyValue);
|
|
1493
|
-
const matchLen = truncationMatchLen(change.path, existingKeyValue.key.value, absPath ? absPath.length - existingKeyValue.key.value.length : 0);
|
|
1494
|
-
if (matchLen > 0 && matchLen < existingKeyValue.key.value.length) {
|
|
1495
|
-
existingKeyValue.key.value = existingKeyValue.key.value.slice(0, matchLen);
|
|
1496
|
-
existingKeyValue.key.raw = generateKey(existingKeyValue.key.value, format.escapeSequenceUpperCase).raw;
|
|
1497
|
-
const oldEndCol = existingKeyValue.key.loc.end.column;
|
|
1498
|
-
const newEndCol = existingKeyValue.key.loc.start.column + existingKeyValue.key.raw.length;
|
|
1499
|
-
const delta = newEndCol - oldEndCol;
|
|
1500
|
-
existingKeyValue.key.loc.end.column = newEndCol;
|
|
1501
|
-
existingKeyValue.equals += delta;
|
|
1502
|
-
existingKeyValue.value.loc.start.column += delta;
|
|
1503
|
-
if (existingKeyValue.value.loc.end.line === existingKeyValue.value.loc.start.line) existingKeyValue.value.loc.end.column += delta;
|
|
1504
|
-
if (existingKeyValue.loc.end.line === existingKeyValue.loc.start.line) existingKeyValue.loc.end.column += delta;
|
|
1505
|
-
if (existingKeyValue.loc.end.line === existingKeyValue.loc.start.line) addExitOffset(original, existingKeyValue, {
|
|
1506
|
-
lines: 0,
|
|
1507
|
-
columns: delta
|
|
1508
|
-
});
|
|
1509
|
-
keyTruncated = true;
|
|
1510
|
-
const truncatedPrefix = existingKeyValue.key.value;
|
|
1511
|
-
let scanParent = containerParent;
|
|
1512
|
-
if (scanParent && isInlineItem(scanParent)) scanParent = scanParent.item;
|
|
1513
|
-
if (scanParent && isKeyValue(scanParent)) scanParent = scanParent.value;
|
|
1514
|
-
if (scanParent && hasItems(scanParent)) {
|
|
1515
|
-
const parentItems = [...scanParent.items];
|
|
1516
|
-
for (let si = parentItems.length - 1; si >= 0; si--) {
|
|
1517
|
-
const sibling = parentItems[si];
|
|
1518
|
-
if (sibling === existing) continue;
|
|
1519
|
-
const siblingKey = isKeyValue(sibling) ? sibling.key.value : isInlineItem(sibling) && isKeyValue(sibling.item) ? sibling.item.key.value : isTable(sibling) || isTableArray(sibling) ? sibling.key.item.value : void 0;
|
|
1520
|
-
if (siblingKey && siblingKey.length >= truncatedPrefix.length && arraysEqual(siblingKey.slice(0, truncatedPrefix.length), truncatedPrefix)) removeMember(original, scanParent, sibling);
|
|
1521
|
-
}
|
|
1522
|
-
}
|
|
1523
|
-
}
|
|
1524
|
-
}
|
|
1525
|
-
preserveFormatting(existingKeyValue.value, replacement.value, format.escapeSequenceUpperCase, format.minimumTimeDecimals ?? 0);
|
|
1526
|
-
parent = existingKeyValue;
|
|
1527
|
-
existing = existingKeyValue.value;
|
|
1528
|
-
replacement = replacement.value;
|
|
1529
|
-
if (keyTruncated && rawUpdated !== void 0) {
|
|
1530
|
-
let jsValue = rawUpdated;
|
|
1531
|
-
for (const k of change.path) jsValue = jsValue === null || jsValue === void 0 ? void 0 : jsValue[k];
|
|
1532
|
-
if (jsValue !== void 0) {
|
|
1533
|
-
const freshValue = regenerateValue(jsValue, format);
|
|
1534
|
-
if (freshValue !== void 0) {
|
|
1535
|
-
replacement = freshValue;
|
|
1536
|
-
preserveFormatting(existing, replacement, format.escapeSequenceUpperCase, format.minimumTimeDecimals ?? 0);
|
|
1537
|
-
}
|
|
1538
|
-
}
|
|
1539
|
-
}
|
|
1540
|
-
} else if (isInlineItem(existing) && isInlineItem(replacement) && isKeyValue(existing.item) && isKeyValue(replacement.item)) {
|
|
1541
|
-
let keyTruncated = false;
|
|
1542
|
-
const existingKV = existing.item;
|
|
1543
|
-
if (existingKV.key.value.length > 1) {
|
|
1544
|
-
const absPath = absolutePathOf(existingKV);
|
|
1545
|
-
const matchLen = truncationMatchLen(change.path, existingKV.key.value, absPath ? absPath.length - existingKV.key.value.length : 0);
|
|
1546
|
-
if (matchLen > 0 && matchLen < existingKV.key.value.length) {
|
|
1547
|
-
existingKV.key.value = existingKV.key.value.slice(0, matchLen);
|
|
1548
|
-
existingKV.key.raw = generateKey(existingKV.key.value, format.escapeSequenceUpperCase).raw;
|
|
1549
|
-
const oldEndCol = existingKV.key.loc.end.column;
|
|
1550
|
-
const newEndCol = existingKV.key.loc.start.column + existingKV.key.raw.length;
|
|
1551
|
-
const delta = newEndCol - oldEndCol;
|
|
1552
|
-
existingKV.key.loc.end.column = newEndCol;
|
|
1553
|
-
existingKV.equals += delta;
|
|
1554
|
-
existingKV.value.loc.start.column += delta;
|
|
1555
|
-
if (existingKV.value.loc.end.line === existingKV.value.loc.start.line) existingKV.value.loc.end.column += delta;
|
|
1556
|
-
if (existingKV.loc.end.line === existingKV.loc.start.line) existingKV.loc.end.column += delta;
|
|
1557
|
-
if (existingKV.loc.end.line === existingKV.loc.start.line) addExitOffset(original, existingKV, {
|
|
1558
|
-
lines: 0,
|
|
1559
|
-
columns: delta
|
|
1560
|
-
});
|
|
1561
|
-
keyTruncated = true;
|
|
1562
|
-
const truncatedPrefix = existingKV.key.value;
|
|
1563
|
-
let scanParent = containerParent;
|
|
1564
|
-
if (scanParent && isInlineItem(scanParent)) scanParent = scanParent.item;
|
|
1565
|
-
if (scanParent && isKeyValue(scanParent)) scanParent = scanParent.value;
|
|
1566
|
-
if (scanParent && hasItems(scanParent)) {
|
|
1567
|
-
const parentItems = [...scanParent.items];
|
|
1568
|
-
for (let si = parentItems.length - 1; si >= 0; si--) {
|
|
1569
|
-
const sibling = parentItems[si];
|
|
1570
|
-
if (sibling === existing) continue;
|
|
1571
|
-
const siblingKey = isKeyValue(sibling) ? sibling.key.value : isInlineItem(sibling) && isKeyValue(sibling.item) ? sibling.item.key.value : isTable(sibling) || isTableArray(sibling) ? sibling.key.item.value : void 0;
|
|
1572
|
-
if (siblingKey && siblingKey.length >= truncatedPrefix.length && arraysEqual(siblingKey.slice(0, truncatedPrefix.length), truncatedPrefix)) removeMember(original, scanParent, sibling);
|
|
1573
|
-
}
|
|
1574
|
-
}
|
|
1575
|
-
}
|
|
1576
|
-
}
|
|
1577
|
-
preserveFormatting(existingKV.value, replacement.item.value, format.escapeSequenceUpperCase, format.minimumTimeDecimals ?? 0);
|
|
1578
|
-
parent = existingKV;
|
|
1579
|
-
existing = existingKV.value;
|
|
1580
|
-
replacement = replacement.item.value;
|
|
1581
|
-
if (keyTruncated && rawUpdated !== void 0) {
|
|
1582
|
-
let jsValue = rawUpdated;
|
|
1583
|
-
for (const k of change.path) jsValue = jsValue === null || jsValue === void 0 ? void 0 : jsValue[k];
|
|
1584
|
-
if (jsValue !== void 0) {
|
|
1585
|
-
const freshValue = regenerateValue(jsValue, format);
|
|
1586
|
-
if (freshValue !== void 0) {
|
|
1587
|
-
replacement = freshValue;
|
|
1588
|
-
preserveFormatting(existing, replacement, format.escapeSequenceUpperCase, format.minimumTimeDecimals ?? 0);
|
|
1589
|
-
}
|
|
1590
|
-
}
|
|
1591
|
-
}
|
|
1592
|
-
} else if (isTable(existing)) {
|
|
1593
|
-
let jsValue = convertedUpdated;
|
|
1594
|
-
for (const key of change.path) jsValue = jsValue === null || jsValue === void 0 ? void 0 : jsValue[key];
|
|
1595
|
-
if (jsValue !== void 0) {
|
|
1596
|
-
const existingTableKey = existing.key.item.value;
|
|
1597
|
-
const lastSegment = existingTableKey.slice(-1);
|
|
1598
|
-
const parentKey = existingTableKey.slice(0, -1);
|
|
1599
|
-
const tableParent = findStructuralParent(original, existing) ?? findParent(original, change.path);
|
|
1600
|
-
const aotOwner = findAotEntryForPath(original, change.path);
|
|
1601
|
-
if (isDocument(tableParent)) {
|
|
1602
|
-
const sectionedSiblings = (aotOwner ? findAotScopedDocumentItems(original, existingTableKey, aotOwner) : findDocumentItemsByKeyPrefix(original, existingTableKey)).filter((n) => n !== existing && (isTable(n) || isTableArray(n)));
|
|
1603
|
-
for (const sibling of sectionedSiblings) removeMember(original, tableParent, sibling);
|
|
1604
|
-
}
|
|
1605
|
-
const freshDoc = parseJS({ [lastSegment[0]]: jsValue }, format);
|
|
1606
|
-
const freshKV = freshDoc.items[0];
|
|
1607
|
-
if (isKeyValue(freshKV) && aotOwner && keyStartsWith(parentKey, aotOwner.key.item.value)) {
|
|
1608
|
-
const scalarParent = arraysEqual(parentKey, aotOwner.key.item.value) ? aotOwner : findAotScopedDocumentItems(original, parentKey, aotOwner).find((item) => isTable(item) && arraysEqual(item.key.item.value, parentKey));
|
|
1609
|
-
if (scalarParent) {
|
|
1610
|
-
removeMember(original, tableParent, existing);
|
|
1611
|
-
insert(original, scalarParent, freshKV, void 0);
|
|
1612
|
-
commentEligibleNodes.add(freshKV);
|
|
1613
|
-
return;
|
|
1614
|
-
}
|
|
1615
|
-
}
|
|
1616
|
-
if (parentKey.length > 0) {
|
|
1617
|
-
if (isTable(freshKV) || isTableArray(freshKV)) {
|
|
1618
|
-
extendSectionKeyWithParentAndReplace(freshKV, parentKey, existing, tableParent);
|
|
1619
|
-
commentEligibleNodes.add(freshKV);
|
|
1620
|
-
} else if (findDocumentItemsByKeyPrefix(original, parentKey).some(isKeyValue)) {
|
|
1621
|
-
extendKeyWithParentAndReplace(freshKV, parentKey, existing, tableParent);
|
|
1622
|
-
commentEligibleNodes.add(freshKV);
|
|
1623
|
-
} else {
|
|
1624
|
-
const survivingParent = original.items.find((n) => n !== existing && (isTable(n) || isTableArray(n)) && arraysEqual(n.key.item.value, parentKey));
|
|
1625
|
-
if (survivingParent) {
|
|
1626
|
-
insert(original, survivingParent, freshKV, void 0);
|
|
1627
|
-
removeMember(original, tableParent, existing);
|
|
1628
|
-
commentEligibleNodes.add(freshKV);
|
|
1629
|
-
} else {
|
|
1630
|
-
const newTable = generateTable(parentKey, format.escapeSequenceUpperCase);
|
|
1631
|
-
materialisedTables.add(newTable);
|
|
1632
|
-
insert(original, newTable, freshKV, 0);
|
|
1633
|
-
replace(original, tableParent, existing, newTable);
|
|
1634
|
-
commentEligibleNodes.add(newTable);
|
|
1635
|
-
}
|
|
1636
|
-
}
|
|
1637
|
-
} else {
|
|
1638
|
-
replace(original, tableParent, existing, freshKV);
|
|
1639
|
-
commentEligibleNodes.add(freshKV);
|
|
1640
|
-
hoistRootKeyValueAboveTables(original, freshKV);
|
|
1641
|
-
}
|
|
1642
|
-
if (freshDoc.items.length > 1) for (let i = 1; i < freshDoc.items.length; i++) {
|
|
1643
|
-
const extraEntry = freshDoc.items[i];
|
|
1644
|
-
if (parentKey.length > 0) extendSectionKeyInPlace(extraEntry, parentKey);
|
|
1645
|
-
const prev = i === 1 ? freshKV : freshDoc.items[i - 1];
|
|
1646
|
-
const prevIndex = tableParent.items.indexOf(prev);
|
|
1647
|
-
insert(original, tableParent, extraEntry, prevIndex + 1);
|
|
1648
|
-
commentEligibleNodes.add(extraEntry);
|
|
1649
|
-
}
|
|
1650
|
-
return;
|
|
1651
|
-
}
|
|
1652
|
-
parent = findParent(original, change.path);
|
|
1653
|
-
} else if (isTableArray(existing)) {
|
|
1654
|
-
const updated_js = convertedUpdated;
|
|
1655
|
-
const existingAotKey = existing.key.item.value;
|
|
1656
|
-
let jsValue = updated_js;
|
|
1657
|
-
const navPath = typeof change.path[change.path.length - 1] === "number" ? change.path.slice(0, -1) : change.path;
|
|
1658
|
-
for (const key of navPath) jsValue = jsValue === null || jsValue === void 0 ? void 0 : jsValue[key];
|
|
1659
|
-
if (jsValue !== void 0) {
|
|
1660
|
-
const lastSegment = existingAotKey.slice(-1);
|
|
1661
|
-
const parentKey = existingAotKey.slice(0, -1);
|
|
1662
|
-
const tableParent = findStructuralParent(original, existing) ?? findParent(original, change.path);
|
|
1663
|
-
const aotOwner = findAotEntryForPath(original, navPath);
|
|
1664
|
-
const freshKV = parseJS({ [lastSegment[0]]: jsValue }, format).items[0];
|
|
1665
|
-
if (Array.isArray(jsValue) && !jsValue.every((v) => isObject(v))) {
|
|
1666
|
-
const siblingEntries = (aotOwner ? findAotScopedDocumentItems(original, existingAotKey, aotOwner) : findDocumentItemsByKeyPrefix(original, existingAotKey)).filter((n) => n !== existing && (isTable(n) || isTableArray(n)));
|
|
1667
|
-
for (const sibling of siblingEntries) removeMember(original, original, sibling);
|
|
1668
|
-
}
|
|
1669
|
-
if (parentKey.length > 0) {
|
|
1670
|
-
if (isKeyValue(freshKV) && aotOwner && keyStartsWith(parentKey, aotOwner.key.item.value)) {
|
|
1671
|
-
const replacementParent = arraysEqual(parentKey, aotOwner.key.item.value) ? aotOwner : findAotScopedDocumentItems(original, parentKey, aotOwner).find((item) => isTable(item) && arraysEqual(item.key.item.value, parentKey));
|
|
1672
|
-
if (replacementParent) {
|
|
1673
|
-
removeMember(original, tableParent, existing);
|
|
1674
|
-
insert(original, replacementParent, freshKV, void 0);
|
|
1675
|
-
commentEligibleNodes.add(freshKV);
|
|
1676
|
-
return;
|
|
1677
|
-
}
|
|
1678
|
-
}
|
|
1679
|
-
if (isTable(freshKV) || isTableArray(freshKV)) {
|
|
1680
|
-
extendSectionKeyWithParentAndReplace(freshKV, parentKey, existing, tableParent);
|
|
1681
|
-
commentEligibleNodes.add(freshKV);
|
|
1682
|
-
} else {
|
|
1683
|
-
const existingParentTable = isDocument(tableParent) ? tableParent.items.find((item) => isTable(item) && arraysEqual(item.key.item.value, parentKey)) : void 0;
|
|
1684
|
-
if (existingParentTable) {
|
|
1685
|
-
const existingRow = existingParentTable.items.find((row) => isKeyValue(row) && arraysEqual(row.key.value, freshKV.key.value));
|
|
1686
|
-
if (existingRow) replace(original, existingParentTable, existingRow, freshKV);
|
|
1687
|
-
else insert(original, existingParentTable, freshKV, void 0);
|
|
1688
|
-
removeMember(original, tableParent, existing);
|
|
1689
|
-
commentEligibleNodes.add(existingParentTable);
|
|
1690
|
-
return;
|
|
1691
|
-
}
|
|
1692
|
-
if (findDocumentItemsByKeyPrefix(original, parentKey).some(isKeyValue)) {
|
|
1693
|
-
extendKeyWithParentAndReplace(freshKV, parentKey, existing, tableParent);
|
|
1694
|
-
commentEligibleNodes.add(freshKV);
|
|
1695
|
-
} else {
|
|
1696
|
-
const newTable = generateTable(parentKey, format.escapeSequenceUpperCase);
|
|
1697
|
-
materialisedTables.add(newTable);
|
|
1698
|
-
insert(original, newTable, freshKV, 0);
|
|
1699
|
-
replace(original, tableParent, existing, newTable);
|
|
1700
|
-
commentEligibleNodes.add(newTable);
|
|
1701
|
-
}
|
|
1702
|
-
}
|
|
1703
|
-
} else {
|
|
1704
|
-
replace(original, tableParent, existing, freshKV);
|
|
1705
|
-
commentEligibleNodes.add(freshKV);
|
|
1706
|
-
hoistRootKeyValueAboveTables(original, freshKV);
|
|
1707
|
-
}
|
|
1708
|
-
return;
|
|
1709
|
-
}
|
|
1710
|
-
parent = findParent(original, change.path);
|
|
1711
|
-
} else {
|
|
1712
|
-
parent = findParent(original, change.path);
|
|
1713
|
-
if (isKeyValue(parent)) {
|
|
1714
|
-
const parentPath = change.path.slice(0, -1);
|
|
1715
|
-
const arrayNode = findByPath(original, parentPath);
|
|
1716
|
-
if (isKeyValue(arrayNode) && isInlineArray(arrayNode.value)) parent = arrayNode.value;
|
|
1717
|
-
} else if (isInlineItem(parent) && isKeyValue(parent.item)) parent = parent.item.value;
|
|
1718
|
-
else if (isInlineItem(parent) && isInlineTable(parent.item)) parent = parent.item;
|
|
1719
|
-
else if (isInlineItem(parent) && isInlineArray(parent.item)) parent = parent.item;
|
|
1720
|
-
}
|
|
1721
|
-
if (isInlineItem(existing) && isInlineItem(replacement)) {
|
|
1722
|
-
if (isString(existing.item) && isString(replacement.item)) {
|
|
1723
|
-
preserveFormatting(existing.item, replacement.item, format.escapeSequenceUpperCase, format.minimumTimeDecimals ?? 0);
|
|
1724
|
-
replacement.loc = {
|
|
1725
|
-
start: { ...replacement.item.loc.start },
|
|
1726
|
-
end: { ...replacement.item.loc.end }
|
|
1727
|
-
};
|
|
1728
|
-
}
|
|
1729
|
-
replacement.comma = existing.comma;
|
|
1730
|
-
}
|
|
1731
|
-
if (inlineTableRowContext) {
|
|
1732
|
-
const existingSpan = getSpan(existing.loc);
|
|
1733
|
-
const deltaColumns = getSpan(replacement.loc).columns - existingSpan.columns;
|
|
1734
|
-
if (deltaColumns !== 0) recordInlineTableCommentDelta(inlineTableRowContext.container, inlineTableRowContext.row, deltaColumns);
|
|
1735
|
-
}
|
|
1736
|
-
replacement = regenerateInlineReplacement(existing, replacement, change.path);
|
|
1737
|
-
replace(original, parent, existing, replacement);
|
|
1738
|
-
if (isInlineArray(replacement)) replacedInlineArrays.add(replacement);
|
|
1739
|
-
if (isInlineArray(replacement) || isInlineTable(replacement)) {
|
|
1740
|
-
applyWrites(original);
|
|
1741
|
-
normalizeGeneratedInlineContainerRows(replacement, format.indentWidth);
|
|
1742
|
-
replacedInlineContainers.push({
|
|
1743
|
-
owner: parent,
|
|
1744
|
-
container: replacement
|
|
1745
|
-
});
|
|
1746
|
-
}
|
|
1747
|
-
if (isDocument(parent) && isKeyValue(existing) && (isTable(replacement) || isTableArray(replacement))) sinkTableBelowRootKeyValues(original, replacement);
|
|
1748
|
-
} else if (isRemove(change)) {
|
|
1749
|
-
const node = tryFindByPath(original, change.path);
|
|
1750
|
-
if (!node) {
|
|
1751
|
-
const nextAotEntry = () => {
|
|
1752
|
-
const direct = tryFindByPath(original, change.path.concat(0));
|
|
1753
|
-
if (direct) return direct;
|
|
1754
|
-
return findDocumentItemsByKeyPrefix(original, change.path).filter(isTableArray)[0];
|
|
1755
|
-
};
|
|
1756
|
-
const first = nextAotEntry();
|
|
1757
|
-
if (first) {
|
|
1758
|
-
const firstIndex = original.items.indexOf(first);
|
|
1759
|
-
let materialiseAotInPlace = false;
|
|
1760
|
-
if (change.path.length > 1 && rawUpdated !== void 0) {
|
|
1761
|
-
const parentPath = change.path.slice(0, -1);
|
|
1762
|
-
const cstParentPath = stripAotEntryIndices(original, parentPath);
|
|
1763
|
-
const owner = findAotEntryForPath(original, change.path);
|
|
1764
|
-
if ((owner ? findAotScopedDocumentItems(original, cstParentPath, owner) : findDocumentItemsByKeyPrefix(original, cstParentPath)).filter((s) => s !== first && !(isTableArray(s) && arraysEqual(s.key.item.value, stripAotEntryIndices(original, change.path)))).length === 0 && !(owner && arraysEqual(cstParentPath, owner.key.item.value))) {
|
|
1765
|
-
let value = rawUpdated;
|
|
1766
|
-
for (const k of parentPath) value = value === null || value === void 0 ? void 0 : value[k];
|
|
1767
|
-
if (isObject(value) && Object.keys(value).length === 0) {
|
|
1768
|
-
const aotNode = first;
|
|
1769
|
-
const aotKeyHolder = aotNode.key;
|
|
1770
|
-
while (aotNode.items.length > 0) removeMember(original, aotNode, last(aotNode.items));
|
|
1771
|
-
applyWrites(original);
|
|
1772
|
-
aotNode.type = "Table";
|
|
1773
|
-
aotKeyHolder.type = "TableKey";
|
|
1774
|
-
const aotKey = hasItem(aotKeyHolder) ? aotKeyHolder.item : aotKeyHolder;
|
|
1775
|
-
aotKey.value = cstParentPath;
|
|
1776
|
-
aotKey.raw = preserveEscapedKeyRaw(aotKey.raw, aotKey.value, format.escapeSequenceUpperCase);
|
|
1777
|
-
aotKey.loc.end.column = aotKey.loc.start.column + aotKey.raw.length;
|
|
1778
|
-
aotKeyHolder.loc.end.column = aotKeyHolder.loc.start.column + aotKey.raw.length + 2;
|
|
1779
|
-
aotNode.loc.end.line = aotKeyHolder.loc.end.line;
|
|
1780
|
-
aotNode.loc.end.column = aotKeyHolder.loc.end.column;
|
|
1781
|
-
trackAdjacentComment(aotNode);
|
|
1782
|
-
materialiseAotInPlace = true;
|
|
1783
|
-
}
|
|
1784
|
-
}
|
|
1785
|
-
}
|
|
1786
|
-
const cstPath = stripAotEntryIndices(original, change.path);
|
|
1787
|
-
const indexedOwner = findAotEntryForPath(original, change.path);
|
|
1788
|
-
let entry;
|
|
1789
|
-
while (entry = nextAotEntry()) {
|
|
1790
|
-
if (isTableArray(entry)) {
|
|
1791
|
-
const ownedItems = findAotEntryScopeItems(original, entry).filter((item) => isTable(item) || isTableArray(item)).filter((item) => keyStartsWith(itemKey(item), cstPath));
|
|
1792
|
-
for (const ownedItem of ownedItems) removeMember(original, original, ownedItem);
|
|
1793
|
-
}
|
|
1794
|
-
removeMember(original, original, entry);
|
|
1795
|
-
}
|
|
1796
|
-
if (!indexedOwner) for (const tableNode of findDocumentItemsByKeyPrefix(original, cstPath).filter(isTable)) removeMember(original, original, tableNode);
|
|
1797
|
-
if (tryFindByPath(updated, change.path)) {
|
|
1798
|
-
const aotPath = change.path;
|
|
1799
|
-
emptiedAotKeys.set(stableStringify(aotPath), aotPath);
|
|
1800
|
-
}
|
|
1801
|
-
if (!materialiseAotInPlace && change.path.length > 1 && rawUpdated !== void 0) {
|
|
1802
|
-
const parentPath = change.path.slice(0, -1);
|
|
1803
|
-
const cstParentPath = stripAotEntryIndices(original, parentPath);
|
|
1804
|
-
const owner = findAotEntryForPath(original, change.path);
|
|
1805
|
-
if (findAotScopedDocumentItems(original, cstParentPath, owner).length === 0 && !(owner && arraysEqual(cstParentPath, owner.key.item.value))) {
|
|
1806
|
-
let value = rawUpdated;
|
|
1807
|
-
for (const k of parentPath) value = value === null || value === void 0 ? void 0 : value[k];
|
|
1808
|
-
if (isObject(value) && Object.keys(value).length === 0) {
|
|
1809
|
-
const emptyTable = generateTable(cstParentPath, format.escapeSequenceUpperCase);
|
|
1810
|
-
materialisedTables.add(emptyTable);
|
|
1811
|
-
const ownerIndex = owner && keyStartsWith(cstParentPath, owner.key.item.value) ? original.items.indexOf(owner) : -1;
|
|
1812
|
-
const insertIdx = ownerIndex >= 0 ? ownerIndex + 1 : firstIndex >= 0 ? Math.min(firstIndex, original.items.length) : original.items.length;
|
|
1813
|
-
applyWrites(original);
|
|
1814
|
-
insert(original, original, emptyTable, insertIdx);
|
|
1815
|
-
if (ownerIndex >= 0) {
|
|
1816
|
-
applyWrites(original);
|
|
1817
|
-
const owner = original.items[ownerIndex];
|
|
1818
|
-
const gap = emptyTable.loc.start.line - owner.loc.end.line - 1;
|
|
1819
|
-
if (gap > 0) for (const item of original.items.slice(ownerIndex + 1)) shiftNode(item, {
|
|
1820
|
-
lines: -gap,
|
|
1821
|
-
columns: 0
|
|
1822
|
-
});
|
|
1823
|
-
}
|
|
1824
|
-
}
|
|
1825
|
-
}
|
|
1826
|
-
}
|
|
1827
|
-
} else {
|
|
1828
|
-
const cstPath = stripAotEntryIndices(original, change.path);
|
|
1829
|
-
const owner = findAotEntryForPath(original, change.path);
|
|
1830
|
-
const prefixNodes = findAotScopedDocumentItems(original, cstPath, owner);
|
|
1831
|
-
if (prefixNodes.length > 0) {
|
|
1832
|
-
const firstPrefixIndex = original.items.indexOf(prefixNodes[0]);
|
|
1833
|
-
for (const prefixNode of prefixNodes) removeMember(original, original, prefixNode);
|
|
1834
|
-
if (cstPath.length > 1 && rawUpdated !== void 0) {
|
|
1835
|
-
const cstParentPath = cstPath.slice(0, -1);
|
|
1836
|
-
if (findAotScopedDocumentItems(original, cstParentPath, owner).length === 0 && !(owner && arraysEqual(cstParentPath, owner.key.item.value))) {
|
|
1837
|
-
let value = rawUpdated;
|
|
1838
|
-
for (const k of change.path.slice(0, -1)) value = value === null || value === void 0 ? void 0 : value[k];
|
|
1839
|
-
if (isObject(value) && Object.keys(value).length === 0) {
|
|
1840
|
-
const emptyTable = generateTable(cstParentPath, format.escapeSequenceUpperCase);
|
|
1841
|
-
materialisedTables.add(emptyTable);
|
|
1842
|
-
const insertIdx = firstPrefixIndex >= 0 ? Math.min(firstPrefixIndex, original.items.length) : original.items.length;
|
|
1843
|
-
applyWrites(original);
|
|
1844
|
-
insert(original, original, emptyTable, insertIdx);
|
|
1845
|
-
}
|
|
1846
|
-
}
|
|
1847
|
-
}
|
|
1848
|
-
} else findByPath(original, change.path);
|
|
1849
|
-
}
|
|
1850
|
-
} else {
|
|
1851
|
-
let parent = findParent(original, change.path);
|
|
1852
|
-
if (parent === node) parent = findParent(original, change.path.slice(0, -1));
|
|
1853
|
-
if (parent === node) {
|
|
1854
|
-
const host = findHostContainer(original, node);
|
|
1855
|
-
if (host) parent = host;
|
|
1856
|
-
}
|
|
1857
|
-
if (isKeyValue(parent)) parent = parent.value;
|
|
1858
|
-
if (isInlineItem(parent) && isKeyValue(parent.item)) parent = parent.item.value;
|
|
1859
|
-
if (isInlineItem(parent) && isInlineTable(parent.item)) parent = parent.item;
|
|
1860
|
-
if (isInlineItem(parent) && isInlineArray(parent.item)) parent = parent.item;
|
|
1861
|
-
if (!hasItems(parent) || !parent.items.includes(node)) parent = findStructuralParent(original, node) ?? (hasItems(parent) ? original : findHostContainer(original, node) ?? original);
|
|
1862
|
-
let materialisedInPlace = false;
|
|
1863
|
-
if (change.path.length > 1 && isTable(node) && rawUpdated !== void 0) {
|
|
1864
|
-
const parentPath = change.path.slice(0, -1);
|
|
1865
|
-
const cstParentPath = node.key.item.value.slice(0, -1);
|
|
1866
|
-
const owner = findAotEntryForPath(original, change.path);
|
|
1867
|
-
if ((owner ? findAotScopedDocumentItems(original, cstParentPath, owner) : findDocumentItemsByKeyPrefix(original, cstParentPath)).filter((s) => s !== node).length === 0 && !(owner && arraysEqual(cstParentPath, owner.key.item.value))) {
|
|
1868
|
-
let value = rawUpdated;
|
|
1869
|
-
for (const k of parentPath) value = value === null || value === void 0 ? void 0 : value[k];
|
|
1870
|
-
if (isObject(value) && Object.keys(value).length === 0) {
|
|
1871
|
-
const table = node;
|
|
1872
|
-
while (table.items.length > 0) remove(original, table, last(table.items));
|
|
1873
|
-
applyWrites(original);
|
|
1874
|
-
const keyHolder = table.key;
|
|
1875
|
-
const key = hasItem(keyHolder) ? keyHolder.item : keyHolder;
|
|
1876
|
-
key.value = cstParentPath;
|
|
1877
|
-
key.raw = preserveEscapedKeyRaw(key.raw, key.value, format.escapeSequenceUpperCase);
|
|
1878
|
-
key.loc.end.column = key.loc.start.column + key.raw.length;
|
|
1879
|
-
keyHolder.loc.end.column = keyHolder.loc.start.column + key.raw.length + 2;
|
|
1880
|
-
table.loc.end.line = keyHolder.loc.end.line;
|
|
1881
|
-
table.loc.end.column = keyHolder.loc.end.column;
|
|
1882
|
-
trackAdjacentComment(table);
|
|
1883
|
-
materialisedInPlace = true;
|
|
1884
|
-
}
|
|
1885
|
-
}
|
|
1886
|
-
}
|
|
1887
|
-
const nodeIndex = isDocument(parent) && hasItems(parent) ? parent.items.indexOf(node) : -1;
|
|
1888
|
-
const containerItemIndex = hasItems(parent) && !isDocument(parent) ? parent.items.indexOf(node) : -1;
|
|
1889
|
-
const removedInlineComma = isInlineItem(node) ? node.comma : void 0;
|
|
1890
|
-
if (isInlineItem(node) && (isInlineArray(parent) || isInlineTable(parent))) setInlineIndentColumn(parent, node.loc.start.column);
|
|
1891
|
-
if (isInlineItem(node) && (isInlineArray(parent) || isInlineTable(parent)) && containerItemIndex === parent.items.length - 1 && removedInlineComma !== void 0) removedInlineTrailingCommas.set(parent, removedInlineComma);
|
|
1892
|
-
const bracketGapBefore = isInlineItem(node) && (isInlineArray(parent) || isInlineTable(parent)) && node.loc.end.line === parent.loc.end.line ? parent.loc.end.column - node.loc.end.column : void 0;
|
|
1893
|
-
const nodeAbsolutePath = absolutePathOf(isInlineItem(node) && isKeyValue(node.item) ? node.item : node);
|
|
1894
|
-
const removePrefixKv = isKeyValue(node) ? node : isInlineItem(node) && isKeyValue(node.item) ? node.item : void 0;
|
|
1895
|
-
if (removePrefixKv && nodeAbsolutePath && hasItems(parent)) {
|
|
1896
|
-
const containerAbsLen = nodeAbsolutePath.filter((segment) => typeof segment === "string").length - removePrefixKv.key.value.length;
|
|
1897
|
-
const relativePrefix = change.path.filter((segment) => typeof segment === "string").slice(containerAbsLen);
|
|
1898
|
-
if (relativePrefix.length > 0 && relativePrefix.length < removePrefixKv.key.value.length) {
|
|
1899
|
-
const siblings = parent.items;
|
|
1900
|
-
const toRemove = [];
|
|
1901
|
-
for (const sibling of siblings) {
|
|
1902
|
-
if (sibling === node) continue;
|
|
1903
|
-
const siblingKey = isKeyValue(sibling) ? sibling.key.value : isInlineItem(sibling) && isKeyValue(sibling.item) ? sibling.item.key.value : isTable(sibling) || isTableArray(sibling) ? sibling.key.item.value : void 0;
|
|
1904
|
-
if (siblingKey && siblingKey.length > relativePrefix.length && arraysEqual(siblingKey.slice(0, relativePrefix.length), relativePrefix)) toRemove.push(sibling);
|
|
1905
|
-
}
|
|
1906
|
-
for (const sibling of toRemove) removeMember(original, parent, sibling);
|
|
1907
|
-
}
|
|
1908
|
-
}
|
|
1909
|
-
if (removePrefixKv && nodeAbsolutePath && hasItems(parent)) {
|
|
1910
|
-
const key = removePrefixKv.key.value;
|
|
1911
|
-
const siblings = parent.items;
|
|
1912
|
-
const extending = [];
|
|
1913
|
-
for (const sibling of siblings) {
|
|
1914
|
-
if (sibling === node) continue;
|
|
1915
|
-
const siblingKey = isKeyValue(sibling) ? sibling.key.value : isInlineItem(sibling) && isKeyValue(sibling.item) ? sibling.item.key.value : isTable(sibling) || isTableArray(sibling) ? sibling.key.item.value : void 0;
|
|
1916
|
-
if (siblingKey && siblingKey.length > key.length && arraysEqual(siblingKey.slice(0, key.length), key)) extending.push(sibling);
|
|
1917
|
-
}
|
|
1918
|
-
for (const sibling of extending) removeMember(original, parent, sibling);
|
|
1919
|
-
}
|
|
1920
|
-
if ((isTable(node) || isTableArray(node)) && hasItems(parent)) {
|
|
1921
|
-
const key = node.key.item.value;
|
|
1922
|
-
const siblings = parent.items;
|
|
1923
|
-
const extending = [];
|
|
1924
|
-
const nodeIndex = siblings.indexOf(node);
|
|
1925
|
-
let aotOwner;
|
|
1926
|
-
if (isTableArray(node)) aotOwner = node;
|
|
1927
|
-
else for (let i = nodeIndex - 1; i >= 0; i--) {
|
|
1928
|
-
const sibling = siblings[i];
|
|
1929
|
-
if (!isTableArray(sibling)) continue;
|
|
1930
|
-
const siblingKey = sibling.key.item.value;
|
|
1931
|
-
if (key.length > siblingKey.length && arraysEqual(key.slice(0, siblingKey.length), siblingKey)) {
|
|
1932
|
-
aotOwner = sibling;
|
|
1933
|
-
break;
|
|
1934
|
-
}
|
|
1935
|
-
}
|
|
1936
|
-
const aotEntry = aotOwner !== void 0;
|
|
1937
|
-
const ownStart = aotOwner ? siblings.indexOf(aotOwner) : nodeIndex;
|
|
1938
|
-
let ownEnd = siblings.length;
|
|
1939
|
-
if (aotEntry) {
|
|
1940
|
-
const ownerKey = aotOwner.key.item.value;
|
|
1941
|
-
for (let i = ownStart + 1; i < siblings.length; i++) {
|
|
1942
|
-
const s = siblings[i];
|
|
1943
|
-
if (!isTableArray(s)) continue;
|
|
1944
|
-
const sKey = s.key.item.value;
|
|
1945
|
-
if (sKey.length <= ownerKey.length && arraysEqual(sKey, ownerKey.slice(0, sKey.length))) {
|
|
1946
|
-
ownEnd = i;
|
|
1947
|
-
break;
|
|
1948
|
-
}
|
|
1949
|
-
}
|
|
1950
|
-
}
|
|
1951
|
-
for (const [index, sibling] of siblings.entries()) {
|
|
1952
|
-
if (sibling === node) continue;
|
|
1953
|
-
if (aotEntry && (index < ownStart || index >= ownEnd)) continue;
|
|
1954
|
-
const siblingKey = isKeyValue(sibling) ? sibling.key.value : isTable(sibling) || isTableArray(sibling) ? sibling.key.item.value : void 0;
|
|
1955
|
-
if (siblingKey && siblingKey.length > key.length && arraysEqual(siblingKey.slice(0, key.length), key)) extending.push(sibling);
|
|
1956
|
-
}
|
|
1957
|
-
for (const sibling of extending) removeMember(original, parent, sibling);
|
|
1958
|
-
}
|
|
1959
|
-
if (removePrefixKv && nodeAbsolutePath) {
|
|
1960
|
-
const owner = isTableArray(parent) ? parent : findAotEntryForPath(original, change.path);
|
|
1961
|
-
if (owner) {
|
|
1962
|
-
const cstPrefix = stripAotEntryIndices(original, change.path);
|
|
1963
|
-
const scopedSections = findAotScopedDocumentItems(original, cstPrefix, owner).filter((item) => item !== node && (isTable(item) || isTableArray(item))).filter((item) => keyStartsWith(itemKey(item), cstPrefix));
|
|
1964
|
-
for (const section of scopedSections) removeMember(original, original, section);
|
|
1965
|
-
}
|
|
1966
|
-
}
|
|
1967
|
-
if (!materialisedInPlace) removeMember(original, parent, node);
|
|
1968
|
-
if (isInlineItem(node) && (isInlineArray(parent) || isInlineTable(parent)) && node.loc.end.line === parent.loc.end.line) {
|
|
1969
|
-
const remaining = parent.items;
|
|
1970
|
-
const tailOnBracketRow = parent.loc.end.line === parent.loc.start.line ? remaining.every((item) => item.loc.end.line === parent.loc.start.line) : remaining.length > 0 && last(remaining).loc.end.line === parent.loc.end.line;
|
|
1971
|
-
if (remaining.length > 0 && containerItemIndex === remaining.length && tailOnBracketRow) {
|
|
1972
|
-
const gap = bracketGapBefore ?? parent.loc.end.column - node.loc.end.column;
|
|
1973
|
-
applyWrites(original);
|
|
1974
|
-
const targetEnd = last(remaining).loc.end.column + gap;
|
|
1975
|
-
if (parent.loc.end.column !== targetEnd) parent.loc.end.column = targetEnd;
|
|
1976
|
-
let owner;
|
|
1977
|
-
const findOwner = (n) => {
|
|
1978
|
-
if (isKeyValue(n)) {
|
|
1979
|
-
if (n.value === parent) return n;
|
|
1980
|
-
return findOwner(n.value);
|
|
1981
|
-
}
|
|
1982
|
-
if (isInlineItem(n)) return findOwner(n.item);
|
|
1983
|
-
if (!hasItems(n)) return void 0;
|
|
1984
|
-
for (const item of n.items) {
|
|
1985
|
-
const found = findOwner(isInlineItem(item) ? item.item : item);
|
|
1986
|
-
if (found) return found;
|
|
1987
|
-
}
|
|
1988
|
-
};
|
|
1989
|
-
owner = findOwner(original);
|
|
1990
|
-
if (owner && owner.loc.end.line === parent.loc.end.line) {
|
|
1991
|
-
owner.loc.end.column = parent.loc.end.column;
|
|
1992
|
-
const findWrapper = (n) => {
|
|
1993
|
-
if (isKeyValue(n)) return findWrapper(n.value);
|
|
1994
|
-
if (isInlineItem(n)) {
|
|
1995
|
-
if (n.item === owner) return n;
|
|
1996
|
-
return findWrapper(n.item);
|
|
1997
|
-
}
|
|
1998
|
-
if (!hasItems(n)) return void 0;
|
|
1999
|
-
for (const item of n.items) {
|
|
2000
|
-
const found = findWrapper(item);
|
|
2001
|
-
if (found) return found;
|
|
2002
|
-
}
|
|
2003
|
-
};
|
|
2004
|
-
const wrapper = findWrapper(original);
|
|
2005
|
-
if (wrapper) {
|
|
2006
|
-
const delta = parent.loc.end.column - wrapper.loc.end.column;
|
|
2007
|
-
wrapper.loc.end.column = parent.loc.end.column;
|
|
2008
|
-
if (delta !== 0) {
|
|
2009
|
-
addExitOffset(original, wrapper, {
|
|
2010
|
-
lines: 0,
|
|
2011
|
-
columns: delta
|
|
2012
|
-
});
|
|
2013
|
-
markDirty(original);
|
|
2014
|
-
applyWrites(original);
|
|
2015
|
-
}
|
|
2016
|
-
}
|
|
2017
|
-
}
|
|
2018
|
-
}
|
|
2019
|
-
}
|
|
2020
|
-
if (!materialisedInPlace && change.path.length > 1 && (isTable(node) || isTableArray(node)) && rawUpdated !== void 0) {
|
|
2021
|
-
const parentPath = change.path.slice(0, -1);
|
|
2022
|
-
const cstParentKey = node.key.item.value.slice(0, -1);
|
|
2023
|
-
if (findDocumentItemsByKeyPrefix(original, cstParentKey).length === 0) {
|
|
2024
|
-
let value = rawUpdated;
|
|
2025
|
-
for (const k of parentPath) value = value === null || value === void 0 ? void 0 : value[k];
|
|
2026
|
-
if (isObject(value) && Object.keys(value).length === 0) {
|
|
2027
|
-
const emptyTable = generateTable(cstParentKey, format.escapeSequenceUpperCase);
|
|
2028
|
-
materialisedTables.add(emptyTable);
|
|
2029
|
-
const insertIdx = nodeIndex >= 0 ? Math.min(nodeIndex, original.items.length) : original.items.length;
|
|
2030
|
-
applyWrites(original);
|
|
2031
|
-
insert(original, original, emptyTable, insertIdx);
|
|
2032
|
-
}
|
|
2033
|
-
}
|
|
2034
|
-
}
|
|
2035
|
-
if (!materialisedInPlace && change.path.length > 1 && rawUpdated !== void 0) {
|
|
2036
|
-
const kv = isKeyValue(node) ? node : isInlineItem(node) && isKeyValue(node.item) ? node.item : void 0;
|
|
2037
|
-
if (kv && kv.key.value.length > 1) {
|
|
2038
|
-
const parentPath = change.path.slice(0, -1);
|
|
2039
|
-
const container = isDocument(parent) || isTable(parent) || isTableArray(parent) || isInlineTable(parent) ? parent : original;
|
|
2040
|
-
if (hasItems(container)) {
|
|
2041
|
-
let relativePrefix;
|
|
2042
|
-
if (isTable(container) || isTableArray(container)) {
|
|
2043
|
-
const containerKey = container.key.item.value;
|
|
2044
|
-
let offset = 0;
|
|
2045
|
-
let matched = 0;
|
|
2046
|
-
for (let k = 0; k < containerKey.length && offset < parentPath.length; k++) {
|
|
2047
|
-
while (offset < parentPath.length && typeof parentPath[offset] === "number") offset++;
|
|
2048
|
-
if (offset >= parentPath.length || parentPath[offset] !== containerKey[k]) break;
|
|
2049
|
-
matched++;
|
|
2050
|
-
offset++;
|
|
2051
|
-
}
|
|
2052
|
-
while (offset < parentPath.length && typeof parentPath[offset] === "number") offset++;
|
|
2053
|
-
if (matched === containerKey.length) relativePrefix = parentPath.slice(offset);
|
|
2054
|
-
else relativePrefix = parentPath.slice(containerKey.length + (isTableArray(container) ? 1 : 0));
|
|
2055
|
-
} else if (isInlineTable(container)) {
|
|
2056
|
-
if (nodeAbsolutePath) {
|
|
2057
|
-
const containerSegs = nodeAbsolutePath.slice(0, nodeAbsolutePath.length - kv.key.value.length);
|
|
2058
|
-
let offset = 0;
|
|
2059
|
-
let matched = 0;
|
|
2060
|
-
while (matched < containerSegs.length && offset < parentPath.length) {
|
|
2061
|
-
while (offset < parentPath.length && typeof parentPath[offset] === "number") offset++;
|
|
2062
|
-
if (offset >= parentPath.length || parentPath[offset] !== containerSegs[matched]) break;
|
|
2063
|
-
matched++;
|
|
2064
|
-
offset++;
|
|
2065
|
-
}
|
|
2066
|
-
while (offset < parentPath.length && typeof parentPath[offset] === "number") offset++;
|
|
2067
|
-
if (matched === containerSegs.length) relativePrefix = parentPath.slice(offset);
|
|
2068
|
-
else relativePrefix = parentPath.slice(containerSegs.length);
|
|
2069
|
-
} else relativePrefix = parentPath.slice(parentPath.length - (kv.key.value.length - 1));
|
|
2070
|
-
} else relativePrefix = parentPath;
|
|
2071
|
-
relativePrefix = relativePrefix.filter((seg) => typeof seg === "string");
|
|
2072
|
-
const remaining = container.items.filter((item) => {
|
|
2073
|
-
const key = isKeyValue(item) ? item.key.value : isInlineItem(item) && isKeyValue(item.item) ? item.item.key.value : void 0;
|
|
2074
|
-
if (!key) return false;
|
|
2075
|
-
return arraysEqual(key.slice(0, relativePrefix.length), relativePrefix);
|
|
2076
|
-
});
|
|
2077
|
-
const isAotEntry = isTableArray(container);
|
|
2078
|
-
let docSiblings = [];
|
|
2079
|
-
if (isAotEntry && remaining.length === 0) docSiblings = findAotScopedDocumentItems(original, container.key.item.value.concat(relativePrefix), container);
|
|
2080
|
-
const containerAlreadyIsParent = relativePrefix.length === 0 && (isTable(container) || isTableArray(container) || isInlineTable(container));
|
|
2081
|
-
if (remaining.length === 0 && docSiblings.length === 0 && !containerAlreadyIsParent) {
|
|
2082
|
-
let value = rawUpdated;
|
|
2083
|
-
for (const k of parentPath) value = value === null || value === void 0 ? void 0 : value[k];
|
|
2084
|
-
if (isObject(value) && Object.keys(value).every((key) => value[key] === void 0)) {
|
|
2085
|
-
if (isInlineTable(container)) {
|
|
2086
|
-
const freshKey = relativePrefix.map((part) => IS_BARE_KEY.test(part) ? part : JSON.stringify(part).replace(/\x7f/g, "\\u007f")).join(".");
|
|
2087
|
-
const freshKv = Array.from(parseTOML(`${freshKey} = {}`))[0];
|
|
2088
|
-
if (isKeyValue(freshKv)) {
|
|
2089
|
-
const inlineItem = generateInlineItem(freshKv);
|
|
2090
|
-
inlineItem.comma = removedInlineComma ?? false;
|
|
2091
|
-
applyWrites(original);
|
|
2092
|
-
if (hasInlineContainerNeedingTighten(container) && container.loc.end.line > container.loc.start.line) container.loc.end.line = container.loc.start.line;
|
|
2093
|
-
insert(original, container, inlineItem, containerItemIndex >= 0 ? containerItemIndex : void 0);
|
|
2094
|
-
if (format.bracketSpacing) shiftNode(inlineItem, {
|
|
2095
|
-
lines: 0,
|
|
2096
|
-
columns: 1
|
|
2097
|
-
});
|
|
2098
|
-
const tailRow = container.items[containerItemIndex + 1];
|
|
2099
|
-
const pendingInsertOffset = getExitOffsets(original).get(inlineItem);
|
|
2100
|
-
if (tailRow && pendingInsertOffset && tailRow.loc.start.line === inlineItem.loc.end.line) pendingInsertOffset.columns = inlineItem.loc.end.column + 2 - tailRow.loc.start.column;
|
|
2101
|
-
}
|
|
2102
|
-
return;
|
|
2103
|
-
}
|
|
2104
|
-
if (isTable(container) || isTableArray(container)) {
|
|
2105
|
-
const freshKey = relativePrefix.map((part) => IS_BARE_KEY.test(part) ? part : JSON.stringify(part).replace(/\x7f/g, "\\u007f")).join(".");
|
|
2106
|
-
const freshKv = Array.from(parseTOML(`${freshKey} = {}`))[0];
|
|
2107
|
-
if (isKeyValue(freshKv)) {
|
|
2108
|
-
applyWrites(original);
|
|
2109
|
-
const insertIndex = containerItemIndex >= 0 ? Math.min(containerItemIndex, container.items.length) : container.items.length;
|
|
2110
|
-
const leadingLines = container.items.length === 0 && isTable(container) ? 0 : void 0;
|
|
2111
|
-
insert(original, container, freshKv, insertIndex, void 0, void 0, leadingLines);
|
|
2112
|
-
}
|
|
2113
|
-
return;
|
|
2114
|
-
}
|
|
2115
|
-
const tableKey = isAotEntry ? container.key.item.value.concat(relativePrefix) : relativePrefix;
|
|
2116
|
-
const emptyTable = generateTable(tableKey, format.escapeSequenceUpperCase);
|
|
2117
|
-
materialisedTables.add(emptyTable);
|
|
2118
|
-
let insertIdx = nodeIndex >= 0 ? nodeIndex : original.items.length;
|
|
2119
|
-
if (isAotEntry) {
|
|
2120
|
-
const entryIdx = original.items.indexOf(container);
|
|
2121
|
-
insertIdx = entryIdx >= 0 ? entryIdx + 1 : original.items.length;
|
|
2122
|
-
} else if (isTable(container)) {
|
|
2123
|
-
const parentIdx = original.items.indexOf(container);
|
|
2124
|
-
insertIdx = parentIdx >= 0 ? parentIdx + 1 : original.items.length;
|
|
2125
|
-
} else if (isDocument(container)) insertIdx = rootKeyValueInsertIndex(original) ?? original.items.length;
|
|
2126
|
-
applyWrites(original);
|
|
2127
|
-
insert(original, original, emptyTable, insertIdx);
|
|
2128
|
-
}
|
|
2129
|
-
}
|
|
2130
|
-
}
|
|
2131
|
-
}
|
|
2132
|
-
}
|
|
2133
|
-
if (isTableArray(node)) {
|
|
2134
|
-
const aotPath = change.path.slice(0, -1);
|
|
2135
|
-
if (!tryFindByPath(original, aotPath.concat(0)) && tryFindByPath(updated, aotPath)) emptiedAotKeys.set(stableStringify(aotPath), aotPath);
|
|
2136
|
-
}
|
|
2137
|
-
}
|
|
2138
|
-
} else if (isMove(change)) {
|
|
2139
|
-
if (change.key !== void 0) {
|
|
2140
|
-
objectMoves.push(change);
|
|
2141
|
-
return;
|
|
2142
|
-
}
|
|
2143
|
-
let parent = tryFindByPath(original, change.path);
|
|
2144
|
-
if (parent) {
|
|
2145
|
-
if (hasItem(parent)) parent = parent.item;
|
|
2146
|
-
if (isKeyValue(parent)) parent = parent.value;
|
|
2147
|
-
if (change.from >= parent.items.length) return;
|
|
2148
|
-
const node = parent.items[change.from];
|
|
2149
|
-
moveInlineElement(original, parent, node, change.to);
|
|
2150
|
-
} else {
|
|
2151
|
-
normalizeAotEntryComments(original);
|
|
2152
|
-
const fromNode = findByPath(original, change.path.concat(change.from));
|
|
2153
|
-
const fromGroup = resolveGroups(original).find((s) => s.member === fromNode);
|
|
2154
|
-
const groupItems = fromGroup ? [...fromGroup.items] : [fromNode];
|
|
2155
|
-
const groupOriginalPos = groupItems.map((item) => ({
|
|
2156
|
-
startLine: item.loc.start.line,
|
|
2157
|
-
endLine: item.loc.end.line
|
|
2158
|
-
}));
|
|
2159
|
-
const groupFirstIdx = original.items.indexOf(fromGroup ? fromGroup.items[0] : fromNode);
|
|
2160
|
-
const lastGroupEnd = groupOriginalPos[groupOriginalPos.length - 1].endLine;
|
|
2161
|
-
let originalAfterGap;
|
|
2162
|
-
for (let k = 0; k < original.items.length; k++) {
|
|
2163
|
-
const item = original.items[k];
|
|
2164
|
-
if (item.loc.start.line > lastGroupEnd) {
|
|
2165
|
-
originalAfterGap = item.loc.start.line - lastGroupEnd;
|
|
2166
|
-
break;
|
|
2167
|
-
}
|
|
2168
|
-
}
|
|
2169
|
-
if (originalAfterGap === void 0 && groupFirstIdx > 0) {
|
|
2170
|
-
const beforeGroup = original.items[groupFirstIdx - 1];
|
|
2171
|
-
originalAfterGap = groupOriginalPos[0].startLine - beforeGroup.loc.end.line;
|
|
2172
|
-
}
|
|
2173
|
-
const memberIdx = fromGroup ? fromGroup.items.indexOf(fromNode) : 0;
|
|
2174
|
-
for (let i = 0; i <= memberIdx; i++) {
|
|
2175
|
-
if (!original.items.includes(groupItems[i])) continue;
|
|
2176
|
-
remove(original, original, groupItems[i]);
|
|
2177
|
-
}
|
|
2178
|
-
for (let i = memberIdx + 1; i < groupItems.length; i++) {
|
|
2179
|
-
const idx = original.items.indexOf(groupItems[i]);
|
|
2180
|
-
if (idx >= 0) original.items.splice(idx, 1);
|
|
2181
|
-
}
|
|
2182
|
-
const toEntry = tryFindByPath(original, change.path.concat(change.to));
|
|
2183
|
-
let toIndex;
|
|
2184
|
-
let targetHasLeadingComment = false;
|
|
2185
|
-
const sourceHadLeadingComment = fromGroup && fromGroup.items[0] !== fromNode && isComment(fromGroup.items[0]);
|
|
2186
|
-
if (toEntry) {
|
|
2187
|
-
const targetGroup = resolveGroups(original).find((s) => s.member === toEntry);
|
|
2188
|
-
targetHasLeadingComment = !!(targetGroup && targetGroup.items[0] !== toEntry && isComment(targetGroup.items[0]));
|
|
2189
|
-
if (targetHasLeadingComment && sourceHadLeadingComment) toIndex = original.items.indexOf(targetGroup.items[0]);
|
|
2190
|
-
else toIndex = original.items.indexOf(toEntry);
|
|
2191
|
-
} else toIndex = original.items.length;
|
|
2192
|
-
const targetPrevEnd = toIndex > 0 ? original.items[toIndex - 1].loc.end.line : 0;
|
|
2193
|
-
const targetFirstLine = toIndex < original.items.length ? original.items[toIndex].loc.start.line : void 0;
|
|
2194
|
-
const isCommentedSwap = targetHasLeadingComment && sourceHadLeadingComment;
|
|
2195
|
-
let insertIdx = toIndex;
|
|
2196
|
-
for (let i = 0; i < groupItems.length; i++) {
|
|
2197
|
-
const item = groupItems[i];
|
|
2198
|
-
let itemLeadingLines;
|
|
2199
|
-
if ((isCommentedSwap || sourceHadLeadingComment && toIndex > 0 && toIndex < original.items.length) && insertIdx > 0) {
|
|
2200
|
-
const prevEnd = i === 0 ? targetPrevEnd : groupOriginalPos[i - 1].endLine;
|
|
2201
|
-
const origLeading = i === 0 && targetFirstLine !== void 0 ? targetFirstLine - targetPrevEnd : groupOriginalPos[i].startLine - prevEnd;
|
|
2202
|
-
if (origLeading !== (isTable(item) || isTableArray(item) ? 2 : 1)) itemLeadingLines = origLeading;
|
|
2203
|
-
}
|
|
2204
|
-
insert(original, original, item, insertIdx, void 0, void 0, itemLeadingLines);
|
|
2205
|
-
insertIdx++;
|
|
2206
|
-
}
|
|
2207
|
-
if (originalAfterGap !== void 0) {
|
|
2208
|
-
applyWrites(original);
|
|
2209
|
-
const lastItem = groupItems[groupItems.length - 1];
|
|
2210
|
-
const afterStart = original.items.indexOf(lastItem) + 1;
|
|
2211
|
-
if (afterStart < original.items.length) {
|
|
2212
|
-
const newGap = original.items[afterStart].loc.start.line - lastItem.loc.end.line;
|
|
2213
|
-
const delta = originalAfterGap - newGap;
|
|
2214
|
-
if (delta !== 0) for (let j = afterStart; j < original.items.length; j++) shiftNode(original.items[j], {
|
|
2215
|
-
lines: delta,
|
|
2216
|
-
columns: 0
|
|
2217
|
-
});
|
|
2218
|
-
}
|
|
2219
|
-
}
|
|
2220
|
-
}
|
|
2221
|
-
} else if (isRename(change)) {
|
|
2222
|
-
const sourcePath = change.path.concat(change.from);
|
|
2223
|
-
let parent = tryFindByPath(original, sourcePath);
|
|
2224
|
-
if (!parent) {
|
|
2225
|
-
const prefixNodes = findDocumentItemsByKeyPrefix(original, sourcePath);
|
|
2226
|
-
if (prefixNodes.length === 1 && (isTable(prefixNodes[0]) || isTableArray(prefixNodes[0]))) {
|
|
2227
|
-
const keyHolder = prefixNodes[0].key;
|
|
2228
|
-
const key = hasItem(keyHolder) ? keyHolder.item : keyHolder;
|
|
2229
|
-
const segmentIndex = sourcePath.length - 1;
|
|
2230
|
-
const originalRaw = key.raw;
|
|
2231
|
-
key.value[segmentIndex] = change.to;
|
|
2232
|
-
key.raw = preserveEscapedKeyRaw(key.raw, key.value, format.escapeSequenceUpperCase);
|
|
2233
|
-
preserveDottedKeySpacing(key, originalRaw);
|
|
2234
|
-
key.loc.end.column = key.loc.start.column + key.raw.length;
|
|
2235
|
-
return;
|
|
2236
|
-
}
|
|
2237
|
-
}
|
|
2238
|
-
if (!parent) parent = findByPath(original, sourcePath);
|
|
2239
|
-
let replacement = findByPath(updated, change.path.concat(change.to));
|
|
2240
|
-
if (hasItem(parent)) parent = parent.item;
|
|
2241
|
-
if (hasItem(replacement)) replacement = replacement.item;
|
|
2242
|
-
const parentHolder = parent.key;
|
|
2243
|
-
const replacementHolder = replacement.key;
|
|
2244
|
-
const parentKey = hasItem(parentHolder) ? parentHolder.item : parentHolder;
|
|
2245
|
-
const replacementKey = hasItem(replacementHolder) ? replacementHolder.item : replacementHolder;
|
|
2246
|
-
if (parentKey.value.length !== replacementKey.value.length) {
|
|
2247
|
-
const fullSourcePath = change.path.concat(change.from);
|
|
2248
|
-
if ((isTable(parent) || isTableArray(parent)) && isKeyValue(replacement) && arraysEqual(parentKey.value.slice(0, fullSourcePath.length), fullSourcePath)) {
|
|
2249
|
-
const segmentIndex = fullSourcePath.length - 1;
|
|
2250
|
-
const originalRaw = parentKey.raw;
|
|
2251
|
-
parentKey.value[segmentIndex] = change.to;
|
|
2252
|
-
parentKey.raw = preserveEscapedKeyRaw(parentKey.raw, parentKey.value, format.escapeSequenceUpperCase);
|
|
2253
|
-
preserveDottedKeySpacing(parentKey, originalRaw);
|
|
2254
|
-
parentKey.loc.end.column = parentKey.loc.start.column + parentKey.raw.length;
|
|
2255
|
-
return;
|
|
2256
|
-
}
|
|
2257
|
-
if (isKeyValue(parent) && isKeyValue(replacement) && parentKey.value.length <= fullSourcePath.length && arraysEqual(parentKey.value, fullSourcePath.slice(fullSourcePath.length - parentKey.value.length))) {
|
|
2258
|
-
const oldKeyRaw = parentKey.raw;
|
|
2259
|
-
parentKey.value[parentKey.value.length - 1] = change.to;
|
|
2260
|
-
parentKey.raw = preserveEscapedKeyRaw(parentKey.raw, parentKey.value, format.escapeSequenceUpperCase);
|
|
2261
|
-
parentKey.loc.end.column = parentKey.loc.start.column + parentKey.raw.length;
|
|
2262
|
-
const keyWidthDelta = parentKey.raw.length - oldKeyRaw.length;
|
|
2263
|
-
if (keyWidthDelta > 0) {
|
|
2264
|
-
parent.equals += keyWidthDelta;
|
|
2265
|
-
shiftNode(parent.value, {
|
|
2266
|
-
lines: 0,
|
|
2267
|
-
columns: keyWidthDelta
|
|
2268
|
-
}, { first_line_only: true });
|
|
2269
|
-
addExitOffset(original, parent.value, {
|
|
2270
|
-
lines: 0,
|
|
2271
|
-
columns: keyWidthDelta
|
|
2272
|
-
});
|
|
2273
|
-
}
|
|
2274
|
-
return;
|
|
2275
|
-
}
|
|
2276
|
-
throw new Error(`Cannot rename "${parentKey.raw}" to "${replacementKey.raw}": the replacement key has ${replacementKey.value.length} segment(s) where the existing key has ${parentKey.value.length}, so one cannot be substituted for the other.`);
|
|
2277
|
-
}
|
|
2278
|
-
replacementKey.raw = preserveEscapedKeyRaw(parentKey.raw, replacementKey.value, format.escapeSequenceUpperCase);
|
|
2279
|
-
preserveDottedKeySpacing(replacementKey, parentKey.raw);
|
|
2280
|
-
replacementKey.loc.end.column = replacementKey.loc.start.column + replacementKey.raw.length;
|
|
2281
|
-
const keyOwner = hasItem(parentHolder) ? parentHolder : parent;
|
|
2282
|
-
replace(original, keyOwner, parentKey, replacementKey);
|
|
2283
|
-
}
|
|
2284
|
-
});
|
|
2285
|
-
applyWrites(original);
|
|
2286
|
-
for (let i = 1; i < original.items.length; i++) {
|
|
2287
|
-
const prev = original.items[i - 1];
|
|
2288
|
-
const curr = original.items[i];
|
|
2289
|
-
if (isComment(prev) && isTable(curr) && materialisedTables.has(curr)) {
|
|
2290
|
-
const gap = curr.loc.start.line - prev.loc.end.line - 1;
|
|
2291
|
-
if (gap > 0) shiftNode(curr, {
|
|
2292
|
-
lines: -gap,
|
|
2293
|
-
columns: 0
|
|
2294
|
-
});
|
|
2295
|
-
}
|
|
2296
|
-
}
|
|
2297
|
-
const rootItems = original.items;
|
|
2298
|
-
for (let i = 0; i < rootItems.length - 1; i++) {
|
|
2299
|
-
const curr = rootItems[i];
|
|
2300
|
-
const next = rootItems[i + 1];
|
|
2301
|
-
if ((isTable(curr) || isTableArray(curr)) && curr.items.length === 0 && (isTable(next) || isTableArray(next))) {
|
|
2302
|
-
const gap = next.loc.start.line - curr.loc.end.line - 1;
|
|
2303
|
-
if (gap > 1) {
|
|
2304
|
-
const delta = 1 - gap;
|
|
2305
|
-
for (let j = i + 1; j < rootItems.length; j++) shiftNode(rootItems[j], {
|
|
2306
|
-
lines: delta,
|
|
2307
|
-
columns: 0
|
|
2308
|
-
});
|
|
2309
|
-
}
|
|
2310
|
-
}
|
|
2311
|
-
}
|
|
2312
|
-
let needsTighten = false;
|
|
2313
|
-
traverse(original, {
|
|
2314
|
-
InlineTable: (node) => {
|
|
2315
|
-
if (hasInlineContainerNeedingTighten(node)) needsTighten = true;
|
|
2316
|
-
},
|
|
2317
|
-
InlineArray: (node) => {
|
|
2318
|
-
if (hasInlineContainerNeedingTighten(node)) needsTighten = true;
|
|
2319
|
-
}
|
|
2320
|
-
});
|
|
2321
|
-
if (needsTighten) applyWrites(original);
|
|
2322
|
-
let hasTightened = false;
|
|
2323
|
-
const tightenedInlineContainers = [];
|
|
2324
|
-
traverse(original, {
|
|
2325
|
-
InlineTable: (node) => {
|
|
2326
|
-
if (hasInlineContainerNeedingTighten(node)) {
|
|
2327
|
-
tightenedInlineContainers.push(node);
|
|
2328
|
-
tightenInlineContainerEnd(node);
|
|
2329
|
-
deleteInlineContainerNeedingTighten(node);
|
|
2330
|
-
applyBracketSpacing(original, node, format.bracketSpacing);
|
|
2331
|
-
hasTightened = true;
|
|
2332
|
-
}
|
|
2333
|
-
},
|
|
2334
|
-
InlineArray: (node) => {
|
|
2335
|
-
if (hasInlineContainerNeedingTighten(node)) {
|
|
2336
|
-
tightenInlineContainerEnd(node);
|
|
2337
|
-
deleteInlineContainerNeedingTighten(node);
|
|
2338
|
-
applyBracketSpacing(original, node, format.bracketSpacing);
|
|
2339
|
-
hasTightened = true;
|
|
2340
|
-
}
|
|
2341
|
-
}
|
|
2342
|
-
});
|
|
2343
|
-
if (hasTightened) {
|
|
2344
|
-
applyWrites(original);
|
|
2345
|
-
for (const container of tightenedInlineContainers) compactInlineContainerAncestors(original, container);
|
|
2346
|
-
recalcInlineContainerEnds(original);
|
|
2347
|
-
}
|
|
2348
|
-
cleanupOrphanedComments(original);
|
|
2349
|
-
replaceEmptiedTableArrays(original, emptiedAotKeys, format);
|
|
2350
|
-
applyKeyOrderMoves(original, objectMoves, commentEligibleNodes);
|
|
2351
|
-
if (objectMoves.length > 0) markDirty(original);
|
|
2352
|
-
if (replacedInlineArrays.size > 0) {
|
|
2353
|
-
applyWrites(original);
|
|
2354
|
-
for (const array of replacedInlineArrays) positionGeneratedNestedInlineTables(array, format.indentWidth);
|
|
2355
|
-
}
|
|
2356
|
-
if (replacedInlineContainers.length > 0) {
|
|
2357
|
-
applyWrites(original);
|
|
2358
|
-
for (const { owner, container } of replacedInlineContainers) {
|
|
2359
|
-
deleteSubtreeRanges(container);
|
|
2360
|
-
normalizeInlineContainerRows(container, owner.loc.start.column, format.indentWidth, format.bracketSpacing);
|
|
2361
|
-
}
|
|
2362
|
-
}
|
|
2363
|
-
return original;
|
|
2364
|
-
}
|
|
2365
|
-
/**
|
|
2366
|
-
* Drops the `range` of a node and every descendant, so the renderer rebuilds
|
|
2367
|
-
* the text from the node locations instead of copying the original slice.
|
|
2368
|
-
*
|
|
2369
|
-
* Regenerated values are parsed from a standalone TOML snippet, so their ranges
|
|
2370
|
-
* point into that snippet. Relocating the subtree shifts the locations but not
|
|
2371
|
-
* the snippet's interior text: a copied slice would keep the snippet's absolute
|
|
2372
|
-
* indentation and ignore the new row positions (fuzz3 seed 18515).
|
|
2373
|
-
*/
|
|
2374
|
-
function deleteSubtreeRanges(node) {
|
|
2375
|
-
delete node.range;
|
|
2376
|
-
if (hasItems(node)) for (const child of node.items) deleteSubtreeRanges(child);
|
|
2377
|
-
if (hasItem(node)) deleteSubtreeRanges(node.item);
|
|
2378
|
-
if (isKeyValue(node)) {
|
|
2379
|
-
deleteSubtreeRanges(node.key);
|
|
2380
|
-
deleteSubtreeRanges(node.value);
|
|
2381
|
-
}
|
|
2382
|
-
}
|
|
2383
|
-
/**
|
|
2384
|
-
* Position of the first `[table]`/`[[array]]` header in `doc`, or -1 if there is none.
|
|
2385
|
-
*
|
|
2386
|
-
* A key-value that physically follows a section header belongs to that section, not to the
|
|
2387
|
-
* root table. So any path that regenerates a root-level key has to land it above the first
|
|
2388
|
-
* header — appending at the end of the document silently reparents it, and the value comes
|
|
2389
|
-
* back under the wrong key on the next parse. See
|
|
2390
|
-
* docs/bug-notes/comment-eligibility-on-structural-replace.md.
|
|
2391
|
-
*/
|
|
2392
|
-
function firstSectionHeaderIndex(doc) {
|
|
2393
|
-
return doc.items.findIndex((item) => isTable(item) || isTableArray(item));
|
|
2394
|
-
}
|
|
2395
|
-
/**
|
|
2396
|
-
* Index to insert a root key-value at so it stays in the root table: above the first section
|
|
2397
|
-
* header, and above the comment block R2 assigns to that header — inserting between them would
|
|
2398
|
-
* leave the comment introducing the new key instead (docs/Comment-Ownership.md, R2).
|
|
2399
|
-
*/
|
|
2400
|
-
function rootKeyValueInsertIndex(doc) {
|
|
2401
|
-
const firstTableIndex = firstSectionHeaderIndex(doc);
|
|
2402
|
-
if (firstTableIndex === -1) return void 0;
|
|
2403
|
-
if (firstTableIndex === 0 || !isComment(doc.items[firstTableIndex - 1])) return firstTableIndex;
|
|
2404
|
-
applyWrites(doc);
|
|
2405
|
-
const header = doc.items[firstTableIndex];
|
|
2406
|
-
const group = resolveGroups(doc).find((candidate) => candidate.member === header);
|
|
2407
|
-
return group ? doc.items.indexOf(group.items[0]) : firstTableIndex;
|
|
2408
|
-
}
|
|
2409
|
-
/**
|
|
2410
|
-
* Moves an already-inserted root-level key-value back above the first section header, for the
|
|
2411
|
-
* paths that place it via `replace()` (which pins it to the replaced node's position) rather
|
|
2412
|
-
* than choosing an index. Also tends to reunite the key with a leading comment left behind at
|
|
2413
|
-
* its original position.
|
|
2414
|
-
*/
|
|
2415
|
-
function hoistRootKeyValueAboveTables(doc, kv) {
|
|
2416
|
-
const kvIndex = doc.items.indexOf(kv);
|
|
2417
|
-
if (kvIndex < 0) return;
|
|
2418
|
-
const firstTableIndex = firstSectionHeaderIndex(doc);
|
|
2419
|
-
if (firstTableIndex === -1 || firstTableIndex > kvIndex) return;
|
|
2420
|
-
remove(doc, doc, kv);
|
|
2421
|
-
applyWrites(doc);
|
|
2422
|
-
insert(doc, doc, kv, rootKeyValueInsertIndex(doc) ?? firstTableIndex);
|
|
2423
|
-
}
|
|
2424
|
-
/**
|
|
2425
|
-
* The converse of hoistRootKeyValueAboveTables: an edit that turned a
|
|
2426
|
-
* root-level KeyValue into a Table section inserts a section header where
|
|
2427
|
-
* the KV was — and a section header captures every key-value that follows it
|
|
2428
|
-
* in the document. If any root KV comes after the new header (and before the
|
|
2429
|
-
* next section), move the table below the root-table scope (fuzz seed 590:
|
|
2430
|
-
* `g86 = "str"` became `[g86]` while `fofc`/`y3n`/`"<w"` followed it).
|
|
2431
|
-
*/
|
|
2432
|
-
function sinkTableBelowRootKeyValues(doc, table) {
|
|
2433
|
-
const tableIndex = doc.items.indexOf(table);
|
|
2434
|
-
if (tableIndex < 0) return;
|
|
2435
|
-
const nextSectionIndex = doc.items.findIndex((item, i) => i > tableIndex && (isTable(item) || isTableArray(item)));
|
|
2436
|
-
if (!doc.items.some((item, i) => i > tableIndex && isKeyValue(item) && (nextSectionIndex === -1 || i < nextSectionIndex))) return;
|
|
2437
|
-
const target = nextSectionIndex === -1 ? doc.items.length : nextSectionIndex;
|
|
2438
|
-
applyWrites(doc);
|
|
2439
|
-
remove(doc, doc, table);
|
|
2440
|
-
insert(doc, doc, table, target - 1);
|
|
2441
|
-
}
|
|
2442
|
-
/**
|
|
2443
|
-
* Handles structural edits where findByPath can't resolve the existing CST node
|
|
2444
|
-
* because the structure type has changed (e.g. table→scalar, AOT→scalar, array→empty).
|
|
2445
|
-
* Swaps the fresh key-value in for the old structural nodes.
|
|
2446
|
-
*/
|
|
2447
|
-
function handleStructuralEdit(original, updated, change, format, temporal, commentEligibleNodes, materialisedTables) {
|
|
2448
|
-
let jsValue = toJS(updated.items, "", { temporal });
|
|
2449
|
-
for (const key of change.path) jsValue = jsValue === null || jsValue === void 0 ? void 0 : jsValue[key];
|
|
2450
|
-
if (jsValue === void 0) return;
|
|
2451
|
-
{
|
|
2452
|
-
let aotIndexPos = -1;
|
|
2453
|
-
let entry;
|
|
2454
|
-
for (let index = change.path.length - 1; index > 0; index--) {
|
|
2455
|
-
if (typeof change.path[index] !== "number") continue;
|
|
2456
|
-
const candidate = tryFindByPath(original, change.path.slice(0, index + 1));
|
|
2457
|
-
if (candidate && isTableArray(candidate)) {
|
|
2458
|
-
aotIndexPos = index;
|
|
2459
|
-
entry = candidate;
|
|
2460
|
-
break;
|
|
2461
|
-
}
|
|
2462
|
-
}
|
|
2463
|
-
if (aotIndexPos > 0 && entry && change.path.slice(aotIndexPos + 1).every((seg) => typeof seg === "string")) {
|
|
2464
|
-
const aotEntry = entry;
|
|
2465
|
-
const entryKey = aotEntry.key.item.value;
|
|
2466
|
-
const changedKey = change.path.slice(aotIndexPos + 1);
|
|
2467
|
-
const fullChangedKey = entryKey.concat(changedKey);
|
|
2468
|
-
const toRemove = findAotScopedDocumentItems(original, entryKey.concat(changedKey), aotEntry);
|
|
2469
|
-
const removedIndex = toRemove.length > 0 ? original.items.indexOf(toRemove[0]) : -1;
|
|
2470
|
-
for (const prefixNode of toRemove) removeMember(original, original, prefixNode);
|
|
2471
|
-
const inlineToRemove = aotEntry.items.filter((item) => {
|
|
2472
|
-
const key = isKeyValue(item) ? item.key.value : void 0;
|
|
2473
|
-
return key && key.length > changedKey.length && arraysEqual(key.slice(0, changedKey.length), changedKey);
|
|
2474
|
-
});
|
|
2475
|
-
for (const item of inlineToRemove) removeMember(original, aotEntry, item);
|
|
2476
|
-
if (changedKey.length > 1) {
|
|
2477
|
-
const explicitParent = findDeepestExplicitTable(original, fullChangedKey, entryKey.length, aotEntry);
|
|
2478
|
-
if (explicitParent) {
|
|
2479
|
-
const relativeKey = fullChangedKey.slice(explicitParent.key.item.value.length);
|
|
2480
|
-
const replacementRow = !isTableArrayValue(jsValue) ? generateExplicitParentRow(relativeKey, jsValue, format) : void 0;
|
|
2481
|
-
if (replacementRow) {
|
|
2482
|
-
upsertExplicitParentRow(original, explicitParent, relativeKey, replacementRow);
|
|
2483
|
-
commentEligibleNodes.add(replacementRow);
|
|
2484
|
-
return;
|
|
2485
|
-
}
|
|
2486
|
-
}
|
|
2487
|
-
}
|
|
2488
|
-
let tail;
|
|
2489
|
-
if (isTableArrayValue(jsValue)) tail = { [changedKey[changedKey.length - 1]]: jsValue };
|
|
2490
|
-
else {
|
|
2491
|
-
tail = jsValue;
|
|
2492
|
-
for (let k = change.path.length - 1; k > aotIndexPos; k--) tail = { [change.path[k]]: tail };
|
|
2493
|
-
}
|
|
2494
|
-
const tailDoc = parseJS(tail, format);
|
|
2495
|
-
const tailToml = toTOML(tailDoc.items, format);
|
|
2496
|
-
const tailCst = Array.from(parseTOML(tailToml));
|
|
2497
|
-
let sectionInsertIndex = removedIndex >= 0 ? removedIndex : ownedSectionsEndIndex(original, aotEntry, findAotEntryScopeItems(original, aotEntry));
|
|
2498
|
-
const upsertEntryKeyValue = (kv) => {
|
|
2499
|
-
const existingRow = aotEntry.items.find((row) => isKeyValue(row) && arraysEqual(row.key.value, kv.key.value));
|
|
2500
|
-
if (existingRow) replace(original, aotEntry, existingRow, kv);
|
|
2501
|
-
else insert(original, aotEntry, kv, void 0);
|
|
2502
|
-
};
|
|
2503
|
-
for (const item of tailCst) {
|
|
2504
|
-
if (isKeyValue(item)) {
|
|
2505
|
-
upsertEntryKeyValue(item);
|
|
2506
|
-
continue;
|
|
2507
|
-
}
|
|
2508
|
-
if (isTable(item)) {
|
|
2509
|
-
const tableKey = item.key.item.value;
|
|
2510
|
-
const fullTableKey = entryKey.concat(tableKey);
|
|
2511
|
-
const explicitParent = findAotScopedDocumentItems(original, fullTableKey, aotEntry).find((candidate) => isTable(candidate) && arraysEqual(candidate.key.item.value, fullTableKey));
|
|
2512
|
-
if (explicitParent) {
|
|
2513
|
-
for (const row of item.items) {
|
|
2514
|
-
if (!isKeyValue(row)) continue;
|
|
2515
|
-
const existingRow = explicitParent.items.find((candidate) => isKeyValue(candidate) && arraysEqual(candidate.key.value, row.key.value));
|
|
2516
|
-
if (existingRow) replace(original, explicitParent, existingRow, row);
|
|
2517
|
-
else insert(original, explicitParent, row, void 0);
|
|
2518
|
-
}
|
|
2519
|
-
continue;
|
|
2520
|
-
}
|
|
2521
|
-
for (const row of item.items) {
|
|
2522
|
-
if (!isKeyValue(row)) continue;
|
|
2523
|
-
rekeyKeyValue(row, tableKey.concat(row.key.value), format);
|
|
2524
|
-
upsertEntryKeyValue(row);
|
|
2525
|
-
}
|
|
2526
|
-
}
|
|
2527
|
-
if (isTableArray(item)) insertReplacementTableArray(original, item, fullChangedKey.slice(0, -1), sectionInsertIndex++, format, commentEligibleNodes);
|
|
2528
|
-
}
|
|
2529
|
-
return;
|
|
2530
|
-
}
|
|
2531
|
-
}
|
|
2532
|
-
if (change.path.length > 1 && change.path.every((seg) => typeof seg === "string")) {
|
|
2533
|
-
const changedKey = change.path;
|
|
2534
|
-
const explicitParent = findDeepestExplicitTable(original, changedKey, 0, void 0);
|
|
2535
|
-
if (explicitParent && replaceBelowExplicitTable(original, explicitParent, changedKey, jsValue, format, commentEligibleNodes)) return;
|
|
2536
|
-
if (!explicitParent && replaceAsRootDottedRow(original, changedKey, jsValue, format, commentEligibleNodes)) return;
|
|
2537
|
-
}
|
|
2538
|
-
let nested = jsValue;
|
|
2539
|
-
for (let i = change.path.length - 1; i >= 0; i--) nested = { [change.path[i]]: nested };
|
|
2540
|
-
const prefixNodes = findDocumentItemsByKeyPrefix(original, change.path);
|
|
2541
|
-
for (const prefixNode of prefixNodes) remove(original, original, prefixNode);
|
|
2542
|
-
const freshDoc = parseJS(nested, format);
|
|
2543
|
-
const replacementToml = toTOML(freshDoc.items, format);
|
|
2544
|
-
const replacementCst = Array.from(parseTOML(replacementToml));
|
|
2545
|
-
const insertIndex = rootKeyValueInsertIndex(original);
|
|
2546
|
-
if (insertIndex !== void 0) applyWrites(original);
|
|
2547
|
-
let firstInserted;
|
|
2548
|
-
for (let i = 0; i < replacementCst.length; i++) {
|
|
2549
|
-
const item = replacementCst[i];
|
|
2550
|
-
const existingTable = isTable(item) ? original.items.find((t) => isTable(t) && arraysEqual(t.key.item.value, item.key.item.value)) : void 0;
|
|
2551
|
-
if (existingTable) {
|
|
2552
|
-
for (const row of item.items) insert(original, existingTable, row, void 0);
|
|
2553
|
-
if (!firstInserted) firstInserted = existingTable;
|
|
2554
|
-
continue;
|
|
2555
|
-
}
|
|
2556
|
-
if (isKeyValue(item) && isInlineTable(item.value)) {
|
|
2557
|
-
const kvKey = item.key.value;
|
|
2558
|
-
const survivingTable = original.items.find((t) => isTable(t) && arraysEqual(t.key.item.value, kvKey));
|
|
2559
|
-
if (survivingTable) {
|
|
2560
|
-
for (const row of item.value.items) {
|
|
2561
|
-
const rowNode = isInlineItem(row) ? row.item : row;
|
|
2562
|
-
insert(original, survivingTable, rowNode, void 0);
|
|
2563
|
-
}
|
|
2564
|
-
if (!firstInserted) firstInserted = survivingTable;
|
|
2565
|
-
continue;
|
|
2566
|
-
}
|
|
2567
|
-
if (findDocumentItemsByKeyPrefix(original, kvKey).filter((t) => isKeyValue(t) || isTable(t) || isTableArray(t)).length > 0) {
|
|
2568
|
-
for (const row of item.value.items) {
|
|
2569
|
-
const rowNode = isInlineItem(row) ? row.item : row;
|
|
2570
|
-
if (isKeyValue(rowNode)) {
|
|
2571
|
-
const oldRaw = rowNode.key.raw;
|
|
2572
|
-
const dotted = kvKey.concat(rowNode.key.value);
|
|
2573
|
-
rowNode.key.value = dotted;
|
|
2574
|
-
rowNode.key.raw = dotted.map((part) => IS_BARE_KEY.test(part) ? part : JSON.stringify(part).replace(/\x7f/g, "\\u007f")).join(".");
|
|
2575
|
-
const delta = rowNode.key.raw.length - oldRaw.length;
|
|
2576
|
-
rowNode.key.loc.end.column = rowNode.key.loc.start.column + rowNode.key.raw.length;
|
|
2577
|
-
rowNode.equals += delta;
|
|
2578
|
-
shiftNode(rowNode.value, {
|
|
2579
|
-
lines: 0,
|
|
2580
|
-
columns: delta
|
|
2581
|
-
}, { first_line_only: true });
|
|
2582
|
-
if (rowNode.loc.end.line === rowNode.loc.start.line) rowNode.loc.end.column += delta;
|
|
2583
|
-
}
|
|
2584
|
-
insert(original, original, rowNode, i === 0 ? insertIndex : void 0);
|
|
2585
|
-
if (!firstInserted) firstInserted = rowNode;
|
|
2586
|
-
}
|
|
2587
|
-
continue;
|
|
2588
|
-
}
|
|
2589
|
-
}
|
|
2590
|
-
if (isTable(item)) {
|
|
2591
|
-
const tableKey = item.key.item.value;
|
|
2592
|
-
if (findDocumentItemsByKeyPrefix(original, tableKey).filter((t) => isKeyValue(t)).length > 0) {
|
|
2593
|
-
for (const row of item.items) {
|
|
2594
|
-
if (isKeyValue(row)) {
|
|
2595
|
-
const oldRaw = row.key.raw;
|
|
2596
|
-
const dotted = tableKey.concat(row.key.value);
|
|
2597
|
-
row.key.value = dotted;
|
|
2598
|
-
row.key.raw = dotted.map((part) => IS_BARE_KEY.test(part) ? part : JSON.stringify(part).replace(/\x7f/g, "\\u007f")).join(".");
|
|
2599
|
-
const delta = row.key.raw.length - oldRaw.length;
|
|
2600
|
-
row.key.loc.end.column = row.key.loc.start.column + row.key.raw.length;
|
|
2601
|
-
row.equals += delta;
|
|
2602
|
-
shiftNode(row.value, {
|
|
2603
|
-
lines: 0,
|
|
2604
|
-
columns: delta
|
|
2605
|
-
}, { first_line_only: true });
|
|
2606
|
-
if (row.loc.end.line === row.loc.start.line) row.loc.end.column += delta;
|
|
2607
|
-
}
|
|
2608
|
-
insert(original, original, row, i === 0 ? insertIndex : void 0);
|
|
2609
|
-
if (!firstInserted) firstInserted = row;
|
|
2610
|
-
}
|
|
2611
|
-
continue;
|
|
2612
|
-
}
|
|
2613
|
-
}
|
|
2614
|
-
if (i > 0 && firstInserted && isTable(firstInserted) && isKeyValue(item)) insert(original, firstInserted, item, void 0);
|
|
2615
|
-
else insert(original, original, item, i === 0 ? insertIndex : void 0);
|
|
2616
|
-
if (!firstInserted) firstInserted = item;
|
|
2617
|
-
}
|
|
2618
|
-
const replacementKV = firstInserted;
|
|
2619
|
-
if (isTable(replacementKV)) materialisedTables.add(replacementKV);
|
|
2620
|
-
commentEligibleNodes.add(replacementKV);
|
|
2621
|
-
}
|
|
2622
|
-
/**
|
|
2623
|
-
* Tighten the end column of a single-line InlineTable or InlineArray whose
|
|
2624
|
-
* only item was removed by remove(). When items remain, the end is tightened
|
|
2625
|
-
* to just after the last item; when empty, to start + 2 so brackets touch
|
|
2626
|
-
* (e.g. `[]` or `{}`). loc.end is exclusive.
|
|
2627
|
-
*/
|
|
2628
|
-
function tightenInlineContainerEnd(node) {
|
|
2629
|
-
if (node.items.length > 0) {
|
|
2630
|
-
const lastItem = node.items[node.items.length - 1];
|
|
2631
|
-
node.loc.end.column = lastItem.loc.end.column + 1;
|
|
2632
|
-
} else {
|
|
2633
|
-
node.loc.end.line = node.loc.start.line;
|
|
2634
|
-
node.loc.end.column = node.loc.start.column + 2;
|
|
2635
|
-
}
|
|
2636
|
-
}
|
|
2637
|
-
function compactInlineContainerAncestors(root, target) {
|
|
2638
|
-
const path = [];
|
|
2639
|
-
const locate = (node) => {
|
|
2640
|
-
path.push(node);
|
|
2641
|
-
if (node === target) return true;
|
|
2642
|
-
if (isKeyValue(node)) {
|
|
2643
|
-
if (locate(node.value)) return true;
|
|
2644
|
-
} else if (isInlineItem(node)) {
|
|
2645
|
-
if (locate(node.item)) return true;
|
|
2646
|
-
} else if (hasItems(node)) {
|
|
2647
|
-
for (const item of node.items) if (locate(item)) return true;
|
|
2648
|
-
}
|
|
2649
|
-
path.pop();
|
|
2650
|
-
return false;
|
|
2651
|
-
};
|
|
2652
|
-
if (!locate(root)) return;
|
|
2653
|
-
const enclosingRow = path.findLast(isInlineItem);
|
|
2654
|
-
if (!enclosingRow) return;
|
|
2655
|
-
const lineDelta = target.loc.end.line - enclosingRow.loc.end.line;
|
|
2656
|
-
if (lineDelta === 0) return;
|
|
2657
|
-
const inlineContainerIndex = path.findLastIndex((node, index) => index < path.length - 1 && (isInlineTable(node) || isInlineArray(node)));
|
|
2658
|
-
if (inlineContainerIndex < 0) return;
|
|
2659
|
-
const inlineContainer = path[inlineContainerIndex];
|
|
2660
|
-
const child = path[inlineContainerIndex + 1];
|
|
2661
|
-
const childIndex = inlineContainer.items.indexOf(child);
|
|
2662
|
-
const next = childIndex >= 0 ? inlineContainer.items[childIndex + 1] : void 0;
|
|
2663
|
-
if (childIndex < 0 || !next) return;
|
|
2664
|
-
const gap = next.loc.start.column - child.loc.end.column;
|
|
2665
|
-
const columnDelta = resolveInnerEndCol(child) + gap - next.loc.start.column;
|
|
2666
|
-
for (let i = childIndex + 1; i < inlineContainer.items.length; i++) shiftNode(inlineContainer.items[i], {
|
|
2667
|
-
lines: lineDelta,
|
|
2668
|
-
columns: columnDelta
|
|
2669
|
-
});
|
|
2670
|
-
inlineContainer.loc.end.column += columnDelta;
|
|
2671
|
-
for (const node of path) if (node !== target && node.loc.end.line > target.loc.end.line) node.loc.end.line += lineDelta;
|
|
2672
|
-
for (let i = path.length - 1; i >= 0; i--) {
|
|
2673
|
-
const node = path[i];
|
|
2674
|
-
if (isKeyValue(node)) {
|
|
2675
|
-
node.loc.end = { ...node.value.loc.end };
|
|
2676
|
-
if (node.value !== target && isInlineTable(node.value)) node.loc.end.column += columnDelta;
|
|
2677
|
-
} else if (isInlineItem(node)) node.loc.end = { ...node.item.loc.end };
|
|
2678
|
-
}
|
|
2679
|
-
}
|
|
2680
|
-
/**
|
|
2681
|
-
* After tightening a nested single-line InlineTable (whose only item was
|
|
2682
|
-
* removed), recalculate the end positions of any parent single-line InlineTable
|
|
2683
|
-
* or InlineArray containers. The remove() call zeroed the offset for the
|
|
2684
|
-
* removed item (single-line, no siblings), so applyWrites can't propagate the
|
|
2685
|
-
* shrink to ancestor containers.
|
|
2686
|
-
*/
|
|
2687
|
-
function recalcInlineContainerEnds(root) {
|
|
2688
|
-
traverse(root, {
|
|
2689
|
-
InlineTable: { exit: (node) => {
|
|
2690
|
-
if (node.items.length === 0) return;
|
|
2691
|
-
if (node.loc.end.line !== node.loc.start.line) return;
|
|
2692
|
-
const lastItem = node.items[node.items.length - 1];
|
|
2693
|
-
const innerEndCol = resolveInnerEndCol(lastItem);
|
|
2694
|
-
const originalEndCol = node.loc.end.column;
|
|
2695
|
-
const newEndCol = innerEndCol + (originalEndCol - lastItem.loc.end.column);
|
|
2696
|
-
if (newEndCol !== originalEndCol) node.loc.end.column = newEndCol;
|
|
2697
|
-
} },
|
|
2698
|
-
InlineArray: { exit: (node) => {
|
|
2699
|
-
if (node.items.length === 0) return;
|
|
2700
|
-
if (node.loc.end.line !== node.loc.start.line) return;
|
|
2701
|
-
const lastItem = node.items[node.items.length - 1];
|
|
2702
|
-
const innerEndCol = resolveInnerEndCol(lastItem);
|
|
2703
|
-
const originalEndCol = node.loc.end.column;
|
|
2704
|
-
const newEndCol = innerEndCol + (originalEndCol - lastItem.loc.end.column);
|
|
2705
|
-
if (newEndCol !== originalEndCol) node.loc.end.column = newEndCol;
|
|
2706
|
-
} }
|
|
2707
|
-
});
|
|
2708
|
-
}
|
|
2709
|
-
/**
|
|
2710
|
-
* Resolve the effective end column of an InlineItem's inner value,
|
|
2711
|
-
* looking through KeyValue wrappers and into tightened InlineTable or
|
|
2712
|
-
* InlineArray containers. Returns lastItem.loc.end.column for items
|
|
2713
|
-
* that don't need or support look-through.
|
|
2714
|
-
*/
|
|
2715
|
-
function resolveInnerEndCol(lastItem) {
|
|
2716
|
-
if (!isInlineItem(lastItem)) return lastItem.loc.end.column;
|
|
2717
|
-
if (isKeyValue(lastItem.item)) {
|
|
2718
|
-
const v = lastItem.item.value;
|
|
2719
|
-
if (isInlineTable(v) || isInlineArray(v)) return v.loc.end.column;
|
|
2720
|
-
}
|
|
2721
|
-
if (isInlineTable(lastItem.item) || isInlineArray(lastItem.item)) return lastItem.item.loc.end.column;
|
|
2722
|
-
return lastItem.loc.end.column;
|
|
2723
|
-
}
|
|
2724
|
-
/**
|
|
2725
|
-
* Removes Comment nodes that were extracted from inside an InlineArray
|
|
2726
|
-
* when that array has been emptied.
|
|
2727
|
-
*
|
|
2728
|
-
* When a multiline inline array has interior comments (e.g. `arr = [\n 1, # one\n 2, # two\n]`),
|
|
2729
|
-
* the parser extracts the comments to the parent Document level as siblings of the KeyValue.
|
|
2730
|
-
* If the array is later emptied, these comments become orphaned and must be removed
|
|
2731
|
-
* to produce valid TOML output.
|
|
2732
|
-
*
|
|
2733
|
-
* This does NOT apply to InlineTables, where interior comments are meaningful
|
|
2734
|
-
* section-level comments that should be preserved even when the table is empty.
|
|
2735
|
-
*/
|
|
2736
|
-
function cleanupOrphanedComments(doc) {
|
|
2737
|
-
traverse(doc, { KeyValue: (kv) => {
|
|
2738
|
-
const value = kv.value;
|
|
2739
|
-
if (!isInlineArray(value)) return;
|
|
2740
|
-
if (value.items.length > 0) return;
|
|
2741
|
-
const parentContainer = findParent(doc, [kv.key.value[0]]);
|
|
2742
|
-
if (!parentContainer || !hasItems(parentContainer)) return;
|
|
2743
|
-
const kvStartLine = kv.loc.start.line;
|
|
2744
|
-
const kvEndLine = kv.loc.end.line;
|
|
2745
|
-
const items = parentContainer.items;
|
|
2746
|
-
for (let i = items.length - 1; i >= 0; i--) {
|
|
2747
|
-
const item = items[i];
|
|
2748
|
-
if (!isComment(item)) continue;
|
|
2749
|
-
const commentLine = item.loc.start.line;
|
|
2750
|
-
if (commentLine >= kvStartLine && commentLine <= kvEndLine) items.splice(i, 1);
|
|
2751
|
-
}
|
|
2752
|
-
} });
|
|
2753
|
-
}
|
|
2754
|
-
/**
|
|
2755
|
-
* Replaces emptied TableArrays with inline empty array key-values so that
|
|
2756
|
-
* keys aren't silently lost when all AOT entries are removed (e.g. b = []).
|
|
2757
|
-
*/
|
|
2758
|
-
function replaceEmptiedTableArrays(doc, emptiedKeys, format) {
|
|
2759
|
-
if (emptiedKeys.size === 0) return;
|
|
2760
|
-
for (const path of emptiedKeys.values()) {
|
|
2761
|
-
if (path.length > 1) {
|
|
2762
|
-
const parentPath = path.slice(0, -1);
|
|
2763
|
-
const parentEntryPath = typeof parentPath[parentPath.length - 1] === "number" ? parentPath : parentPath.concat([0]);
|
|
2764
|
-
const parentEntry = tryFindByPath(doc, parentEntryPath);
|
|
2765
|
-
if (parentEntry && isTableArray(parentEntry)) {
|
|
2766
|
-
const lastSegment = path[path.length - 1];
|
|
2767
|
-
if (typeof lastSegment !== "string") continue;
|
|
2768
|
-
const emptyArrayDoc = parseJS({ [lastSegment]: [] }, format);
|
|
2769
|
-
const emptyKV = generateKeyValue([lastSegment], emptyArrayDoc.items[0].value);
|
|
2770
|
-
applyWrites(doc);
|
|
2771
|
-
insert(doc, parentEntry, emptyKV);
|
|
2772
|
-
continue;
|
|
2773
|
-
}
|
|
2774
|
-
let hostContainer;
|
|
2775
|
-
for (let len = path.length - 1; len >= 1; len--) {
|
|
2776
|
-
const ancestor = tryFindByPath(doc, path.slice(0, len));
|
|
2777
|
-
if (ancestor && (isTable(ancestor) || isTableArray(ancestor))) {
|
|
2778
|
-
hostContainer = ancestor;
|
|
2779
|
-
break;
|
|
2780
|
-
}
|
|
2781
|
-
}
|
|
2782
|
-
if (hostContainer) {
|
|
2783
|
-
const relativeKey = path.filter((segment) => typeof segment === "string").slice(hostContainer.key.item.value.length);
|
|
2784
|
-
const emptyArrayDoc = parseJS({ [relativeKey[relativeKey.length - 1]]: [] }, format);
|
|
2785
|
-
const emptyKV = generateKeyValue(relativeKey, emptyArrayDoc.items[0].value);
|
|
2786
|
-
applyWrites(doc);
|
|
2787
|
-
insert(doc, hostContainer, emptyKV);
|
|
2788
|
-
continue;
|
|
2789
|
-
}
|
|
2790
|
-
}
|
|
2791
|
-
const keyPath = path.filter((segment) => typeof segment === "string");
|
|
2792
|
-
const emptyArrayDoc = parseJS({ [keyPath[keyPath.length - 1]]: [] }, format);
|
|
2793
|
-
const emptyKV = generateKeyValue(keyPath, emptyArrayDoc.items[0].value);
|
|
2794
|
-
applyWrites(doc);
|
|
2795
|
-
insert(doc, doc, emptyKV, rootKeyValueInsertIndex(doc));
|
|
2796
|
-
}
|
|
2797
|
-
applyWrites(doc);
|
|
2798
|
-
}
|
|
2799
|
-
/**
|
|
2800
|
-
* Converts nested inline tables to separate table sections based on the inlineTableStart depth setting.
|
|
2801
|
-
* This function recursively processes a table and extracts any inline tables within it,
|
|
2802
|
-
* creating separate table sections with properly nested keys.
|
|
2803
|
-
*
|
|
2804
|
-
* @param table - The table to process for nested inline tables
|
|
2805
|
-
* @param original - The original document for inserting new items
|
|
2806
|
-
* @param format - The formatting options
|
|
2807
|
-
* @returns Array of additional tables that should be added to the document
|
|
2808
|
-
*/
|
|
2809
|
-
function convertNestedInlineTablesToMultiline(table, original, format) {
|
|
2810
|
-
const additionalTables = [];
|
|
2811
|
-
const processTableForNestedInlines = (currentTable, tablesToAdd) => {
|
|
2812
|
-
for (let i = currentTable.items.length - 1; i >= 0; i--) {
|
|
2813
|
-
const item = currentTable.items[i];
|
|
2814
|
-
if (isKeyValue(item) && isInlineTable(item.value)) {
|
|
2815
|
-
const nestedTableKey = [...currentTable.key.item.value, ...item.key.value];
|
|
2816
|
-
if (calculateTableDepth(nestedTableKey) < (format.inlineTableStart ?? 1) && format.inlineTableStart !== 0) {
|
|
2817
|
-
const separateTable = generateTable(nestedTableKey, format.escapeSequenceUpperCase);
|
|
2818
|
-
for (const inlineItem of item.value.items) if (isInlineItem(inlineItem) && isKeyValue(inlineItem.item)) insert(original, separateTable, inlineItem.item, void 0);
|
|
2819
|
-
currentTable.items.splice(i, 1);
|
|
2820
|
-
postInlineItemRemovalAdjustment(currentTable);
|
|
2821
|
-
tablesToAdd.push(separateTable);
|
|
2822
|
-
processTableForNestedInlines(separateTable, tablesToAdd);
|
|
2823
|
-
}
|
|
2824
|
-
}
|
|
2825
|
-
}
|
|
2826
|
-
};
|
|
2827
|
-
processTableForNestedInlines(table, additionalTables);
|
|
2828
|
-
return additionalTables;
|
|
2829
|
-
}
|
|
2830
|
-
/**
|
|
2831
|
-
* Converts an inline table to a separate table section when adding to an existing table.
|
|
2832
|
-
* This function creates a new table section with the combined key path and moves all
|
|
2833
|
-
* properties from the inline table to the separate table section.
|
|
2834
|
-
*
|
|
2835
|
-
* @param child - The KeyValue node with an InlineTable as its value
|
|
2836
|
-
* @param parent - The parent table where the KeyValue would be added
|
|
2837
|
-
* @param original - The original document for inserting new items
|
|
2838
|
-
* @param format - The formatting options
|
|
2839
|
-
*/
|
|
2840
|
-
function convertInlineTableToSeparateSection(child, parent, original, format) {
|
|
2841
|
-
const nestedTableKey = [...parent.key.item.value, ...child.key.value];
|
|
2842
|
-
const separateTable = generateTable(nestedTableKey, format.escapeSequenceUpperCase);
|
|
2843
|
-
if (isInlineTable(child.value)) {
|
|
2844
|
-
for (const inlineItem of child.value.items) if (isInlineItem(inlineItem) && isKeyValue(inlineItem.item)) insert(original, separateTable, inlineItem.item, void 0);
|
|
2845
|
-
}
|
|
2846
|
-
insert(original, original, separateTable, void 0);
|
|
2847
|
-
postInlineItemRemovalAdjustment(parent);
|
|
2848
|
-
const additionalTables = convertNestedInlineTablesToMultiline(separateTable, original, format);
|
|
2849
|
-
for (const table of additionalTables) insert(original, original, table, void 0);
|
|
2850
|
-
}
|
|
2851
|
-
/**
|
|
2852
|
-
* Regenerate a replacement Value node with clean loc values through a
|
|
2853
|
-
* TOML round-trip. Handles both KeyValue and Table output from parseJS
|
|
2854
|
-
* (formatTopLevel may convert a root-level key to a [section] when
|
|
2855
|
-
* inlineTableStart is 0). Returns undefined when the value cannot be
|
|
2856
|
-
* safely regenerated.
|
|
2857
|
-
*/
|
|
2858
|
-
function regenerateValue(jsValue, format) {
|
|
2859
|
-
const inlineFmt = resolveTomlFormat({
|
|
2860
|
-
...format,
|
|
2861
|
-
inlineTableStart: 0
|
|
2862
|
-
}, format);
|
|
2863
|
-
const freshDoc = parseJS({ __tmp__: jsValue }, inlineFmt);
|
|
2864
|
-
const replacementToml = toTOML(freshDoc.items, inlineFmt);
|
|
2865
|
-
const freshItem = Array.from(parseTOML(replacementToml))[0];
|
|
2866
|
-
if (isKeyValue(freshItem)) return freshItem.value;
|
|
2867
|
-
if (isTable(freshItem) && freshItem.items.length > 0 && isKeyValue(freshItem.items[0])) return freshItem.items[0].value;
|
|
2868
|
-
}
|
|
2869
|
-
function findEnclosingInlineTableRowContext(document, path) {
|
|
2870
|
-
for (let i = path.length - 1; i > 0; i--) {
|
|
2871
|
-
const candidate = tryFindByPath(document, path.slice(0, i));
|
|
2872
|
-
if (!candidate || !isKeyValue(candidate) || !isInlineTable(candidate.value)) continue;
|
|
2873
|
-
const container = tryFindByPath(document, path.slice(0, i - 1));
|
|
2874
|
-
if (container && hasItems(container)) return {
|
|
2875
|
-
container,
|
|
2876
|
-
row: candidate
|
|
2877
|
-
};
|
|
2878
|
-
}
|
|
2879
|
-
}
|
|
2880
|
-
/**
|
|
2881
|
-
* Finds all Document-level items whose key path starts with the given prefix.
|
|
2882
|
-
* This handles implicit intermediate keys — keys that are a prefix of a dotted
|
|
2883
|
-
* table key but have no CST node of their own (e.g. path ["references"] when
|
|
2884
|
-
* only ["references", "VBIDE"] exists in the document).
|
|
2885
|
-
*/
|
|
2886
|
-
function findDocumentItemsByKeyPrefix(document, pathPrefix) {
|
|
2887
|
-
const matchingNodes = [];
|
|
2888
|
-
for (const item of document.items) {
|
|
2889
|
-
let key;
|
|
2890
|
-
if (isKeyValue(item)) key = item.key.value;
|
|
2891
|
-
else if (isTable(item)) key = item.key.item.value;
|
|
2892
|
-
else if (isTableArray(item)) key = item.key.item.value;
|
|
2893
|
-
if (key && arraysEqual(key.slice(0, pathPrefix.length), pathPrefix)) matchingNodes.push(item);
|
|
2894
|
-
}
|
|
2895
|
-
return matchingNodes;
|
|
2896
|
-
}
|
|
2897
|
-
function itemKey(item) {
|
|
2898
|
-
if (isKeyValue(item)) return item.key.value;
|
|
2899
|
-
if (isTable(item) || isTableArray(item)) return item.key.item.value;
|
|
2900
|
-
}
|
|
2901
|
-
function keyStartsWith(key, prefix) {
|
|
2902
|
-
return key !== void 0 && prefix.every((segment, index) => typeof segment === "string" && key[index] === segment);
|
|
2903
|
-
}
|
|
2904
|
-
function findAotEntryScopeItems(document, entry, knownStart, sourceItems) {
|
|
2905
|
-
const items = sourceItems ?? document.items;
|
|
2906
|
-
const start = knownStart ?? items.indexOf(entry);
|
|
2907
|
-
if (start < 0) return [];
|
|
2908
|
-
const entryKey = entry.key.item.value;
|
|
2909
|
-
let end = items.length;
|
|
2910
|
-
for (let index = start + 1; index < items.length; index++) {
|
|
2911
|
-
const sibling = items[index];
|
|
2912
|
-
if (!isTableArray(sibling)) continue;
|
|
2913
|
-
const siblingKey = sibling.key.item.value;
|
|
2914
|
-
if (siblingKey.length <= entryKey.length && arraysEqual(siblingKey, entryKey.slice(0, siblingKey.length))) {
|
|
2915
|
-
end = index;
|
|
2916
|
-
break;
|
|
2917
|
-
}
|
|
2918
|
-
}
|
|
2919
|
-
return items.slice(start + 1, end);
|
|
2920
|
-
}
|
|
2921
|
-
function findAotEntryForPath(document, path) {
|
|
2922
|
-
for (let index = path.length - 1; index >= 0; index--) {
|
|
2923
|
-
if (typeof path[index] !== "number") continue;
|
|
2924
|
-
const candidate = tryFindByPath(document, path.slice(0, index + 1));
|
|
2925
|
-
if (candidate && isTableArray(candidate)) return candidate;
|
|
2926
|
-
}
|
|
2927
|
-
}
|
|
2928
|
-
function findAotScopedDocumentItems(document, pathPrefix, owner) {
|
|
2929
|
-
const matchingNodes = findDocumentItemsByKeyPrefix(document, pathPrefix);
|
|
2930
|
-
if (!owner) return matchingNodes;
|
|
2931
|
-
const scopedItems = new Set(findAotEntryScopeItems(document, owner));
|
|
2932
|
-
return matchingNodes.filter((item) => scopedItems.has(item));
|
|
2933
|
-
}
|
|
2934
|
-
function isTableArrayValue(value) {
|
|
2935
|
-
return Array.isArray(value) && value.length > 0 && value.every(isObject);
|
|
2936
|
-
}
|
|
2937
|
-
/**
|
|
2938
|
-
* Finds the deepest explicit `[table]` header that is a strict ancestor of
|
|
2939
|
-
* `key` and longer than `minLength` segments, restricted to `owner`'s scope
|
|
2940
|
-
* when the key lives inside an array-of-tables entry.
|
|
2941
|
-
*/
|
|
2942
|
-
function findDeepestExplicitTable(document, key, minLength, owner) {
|
|
2943
|
-
for (let keyLength = key.length - 1; keyLength > minLength; keyLength--) {
|
|
2944
|
-
const parentKey = key.slice(0, keyLength);
|
|
2945
|
-
const parent = findAotScopedDocumentItems(document, parentKey, owner).find((item) => isTable(item) && arraysEqual(item.key.item.value, parentKey));
|
|
2946
|
-
if (parent) return parent;
|
|
2947
|
-
}
|
|
2948
|
-
}
|
|
2949
|
-
/**
|
|
2950
|
-
* Renders `key = value` as a single row for an explicit parent table. An
|
|
2951
|
-
* object value must stay one row: at the default inlineTableStart parseJS
|
|
2952
|
-
* would emit it as a `[key]` section, which can't be placed inside the parent.
|
|
2953
|
-
* A multi-segment key becomes a dotted row (`args.deep = …`): the
|
|
2954
|
-
* intermediate tables are implicit, and nesting them as `args = { deep = … }`
|
|
2955
|
-
* would close `args` to any surviving `[parent.args.other]` header.
|
|
2956
|
-
*/
|
|
2957
|
-
function generateExplicitParentRow(key, value, format) {
|
|
2958
|
-
const inlineFormat = resolveTomlFormat({
|
|
2959
|
-
...format,
|
|
2960
|
-
inlineTableStart: 0
|
|
2961
|
-
}, format);
|
|
2962
|
-
const rowDoc = parseJS({ [key[key.length - 1]]: value }, inlineFormat);
|
|
2963
|
-
const rowItems = Array.from(parseTOML(toTOML(rowDoc.items, format)));
|
|
2964
|
-
if (rowItems.length !== 1 || !isKeyValue(rowItems[0])) return void 0;
|
|
2965
|
-
const row = rowItems[0];
|
|
2966
|
-
if (key.length > 1) rekeyKeyValue(row, key, format);
|
|
2967
|
-
return row;
|
|
2968
|
-
}
|
|
2969
|
-
/**
|
|
2970
|
-
* Rewrites a freshly parsed row's key to `key`, shifting the value along so
|
|
2971
|
-
* its locations still line up with the new key's width.
|
|
2972
|
-
*/
|
|
2973
|
-
function rekeyKeyValue(row, key, format) {
|
|
2974
|
-
const oldRaw = row.key.raw;
|
|
2975
|
-
row.key.value = key;
|
|
2976
|
-
row.key.raw = generateKey(key, format.escapeSequenceUpperCase).raw;
|
|
2977
|
-
const delta = row.key.raw.length - oldRaw.length;
|
|
2978
|
-
row.key.loc.end.column = row.key.loc.start.column + row.key.raw.length;
|
|
2979
|
-
row.equals += delta;
|
|
2980
|
-
shiftNode(row.value, {
|
|
2981
|
-
lines: 0,
|
|
2982
|
-
columns: delta
|
|
2983
|
-
}, { first_line_only: true });
|
|
2984
|
-
if (row.loc.end.line === row.loc.start.line) row.loc.end.column += delta;
|
|
2985
|
-
}
|
|
2986
|
-
/**
|
|
2987
|
-
* Puts `row` in place of `relativeKey` inside `parent`, dropping any dotted
|
|
2988
|
-
* rows that extended the old value.
|
|
2989
|
-
*/
|
|
2990
|
-
function upsertExplicitParentRow(document, parent, relativeKey, row) {
|
|
2991
|
-
const oldRows = parent.items.filter((item) => isKeyValue(item) && keyStartsWith(item.key.value, relativeKey));
|
|
2992
|
-
const exactRow = oldRows.find((item) => arraysEqual(item.key.value, relativeKey));
|
|
2993
|
-
for (const oldRow of oldRows) if (oldRow !== exactRow) removeMember(document, parent, oldRow);
|
|
2994
|
-
if (exactRow) replace(document, parent, exactRow, row);
|
|
2995
|
-
else insert(document, parent, row, void 0);
|
|
2996
|
-
}
|
|
2997
|
-
/**
|
|
2998
|
-
* Index just past `owner` and the contiguous `followers` keyed under it. Used
|
|
2999
|
-
* when no removed sub-entry marks the spot for a replacement section: the
|
|
3000
|
-
* followers can include unrelated tables (e.g. a later `[settings]`), and a
|
|
3001
|
-
* section placed after those would read as belonging to them.
|
|
3002
|
-
*/
|
|
3003
|
-
function ownedSectionsEndIndex(document, owner, followers) {
|
|
3004
|
-
const ownerKey = owner.key.item.value;
|
|
3005
|
-
let index = document.items.indexOf(owner) + 1;
|
|
3006
|
-
for (const item of followers) {
|
|
3007
|
-
if (!keyStartsWith(isTable(item) || isTableArray(item) ? item.key.item.value : void 0, ownerKey)) break;
|
|
3008
|
-
index = document.items.indexOf(item) + 1;
|
|
3009
|
-
}
|
|
3010
|
-
return index;
|
|
3011
|
-
}
|
|
3012
|
-
/**
|
|
3013
|
-
* Re-keys a section rendered relative to `parentKey` (`[[items]]` →
|
|
3014
|
-
* `[[route.args.deep.items]]`) and inserts it at `index` in the document.
|
|
3015
|
-
*/
|
|
3016
|
-
function insertReplacementTableArray(document, section, parentKey, index, format, commentEligibleNodes) {
|
|
3017
|
-
const keyNode = section.key.item;
|
|
3018
|
-
const oldRaw = keyNode.raw;
|
|
3019
|
-
const dotted = parentKey.concat(keyNode.value);
|
|
3020
|
-
keyNode.value = dotted;
|
|
3021
|
-
keyNode.raw = generateKey(dotted, format.escapeSequenceUpperCase).raw;
|
|
3022
|
-
const delta = keyNode.raw.length - oldRaw.length;
|
|
3023
|
-
keyNode.loc.end.column = keyNode.loc.start.column + keyNode.raw.length;
|
|
3024
|
-
if (section.key.loc.end.line === section.key.loc.start.line) section.key.loc.end.column += delta;
|
|
3025
|
-
insert(document, document, section, index, void 0, void 0, 1);
|
|
3026
|
-
commentEligibleNodes.add(section);
|
|
3027
|
-
}
|
|
3028
|
-
/**
|
|
3029
|
-
* Replaces the value at `fullKey` below the explicit `parent` table, keeping
|
|
3030
|
-
* the edit inside that table: a single row for scalars, arrays and objects,
|
|
3031
|
-
* or `[[fullKey]]` sections at the old sub-table's position for an array of
|
|
3032
|
-
* tables. Returns false when the shape isn't handled here.
|
|
3033
|
-
*/
|
|
3034
|
-
function replaceBelowExplicitTable(document, parent, fullKey, jsValue, format, commentEligibleNodes) {
|
|
3035
|
-
const relativeKey = fullKey.slice(parent.key.item.value.length);
|
|
3036
|
-
if (isTableArrayValue(jsValue)) {
|
|
3037
|
-
const parentRows = parent.items.filter((item) => isKeyValue(item) && item.key.value[0] === relativeKey[0]);
|
|
3038
|
-
if (parentRows.some((row) => !keyStartsWith(row.key.value, relativeKey))) return false;
|
|
3039
|
-
const leaf = fullKey[fullKey.length - 1];
|
|
3040
|
-
const sectionDoc = parseJS({ [leaf]: jsValue }, format);
|
|
3041
|
-
const sections = Array.from(parseTOML(toTOML(sectionDoc.items, format)));
|
|
3042
|
-
if (!sections.every((item) => isTable(item) || isTableArray(item))) return false;
|
|
3043
|
-
const toRemove = findDocumentItemsByKeyPrefix(document, fullKey);
|
|
3044
|
-
const removedIndex = toRemove.length > 0 ? document.items.indexOf(toRemove[0]) : -1;
|
|
3045
|
-
for (const node of toRemove) removeMember(document, document, node);
|
|
3046
|
-
for (const row of parentRows) removeMember(document, parent, row);
|
|
3047
|
-
let index = removedIndex >= 0 ? removedIndex : ownedSectionsEndIndex(document, parent, document.items.slice(document.items.indexOf(parent) + 1));
|
|
3048
|
-
for (const section of sections) insertReplacementTableArray(document, section, fullKey.slice(0, -1), index++, format, commentEligibleNodes);
|
|
3049
|
-
return true;
|
|
3050
|
-
}
|
|
3051
|
-
const row = generateExplicitParentRow(relativeKey, jsValue, format);
|
|
3052
|
-
if (!row) return false;
|
|
3053
|
-
for (const node of findDocumentItemsByKeyPrefix(document, fullKey)) removeMember(document, document, node);
|
|
3054
|
-
upsertExplicitParentRow(document, parent, relativeKey, row);
|
|
3055
|
-
commentEligibleNodes.add(row);
|
|
3056
|
-
return true;
|
|
3057
|
-
}
|
|
3058
|
-
/**
|
|
3059
|
-
* Replaces the value at `key` with a root dotted row when no explicit table
|
|
3060
|
-
* owns it but a surviving header still extends one of its implicit
|
|
3061
|
-
* intermediate tables (`[route.args.other]` beside `route.args.deep`): the
|
|
3062
|
-
* generic rebuild would emit `[route]` + `args = { … }`, which that header
|
|
3063
|
-
* can't extend. Returns false when nothing would collide.
|
|
3064
|
-
*/
|
|
3065
|
-
function replaceAsRootDottedRow(document, key, jsValue, format, commentEligibleNodes) {
|
|
3066
|
-
if (key.length < 3 || isTableArrayValue(jsValue)) return false;
|
|
3067
|
-
const intermediateKey = key.slice(0, 2);
|
|
3068
|
-
if (!document.items.some((item) => {
|
|
3069
|
-
const sectionKey = isTable(item) || isTableArray(item) ? item.key.item.value : void 0;
|
|
3070
|
-
return keyStartsWith(sectionKey, intermediateKey) && !keyStartsWith(sectionKey, key);
|
|
3071
|
-
})) return false;
|
|
3072
|
-
const row = generateExplicitParentRow(key, jsValue, format);
|
|
3073
|
-
if (!row) return false;
|
|
3074
|
-
for (const node of findDocumentItemsByKeyPrefix(document, key)) removeMember(document, document, node);
|
|
3075
|
-
const insertIndex = rootKeyValueInsertIndex(document);
|
|
3076
|
-
if (insertIndex !== void 0) applyWrites(document);
|
|
3077
|
-
insert(document, document, row, insertIndex);
|
|
3078
|
-
commentEligibleNodes.add(row);
|
|
3079
|
-
return true;
|
|
3080
|
-
}
|
|
3081
|
-
/**
|
|
3082
|
-
* Converts a JS-object change path into CST key coordinates by dropping the
|
|
3083
|
-
* numeric array-of-tables entry indices. The diff walks the JS object, so a
|
|
3084
|
-
* path into an AOT entry interleaves the entry's numeric index (e.g. `["", 0,
|
|
3085
|
-
* "fv"]`), but CST keys carry no such index — `[[""]]` entries live at the
|
|
3086
|
-
* document level and their sub-tables are keyed `["", "fv"]`. A numeric
|
|
3087
|
-
* segment is an AOT entry index when the accumulated path is a document-level
|
|
3088
|
-
* TableArray key; inline-array indices (which ARE part of the CST path) never
|
|
3089
|
-
* resolve to a document-level TableArray.
|
|
3090
|
-
*/
|
|
3091
|
-
function stripAotEntryIndices(original, jsPath) {
|
|
3092
|
-
const cstPath = [];
|
|
3093
|
-
for (const seg of jsPath) {
|
|
3094
|
-
if (typeof seg === "number") {
|
|
3095
|
-
if (findDocumentItemsByKeyPrefix(original, cstPath).some(isTableArray)) continue;
|
|
3096
|
-
}
|
|
3097
|
-
cstPath.push(seg);
|
|
3098
|
-
}
|
|
3099
|
-
return cstPath;
|
|
3100
|
-
}
|
|
3101
|
-
/**
|
|
3102
|
-
* Removes sibling key-values and table/table-array sections whose key extends
|
|
3103
|
-
* `prefix` (i.e. `prefix` is a strict prefix of the sibling's key) from
|
|
3104
|
-
* `container`'s items, leaving `keep` untouched. Used when a key collapses to
|
|
3105
|
-
* a scalar: any dotted-key or section that still nests under it would re-define
|
|
3106
|
-
* the key on re-parse ("Value already defined").
|
|
3107
|
-
*
|
|
3108
|
-
* Iterates a snapshot of the items because `removeMember` splices the live
|
|
3109
|
-
* array (fuzz seed 31662).
|
|
3110
|
-
*/
|
|
3111
|
-
function removeSiblingsExtendingPrefix(original, container, prefix, keep) {
|
|
3112
|
-
const parentItems = [...container.items];
|
|
3113
|
-
for (let si = parentItems.length - 1; si >= 0; si--) {
|
|
3114
|
-
const sibling = parentItems[si];
|
|
3115
|
-
if (sibling === keep) continue;
|
|
3116
|
-
const siblingKey = isKeyValue(sibling) ? sibling.key.value : isTable(sibling) || isTableArray(sibling) ? sibling.key.item.value : void 0;
|
|
3117
|
-
if (siblingKey && siblingKey.length > prefix.length && arraysEqual(siblingKey.slice(0, prefix.length), prefix)) removeMember(original, container, sibling);
|
|
3118
|
-
}
|
|
3119
|
-
}
|
|
3120
|
-
function removeDocumentItemsExtendingPath(document, path, keep) {
|
|
3121
|
-
const cstPath = stripAotEntryIndices(document, path);
|
|
3122
|
-
const owner = findAotEntryForPath(document, path);
|
|
3123
|
-
for (const item of findAotScopedDocumentItems(document, cstPath, owner)) if (item !== keep) removeMember(document, document, item);
|
|
3124
|
-
}
|
|
3125
|
-
|
|
3126
|
-
//#endregion
|
|
3127
|
-
export { patch as default, patchCst };
|
|
3128
|
-
//# sourceMappingURL=patch.js.map
|