* Given an absolute match offset, read ±contextLines around it and return * the decoded slice with its starting line number. Reuses `scratch` (the * caller's scan buffer) for back/forward/output reads — zero new allocs * when the context fits, one alloc otherwise.
( handle: FileHandle, scratch: Buffer, matchStart: number, matchLen: number, contextLines: number, linesBeforeMatch: number, )
| 169 | * when the context fits, one alloc otherwise. |
| 170 | */ |
| 171 | async function sliceContext( |
| 172 | handle: FileHandle, |
| 173 | scratch: Buffer, |
| 174 | matchStart: number, |
| 175 | matchLen: number, |
| 176 | contextLines: number, |
| 177 | linesBeforeMatch: number, |
| 178 | ): Promise<EditContext> { |
| 179 | // Scan backward from matchStart to find contextLines prior newlines. |
| 180 | const backChunk = Math.min(matchStart, CHUNK_SIZE) |
| 181 | const { bytesRead: backRead } = await handle.read( |
| 182 | scratch, |
| 183 | 0, |
| 184 | backChunk, |
| 185 | matchStart - backChunk, |
| 186 | ) |
| 187 | let ctxStart = matchStart |
| 188 | let nlSeen = 0 |
| 189 | for (let i = backRead - 1; i >= 0 && nlSeen <= contextLines; i--) { |
| 190 | if (scratch[i] === NL) { |
| 191 | nlSeen++ |
| 192 | if (nlSeen > contextLines) break |
| 193 | } |
| 194 | ctxStart-- |
| 195 | } |
| 196 | // Compute lineOffset now, before scratch is overwritten by the forward read. |
| 197 | const walkedBack = matchStart - ctxStart |
| 198 | const lineOffset = |
| 199 | linesBeforeMatch - |
| 200 | countNewlines(scratch, backRead - walkedBack, backRead) + |
| 201 | 1 |
| 202 | |
| 203 | // Scan forward from matchEnd to find contextLines trailing newlines. |
| 204 | const matchEnd = matchStart + matchLen |
| 205 | const { bytesRead: fwdRead } = await handle.read( |
| 206 | scratch, |
| 207 | 0, |
| 208 | CHUNK_SIZE, |
| 209 | matchEnd, |
| 210 | ) |
| 211 | let ctxEnd = matchEnd |
| 212 | nlSeen = 0 |
| 213 | for (let i = 0; i < fwdRead; i++) { |
| 214 | ctxEnd++ |
| 215 | if (scratch[i] === NL) { |
| 216 | nlSeen++ |
| 217 | if (nlSeen >= contextLines + 1) break |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | // Read the exact context range. Reuse scratch if it fits. |
| 222 | const len = ctxEnd - ctxStart |
| 223 | const out = len <= scratch.length ? scratch : Buffer.allocUnsafe(len) |
| 224 | const { bytesRead: outRead } = await handle.read(out, 0, len, ctxStart) |
| 225 | |
| 226 | return { content: normalizeCRLF(out, outRead), lineOffset, truncated: false } |
| 227 | } |
| 228 |
no test coverage detected