@decimalturn/toml-patch 3.3.2-lite → 3.3.3-dev

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (81) hide show
  1. package/dist/dev/comment-alignment.js +371 -0
  2. package/dist/dev/comment-alignment.js.map +1 -0
  3. package/dist/dev/comment-ownership.js +672 -0
  4. package/dist/dev/comment-ownership.js.map +1 -0
  5. package/dist/dev/cst-source.js +117 -0
  6. package/dist/dev/cst-source.js.map +1 -0
  7. package/dist/dev/cst.js +67 -0
  8. package/dist/dev/cst.js.map +1 -0
  9. package/dist/dev/cursor.js +65 -0
  10. package/dist/dev/cursor.js.map +1 -0
  11. package/dist/dev/date-format.js +286 -0
  12. package/dist/dev/date-format.js.map +1 -0
  13. package/dist/dev/decode-utf8.js +26 -0
  14. package/dist/dev/decode-utf8.js.map +1 -0
  15. package/dist/dev/diff-lite.js +107 -0
  16. package/dist/dev/diff-lite.js.map +1 -0
  17. package/dist/dev/diff.js +264 -0
  18. package/dist/dev/diff.js.map +1 -0
  19. package/dist/dev/encode-lite.js +209 -0
  20. package/dist/dev/encode-lite.js.map +1 -0
  21. package/dist/dev/escape-preference.js +165 -0
  22. package/dist/dev/escape-preference.js.map +1 -0
  23. package/dist/dev/find-by-path.js +114 -0
  24. package/dist/dev/find-by-path.js.map +1 -0
  25. package/dist/dev/formatter.js +297 -0
  26. package/dist/dev/formatter.js.map +1 -0
  27. package/dist/dev/generate.js +479 -0
  28. package/dist/dev/generate.js.map +1 -0
  29. package/dist/dev/inline-comma-space.js +12 -0
  30. package/dist/dev/inline-comma-space.js.map +1 -0
  31. package/dist/dev/inline-format.js +105 -0
  32. package/dist/dev/inline-format.js.map +1 -0
  33. package/dist/dev/inline-layout.js +106 -0
  34. package/dist/dev/inline-layout.js.map +1 -0
  35. package/dist/dev/line-ending-backslash.js +319 -0
  36. package/dist/dev/line-ending-backslash.js.map +1 -0
  37. package/dist/dev/literal-string.js +45 -0
  38. package/dist/dev/literal-string.js.map +1 -0
  39. package/dist/dev/location.js +103 -0
  40. package/dist/dev/location.js.map +1 -0
  41. package/dist/dev/parse-error.js +26 -0
  42. package/dist/dev/parse-error.js.map +1 -0
  43. package/dist/dev/parse-js.js +149 -0
  44. package/dist/dev/parse-js.js.map +1 -0
  45. package/dist/dev/parse-string.js +119 -0
  46. package/dist/dev/parse-string.js.map +1 -0
  47. package/dist/dev/parse-toml.js +713 -0
  48. package/dist/dev/parse-toml.js.map +1 -0
  49. package/dist/dev/patch-lite.js +4 -0
  50. package/dist/dev/patch-lite2.js +96 -0
  51. package/dist/dev/patch-lite2.js.map +1 -0
  52. package/dist/dev/patch.js +3143 -0
  53. package/dist/dev/patch.js.map +1 -0
  54. package/dist/dev/smol-toml-date.js +60 -0
  55. package/dist/dev/smol-toml-date.js.map +1 -0
  56. package/dist/dev/to-js.js +307 -0
  57. package/dist/dev/to-js.js.map +1 -0
  58. package/dist/dev/to-toml.js +873 -0
  59. package/dist/dev/to-toml.js.map +1 -0
  60. package/dist/dev/tokenizer.js +224 -0
  61. package/dist/dev/tokenizer.js.map +1 -0
  62. package/dist/dev/toml-document.js +220 -0
  63. package/dist/dev/toml-document.js.map +1 -0
  64. package/dist/dev/toml-format.js +548 -0
  65. package/dist/dev/toml-format.js.map +1 -0
  66. package/dist/dev/toml-patch.js +93 -0
  67. package/dist/dev/toml-patch.js.map +1 -0
  68. package/dist/dev/traverse.js +60 -0
  69. package/dist/dev/traverse.js.map +1 -0
  70. package/dist/dev/truncate.js +63 -0
  71. package/dist/dev/truncate.js.map +1 -0
  72. package/dist/dev/update-order.js +234 -0
  73. package/dist/dev/update-order.js.map +1 -0
  74. package/dist/dev/utils.js +295 -0
  75. package/dist/dev/utils.js.map +1 -0
  76. package/dist/dev/writer.js +757 -0
  77. package/dist/dev/writer.js.map +1 -0
  78. package/dist/{patch.d.ts → patch-lite.d.ts} +0 -2
  79. package/dist/toml-patch.d.ts +548 -0
  80. package/package.json +11 -6
  81. package/dist/patch.js +0 -20
