MCPcopy Create free account
hub / github.com/arktypeio/arktype / readAtOf

Function readAtOf

ark/aot/unwrite.ts:181–227  ·  view source on GitHub ↗
(
	base: ModuleTokens,
	read: ModuleTokens,
	{ code, mappings }: WrittenModule,
	reached: string
)

Source from the content-addressed store, hash-verified

179// has a token of the first step's as far past where that step started
180// writing as it is, where the definition may hold the same one
181const readAtOf = (
182 base: ModuleTokens,
183 read: ModuleTokens,
184 { code, mappings }: WrittenModule,
185 reached: string
186): Int32Array => {
187 const readAt = new Int32Array(base.tokens.length).fill(-1)
188 const lines = decode(mappings)
189 const starts = lineStartsOf(code)
190 const readStarts = lineStartsOf(reached)
191 const byStart = new Map(read.tokens.map((token, i) => [token.start, i]))
192 let line = -1
193 // where each segment of the line maps to, by the column it starts at
194 let mapped = new Map<number, number>()
195 // whether the map has a token it wrote where it read another
196 const isMapped = (b: number, o: number): boolean => {
197 const { start } = base.tokens[b]
198 let at = Math.max(line, 0)
199 while (at + 1 < starts.length && starts[at + 1] <= start) at++
200 if (at !== line) {
201 line = at
202 mapped = new Map(
203 (lines[line] ?? []).flatMap((segment): [number, number][] =>
204 segment.length === 1 || segment[2] >= readStarts.length ?
205 []
206 : [[segment[0], readStarts[segment[2]] + segment[3]]]
207 )
208 )
209 }
210 // its own segment, as the map has one where each token it kept starts
211 const from = mapped.get(start - starts[line])
212 return from !== undefined && byStart.get(from) === o
213 }
214 let [b, o] = [0, 0]
215 const keep = () => {
216 if (isMapped(b, o)) readAt[b] = o
217 b++
218 o++
219 }
220 for (const [start, end, , readEnd] of diff(base, read)) {
221 while (b < start) keep()
222 b = end
223 o = readEnd
224 }
225 while (b < base.tokens.length) keep()
226 return readAt
227}
228
229// where each line of a text starts
230const lineStartsOf = (text: string): number[] => {

Callers 1

unwriteFunction · 0.85

Calls 5

lineStartsOfFunction · 0.85
diffFunction · 0.85
keepFunction · 0.70
mapMethod · 0.65
decodeFunction · 0.50

Tested by

no test coverage detected