Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355/** Path-create for {@link tomlSet}: insert a fresh dotted key-value when the path does not resolve to an existing value. A new key-value is placed inside the deepest standard table whose header is a prefix of the path, else at top level before the first table header (the TOML grammar requires bare keys to precede section headers). The inserted block is synthetic and renders canonically. @module */
import { buildValueFromInput, } from './build-input.ts';import type { Block, KeyValueNode,} from './document.ts';import { TomlImmutableNodeError, } from './errors.ts';import { formatPath, } from './path.ts';import type { CanonicalOptions, TomlEditState, TomlPath,} from './types.ts';
/** Build a synthetic key-value block for `segments = value`. @returns Computed {@link KeyValueNode}. @example ```ts makeKeyValue({ segments: ['a','b'], value: 1, options, },); ``` */export function makeKeyValue( { segments, value, options, }: { readonly segments: readonly string[]; readonly value: unknown; readonly options: CanonicalOptions; },): KeyValueNode { return { kind: 'keyvalue', keySegments: segments, value: buildValueFromInput({ input: value, options, },), origin: { kind: 'synthetic', }, commentsBefore: [], };}
/** Create a new key-value at a currently-absent `path`. @returns Fresh {@link TomlEditState}. @throws {@link TomlImmutableNodeError} when the remaining tail has a numeric segment. @example ```ts doCreate({ edit, path: ['a','b','c'], value: 42, },); ``` */export function doCreate( { edit, path, value, }: { readonly edit: TomlEditState; readonly path: TomlPath; readonly value: unknown; },): TomlEditState { assertNoScalarPrefix({ blocks: edit.blocks, path, basePath: [], },); /** Index of the deepest standard table whose header is a strict prefix of path. */ const tableIndex = deepestTableIndex({ blocks: edit.blocks, path, },); if (tableIndex === (-1)) return createAtTopLevel({ edit, path, value, },); /** The owning table block; its body gains the new dotted key-value. */ const table = edit.blocks[tableIndex]; if ((table === undefined) || (table.kind !== 'table')) return createAtTopLevel({ edit, path, value, },); /** Dotted tail relative to the table header. */ const segments = dottedTail({ path, consumed: table.headerSegments .length, },); return { ...edit, blocks: edit.blocks .with( tableIndex, { ...table, body: [ ...table.body, makeKeyValue({ segments, value, options: edit.canonical, },), ], }, ), };}
/** Insert a new top-level dotted key-value before the first table header. @returns Fresh {@link TomlEditState}. */function createAtTopLevel( { edit, path, value, }: { readonly edit: TomlEditState; readonly path: TomlPath; readonly value: unknown; },): TomlEditState { /** New key-value block for the full path (all segments are top-level here). */ const kv = makeKeyValue({ segments: dottedTail({ path, consumed: 0, },), value, options: edit.canonical, },); /** First table header index; the key-value must land before it. */ const firstTable = edit.blocks .findIndex(function isTable(b,) { return b.kind === 'table'; },); /** Leading newline filler when the source does not already end on a line break. */ const needsNewline = (firstTable === (-1)) && (edit.source !== '') && (!edit.source .endsWith('\n',)); /** Blocks to splice in: the key-value, preceded by a newline filler if needed. */ const inserted: readonly Block[] = needsNewline ? [ { kind: 'filler', text: '\n', }, kv, ] : [kv,]; if (firstTable === (-1)) return { ...edit, blocks: [ ...edit.blocks, ...inserted, ], }; return { ...edit, blocks: [ ...edit.blocks .slice( 0, firstTable, ), kv, ...edit.blocks .slice(firstTable,), ], };}
/** Throw when `path` would be created under an existing scalar or array leaf (a key-value whose absolute key strictly prefixes `path` and whose value is not a table shape). Path-create cannot descend through such a value. @throws {@link TomlImmutableNodeError} on a scalar/array prefix conflict. */function assertNoScalarPrefix( { blocks, path, basePath, }: { readonly blocks: readonly Block[]; readonly path: TomlPath; readonly basePath: readonly (string | number)[]; },): void { for (const block of blocks) { if (block.kind === 'keyvalue') { /** Absolute key of this entry so the prefix test spans table context. */ const abs = [ ...basePath, ...block.keySegments, ]; if ((abs.length < path.length) && abs.every(function eq( seg, i, ) { return seg === path[i]; },) && (block.value .kind !== 'inline-table')) { throw new TomlImmutableNodeError( `Cannot path-create through ${block.value .kind} at path ${formatPath({ path, },)}`, ); } continue; } if (block.kind === 'table') assertNoScalarPrefix({ blocks: block.body, path, basePath: block.headerSegments, },); }}
/** Index of the deepest standard table whose header strictly prefixes `path`. @returns Block index, or `-1` when none. */function deepestTableIndex( { blocks, path, }: { readonly blocks: readonly Block[]; readonly path: TomlPath; },): number { return blocks.reduce( function step( best, block, index, ) { if ((block.kind !== 'table') || (block.tableKind !== 'standard')) return best; /** Header length; a strict prefix qualifies this table as a container. */ const len = block.headerSegments .length; if ((len >= path.length) || (!block.headerSegments .every(function eq( seg, i, ) { return seg === path[i]; },))) return best; /** Current best header length so the longest (deepest) prefix wins. */ const bestLen = (best === (-1)) || (blocks[best] === undefined) || (blocks[best] ?.kind !== 'table') ? -1 : (blocks[best] as Extract<Block, { kind: 'table'; }>).headerSegments .length; return len > bestLen ? index : best; }, -1, );}
/** Remaining path segments after `consumed`, verified all-string. @returns Computed dotted segments. @throws {@link TomlImmutableNodeError} when a remaining segment is numeric. */function dottedTail( { path, consumed, }: { readonly path: TomlPath; readonly consumed: number; },): readonly string[] { return path.slice(consumed,) .map(function asString(seg,) { if ((typeof seg) !== 'string') { throw new TomlImmutableNodeError( `Cannot path-create at numeric segment ${String(seg,)} in ${formatPath({ path, },)}`, ); } return seg; },);}