@@ -0,0 +1,757 @@
1
+ //! @decimalturn/toml-patch v3.3.3 - https://github.com/DecimalTurn/toml-patch - @license: MIT
2
+ import { hasItem, hasItems, isBlock, isComment, isDocument, isInlineArray, isInlineItem, isInlineTable, isKeyValue, isTable, isTableArray } from "./cst.js";
3
+ import { clonePosition, getSpan } from "./location.js";
4
+ import { last } from "./utils.js";
5
+ import traverse from "./traverse.js";
6
+ import { getCommaSpace } from "./inline-comma-space.js";
7
+ import { markMutation, markTreeDirty } from "./cst-source.js";
8
+ import { getInlineContainerLayout, isInlineContainerPositioned, markPreservedMultilineEmpty } from "./inline-format.js";
9
+
10
+ // src/writer.ts
11
+ const dirty_roots = /* @__PURE__ */ new WeakSet();
12
+ const stringifyRoots = /* @__PURE__ */ new WeakSet();
13
+ const rootIndentWidths = /* @__PURE__ */ new WeakMap();
14
+ const inlineIndentColumns = /* @__PURE__ */ new WeakMap();
15
+ function setRootIndentWidth(root, indentWidth) {
16
+ rootIndentWidths.set(root, indentWidth);
17
+ }
18
+ function setInlineIndentColumn(container, column) {
19
+ inlineIndentColumns.set(container, column);
20
+ }
21
+ /** Mark a root as being built by parseJS — enables stringify fast paths. */
22
+ function markStringifyRoot(root) {
23
+ stringifyRoots.add(root);
24
+ }
25
+ const emptiedByRemove = /* @__PURE__ */ new WeakMap();
26
+ const hadNonLastRemoval = /* @__PURE__ */ new WeakSet();
27
+ const inlineContainersNeedingTighten = /* @__PURE__ */ new WeakSet();
28
+ function hasInlineContainerNeedingTighten(node) {
29
+ return inlineContainersNeedingTighten.has(node);
30
+ }
31
+ function deleteInlineContainerNeedingTighten(node) {
32
+ inlineContainersNeedingTighten.delete(node);
33
+ }
34
+ /**
35
+ * Add an exit offset at `node` so that applyWrites shifts everything
36
+ * after the node in depth-first order by `span`. Used by key-truncation
37
+ * in the Edit handler to account for a shortened key without having to
38
+ * manually adjust every downstream position.
39
+ */
40
+ function addExitOffset(root, node, span) {
41
+ addOffset(span, getExitOffsets(root), node);
42
+ }
43
+ /** Marks a root so the next applyWrites() walk actually processes it. */
44
+ function markDirty(root) {
45
+ markTreeDirty(root);
46
+ dirty_roots.add(root);
47
+ }
48
+ const enter_offsets = /* @__PURE__ */ new WeakMap();
49
+ const getEnterOffsets = (root) => {
50
+ if (!enter_offsets.has(root)) enter_offsets.set(root, /* @__PURE__ */ new WeakMap());
51
+ return enter_offsets.get(root);
52
+ };
53
+ /**
54
+ * The pending enter-offset map for a root. Exposed so callers can detect a
55
+ * block container that still carries a removal's enter offset before
56
+ * inserting at index 0 (fuzz seed 11557: the new row was dragged above
57
+ * line 1 by the unresolved removal shift).
58
+ */
59
+ function getPendingEnterOffsets(root) {
60
+ if (!enter_offsets.has(root)) enter_offsets.set(root, /* @__PURE__ */ new WeakMap());
61
+ return enter_offsets.get(root);
62
+ }
63
+ const exit_offsets = /* @__PURE__ */ new WeakMap();
64
+ /**
65
+ * The pending exit-offset map for a root. Exposed so moveInlineElement can
66
+ * cancel a removal's offset before it leaks past the container (fuzz seed 900).
67
+ */
68
+ function getExitOffsets(root) {
69
+ if (!exit_offsets.has(root)) exit_offsets.set(root, /* @__PURE__ */ new WeakMap());
70
+ return exit_offsets.get(root);
71
+ }
72
+ function replace(root, parent, existing, replacement) {
73
+ if (hasItems(parent)) {
74
+ const index = parent.items.indexOf(existing);
75
+ if (index < 0) throw new Error(`Item not found in parent for replace`);
76
+ parent.items.splice(index, 1, replacement);
77
+ } else if (isKeyValue(parent) && isInlineTable(parent.value) && !isInlineTable(existing)) {
78
+ const index = parent.value.items.indexOf(existing);
79
+ if (index < 0) throw new Error(`Item not found in parent for replace`);
80
+ parent.value.items.splice(index, 1, replacement);
81
+ } else if (hasItem(parent)) parent.item = replacement;
82
+ else if (isKeyValue(parent)) {
83
+ if (parent.key === existing) parent.key = replacement;
84
+ else parent.value = replacement;
85
+ } else throw new Error(`Unsupported parent type "${parent.type}" for replace`);
86
+ shiftNode(replacement, {
87
+ lines: existing.loc.start.line - replacement.loc.start.line,
88
+ columns: existing.loc.start.column - replacement.loc.start.column
89
+ });
90
+ const existing_span = getSpan(existing.loc);
91
+ const replacement_span = getSpan(replacement.loc);
92
+ addOffset({
93
+ lines: replacement_span.lines - existing_span.lines,
94
+ columns: replacement_span.columns - existing_span.columns
95
+ }, getExitOffsets(root), replacement, existing);
96
+ if (!stringifyRoots.has(root)) markMutation(root, parent, replacement);
97
+ dirty_roots.add(root);
98
+ }
99
+ /**
100
+ * Inserts a child node into the CST.
101
+ *
102
+ * @param root - The root node of the CST
103
+ * @param parent - The parent node to insert the child into
104
+ * @param child - The child node to insert
105
+ * @param index - The index at which to insert the child (optional)
106
+ * @param forceInline - Whether to force inline positioning even for document-level insertions (optional)
107
+ */
108
+ function insert(root, parent, child, index, forceInline, hostItems, leadingLines, firstLineOnly = false) {
109
+ if (!hasItems(parent)) throw new Error(`Unsupported parent type "${parent.type}" for insert`);
110
+ index = index != null && typeof index === "number" ? index : parent.items.length;
111
+ let shift;
112
+ let offset;
113
+ if (isInlineArray(parent) || isInlineTable(parent)) ({shift, offset} = insertInline(parent, child, index, rootIndentWidths.get(root) ?? 2));
114
+ else if (forceInline && isDocument(parent)) ({shift, offset} = insertInlineAtRoot(parent, child, index));
115
+ else ({shift, offset} = insertOnNewLine(root, parent, child, index, leadingLines));
116
+ shiftNode(child, shift, { first_line_only: firstLineOnly });
117
+ const previous = parent.items[index - 1];
118
+ const previous_offset = previous && getExitOffsets(root).get(previous);
119
+ if (previous_offset) {
120
+ offset.lines += previous_offset.lines;
121
+ offset.columns += previous_offset.columns;
122
+ getExitOffsets(root).delete(previous);
123
+ }
124
+ if ((isInlineTable(parent) || isInlineArray(parent)) && offset.lines !== 0 && (hostItems || hasItems(root)) && root !== parent) {
125
+ const insertionLine = child.loc.start.line;
126
+ const commentHostItems = hostItems ?? root.items;
127
+ const commentContainer = enclosingInlineValue(parent, commentHostItems);
128
+ for (let i = 0; i < commentHostItems.length; i++) {
129
+ const item = commentHostItems[i];
130
+ if (!isComment(item)) continue;
131
+ const commentLine = item.loc.start.line;
132
+ if (commentLine < commentContainer.loc.start.line || commentLine > commentContainer.loc.end.line) continue;
133
+ if (commentLine < insertionLine) {
134
+ item.loc.start.line -= offset.lines;
135
+ item.loc.end.line -= offset.lines;
136
+ }
137
+ }
138
+ }
139
+ getExitOffsets(root).set(child, offset);
140
+ if (!stringifyRoots.has(root)) markMutation(root, parent, child);
141
+ dirty_roots.add(root);
142
+ }
143
+ function enclosingInlineValue(parent, hostItems) {
144
+ for (const hostItem of hostItems) {
145
+ if (!isKeyValue(hostItem)) continue;
146
+ const value = hostItem.value;
147
+ if (!isInlineArray(value) && !isInlineTable(value)) continue;
148
+ let containsParent = false;
149
+ const findParent = (node) => {
150
+ if (node === parent) containsParent = true;
151
+ };
152
+ traverse(value, {
153
+ InlineArray: findParent,
154
+ InlineTable: findParent
155
+ });
156
+ if (containsParent) return value;
157
+ }
158
+ return parent;
159
+ }
160
+ function insertOnNewLine(root, parent, child, index, leadingLinesOverride) {
161
+ if (!isBlock(child)) throw new Error(`Incompatible child type "${child.type}"`);
162
+ const previous = parent.items[index - 1];
163
+ const use_first_line = isDocument(parent) && !parent.items.length;
164
+ const prepend_to_document = isDocument(parent) && !use_first_line && previous === void 0;
165
+ parent.items.splice(index, 0, child);
166
+ let furthestPrevious;
167
+ if (previous !== void 0) {
168
+ if (stringifyRoots.has(root)) furthestPrevious = previous;
169
+ else {
170
+ let hasComment = false;
171
+ for (let i = 0; i < index; i++) if (isComment(parent.items[i])) {
172
+ hasComment = true;
173
+ break;
174
+ }
175
+ if (!hasComment) furthestPrevious = previous;
176
+ else {
177
+ let maxEndLine = -1;
178
+ let maxEndColumn = -1;
179
+ for (let i = 0; i < index; i++) {
180
+ const item = parent.items[i];
181
+ const end = item.loc.end;
182
+ if (end.line > maxEndLine || end.line === maxEndLine && end.column > maxEndColumn) {
183
+ maxEndLine = end.line;
184
+ maxEndColumn = end.column;
185
+ furthestPrevious = item;
186
+ }
187
+ }
188
+ }
189
+ }
190
+ }
191
+ const start = furthestPrevious ? {
192
+ line: furthestPrevious.loc.end.line,
193
+ column: !isComment(furthestPrevious) ? furthestPrevious.loc.start.column : parent.loc.start.column
194
+ } : clonePosition(parent.loc.start);
195
+ const isSquareBracketsStructure = isTable(child) || isTableArray(child);
196
+ let leading_lines = 0;
197
+ if (leadingLinesOverride !== void 0) leading_lines = leadingLinesOverride;
198
+ else if (use_first_line || prepend_to_document) {} else if (isSquareBracketsStructure) leading_lines = 2;
199
+ else leading_lines = 1;
200
+ start.line += leading_lines;
201
+ const shift = {
202
+ lines: start.line - child.loc.start.line,
203
+ columns: start.column - child.loc.start.column
204
+ };
205
+ const child_span = getSpan(child.loc);
206
+ const wasEmptied = !stringifyRoots.has(root) && previous === void 0 && (isTable(parent) || isTableArray(parent) || isDocument(parent)) && emptiedByRemove.has(parent);
207
+ const wasSingleRemoval = wasEmptied && !hadNonLastRemoval.has(parent);
208
+ if (wasEmptied) {
209
+ emptiedByRemove.delete(parent);
210
+ hadNonLastRemoval.delete(parent);
211
+ }
212
+ const needsCompensation = wasEmptied && !wasSingleRemoval;
213
+ let compensation_lines = 0;
214
+ if (needsCompensation) {
215
+ const staleOffset = isTable(parent) || isTableArray(parent) ? getExitOffsets(root).get(parent.key) : getEnterOffsets(root).get(parent);
216
+ compensation_lines = staleOffset ? -staleOffset.lines : 1;
217
+ }
218
+ const offset_leading = wasSingleRemoval || needsCompensation ? leading_lines - 2 : leading_lines - 1;
219
+ const offset = {
220
+ lines: prepend_to_document ? child_span.lines + 1 : child_span.lines + offset_leading,
221
+ columns: child_span.columns
222
+ };
223
+ return {
224
+ shift: {
225
+ lines: shift.lines + compensation_lines,
226
+ columns: shift.columns
227
+ },
228
+ offset
229
+ };
230
+ }
231
+ /**
232
+ * Calculates positioning (shift and offset) for inserting a child into a parent container.
233
+ * This function handles the core positioning logic used to insert an inline item inside a table (or at the document root level).
234
+ *
235
+ * @param parent - The parent container (Document, InlineArray or InlineTable)
236
+ * @param child - The child node to be inserted
237
+ * @param index - The insertion index within the parent's items
238
+ * @param options - Configuration options for positioning calculation
239
+ * @param options.useNewLine - Whether to place the child on a new line
240
+ * @param options.skipCommaSpace - Number of columns to skip for comma + space (default: 2)
241
+ * @param options.skipBracketSpace - Number of columns to skip for bracket/space (default: 1)
242
+ * @param options.hasCommaHandling - Whether comma handling logic should be applied
243
+ * @param options.isLastElement - Whether this is the last element in the container
244
+ * @param options.hasSeparatingCommaBefore - Whether a comma should precede this element
245
+ * @param options.hasSeparatingCommaAfter - Whether a comma should follow this element
246
+ * @param options.hasTrailingComma - Whether the element has a trailing comma
247
+ * @returns Object containing shift (positioning adjustment for the child) and offset (adjustment for following elements)
248
+ */
249
+ function calculateInlinePositioning(parent, child, index, options = {}) {
250
+ const { useNewLine = false, skipCommaSpace = 2, skipBracketSpace = 1, hasCommaHandling = false, isLastElement = false, hasSeparatingCommaBefore = false, hasSeparatingCommaAfter = false, hasTrailingComma = false, indentWidth = 2 } = options;
251
+ const previous = index > 0 ? parent.items[index - 1] : void 0;
252
+ const start = previous ? {
253
+ line: previous.loc.end.line,
254
+ column: useNewLine ? !isComment(previous) ? previous.loc.start.column : parent.loc.start.column : previous.loc.end.column
255
+ } : clonePosition(parent.loc.start);
256
+ if (!previous && useNewLine) {
257
+ const following = parent.items.find((item, i) => i > index && !isComment(item));
258
+ if (following) start.column = following.loc.start.column;
259
+ else if (parent.loc.end.line > parent.loc.start.line) start.column = (isInlineArray(parent) || isInlineTable(parent) ? inlineIndentColumns.get(parent) : void 0) ?? parent.loc.end.column - 1 + indentWidth;
260
+ }
261
+ let leading_lines = 0;
262
+ if (useNewLine) leading_lines = 1;
263
+ else {
264
+ const hasSpacing = hasSeparatingCommaBefore || !hasCommaHandling && !!previous;
265
+ if (hasSpacing && hasCommaHandling) start.column += skipCommaSpace;
266
+ else if (hasSpacing || hasCommaHandling && !previous) start.column += skipBracketSpace;
267
+ }
268
+ start.line += leading_lines;
269
+ const shift = {
270
+ lines: start.line - child.loc.start.line,
271
+ columns: start.column - child.loc.start.column
272
+ };
273
+ const child_span = getSpan(child.loc);
274
+ if (!hasCommaHandling) return {
275
+ shift,
276
+ offset: {
277
+ lines: child_span.lines + (leading_lines - 1),
278
+ columns: child_span.columns
279
+ }
280
+ };
281
+ const has_trailing_comma_spacing_bug = hasSeparatingCommaBefore && hasTrailingComma && !hasSeparatingCommaAfter && isLastElement;
282
+ let trailing_comma_offset_adjustment = 0;
283
+ if (has_trailing_comma_spacing_bug) trailing_comma_offset_adjustment = -1;
284
+ let offset_columns = child_span.columns + (hasSeparatingCommaBefore || hasSeparatingCommaAfter ? skipCommaSpace : 0) + (hasTrailingComma ? 1 + trailing_comma_offset_adjustment : 0);
285
+ if (useNewLine && isLastElement && previous && parent.loc.end.line === previous.loc.end.line) offset_columns = child_span.columns - (previous.loc.end.column - previous.loc.start.column);
286
+ return {
287
+ shift,
288
+ offset: {
289
+ lines: child_span.lines + (leading_lines - 1),
290
+ columns: offset_columns
291
+ }
292
+ };
293
+ }
294
+ /**
295
+ * The horizontal gap a separating comma occupies between two adjacent inline
296
+ * siblings: 1 column for a compact list (`[1,2]`), 2 for a spaced list
297
+ * (`[1, 2]`). Detected once at parse time and stored in a WeakMap registry
298
+ * (`inline-comma-space`), so it stays accurate even after later edits shift
299
+ * the items' own positions. Defaults to 2 (spaced) for containers the parser
300
+ * didn't tag (e.g. values regenerated by parseJS).
301
+ */
302
+ function commaSpaceOf(container) {
303
+ return getCommaSpace(container) ?? 2;
304
+ }
305
+ function insertInline(parent, child, index, indentWidth) {
306
+ if (!isInlineItem(child)) throw new Error(`Incompatible child type "${child.type}"`);
307
+ const skip_comma_space = commaSpaceOf(parent);
308
+ const previous = index != null ? parent.items[index - 1] : last(parent.items);
309
+ const is_last = index == null || index === parent.items.length;
310
+ const next = index != null ? parent.items[index] : void 0;
311
+ parent.items.splice(index, 0, child);
312
+ const has_separating_comma_before = !!previous;
313
+ const has_separating_comma_after = !is_last;
314
+ if (has_separating_comma_before) previous.comma = true;
315
+ if (has_separating_comma_after) child.comma = true;
316
+ return calculateInlinePositioning(parent, child, index, {
317
+ useNewLine: perLine(parent, child) && !(previous && next && next.loc.start.line === previous.loc.end.line && next.loc.start.column >= previous.loc.end.column) && !(getInlineContainerLayout(parent) !== true && isInlineTable(parent) && !next && previous && previous.loc.end.line === parent.loc.end.line),
318
+ skipCommaSpace: skip_comma_space,
319
+ hasCommaHandling: true,
320
+ isLastElement: is_last,
321
+ hasSeparatingCommaBefore: has_separating_comma_before,
322
+ hasSeparatingCommaAfter: has_separating_comma_after,
323
+ hasTrailingComma: is_last && child.comma === true,
324
+ indentWidth
325
+ });
326
+ }
327
+ /**
328
+ * Inserts a child into a Document with inline positioning behavior.
329
+ * This provides inline-style spacing while maintaining Document's Block item types.
330
+ */
331
+ function insertInlineAtRoot(parent, child, index) {
332
+ const result = calculateInlinePositioning(parent, child, index, {
333
+ useNewLine: false,
334
+ hasCommaHandling: false
335
+ });
336
+ parent.items.splice(index, 0, child);
337
+ return result;
338
+ }
339
+ function remove(root, parent, node, hostItems) {
340
+ if (!hasItems(parent)) throw new Error(`Unsupported parent type "${parent.type}" for remove`);
341
+ if (!stringifyRoots.has(root)) markMutation(root, parent);
342
+ let index = parent.items.indexOf(node);
343
+ if (index < 0) {
344
+ index = parent.items.findIndex((item) => hasItem(item) && item.item === node);
345
+ if (index < 0) throw new Error("Node not found in parent for removal");
346
+ node = parent.items[index];
347
+ }
348
+ const previous = parent.items[index - 1];
349
+ let next = parent.items[index + 1];
350
+ parent.items.splice(index, 1);
351
+ let removed_span = getSpan(node.loc);
352
+ if (next && isComment(next) && next.loc.start.line === node.loc.end.line) {
353
+ removed_span = getSpan({
354
+ start: node.loc.start,
355
+ end: next.loc.end
356
+ });
357
+ next = parent.items[index + 1];
358
+ parent.items.splice(index, 1);
359
+ }
360
+ const is_inline = previous && isInlineItem(previous) || next && isInlineItem(next);
361
+ const prevPendingExit = previous ? getExitOffsets(root).get(previous) : void 0;
362
+ const previous_on_same_line = previous && previous.loc.end.line === node.loc.start.line + ((prevPendingExit === null || prevPendingExit === void 0 ? void 0 : prevPendingExit.lines) ?? 0);
363
+ const next_on_sameLine = next && next.loc.start.line === node.loc.end.line;
364
+ const keep_line = is_inline && (previous_on_same_line || next_on_sameLine);
365
+ const offset = {
366
+ lines: -(removed_span.lines - (keep_line ? 1 : 0)),
367
+ columns: keep_line ? -removed_span.columns : 0
368
+ };
369
+ if ((isDocument(parent) || isTable(parent) || isTableArray(parent)) && next && !keep_line && next.loc.start.line > node.loc.end.line) {
370
+ const removedIsSection = isTable(node) || isTableArray(node);
371
+ const nextIsSection = isTable(next) || isTableArray(next);
372
+ let precedingEndLine = -1;
373
+ for (let i = 0; i < index; i++) {
374
+ const end = parent.items[i].loc.end.line;
375
+ if (end > precedingEndLine) precedingEndLine = end;
376
+ }
377
+ const prevPendingExit = previous ? getExitOffsets(root).get(previous) : void 0;
378
+ const nodePendingExit = getExitOffsets(root).get(node);
379
+ const extra = previous ? removedIsSection ? node.loc.start.line + ((prevPendingExit === null || prevPendingExit === void 0 ? void 0 : prevPendingExit.lines) ?? 0) - precedingEndLine - 1 : 0 : nextIsSection ? next.loc.start.line + ((nodePendingExit === null || nodePendingExit === void 0 ? void 0 : nodePendingExit.lines) ?? 0) - node.loc.end.line - 1 : 0;
380
+ if (extra > 0) offset.lines -= extra;
381
+ }
382
+ const isMultilineInlineContainer = (isInlineTable(parent) || isInlineArray(parent)) && parent.loc.end.line > parent.loc.start.line;
383
+ const emptiedFromContainerLine = isMultilineInlineContainer && node.loc.start.line === parent.loc.start.line;
384
+ if (isMultilineInlineContainer && !emptiedFromContainerLine && previous === void 0 && next === void 0 && node.loc.start.line > parent.loc.start.line && parent.loc.end.line > node.loc.end.line) markPreservedMultilineEmpty(parent);
385
+ if (previous === void 0 && next === void 0 && (!isMultilineInlineContainer || emptiedFromContainerLine)) {
386
+ offset.lines = 0;
387
+ offset.columns = 0;
388
+ if (isInlineTable(parent) || isInlineArray(parent)) inlineContainersNeedingTighten.add(parent);
389
+ if (isTable(parent) || isTableArray(parent) || isDocument(parent)) emptiedByRemove.set(parent, true);
390
+ }
391
+ if (!(previous === void 0 && next === void 0) && (isTable(parent) || isTableArray(parent) || isDocument(parent))) hadNonLastRemoval.add(parent);
392
+ const removed_comma_gap = isInlineArray(parent) || isInlineTable(parent) ? commaSpaceOf(parent) : 2;
393
+ if (is_inline && previous_on_same_line) offset.columns -= removed_comma_gap;
394
+ if (is_inline && !previous && next) {
395
+ if (!perLine(parent)) offset.columns -= removed_comma_gap;
396
+ }
397
+ if (is_inline && previous && !next) {
398
+ if (node.comma) previous.comma = true;
399
+ else previous.comma = false;
400
+ if (previous_on_same_line) {
401
+ var _getExitOffsets$get;
402
+ const pendingPrevColumns = previous ? ((_getExitOffsets$get = getExitOffsets(root).get(previous)) === null || _getExitOffsets$get === void 0 ? void 0 : _getExitOffsets$get.columns) ?? 0 : 0;
403
+ offset.columns = previous.loc.end.column - node.loc.end.column - pendingPrevColumns;
404
+ }
405
+ }
406
+ let target;
407
+ let target_offsets;
408
+ if (previous) {
409
+ target = previous;
410
+ target_offsets = getExitOffsets(root);
411
+ } else if ((isTable(parent) || isTableArray(parent)) && "key" in parent) {
412
+ target = parent.key;
413
+ target_offsets = getExitOffsets(root);
414
+ } else {
415
+ target = parent;
416
+ target_offsets = getEnterOffsets(root);
417
+ }
418
+ const node_offsets = getExitOffsets(root);
419
+ const previous_offset = target_offsets.get(target);
420
+ if (previous_offset) {
421
+ offset.lines += previous_offset.lines;
422
+ offset.columns += previous_offset.columns;
423
+ }
424
+ const removed_offset = node_offsets.get(node);
425
+ if (removed_offset) {
426
+ offset.lines += removed_offset.lines;
427
+ if (keep_line) offset.columns += removed_offset.columns;
428
+ }
429
+ target_offsets.set(target, offset);
430
+ dirty_roots.add(root);
431
+ if (isMultilineInlineContainer && (hostItems || hasItems(root)) && root !== parent) {
432
+ const removedLine = node.loc.start.line;
433
+ const commentHostItems = hostItems ?? root.items;
434
+ const commentContainer = enclosingInlineValue(parent, commentHostItems);
435
+ const toRemove = [];
436
+ for (let i = 0; i < commentHostItems.length; i++) {
437
+ const item = commentHostItems[i];
438
+ if (!isComment(item)) continue;
439
+ const commentLine = item.loc.start.line;
440
+ if (commentLine < commentContainer.loc.start.line || commentLine > commentContainer.loc.end.line) continue;
441
+ if (commentLine === removedLine && (commentLine < parent.loc.end.line || item.loc.start.column < parent.loc.end.column)) toRemove.push(i);
442
+ else if (offset.lines !== 0 && commentLine < removedLine) {
443
+ item.loc.start.line -= offset.lines;
444
+ item.loc.end.line -= offset.lines;
445
+ }
446
+ }
447
+ for (let i = toRemove.length - 1; i >= 0; i--) commentHostItems.splice(toRemove[i], 1);
448
+ }
449
+ }
450
+ /**
451
+ * Recalculates a container's end position as the max of its key (for Table/
452
+ * TableArray) and all its children's ends. Necessary because the offset-based
453
+ * shiftEnd approach can produce wrong container ends when a block-level child
454
+ * is removed: the column offset for the removed line bleeds into the previous
455
+ * sibling's line after the line-count shift, causing the container end to
456
+ * shrink below its remaining children.
457
+ */
458
+ function recalcContainerEnd(container) {
459
+ let endLine = container.loc.start.line;
460
+ let endCol = container.loc.start.column;
461
+ if ("key" in container) {
462
+ const ke = container.key.loc.end;
463
+ if (ke.line > endLine || ke.line === endLine && ke.column > endCol) {
464
+ endLine = ke.line;
465
+ endCol = ke.column;
466
+ }
467
+ }
468
+ for (let i = 0; i < container.items.length; i++) {
469
+ const e = container.items[i].loc.end;
470
+ if (e.line > endLine || e.line === endLine && e.column > endCol) {
471
+ endLine = e.line;
472
+ endCol = e.column;
473
+ }
474
+ }
475
+ container.loc.end = {
476
+ line: endLine,
477
+ column: endCol
478
+ };
479
+ }
480
+ function applyBracketSpacing(root, node, bracket_spacing = true) {
481
+ if (!bracket_spacing) return;
482
+ if (!node.items.length) return;
483
+ addOffset({
484
+ lines: 0,
485
+ columns: 1
486
+ }, getEnterOffsets(root), node);
487
+ const last_item = last(node.items);
488
+ addOffset({
489
+ lines: 0,
490
+ columns: 1
491
+ }, getExitOffsets(root), last_item);
492
+ dirty_roots.add(root);
493
+ }
494
+ function applyTrailingComma(root, node, trailing_commas = false) {
495
+ if (!trailing_commas) return;
496
+ if (!node.items.length) return;
497
+ const last_item = last(node.items);
498
+ last_item.comma = true;
499
+ addOffset({
500
+ lines: 0,
501
+ columns: 1
502
+ }, getExitOffsets(root), last_item);
503
+ dirty_roots.add(root);
504
+ }
505
+ /**
506
+ * Applies all accumulated write offsets (enter and exit) to the given CST node.
507
+ * This function adjusts the start and end locations of each node in the tree based on
508
+ * the offsets stored in the `enter` and `exit` maps. It ensures that the tree's location
509
+ * data is consistent after modifications.
510
+ *
511
+ * @param root - The root node of the CST tree to which the write offsets will be applied.
512
+ */
513
+ function applyWrites(root) {
514
+ if (!dirty_roots.has(root)) return;
515
+ const enter = getEnterOffsets(root);
516
+ const exit = getExitOffsets(root);
517
+ let offsetLines = 0;
518
+ const offsetColumns = {};
519
+ function visitNode(node) {
520
+ switch (node.type) {
521
+ case "Document": {
522
+ const doc = node;
523
+ shiftLoc(doc);
524
+ for (let i = 0; i < doc.items.length; i++) visitNode(doc.items[i]);
525
+ shiftEnd(doc);
526
+ recalcContainerEnd(doc);
527
+ break;
528
+ }
529
+ case "Table": {
530
+ const tbl = node;
531
+ shiftLoc(tbl);
532
+ visitNode(tbl.key);
533
+ for (let i = 0; i < tbl.items.length; i++) visitNode(tbl.items[i]);
534
+ shiftEnd(tbl);
535
+ recalcContainerEnd(tbl);
536
+ break;
537
+ }
538
+ case "TableArray": {
539
+ const ta = node;
540
+ shiftLoc(ta);
541
+ visitNode(ta.key);
542
+ for (let i = 0; i < ta.items.length; i++) visitNode(ta.items[i]);
543
+ shiftEnd(ta);
544
+ recalcContainerEnd(ta);
545
+ break;
546
+ }
547
+ case "TableKey": {
548
+ const tk = node;
549
+ shiftLoc(tk);
550
+ visitNode(tk.item);
551
+ shiftEnd(tk);
552
+ break;
553
+ }
554
+ case "TableArrayKey": {
555
+ const tak = node;
556
+ shiftLoc(tak);
557
+ visitNode(tak.item);
558
+ shiftEnd(tak);
559
+ break;
560
+ }
561
+ case "KeyValue": {
562
+ const kv = node;
563
+ const startLine = kv.loc.start.line + offsetLines;
564
+ const keyExit = exit.get(kv.key);
565
+ kv.equals += (offsetColumns[startLine] || 0) + (keyExit ? keyExit.columns : 0);
566
+ shiftLoc(kv);
567
+ visitNode(kv.key);
568
+ visitNode(kv.value);
569
+ shiftEnd(kv);
570
+ break;
571
+ }
572
+ case "InlineArray": {
573
+ const ia = node;
574
+ if (!enter.get(ia) && !exit.get(ia)) {
575
+ shiftPositionsNoOffsets(ia);
576
+ for (let i = 0; i < ia.items.length; i++) visitNode(ia.items[i]);
577
+ shiftPositionsNoOffsetsEnd(ia);
578
+ break;
579
+ }
580
+ shiftLoc(ia);
581
+ for (let i = 0; i < ia.items.length; i++) visitNode(ia.items[i]);
582
+ shiftEnd(ia);
583
+ break;
584
+ }
585
+ case "InlineTable": {
586
+ const it = node;
587
+ if (!enter.get(it) && !exit.get(it)) {
588
+ shiftPositionsNoOffsets(it);
589
+ for (let i = 0; i < it.items.length; i++) visitNode(it.items[i]);
590
+ shiftPositionsNoOffsetsEnd(it);
591
+ break;
592
+ }
593
+ shiftLoc(it);
594
+ for (let i = 0; i < it.items.length; i++) visitNode(it.items[i]);
595
+ shiftEnd(it);
596
+ break;
597
+ }
598
+ case "InlineItem": {
599
+ const ii = node;
600
+ shiftLoc(ii);
601
+ visitNode(ii.item);
602
+ shiftEnd(ii);
603
+ break;
604
+ }
605
+ case "Key":
606
+ case "String":
607
+ case "Integer":
608
+ case "Float":
609
+ case "Boolean":
610
+ case "DateTime":
611
+ case "Comment":
612
+ shiftLoc(node);
613
+ shiftEnd(node);
614
+ break;
615
+ default: throw new Error(`Unrecognized node type "${node.type}"`);
616
+ }
617
+ }
618
+ function shiftLoc(node) {
619
+ node.loc.start.line += offsetLines;
620
+ const colOff = offsetColumns[node.loc.start.line] || 0;
621
+ node.loc.start.column += colOff;
622
+ const entering = enter.get(node);
623
+ if (entering) {
624
+ offsetLines += entering.lines;
625
+ offsetColumns[node.loc.start.line] = (offsetColumns[node.loc.start.line] || 0) + entering.columns;
626
+ }
627
+ }
628
+ function shiftEnd(node) {
629
+ node.loc.end.line += offsetLines;
630
+ const colOff = offsetColumns[node.loc.end.line] || 0;
631
+ node.loc.end.column += colOff;
632
+ const exiting = exit.get(node);
633
+ if (exiting) {
634
+ offsetLines += exiting.lines;
635
+ offsetColumns[node.loc.end.line] = (offsetColumns[node.loc.end.line] || 0) + exiting.columns;
636
+ }
637
+ }
638
+ function shiftPositionsNoOffsets(node) {
639
+ node.loc.start.line += offsetLines;
640
+ node.loc.start.column += offsetColumns[node.loc.start.line] || 0;
641
+ }
642
+ function shiftPositionsNoOffsetsEnd(node) {
643
+ node.loc.end.line += offsetLines;
644
+ node.loc.end.column += offsetColumns[node.loc.end.line] || 0;
645
+ }
646
+ visitNode(root);
647
+ dirty_roots.delete(root);
648
+ enter_offsets.delete(root);
649
+ exit_offsets.delete(root);
650
+ }
651
+ function shiftNode(node, span, options = {}) {
652
+ const { lines, columns } = span;
653
+ if (lines === 0 && columns === 0) return node;
654
+ const { first_line_only = false, shift_generated_multiline_end = false } = options;
655
+ const start_line = node.loc.start.line;
656
+ const type = node.type;
657
+ if (type === "Key" || type === "String" || type === "Integer" || type === "Float" || type === "Boolean" || type === "DateTime" || type === "Comment") {
658
+ if (!first_line_only || node.loc.start.line === start_line) {
659
+ node.loc.start.column += columns;
660
+ if (node.loc.end.line === node.loc.start.line) node.loc.end.column += columns;
661
+ }
662
+ node.loc.start.line += lines;
663
+ node.loc.end.line += lines;
664
+ return node;
665
+ }
666
+ if (type === "KeyValue") {
667
+ const kv = node;
668
+ const valType = kv.value.type;
669
+ if (valType === "String" || valType === "Integer" || valType === "Float" || valType === "Boolean" || valType === "DateTime") {
670
+ const onFirstLine = !first_line_only || kv.loc.start.line === start_line;
671
+ if (onFirstLine) {
672
+ kv.loc.start.column += columns;
673
+ if (kv.loc.end.line === kv.loc.start.line) kv.loc.end.column += columns;
674
+ }
675
+ kv.loc.start.line += lines;
676
+ kv.loc.end.line += lines;
677
+ if (onFirstLine) kv.equals += columns;
678
+ const key = kv.key;
679
+ if (!first_line_only || key.loc.start.line === start_line) {
680
+ key.loc.start.column += columns;
681
+ key.loc.end.column += columns;
682
+ }
683
+ key.loc.start.line += lines;
684
+ key.loc.end.line += lines;
685
+ const val = kv.value;
686
+ if (!first_line_only || val.loc.start.line === start_line) {
687
+ val.loc.start.column += columns;
688
+ if (val.loc.end.line === val.loc.start.line) val.loc.end.column += columns;
689
+ }
690
+ val.loc.start.line += lines;
691
+ val.loc.end.line += lines;
692
+ return node;
693
+ }
694
+ }
695
+ const move = (node) => {
696
+ if (!first_line_only || node.loc.start.line === start_line) {
697
+ node.loc.start.column += columns;
698
+ const generatedMultilineEnd = (!first_line_only || shift_generated_multiline_end) && node.loc.end.line !== node.loc.start.line && ((isInlineArray(node) || isInlineTable(node)) && getInlineContainerLayout(node) !== void 0 && !isInlineContainerPositioned(node) || isInlineItem(node) && ((isInlineArray(node.item) || isInlineTable(node.item)) && getInlineContainerLayout(node.item) !== void 0 && !isInlineContainerPositioned(node.item) || isKeyValue(node.item) && (isInlineArray(node.item.value) || isInlineTable(node.item.value)) && getInlineContainerLayout(node.item.value) !== void 0 && !isInlineContainerPositioned(node.item.value)) || isKeyValue(node) && (isInlineArray(node.value) || isInlineTable(node.value)) && getInlineContainerLayout(node.value) !== void 0);
699
+ if (node.loc.end.line === node.loc.start.line || generatedMultilineEnd) node.loc.end.column += columns;
700
+ }
701
+ node.loc.start.line += lines;
702
+ node.loc.end.line += lines;
703
+ };
704
+ traverse(node, {
705
+ ["Table"]: move,
706
+ ["TableKey"]: move,
707
+ ["TableArray"]: move,
708
+ ["TableArrayKey"]: move,
709
+ ["KeyValue"](node) {
710
+ const onFirstLine = !first_line_only || node.loc.start.line === start_line;
711
+ move(node);
712
+ if (onFirstLine) node.equals += columns;
713
+ },
714
+ ["Key"]: move,
715
+ ["String"]: move,
716
+ ["Integer"]: move,
717
+ ["Float"]: move,
718
+ ["Boolean"]: move,
719
+ ["DateTime"]: move,
720
+ ["InlineArray"]: move,
721
+ ["InlineItem"]: move,
722
+ ["InlineTable"]: move,
723
+ ["Comment"]: move
724
+ });
725
+ return node;
726
+ }
727
+ /**
728
+ * Answers "is this container laid out with one item per row?" for insertion
729
+ * decisions. Rows are inferred from the items' START lines, so a container
730
+ * whose next item continues on a MULTILINE item's end line still counts as
731
+ * per-row here. Callers must handle that shape themselves: `insert` suppresses
732
+ * the new line when `next` starts on `previous`'s end line (fuzz seed 620).
733
+ *
734
+ * Structural layout decisions (`'parent'`, multiline formatting) must not use
735
+ * this loose answer. They use `hasStructuralMultilineRows`, which compares each
736
+ * item's start line against the previous item's END line.
737
+ */
738
+ function perLine(array, excluded) {
739
+ const layout = getInlineContainerLayout(array);
740
+ if (layout === true) return true;
741
+ if (layout !== void 0) return false;
742
+ if (!array.items.length) return false;
743
+ const items = excluded ? array.items.filter((item) => item !== excluded) : array.items;
744
+ if (!items.length) return array.loc.end.line > array.loc.start.line;
745
+ return array.loc.end.line > array.loc.start.line && items.every((item, index) => index === 0 || item.loc.start.line > items[index - 1].loc.start.line);
746
+ }
747
+ function addOffset(offset, offsets, node, from) {
748
+ const previous_offset = offsets.get(from || node);
749
+ if (previous_offset) {
750
+ offset.lines += previous_offset.lines;
751
+ offset.columns += previous_offset.columns;
752
+ }
753
+ offsets.set(node, offset);
754
+ }
755
+
756
+ export { addExitOffset, applyBracketSpacing, applyTrailingComma, applyWrites, deleteInlineContainerNeedingTighten, getExitOffsets, getPendingEnterOffsets, hasInlineContainerNeedingTighten, insert, markDirty, markStringifyRoot, perLine, recalcContainerEnd, remove, replace, setInlineIndentColumn, setRootIndentWidth, shiftNode };
757
+ //# sourceMappingURL=writer.js.map