(content: string, startLine: number = 1)
| 134 | } |
| 135 | |
| 136 | export function addLineNumbers(content: string, startLine: number = 1): string { |
| 137 | // If content is empty, return empty string - empty files should not have line numbers |
| 138 | // If content is empty but startLine > 1, return "startLine | " because we know the file is not empty |
| 139 | // but the content is empty at that line offset |
| 140 | if (content === "") { |
| 141 | return startLine === 1 ? "" : `${startLine} | \n` |
| 142 | } |
| 143 | |
| 144 | // Split into lines and handle trailing line feeds (\n) |
| 145 | const lines = content.split("\n") |
| 146 | const lastLineEmpty = lines[lines.length - 1] === "" |
| 147 | if (lastLineEmpty) { |
| 148 | lines.pop() |
| 149 | } |
| 150 | |
| 151 | const maxLineNumberWidth = String(startLine + lines.length - 1).length |
| 152 | const numberedContent = lines |
| 153 | .map((line, index) => { |
| 154 | const lineNumber = String(startLine + index).padStart(maxLineNumberWidth, " ") |
| 155 | return `${lineNumber} | ${line}` |
| 156 | }) |
| 157 | .join("\n") |
| 158 | |
| 159 | return numberedContent + "\n" |
| 160 | } |
| 161 | // Checks if every line in the content has line numbers prefixed (e.g., "1 | content" or "123 | content") |
| 162 | // Line numbers must be followed by a single pipe character (not double pipes) |
| 163 | export function everyLineHasLineNumbers(content: string): boolean { |
no test coverage detected