| 160 | * (no `paths:` block), so the caller can fall back to a whole-document parse. |
| 161 | */ |
| 162 | export const structuralSplit = (text: string): SpecStructure | null => { |
| 163 | const len = text.length; |
| 164 | |
| 165 | // Top-level keys (column 0). Record each key's start + name, respecting |
| 166 | // block scalars whose content could sit at column >= 1. |
| 167 | const topStarts: number[] = []; |
| 168 | const topNames: string[] = []; |
| 169 | { |
| 170 | let blockScalarIndent = -1; |
| 171 | let pos = 0; |
| 172 | while (pos < len) { |
| 173 | const line = lineAt(text, pos, len); |
| 174 | if (!line) break; |
| 175 | pos = line.nextStart; |
| 176 | if (isBlankOrComment(text, line.contentStart, line.lineEnd)) continue; |
| 177 | if (blockScalarIndent >= 0) { |
| 178 | if (line.indent > blockScalarIndent) continue; |
| 179 | blockScalarIndent = -1; |
| 180 | } |
| 181 | if (line.indent === 0) { |
| 182 | topStarts.push(line.lineStart); |
| 183 | topNames.push(keyNameAt(text, line.lineStart, line.lineEnd)); |
| 184 | } |
| 185 | if (opensBlockScalar(text.slice(line.contentStart, line.lineEnd))) { |
| 186 | blockScalarIndent = line.indent; |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | const topRanges = rangesFromStarts(topStarts, len); |
| 192 | const pathsIdx = topNames.indexOf("paths"); |
| 193 | if (pathsIdx === -1) return null; |
| 194 | const componentsIdx = topNames.indexOf("components"); |
| 195 | |
| 196 | const headRanges: ByteRange[] = []; |
| 197 | for (let i = 0; i < topRanges.length; i++) { |
| 198 | if (i === pathsIdx || i === componentsIdx) continue; |
| 199 | headRanges.push(topRanges[i]!); |
| 200 | } |
| 201 | |
| 202 | // Path-items: indent-2 keys inside the `paths:` block (after its key line). |
| 203 | const pathsRange = topRanges[pathsIdx]!; |
| 204 | const pathsBodyStart = |
| 205 | lineAt(text, pathsRange.start, pathsRange.end)?.nextStart ?? pathsRange.end; |
| 206 | const pathItems = rangesFromStarts( |
| 207 | keyLineStartsAtIndent(text, pathsBodyStart, pathsRange.end, 2), |
| 208 | pathsRange.end, |
| 209 | ); |
| 210 | |
| 211 | const schemas: ByteRange[] = []; |
| 212 | const smallComponentRanges: ByteRange[] = []; |
| 213 | if (componentsIdx !== -1) { |
| 214 | const componentsRange = topRanges[componentsIdx]!; |
| 215 | const componentsBodyStart = |
| 216 | lineAt(text, componentsRange.start, componentsRange.end)?.nextStart ?? componentsRange.end; |
| 217 | const subStarts = keyLineStartsAtIndent(text, componentsBodyStart, componentsRange.end, 2); |
| 218 | const subRanges = rangesFromStarts(subStarts, componentsRange.end); |
| 219 | for (const range of subRanges) { |