Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions src/chunking/rebuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,15 @@ export const rebuildText = (window: ASTWindow, code: string): RebuiltText => {

// Normal case: slice from first node start to last node end
// Use startPosition/endPosition from nodes for optimized line calculation
const firstNode = window.nodes[0]!
const lastNode = window.nodes[window.nodes.length - 1]!
const firstNode = window.nodes[0]
const lastNode = window.nodes[window.nodes.length - 1]
if (!firstNode || !lastNode) {
return {
text: '',
byteRange: { start: 0, end: 0 },
lineRange: { start: 0, end: 0 },
}
}

const startByte = firstNode.startIndex
const endByte = lastNode.endIndex
Expand Down Expand Up @@ -114,20 +121,36 @@ const rebuildFromLineRanges = (
window: ASTWindow,
code: string,
): RebuiltText => {
const lineRanges = window.lineRanges!
const lineRanges = window.lineRanges
if (!lineRanges || lineRanges.length === 0) {
return {
text: '',
byteRange: { start: 0, end: 0 },
lineRange: { start: 0, end: 0 },
}
}
const lineStarts = buildLineStartsTable(code)

// Get the overall line range
const firstRange = lineRanges[0]!
const lastRange = lineRanges[lineRanges.length - 1]!
const firstRange = lineRanges[0]
const lastRange = lineRanges[lineRanges.length - 1]
if (!firstRange || !lastRange) {
return {
text: '',
byteRange: { start: 0, end: 0 },
lineRange: { start: 0, end: 0 },
}
}
const startLine = firstRange.start
const endLine = lastRange.end

// Calculate byte offsets from line numbers
const startByte = lineStarts[startLine] ?? 0
// End byte is start of line after endLine, or end of file
const endByte =
endLine + 1 < lineStarts.length ? lineStarts[endLine + 1]! : code.length
endLine + 1 < lineStarts.length
? (lineStarts[endLine + 1] ?? code.length)
: code.length

const text = code.slice(startByte, endByte)

Expand Down