-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
feat(output): Add --show-file-offsets option to annotate directory tree with line ranges #1464
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
bc06014
8f367c3
279fb7a
96d1c4d
dfed326
efb81f7
b9ea60d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| export interface FileLineOffset { | ||
| start: number; | ||
| end: number; | ||
| } | ||
|
|
||
| /** | ||
| * Scans a rendered output string and returns the line range (1-indexed, inclusive) | ||
| * for each file's content block. | ||
| * | ||
| * Supports XML, Markdown, and plain text output styles. | ||
| * JSON output is structured and does not use this function. | ||
| */ | ||
| export const computeFileLineOffsets = (output: string, style: string): Record<string, FileLineOffset> => { | ||
|
nuthalapativarun marked this conversation as resolved.
Outdated
|
||
| const offsets: Record<string, FileLineOffset> = {}; | ||
| const lines = output.split('\n'); | ||
|
nuthalapativarun marked this conversation as resolved.
Outdated
|
||
|
|
||
| if (style === 'xml') { | ||
| let currentPath: string | null = null; | ||
| let currentStart = 0; | ||
|
|
||
| for (let i = 0; i < lines.length; i++) { | ||
| const line = lines[i]; | ||
| const lineNum = i + 1; | ||
|
|
||
| const startMatch = line.match(/^<file path="(.+)">$/); | ||
|
nuthalapativarun marked this conversation as resolved.
Outdated
|
||
| if (startMatch) { | ||
| currentPath = startMatch[1]; | ||
| currentStart = lineNum; | ||
| } else if (line === '</file>' && currentPath !== null) { | ||
|
nuthalapativarun marked this conversation as resolved.
Outdated
|
||
| offsets[currentPath] = { start: currentStart, end: lineNum }; | ||
| currentPath = null; | ||
| } | ||
|
Comment on lines
+87
to
+92
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 XML offset scanning falsely matches In Was this helpful? React with 👍 or 👎 to provide feedback. |
||
| } | ||
| } else if (style === 'markdown') { | ||
| const fileStarts: Array<{ path: string; line: number }> = []; | ||
|
|
||
| for (let i = 0; i < lines.length; i++) { | ||
| const match = lines[i].match(/^## File: (.+)$/); | ||
| if (match) { | ||
| fileStarts.push({ path: match[1], line: i + 1 }); | ||
| } | ||
|
Comment on lines
+105
to
+109
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔴 Markdown offset scanning prematurely terminates when file content contains lines starting with In Prompt for agentsWas this helpful? React with 👍 or 👎 to provide feedback. |
||
| } | ||
|
|
||
| for (let j = 0; j < fileStarts.length; j++) { | ||
| const { path, line } = fileStarts[j]; | ||
| const endLine = j + 1 < fileStarts.length ? fileStarts[j + 1].line - 1 : lines.length; | ||
| offsets[path] = { start: line, end: endLine }; | ||
| } | ||
|
nuthalapativarun marked this conversation as resolved.
|
||
| } else if (style === 'plain') { | ||
| // Plain format: "================" then "File: path" then "================" then content | ||
| // End of content = line before next "================" separator | ||
| const SEPARATOR = '================'; | ||
|
nuthalapativarun marked this conversation as resolved.
|
||
| const fileSeparatorLines: number[] = []; | ||
| const fileHeaderLines: Array<{ path: string; line: number }> = []; | ||
|
|
||
| for (let i = 0; i < lines.length; i++) { | ||
| const line = lines[i]; | ||
| const lineNum = i + 1; | ||
|
|
||
| if (line === SEPARATOR) { | ||
| fileSeparatorLines.push(lineNum); | ||
| // Check if next line is a File: header | ||
| if (i + 1 < lines.length && lines[i + 1].startsWith('File: ')) { | ||
| const filePath = lines[i + 1].slice('File: '.length); | ||
|
nuthalapativarun marked this conversation as resolved.
Outdated
|
||
| fileHeaderLines.push({ path: filePath, line: lineNum }); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| for (let j = 0; j < fileHeaderLines.length; j++) { | ||
| const { path, line } = fileHeaderLines[j]; | ||
| // Content starts after: separator → File: header → separator → content | ||
| const contentStart = line + 3; | ||
| // Content ends before the next file separator, or at the last line | ||
| const nextSeparatorLine = j + 1 < fileHeaderLines.length ? fileHeaderLines[j + 1].line - 1 : lines.length; | ||
| offsets[path] = { start: contentStart, end: nextSeparatorLine }; | ||
| } | ||
| } | ||
|
nuthalapativarun marked this conversation as resolved.
Outdated
|
||
|
|
||
| return offsets; | ||
| }; | ||
|
|
||
| /** | ||
| * Formats a FileLineOffset as a human-readable annotation string. | ||
| * Example: " [lines 42–78]" | ||
| */ | ||
| export const formatFileOffsetAnnotation = (offset: FileLineOffset): string => { | ||
| return ` [lines ${offset.start}–${offset.end}]`; | ||
| }; | ||
Uh oh!
There was an error while loading. Please reload this page.